Skip to content

Commit

Permalink
✨ Add weth tokens with --weth flag
Browse files Browse the repository at this point in the history
  • Loading branch information
coccoinomane committed Feb 28, 2024
1 parent decb150 commit 84ab7e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
27 changes: 24 additions & 3 deletions src/web3cli/controllers/token_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import decimal

from cement import ex
from playhouse.shortcuts import model_to_dict

from web3cli.exceptions import Web3CliError
from web3cli.framework.controller import Controller
Expand Down Expand Up @@ -193,6 +194,19 @@ def transfer(self) -> None:
# | |___ | | | |_| | | (_| |
# \____| |_| \__,_| \__,_|

@ex(
help="show details of token by name and optionally chain",
arguments=[
(["name"], {"help": "name of the token"}),
args.chain(),
],
)
def get(self) -> None:
contract = Contract.get_by_name_chain_and_types_or_raise(
self.app.pargs.name, self.app.chain.name, ["erc20", "weth"]
)
render(self.app, model_to_dict(contract))

@ex(
help="add a new token to the database",
arguments=[
Expand All @@ -202,6 +216,13 @@ def transfer(self) -> None:
["address"],
{"help": "address of the contract on the blockchain (0x...)"},
),
(
["--weth"],
{
"help": "if the token is a wrapped native token, set this flag",
"action": "store_true",
},
),
(
["-u", "--update"],
{
Expand All @@ -222,7 +243,7 @@ def add(self) -> None:
{
"name": self.app.pargs.name,
"desc": self.app.pargs.desc,
"type": "erc20",
"type": "weth" if self.app.pargs.weth else "erc20",
"address": self.app.pargs.address,
"chain": self.app.chain.name,
},
Expand Down Expand Up @@ -251,8 +272,8 @@ def list(self) -> None:
arguments=[(["name"], {"help": "name of the token to delete"}), args.chain()],
)
def delete(self) -> None:
contract = Contract.get_by_name_chain_and_type_or_raise(
self.app.pargs.name, self.app.chain.name, "erc20"
contract = Contract.get_by_name_chain_and_types_or_raise(
self.app.pargs.name, self.app.chain.name, ["erc20", "weth"]
)
contract.delete_instance()
self.app.log.info(
Expand Down
35 changes: 18 additions & 17 deletions tests/web3cli/controllers/crud/test_token_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,25 @@ def test_token_add(contracts: List[ContractFields], chains: List[ChainFields]) -
for t in tokens:
with Web3CliTest() as app:
seed_chains(chains)
app.set_args(
[
"token",
"add",
t["name"],
t["address"],
"--desc",
t["desc"],
"--chain",
t["chain"],
]
).run()
args = [
"token",
"add",
t["name"],
t["address"],
"--desc",
t["desc"],
"--chain",
t["chain"],
]
if t["type"] == "weth":
args += ["--weth"]
app.set_args(args).run()
contract = Contract.get_by_name_and_chain(t["name"], t["chain"])
assert contract.select().count() == 1
assert Contract.name == t["name"]
assert Contract.desc == t["desc"]
assert Contract.type == t["type"]
assert Contract.address == t["address"]
assert Contract.select().count() == 1
assert contract.name == t["name"]
assert contract.desc == t["desc"]
assert contract.type == t["type"]
assert contract.address == t["address"]


def test_token_delete(
Expand Down

0 comments on commit 84ab7e7

Please sign in to comment.