Web3js

如何獲取為解碼的交換事件交換的令牌?

  • April 19, 2022

所以我製作了監聽交換事件並對其進行解碼的程序,以及如何獲得交換的內容,例如 usdt 到 ethereum 或 etherum 到 usdt,這是輸出:

{
 name: 'Swap',
 events: [
   {
     name: 'sender',
     type: 'address',
     value: '0x10ed43c718714eb63d5aa57b78b54704e256024e'
   },
   { name: 'amount0In', type: 'uint256', value: '0' },
   {
     name: 'amount1In',
     type: 'uint256',
     value: '1202473582518654197'
   },
   {
     name: 'amount0Out',
     type: 'uint256',
     value: '777540000000000000000'
   },
   { name: 'amount1Out', type: 'uint256', value: '0' },
   {
     name: 'to',
     type: 'address',
     value: '0x431731d312dffa688a37d81af8874a58776eacfd'
   }
 ],
 address: '0x3D2Bcc3Ec4f97b0d4209Fd947FC70F666F39D995'
}

如果您使用 Web3 庫,您可以0x3D2Bcc3Ec4f97b0d4209Fd947FC70F666F39D995通過提供其 ABI 和地址來實例化對合約 ( )(您可以通過 BSCScan API 獲取 ABI)。

然後,只需呼叫token1andtoken2方法,您將獲得 ERC20 代幣的地址。您可以在 BSCScan 上查看這些方法以及它們返回的內容。

IE。:

// Hardcode the ABI or get it via API (eg.: via BSCScan API)
const abi = {};

// Instantiate the contract
const contract = new web3.eth.Contract(abi, '0x3D2Bcc3Ec4f97b0d4209Fd947FC70F666F39D995')

// Get the addresses of the pair's tokens
const [token1Addr, token2Addr] = await Promise.all([
 contract.methods.token1().call(),
 contract.methods.token2().call()
]);

// From here you can instantiate the token contracts and
// call their `symbol()` method to find out what they are

引用自:https://ethereum.stackexchange.com/questions/114475