|
| 1 | +import subprocess |
1 | 2 | from typing import Any
|
2 | 3 | from hexbytes import HexBytes
|
3 | 4 | from requests.exceptions import ConnectionError
|
@@ -67,6 +68,44 @@ def contract(
|
67 | 68 | contract_factory = Contract.factory(self.w3, contract_name)
|
68 | 69 | return contract_factory(signer, address, **kwargs)
|
69 | 70 |
|
| 71 | + def cast_call(self, to: str, signature: str, *_args) -> str: |
| 72 | + """Use cast with default settings to interact with a smart contract |
| 73 | + without creating a new transaction on the blockchain. |
| 74 | + View `Foundry book <https://book.getfoundry.sh/reference/cast/cast-call>`_ for more details. |
| 75 | +
|
| 76 | + :param to: The address of the target contract. |
| 77 | + :type to: str |
| 78 | + :param signature: The function signature. |
| 79 | + :type signature: str |
| 80 | + :param `*args`: Function arguments. |
| 81 | + """ |
| 82 | + args = [] |
| 83 | + for a in _args: |
| 84 | + if isinstance(a, (int, list)): |
| 85 | + args.append(str(a)) |
| 86 | + elif isinstance(a, bool): |
| 87 | + args.append(str(a).lower()) |
| 88 | + else: |
| 89 | + args.append(a) |
| 90 | + ret = subprocess.run( |
| 91 | + [ |
| 92 | + "cast", |
| 93 | + "call", |
| 94 | + "--rpc-url", |
| 95 | + self.w3.provider.endpoint_uri, |
| 96 | + to, |
| 97 | + signature, |
| 98 | + *args, |
| 99 | + ], |
| 100 | + stdout=subprocess.PIPE, |
| 101 | + stderr=subprocess.PIPE, |
| 102 | + ) |
| 103 | + if ret.returncode != 0: |
| 104 | + err = ret.stderr.decode().strip() |
| 105 | + raise Exception(err[err.find("Context:"):]) |
| 106 | + ret = ret.stdout.decode().strip() |
| 107 | + return ret |
| 108 | + |
70 | 109 | def get_balance(self, address: str) -> int:
|
71 | 110 | """Returns the balance of the given account.
|
72 | 111 |
|
|
0 commit comments