Last active
January 23, 2019 21:31
-
-
Save banteg/3d12d06ee78703cc99a221897b9fdcd2 to your computer and use it in GitHub Desktop.
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
import requests | |
from tqdm import trange | |
from web3 import Web3, HTTPProvider | |
w3 = Web3(HTTPProvider('http://127.0.0.1:8545', {'timeout': 120})) | |
def get_contract(address): | |
resp = requests.get( | |
'http://api.etherscan.io/api', | |
params=dict(module='contract', action='getabi', address=address, format='raw') | |
) | |
return w3.eth.contract(address, abi=resp.json()) | |
def stream_logs(event, from_block, to_block, batch_size): | |
for start in trange(from_block, to_block, batch_size): | |
for log in event.createFilter(fromBlock=start, toBlock=start+step-1).get_all_entries(): | |
yield log | |
def count_total(logs): | |
total_wei = sum(log['args']['wad'] for log in logs) | |
return w3.fromWei(total_wei, 'ether') | |
def main(): | |
dai = get_contract('0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359') | |
first_block, last_block = 4752008, w3.eth.blockNumber | |
mint = count_total(stream_logs(dai.events.Mint, first_block, last_block, 4096)) | |
burn = count_total(stream_logs(dai.events.Burn, first_block, last_block, 4096)) | |
print( | |
f'mint: {mint}', | |
f'burn: {burn}', | |
f'circ: {mint-burn}', | |
sep='\n' | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment