Skip to content

Commit e0f2ff4

Browse files
committed
add cast_call()
1 parent d5bd9e1 commit e0f2ff4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

cheb3/connection.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import subprocess
12
from typing import Any
23
from hexbytes import HexBytes
34
from requests.exceptions import ConnectionError
@@ -67,6 +68,44 @@ def contract(
6768
contract_factory = Contract.factory(self.w3, contract_name)
6869
return contract_factory(signer, address, **kwargs)
6970

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+
70109
def get_balance(self, address: str) -> int:
71110
"""Returns the balance of the given account.
72111

0 commit comments

Comments
 (0)