// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract TransactionHistory { event Transaction(address indexed from, address indexed to, uint256 indexed amount); function getTransactionHistory(address account) public { uint256 count = 0; uint256 currentBlock = block.number; uint256 lastBlock = currentBlock - 10; while (count < 10 && currentBlock > lastBlock) { currentBlock--; bytes32 blockHash = blockhash(currentBlock); if (blockHash != bytes32(0)) { uint256 txCount = block.number - currentBlock; for (uint256 i = 0; i < txCount && count < 10; i++) { bytes32 txHash = blockhash(currentBlock + 1); (address from, address to, uint256 amount) = parseTransaction(txHash); if (account == from || account == to) { emit Transaction(from, to, amount); count++; } } } } } function parseTransaction (bytes32 txHash) private pure returns (address, address, uint256) { (address from, address to, uint256 amount) = abi.decode(abi.encodePacked(txHash), (address, address, uint256)); return (from, to, amount); } }
时间: 2024-02-15 08:28:18 浏览: 207
_SPDX-License-Identifier:MIT_pragma_Solidity_^0._0x8a47f3
这是一个基于以太坊的智能合约,名为TransactionHistory。它记录了过去10个区块内涉及到指定账户的交易记录,并提供getTransactionHistory函数来获取这些交易记录。当有符合条件的交易时,会触发Transaction事件,将交易的发送方、接收方、金额等信息记录下来。私有函数parseTransaction用于解析交易的哈希值,获取其中的发送方、接收方、金额等信息。
阅读全文