forked from coccoinomane/web3cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransact_controller.py
More file actions
75 lines (70 loc) · 2.96 KB
/
Copy pathtransact_controller.py
File metadata and controls
75 lines (70 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import web3
from cement import ex
from web3cli.exceptions import Web3CliError
from web3cli.framework.controller import Controller
from web3cli.helpers import args
from web3cli.helpers.client_factory import make_contract_wallet
from web3cli.helpers.render import render_web3py
from web3cli.helpers.tx import send_contract_tx
from web3core.helpers.abi import parse_abi_values
from web3core.helpers.misc import yes_or_exit
from web3core.helpers.resolve import resolve_address
class TransactController(Controller):
"""Handler of the `w3 transact` top-level commands"""
class Meta:
label = "transact"
stacked_type = "embedded"
stacked_on = "base"
@ex(
help="""Execute a function in the given smart contract and, by default,
return the transaction hash. This will cost gas and write to the
blockchain, unless the --dry-run or --call flags are used. To send
tokens, please use `w3 send` as it is easier to use. To see the list
of functions in a given contract, run `w3 abi functions <contract>`.""",
arguments=[
(["contract"], {"action": "store"}),
(["function"], {"action": "store"}),
(["args"], {"action": "store", "nargs": "*"}),
(["--value"], {"help": "Send some value, in wei", "type": int}),
*args.tx_args(),
*args.chain_and_rpc(),
*args.signer_and_gas(),
args.force(),
],
)
def transact(self) -> None:
# Try to fetch the function from the ABI
client = make_contract_wallet(self.app, self.app.pargs.contract)
functions = client.functions
try:
function = functions[self.app.pargs.function]
except web3.exceptions.ABIFunctionNotFound:
raise Web3CliError(f"Function must be one of: {', '.join(functions)}")
# Parse function args
function_args, input_names = parse_abi_values(
self.app.pargs.args,
client.contract.abi,
self.app.pargs.function,
checksum_addresses=True,
resolve_address_fn=lambda x: resolve_address(x, chain=self.app.chain.name),
allow_exp_notation=True,
)
# Ask for confirmation
if not self.app.pargs.force:
print(
f"You are about to execute '{self.app.pargs.function}' on contract '{self.app.pargs.contract}' on the {self.app.chain.name} chain with the following arguments:"
)
for i, arg in enumerate(function_args):
print(f" {input_names[i]}: {arg}")
if self.app.pargs.value:
print(f" value: {self.app.pargs.value:,}")
yes_or_exit(logger=self.app.log.info)
# Send transaction
output = send_contract_tx(
self.app,
client,
function(*function_args),
value_in_wei=self.app.pargs.value,
)
# Print output
render_web3py(self.app, output)