-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guido W. Pettinari
committed
Dec 16, 2022
1 parent
65a4712
commit 9b01327
Showing
12 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "web3cli" | ||
version = "0.10.0" | ||
version = "0.11.0" | ||
description = "Interact with blockchains and smart contracts using the command line" | ||
authors = [ | ||
{name = "coccoinomane", email = "[email protected]"}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from cement import ex | ||
from web3cli.controllers.controller import Controller | ||
from web3cli.core.helpers.os import read_json | ||
from web3cli.core.models.contract import Contract | ||
from web3cli.core.exceptions import Web3CliError | ||
from web3cli.helpers.render import render_table | ||
from playhouse.shortcuts import model_to_dict | ||
|
||
|
||
class ContractController(Controller): | ||
"""Handler of the `w3 db contract` commands""" | ||
|
||
class Meta: | ||
label = "contract" | ||
help = "add, list or delete contracts" | ||
stacked_type = "nested" | ||
stacked_on = "db" | ||
|
||
@ex(help="list contracts") | ||
def list(self) -> None: | ||
render_table( | ||
self.app, | ||
data=[ | ||
[c.name, c.chain, c.address] for c in Contract.get_all(Contract.name) | ||
], | ||
headers=["NAME", "CHAIN", "ADDRESS"], | ||
wrap=42, | ||
) | ||
|
||
@ex( | ||
help="show details of the given contract", | ||
arguments=[ | ||
(["name"], {"help": "name of the contract"}), | ||
], | ||
) | ||
def get(self) -> None: | ||
contract = Contract.get_by_name_or_raise(self.app.pargs.name) | ||
self.app.render(model_to_dict(contract), indent=4, handler="json") | ||
|
||
@ex( | ||
help="add a new contract to the database", | ||
arguments=[ | ||
(["name"], {"help": "name of the contract, for reference"}), | ||
( | ||
["address"], | ||
{"help": "address of the contract on the blockchain (0x...)"}, | ||
), | ||
(["-d", "--desc"], {"action": "store"}), | ||
( | ||
["-u", "--update"], | ||
{ | ||
"help": "if a contract with the same name is present, overwrite it", | ||
"action": "store_true", | ||
}, | ||
), | ||
( | ||
["--abi"], | ||
{ | ||
"help": "json file containing the contract's ABI", | ||
}, | ||
), | ||
], | ||
) | ||
def add(self) -> None: | ||
|
||
# Parse ABI file | ||
abi = None | ||
if self.app.pargs.abi: | ||
try: | ||
abi = read_json(self.app.pargs.abi) | ||
except: | ||
raise Web3CliError(f"Could not read ABI from file {self.app.pargs.abi}") | ||
|
||
# Add or update contract | ||
contract = Contract.get_by_name(self.app.pargs.name) | ||
if not contract or self.app.pargs.update: | ||
Contract.upsert( | ||
{ | ||
"name": self.app.pargs.name, | ||
"desc": self.app.pargs.desc, | ||
"address": self.app.pargs.address, | ||
"chain": self.app.chain_name, | ||
"abi": abi, | ||
}, | ||
logger=self.app.log.info, | ||
) | ||
else: | ||
raise Web3CliError( | ||
f"Contract '{self.app.pargs.name}' already exists. Use `--update` or `-u` to update it." | ||
) | ||
|
||
@ex( | ||
help="delete a contract", | ||
arguments=[ | ||
(["name"], {"help": "hash of the contract to delete"}), | ||
], | ||
) | ||
def delete(self) -> None: | ||
contract = Contract.get_by_name_or_raise(self.app.pargs.name) | ||
contract.delete_instance() | ||
self.app.log.info(f"Contract '{self.app.pargs.name}' deleted correctly") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
from typing import Type | ||
from peewee import TextField | ||
from playhouse.signals import pre_save | ||
from web3cli.core.exceptions import ContractIsInvalid, ContractNotFound | ||
from web3cli.core.models.address import Address | ||
from web3cli.core.models.base_model import BaseModel | ||
from playhouse.sqlite_ext import JSONField | ||
from web3._utils.validation import validate_abi | ||
from web3cli.core.models.types import ContractFields | ||
from web3cli.core.types import Logger | ||
|
||
|
||
class Contract(BaseModel): | ||
class Meta: | ||
table_name = "contracts" | ||
|
||
name = TextField(unique=True) | ||
desc = TextField(null=True) | ||
address = TextField() | ||
chain = TextField() | ||
abi = JSONField(null=True) | ||
|
||
@classmethod | ||
def upsert(cls, fields: ContractFields, logger: Logger = None) -> Contract: | ||
"""Create contract or update it if one with the same name already exists""" | ||
return cls.upsert_by_field(cls.name, fields["name"], fields, logger, True) | ||
|
||
|
||
@pre_save(sender=Contract) | ||
def validate(model_class: Contract, instance: Type[Contract], created: bool) -> None: | ||
"""Validate the contract which is about to be saved""" | ||
if not Address.is_valid_address(instance.address): | ||
raise ContractIsInvalid( | ||
f"Invalid address given for contract: {instance.address}" | ||
) | ||
if instance.abi: | ||
validate_abi(instance.abi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters