用Web3.py、Infura和Graph查询以太坊数据
共 3994字,需浏览 8分钟
·
2022-04-28 00:37
本文我们将使用Web3.py这个Python库直接查询以太坊数据。Web3.py是一个为与Ethereum区块链交互而建立的Python库。有了它,我们可以为去中心化的应用程序建立各种核心功能。我们可以直接与智能合约互动,收集区块链数据,并发送交易。让我们开始安装Web3.py。
pip install web3
from web3 importWeb3
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID'))
# Get information about the latest block
w3.eth.getBlock('latest')
# Get the ETH balance of an address
w3.eth.getBalance('YOUR_ADDRESS_HERE')
https://zapper.fi/dashboard
)这样的产品功能,跟踪我们代币的美元价值如何?首先,需要扫描我们的地址,看看持有哪些代币。为了做到这一点,我们将与各个代币的智能合约进行交互。这些合约的地址看起来像我们的钱包地址,只不过这些是合约地址。在这个地址上有智能合约代码。代币将遵守ERC-20标准,使我们更容易与这些合约进行交互。一个ERC-20合约默认具有以下功能:
function name() public view returns (string)
function symbol() public view returns (string)
function decimals() public view returns (uint8)
function totalSupply() public view returns (uint256)
function balanceOf(address _owner) public view returns (uint256 balance)
function transfer(address _to, uint256 _value) public returns (bool success)
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
function approve(address _spender, uint256 _value) public returns (bool success)
function allowance(address _owner, address _spender) public view returns (uint256 remaining)
balanceOf
是让我们看到我们查询的钱包地址持有多少代币的函数。
import json
ABI = json.loads('[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf",
"outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]')
wallet_address = 'YOUR_ADDRESS_HERE'
wallet_address = Web3.toChecksumAddress(wallet_address)
token_contract_address = '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f'
token_contract_address = Web3.toChecksumAddress(token_contract_address)
# define contract
contract = w3.eth.contract(token_contract_address, abi=ABI)
# call contract and get data from balanceOf for argument wallet_address
raw_balance = contract.functions.balanceOf(wallet_address).call()
# convert the value from Wei to Ether
synthetix_value = Web3.fromWei(raw_balance, 'ether')
toChecksumAddress()
来确保我们的地址是校验格式的。我们使用 fromWei()
将我们的Wei价格转换为 ether。1ETH是1E18 Wei。
from gql import gql, Client
from gql.transport.requests importRequestsHTTPTransport
sample_transport=RequestsHTTPTransport(
url='https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2',
verify=True,
retries=5,
)
client = Client(
transport=sample_transport
)
# Get the value of SNX/ETH
query = gql('''
query {
pair(id: "0x43ae24960e5534731fc831386c07755a2dc33d47"){
reserve0
reserve1
}
}
''')
response = client.execute(query)
snx_eth_pair = response['pair']
eth_value = float(snx_eth_pair['reserve1']) / float(snx_eth_pair['reserve0'])
# Get the value of ETH/DAI
query = gql('''
query {
pair(id: "0xa478c2975ab1ea89e8196811f51a7b7ade33eb11"){
reserve0
reserve1
}
}
''')
response = client.execute(query)
eth_dai_pair = response['pair']
dai_value = float(eth_dai_pair['reserve0']) / float(eth_dai_pair['reserve1'])
snx_dai_value = eth_value * dai_value
- 点击下方阅读原文加入社区会员 -