https://medium.com/@jseid212/getting-price-of-nft-transaction-using-web3-and-python-8a36c58d3cef
上記記事を実行するためのPythonコードを記載する。
https://etherscan.io/tx/0x7399ca3b55d9966a5db015314d81d3419fec365296109304911393ec253689a8
全く同じでは面白くないので、上記のTransactionのETH価格(CryptoPunks #1)を取得する。

import datetime
import json
import requests
from web3 import Web3
# Connect to web3
INFURA_API_KEY = "1234_your_project_id_here"
infura_url = "https://mainnet.infura.io/v3/{0}".format(INFURA_API_KEY)
w3 = Web3(Web3.HTTPProvider(infura_url))
w3.isConnected() # this should be true
# Get transaction information
transaction_id = '0x7399ca3b55d9966a5db015314d81d3419fec365296109304911393ec253689a8'
tx = w3.eth.get_transaction(transaction_id)
# Get ETH transaction amount
tx_value_wei = tx['value']
tx_value_eth = Web3.fromWei(tx_value_wei, 'ether')
# Get address from and to
tx_from = tx['from']
tx_to = tx['to']
# Get timestamp
tx_block_number = tx['blockNumber']
tx_timestamp = w3.eth.getBlock(tx_block_number).timestamp
tx_datetime = datetime.datetime.utcfromtimestamp(tx_timestamp).strftime('%Y-%m-%d %H:%M:%S')
print (tx_value_eth, tx_from, tx_to, tx_datetime)
Out:
60 0xcf6165e56C3383978F6Eaaf58b314Af41569c0f0 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB 2020-11-30 18:17:35

コメント