如何使用 Web3j 获取 ERC721 Token 最近一次交易时间
要获取 ERC721 token ID 的最近一次交易时间,您可以使用以下代码:
// 创建 Web3j 客户端
Web3j web3j = Web3j.build(new HttpService('https://mainnet.infura.io/v3/your-project-id'));
// ERC721 合约地址
String contractAddress = '0x123...';
// ERC721 合约实例
ERC721 erc721 = ERC721.load(contractAddress, web3j, credentials, gasPrice, gasLimit);
// Token ID
BigInteger tokenId = BigInteger.valueOf(123);
// 获取最近一次交易
TransactionReceipt transactionReceipt = erc721.tokenTransferEvents(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST)
.filter(eventResponse -> eventResponse.tokenId.equals(tokenId))
.last()
.send();
// 获取交易时间
Date transactionTime = new Date(transactionReceipt.getBlockTimestamp().longValue() * 1000);
在上面的代码中,您需要将 contractAddress 替换为您的 ERC721 合约地址,并将 tokenId 替换为您要查询的 token ID。
然后,我们使用 load 方法加载 ERC721 合约实例,并使用 tokenTransferEvents 方法获取所有的 token 转移事件。我们使用 filter 方法筛选出与我们要查询的 token ID 相关的事件,并使用 last 方法获取最近一次事件。最后,我们从交易收据中获取时间戳,并将其转换为日期格式。
原文地址: https://www.cveoy.top/t/topic/fZhK 著作权归作者所有。请勿转载和采集!