Created
August 7, 2019 15:28
-
-
Save godsflaw/371e41d82802c27344b80522d2b18185 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python3 | |
import json | |
import click | |
import appdirs | |
import requests | |
from pathlib import Path | |
from graphqlclient import GraphQLClient | |
from web3.auto import w3 | |
# | |
# WEB3_PROVIDER_URI=https://parity.expotrading.com/ | |
# | |
TUB = '0x448a5065aebb8e423f0896e6c5d525c040f59af3' | |
GQLURL = 'https://sai-mainnet.makerfoundation.com/v1' | |
QUERY = ''' | |
{ | |
allCups(condition: {deleted: false}, filter: {id: {greaterThanOrEqualTo: XXXX}}, orderBy: ID_ASC) { | |
nodes { | |
id | |
lad | |
} | |
} | |
} | |
''' | |
cache = Path(appdirs.user_cache_dir('contracts')) | |
cache.mkdir(exist_ok=True) | |
def get_contract(address): | |
'''Get contract interface and cache it.''' | |
f = cache / f'{address}.json' | |
if not f.exists(): | |
# cache the response | |
abi = get_contract_abi(address) | |
f.write_text(json.dumps(abi)) | |
abi = json.loads(f.read_text()) | |
return w3.eth.contract(w3.toChecksumAddress(address), abi=abi) | |
def get_contract_abi(address): | |
'''Get contract interface from Etherscan.''' | |
resp = requests.get('http://api.etherscan.io/api', params={ | |
'module': 'contract', | |
'action': 'getabi', | |
'format': 'raw', | |
'address': address, | |
}) | |
try: | |
return resp.json() | |
except json.JSONDecodeError: | |
return | |
def build_index(cdps): | |
for cdp in cdps: | |
lad = cdp["lad"] | |
owner = lad | |
try: | |
proxy = get_contract(lad) | |
owner = proxy.functions.owner().call() | |
except: | |
pass | |
click.secho(f'{cdp["id"]:>6}: {owner:>12}', bold=True) | |
@click.command() | |
@click.option( | |
'--cdpid', | |
default=0, | |
prompt='CDP id to start at', | |
help='The CDP id start building the index from.' | |
) | |
def main(cdpid): | |
client = GraphQLClient(GQLURL) | |
result = client.execute(QUERY.replace('XXXX', f'{cdpid}')) | |
data = json.loads(result) | |
build_index(data["data"]["allCups"]["nodes"]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how I built that index. For example, you can build off the existing index file I sent you with: