forked from banteg/multicall.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters