我正在学习区块链开发,构建一个业余项目来构建投资组合。试图复制Poocoin/Dextools的底部,其中显示了合约的实时交易,并指示这是买入/卖出和BNB/USD金额。
我已经成功地完成了一半的工作,我可以实时登录";"转移";使用以下代码使用ethers库的事件侦听器。。
require("dotenv").config({});
const { ethers } = require("ethers");
var wsProvider = new ethers.providers.WebSocketProvider(process.env.WSS_URL);
// I use moralis speedy nodes as my provider
const CONTRACT_ABI = require("./myABI.json");
let contract = new ethers.Contract(process.env.TOKEN_CONTRACT_ADDRESS, CONTRACT_ABI, wsProvider);
contract.on("Transfer", (from, to, amount, value, event) => {
console.log(JSON.stringify(value));
});
我确实得到了以下结构的实时交易
{
"blockNumber": 14696508,
"blockHash": "0xd6aeef435bd4067e8b4ab75e3b7dfa2645d8d33726762e90bdeffc46c51d12da",
"transactionIndex": 125,
"removed": false,
"address": "0xc748673057861a797275CD8A068AbB95A902e8de",
"data": "0x0000000000000000000000000000000000000000000000002809d94ed7d3ca13",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000c736ca3d9b1e90af4230bd8f9626528b3d4e0ee0",
"0x000000000000000000000000e962dad670434e5f69f7ef8862a597fd4b94a9fc"
],
"transactionHash": "0x960aa714fdb4f2289a58b14d89facd1ea8035b03d27aba7f663bd0d179162662",
"logIndex": 694,
"event": "Transfer",
"eventSignature": "Transfer(address,address,uint256)",
"args": [
"0xc736cA3d9b1E90Af4230BD8F9626528B3D4e0Ee0",
"0xe962DaD670434e5f69f7Ef8862A597fD4B94A9FC",
{
"type": "BigNumber",
"hex": "0x2809d94ed7d3ca13"
}
]
}
但从这些数据来看,我无法理解这是买入还是卖出,以及这笔交易的金额是多少。我更深入地研究如何推断出";购买";或一个";出售";根据上述数据。我取得了一些进展,读到我可以查询";transactionHash";并获得更多细节,从而可以将其归类为买入或卖出?
Transfer
事件告诉令牌从地址A转移到地址B。
转移的上下文可以是任何内容:
- 用户在自己的钱包之间移动代币
- 向集中交易所存款
- 贸易等等
如果是买入、卖出、交易或除转让以外的任何交易,您不能根据此事件进行扣除。