Created
August 8, 2018 18:21
-
-
Save vladimirmyshkovski/3315b25c34bac81a4670ed46454720c5 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
from web3 import Web3, HTTPProvider | |
from web3.middleware import geth_poa_middleware | |
NET = 'rinkeby' | |
INFURA_API_KEY = '' | |
ETHERSCAN_API_KEY = '' | |
import requests | |
w3 = Web3(HTTPProvider('https://{}.infura.io/{}/'.format( | |
NET, | |
INFURA_API_KEY), | |
request_kwargs={'timeout': 60})) | |
w3.middleware_stack.inject(geth_poa_middleware, layer=0) | |
def get_abi_from_contract_address(address): | |
assert type(address) is str, ( | |
'address must have a type str' | |
) | |
assert address[:2] == '0x', ( | |
'First two symbols of address must be 0x' | |
) | |
address = w3.toChecksumAddress(address) | |
response = requests.get( | |
'https://api{}.etherscan.io/api?module=contract&action=getabi&address={}&apikey={}'.format( | |
'-rinkeby' if NET == 'rinkeby' else '', | |
address, | |
ETHERSCAN_API_KEY | |
) | |
) | |
json_response = response.json() | |
result = json_response['result'] | |
#assert result[0] == '[' and result[-1] == ']', ( | |
# 'ABI must have a type list' | |
#) | |
return result | |
def check_contract_functions(contract): | |
list_of_functions = ['name', 'totalSupply', 'decimals', 'balanceOf', | |
'symbol'] | |
for function in list_of_functions: | |
functions = contract.find_functions_by_name(function) | |
if not functions: | |
raise AssertionError('{} not found in contract'.format(function)) | |
def get_contract(contract_address): | |
assert type(contract_address) is str, ( | |
'contract address must have a type str' | |
) | |
assert contract_address[:2] == '0x', ( | |
'First two symbols of cotract address must be 0x' | |
) | |
contract_address = w3.toChecksumAddress(contract_address) | |
abi = get_abi_from_contract_address(contract_address) | |
contract = w3.eth.contract(address=contract_address, abi=abi) | |
check_contract_functions(contract) | |
return contract | |
def get_token_balance_with_decimals(balance, decimal): | |
return balance / 10**decimal | |
def get_balance_from_contract(address, contract_address): | |
contract_address = w3.toChecksumAddress(contract_address) | |
result = w3.eth.call({ | |
'to': contract_address, | |
'data': '0x70a08231000000000000000000000000' + address[2:], | |
}) | |
balance = int(result.hex(), 16) | |
return balance | |
def get_internal_contract_transaction(contract_address): | |
url = 'https://api{}.etherscan.io/api?module=account&action=tokentx&contractaddress={}&offset=1000000&sort=asc&apikey={}'.format( | |
'-rinkeby' if settings.NET == 'rinkeby' else '', | |
contract_address, | |
ETHERSCAN_API_KEY | |
) | |
response = requests.get(url) | |
json_response = response.json() | |
result = json_response['result'] | |
return result | |
def get_current_supply(address): | |
transactions = get_internal_contract_transaction(address) | |
return sum([int(tx['value']) for tx in transactions]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment