作者:书友74562696 | 来源:互联网 | 2022-12-26 21:43
我正在使用web3
js.我想要地址的令牌交易清单(非交易清单).我已经使用了该getBlock
函数,但它仅用于特定块.我没有阻止列表,我只想按地址列表.所以请帮助我如何获得令牌交易清单
1> dcusan..:
我使用getPastEvents用1.0 web3-eth
和web3-utils
beta 实现了这个.
在standardAbi
为ERC20令牌我从这个检索回购
import Eth from "web3-eth";
import Utils from "web3-utils";
async function getERC20TransactionsByAddress({
tokenContractAddress,
tokenDecimals,
address,
fromBlock
}) {
// initialize the ethereum client
const eth = new Eth(
Eth.givenProvider || "ws://some.local-or-remote.node:8546"
);
const currentBlockNumber = await eth.getBlockNumber();
// if no block to start looking from is provided, look at tx from the last day
// 86400s in a day / eth block time 10s ~ 8640 blocks a day
if (!fromBlock) fromBlock = currentBlockNumber - 8640;
const cOntract= new eth.Contract(standardAbi, tokenContractAddress);
const transferEvents = await contract.getPastEvents("Transfer", {
fromBlock,
filter: {
isError: 0,
txreceipt_status: 1
},
topics: [
Utils.sha3("Transfer(address,address,uint256)"),
null,
Utils.padLeft(address, 64)
]
});
return transferEvents
.sort((evOne, evTwo) => evOne.blockNumber - evTwo.blockNumber)
.map(({ blockNumber, transactionHash, returnValues }) => {
return {
transactionHash,
confirmations: currentBlockNumber - blockNumber,
amount: returnValues._value * Math.pow(10, -tokenDecimals)
};
});
}
我没有测试过这段代码,因为它稍微修改了我的版本,它绝对可以优化,但我希望它有所帮助.
我正在根据影响Transfer
事件的主题进行过滤,以address
params中提供的为目标.