From 2f64a8f248ff63db1b9f927252f20bf952411d7d Mon Sep 17 00:00:00 2001 From: Sergey Potekhin Date: Mon, 15 Feb 2021 12:53:00 +0300 Subject: [PATCH] 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 --- multicall/call.py | 11 +++++++++-- multicall/multicall.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/multicall/call.py b/multicall/call.py index 83143d9..7bb82a1 100644 --- a/multicall/call.py +++ b/multicall/call.py @@ -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) diff --git a/multicall/multicall.py b/multicall/multicall.py index ba856aa..574048b 100644 --- a/multicall/multicall.py +++ b/multicall/multicall.py @@ -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)