Add ability to initialize web3 instance and pass it (#3)

- Back compatability saved. In case `_w3` parameter remains empty, `web3.auto` is used
- Should be pretty useful in case web3 should be initialized once or if it's custom implementation
This commit is contained in:
Sergey Potekhin 2021-02-15 12:53:00 +03:00 committed by GitHub
parent 089b39067d
commit 2f64a8f248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -4,13 +4,20 @@ from multicall import Signature
class Call:
def __init__(self, target, function, returns=None):
def __init__(self, target, function, returns=None, _w3=None):
self.target = to_checksum_address(target)
if isinstance(function, list):
self.function, *self.args = function
else:
self.function = function
self.args = None
if _w3 is None:
self.w3 = w3
else:
self.w3 = _w3
self.signature = Signature(self.function)
self.returns = returns
@ -32,5 +39,5 @@ class Call:
def __call__(self, args=None):
args = args or self.args
calldata = self.signature.encode_data(args)
output = w3.eth.call({'to': self.target, 'data': calldata})
output = self.w3.eth.call({'to': self.target, 'data': calldata})
return self.decode_output(output)

View File

@ -7,13 +7,20 @@ from multicall.constants import MULTICALL_ADDRESSES
class Multicall:
def __init__(self, calls: List[Call]):
def __init__(self, calls: List[Call], _w3=None):
self.calls = calls
if _w3 is None:
self.w3 = w3
else:
self.w3 = _w3
def __call__(self):
aggregate = Call(
MULTICALL_ADDRESSES[w3.eth.chainId],
MULTICALL_ADDRESSES[self.w3.eth.chainId],
'aggregate((address,bytes)[])(uint256,bytes[])',
None,
self.w3
)
args = [[[call.target, call.data] for call in self.calls]]
block, outputs = aggregate(args)