Skip to content

Instantly share code, notes, and snippets.

@godsflaw
Created August 7, 2019 15:28
Show Gist options
  • Save godsflaw/371e41d82802c27344b80522d2b18185 to your computer and use it in GitHub Desktop.
Save godsflaw/371e41d82802c27344b80522d2b18185 to your computer and use it in GitHub Desktop.
#!/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()
@godsflaw
Copy link
Author

godsflaw commented Aug 7, 2019

This is how I built that index. For example, you can build off the existing index file I sent you with:

./build_cdp_to_address_index.py --cdpid=62449 >> cdp-id-by-owner.idx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment