获取区块链代币历史转账事件的例子代码
时间: 2023-03-28 12:03:56 浏览: 147
以下是获取区块链代币历史转账事件的例子代码:
```
const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/your-project-id');
const contractAddress = 'x...'; // 代币合约地址
const fromBlock = ; // 起始块号
const toBlock = 'latest'; // 结束块号,设为 'latest' 表示最新块
// 获取 Transfer 事件
const transferEvent = new web3.eth.Contract([
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
], contractAddress);
// 获取历史事件
transferEvent.getPastEvents('Transfer', {
fromBlock,
toBlock
}, (error, events) => {
console.log(events);
});
```
希望能对你有所帮助。
阅读全文