1

So I made program that listens for swap events and decode it and how would I get what is swapped eg usdt to ethereum or etherum to usdt, Here is output:

{
  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'
}
1
  • What's the contract source code? To interpret the event you have to look at the source code. It might happen that the information isn't encoded in the event.
    – Ismael
    Dec 1, 2021 at 13:54

1 Answer 1

0

If you're using the Web3 library you can instantiate the pair contract (0x3D2Bcc3Ec4f97b0d4209Fd947FC70F666F39D995) by providing its ABI and address (you can get the ABI via BSCScan API).

Then, just call the token1 and token2 methods and you'll get the addresses of the ERC20 tokens. You can see those methods and what they return here on 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.