mirror of
https://github.com/FlipsideCrypto/multicall.py.git
synced 2026-02-06 02:36:46 +00:00
add multicall
This commit is contained in:
parent
ee7aee83a5
commit
92034e9b15
@ -9,7 +9,7 @@ class Network(IntEnum):
|
||||
xDai = 100
|
||||
|
||||
|
||||
MULTICALL_ADDRESS = {
|
||||
MULTICALL_ADDRESSES = {
|
||||
Network.Mainnet: '0xeefBa1e63905eF1D7ACbA5a8513c70307C1cE441',
|
||||
Network.Kovan: '0x2cc8688C5f75E365aaEEb4ea8D6a480405A48D2A',
|
||||
Network.Rinkeby: '0x42Ad527de7d4e9d9d011aC45B31D8551f8Fe9821',
|
||||
|
||||
23
multicall/multicall.py
Normal file
23
multicall/multicall.py
Normal file
@ -0,0 +1,23 @@
|
||||
from typing import List
|
||||
|
||||
from web3.auto import w3
|
||||
|
||||
from multicall import Call
|
||||
from multicall.constants import MULTICALL_ADDRESSES
|
||||
|
||||
|
||||
class Multicall:
|
||||
def __init__(self, calls: List[Call]):
|
||||
self.calls = calls
|
||||
|
||||
def __call__(self):
|
||||
aggregate = Call(
|
||||
MULTICALL_ADDRESSES[w3.eth.chainId],
|
||||
'aggregate((address,bytes)[])(uint256,bytes[])',
|
||||
)
|
||||
args = [[[call.target, call.data] for call in self.calls]]
|
||||
block, outputs = aggregate(args)
|
||||
result = {}
|
||||
for call, output in zip(self.calls, outputs):
|
||||
result.update(call.decode_output(output))
|
||||
return result
|
||||
21
tests/test_multicall.py
Normal file
21
tests/test_multicall.py
Normal file
@ -0,0 +1,21 @@
|
||||
from multicall import Call, Multicall
|
||||
|
||||
CHAI = '0x06AF07097C9Eeb7fD685c692751D5C66dB49c215'
|
||||
|
||||
|
||||
def from_wei(val):
|
||||
return val / 1e18
|
||||
|
||||
|
||||
def from_ray(val):
|
||||
return val / 1e27
|
||||
|
||||
|
||||
def test_multicall():
|
||||
multi = Multicall([
|
||||
Call(CHAI, 'totalSupply()(uint256)', [['supply', from_wei]]),
|
||||
Call(CHAI, ['balanceOf(address)(uint256)', CHAI], [['balance', from_ray]]),
|
||||
])
|
||||
result = multi()
|
||||
assert isinstance(result['supply'], float)
|
||||
assert isinstance(result['balance'], float)
|
||||
@ -1,4 +1,4 @@
|
||||
from eth_abi import encode_abi, decode_abi
|
||||
from eth_abi import encode_abi
|
||||
from multicall import Signature
|
||||
|
||||
args = ((1, 2, 3), '0x' + 'f' * 40, b'data')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user