Created
August 1, 2018 20:49
-
-
Save vladimirmyshkovski/eaa7625ccd014e9e8a0bc2c667b48ad9 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 | |
from django.conf import settings | |
import requests | |
NET = 'rinkeby' | |
INFURA_API_KEY = 'Твой infura.io API KEY' | |
ETHERSCAN_API_KEY = 'Твой etherscan.com API KEY' | |
# Смотри документацию infura.io здесь: https://infura.io/docs | |
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): | |
""" | |
Эта функция получает ABI смарт контракта | |
Она обращается к etherscan API, потому что так проще :) | |
""" | |
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'] | |
return result | |
def check_contract_functions(contract): | |
""" | |
Эта функция проверяет соответствует ли данный контракт стандарту ERC20 | |
Функции в списке list_of_functions - те функции, которые обязательно должны присутствовать, | |
в контракте ERC20. | |
""" | |
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): | |
""" | |
Эта функция возвращает экземпляр web3.py Contract | |
""" | |
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_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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment