Getting the price of an NFT transaction using web3 and python
I had a surprisingly hard time finding a straightforward answer to “How do I get the ETH price of an OpenSea NFT transaction using…
上記記事を実行するためのPythonコードを記載する。
![](https://informationsciencememo.com/wp-content/uploads/cocoon-resources/blog-card-cache/0f2e631275901a7ea4fdc6b40971e012.jpg)
Ethereum Transaction Hash (Txhash) Details | Etherscan
Ethereum (ETH) detailed transaction info for txhash 0x7399ca3b55d9966a5db015314d81d3419fec365296109304911393ec253689a8. The transaction status, block confirmati...
全く同じでは面白くないので、上記のTransactionのETH価格(CryptoPunks #1)を取得する。
![](https://informationsciencememo.com/wp-content/uploads/2023/10/image-5.png)
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
コメント