Skip to content

Commit

Permalink
add multicall
Browse files Browse the repository at this point in the history
  • Loading branch information
banteg committed Jan 28, 2020
1 parent ee7aee8 commit 92034e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion multicall/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Network(IntEnum):
xDai = 100


MULTICALL_ADDRESS = {
MULTICALL_ADDRESSES = {
Network.Mainnet: '0xeefBa1e63905eF1D7ACbA5a8513c70307C1cE441',
Network.Kovan: '0x2cc8688C5f75E365aaEEb4ea8D6a480405A48D2A',
Network.Rinkeby: '0x42Ad527de7d4e9d9d011aC45B31D8551f8Fe9821',
Expand Down
23 changes: 23 additions & 0 deletions multicall/multicall.py
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions tests/test_multicall.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion tests/test_signature.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit 92034e9

Please sign in to comment.