How to get token transfer information

Hi,

I’m writing a dapp using Moralis. I have a smart contract that is using the pancake router to perform a swap. I’m able to see the transaction hash on so on but i would love to be able to get the information like this :

I need to get token addresses and quantities moved. I need this so i can create a modal where i show exactly how many tokens have been swapped like in any dex out there after a swap.

Thanks! <3

1 Like

Can you give an example of a transaction hash?

2 Likes

sometimes you can extract that information from processing the logs/events

2 Likes

Hi,

Thanks for your reply!

For example this hash - https://testnet.bscscan.com/tx/0xbb04749a592f19f7d4c56746e44dd1b502d422eb933e8fef61ff496e6e290c5d

I would love to display in my dapp this with something like -
“swapped 0.005126804006066409 bnb for 2 busd”

Thanks!

2 Likes

you can find that information here: https://testnet.bscscan.com/tx/0xbb04749a592f19f7d4c56746e44dd1b502d422eb933e8fef61ff496e6e290c5d#eventlog

for example:

Swap (index_topic_1 address sender, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out, index_topic_2 address to)

Topics
Data
  • amount0In :0
  • amount1In :5126804006066409
  • amount0Out :2000000000000000000
  • amount1Out :0

and you can get transaction info (including logs/events) with Moralis this way:
https://docs.moralis.io/moralis-server/web3-sdk/native#gettransaction-new

const options = {
chain: “eth”,
transaction_hash: “0xd66e99990b16d3ab23b0e3027a78a34b59cd75843c2331f809dae1965f477246”
};
t = await Moralis.Web3API.native.getTransaction(options);

2 Likes

Hi,

Thanks for your reply! In the logs i do not see anywhere the token address transferred noted by lets say “from X token” “to Y” or w/e. Can you please show me where in the transaction logs do you see both tokens?

Have in mind I’m using React and the web3Api that i’m able to use is Web3Api.getTransaction

Thanks!!

2 Likes

I’ll try to come up tomorrow with an example, today I’m having some problems with that particular API.

2 Likes

options = {
chain: “bsc testnet”,
transaction_hash: “0xbb04749a592f19f7d4c56746e44dd1b502d422eb933e8fef61ff496e6e290c5d”
};
t = await Moralis.Web3API.native.getTransaction(options);

=>

logs = t[‘logs’]
data1 = logs[1][“data”]
data1_int = parseInt(Number(data1), 10)
=>
5126804006066409

data1_eth = Moralis.Units.FromWei(data1_int)
=> 0.005126804006066409

3 Likes

Okay cool but how do i get the actual token addresses (or token names) of the tokens involved. I understand how to get the quantities now thanks, but i also need to get which are those tokens. Do you have any idea how? Thanks

1 Like

logs[1][“address”]
=>
‘0xae13d989dac2f0debff460ac112a837c89baa7cd’ => WBNB token

you have more information in that event, I only extracted the value/qunatity there

2 Likes

Hi,

I know time has passed after this but i want to re-itterate…

This functionality for my project is needed so that i’m able to have a modal where i present what type of transactions have taken place (approve,swap,transfer) and display them.

With the current solution which you have showed here, this is not working since the swap method will have different log length (for example for a swap from BUSD–> BNB will have 8 logs but a swap BUSD–> SPARTA, which goes through BUSD --> BNB --> SPARTA the logs will be atleast 9 or more). Thus breaking our logic to check x and y log to get the tokens addresses and conclude which type of function it is.

Can you assist with a solution to this from Moralis?

1 Like

can you give an example of two transactions that have different number of logs?

the function name that was called should be easy to identify from me method name (hash/topic)

2 Likes

Hi, thanks for your fast answer!

I just ran 2 swaps to produce transaction logs and show you:

https://testnet.bscscan.com/tx/0x742f7e4bd25bbb07a4a19b21cfaade9cba60300da43d4c3c17dc258702f98e7e

https://testnet.bscscan.com/tx/0xc1f597fe8ba8263cabe861a7465481c1b3d44a275816186030ca8ddd6b07f016

1 Like

from logs it looks like one transaction made only one swap and the other transaction did 2 swaps, you could identify those two swap events or how many there are

2 Likes

Yes this additional swap comes from the point i made above - the router of Pancakeswap is figuring out that this route is better, thus going with it.

What i dont understand is how to identify all types of swap events - for example how can i know how many logs the swap will produce so that i use your logic above with specifying the exact log number (log[1]) for which i want to get the token address? If all swaps had exact same log structure, your solution will always work, but in this case i cannot figure out any way that it will work. Sometimes we will need log[8] to get the 2nd token address, other times it will be log[9]… it is quite confusing. Can you clarify? Thanks!

1 Like

you will check all the logs and identify which one of them is a swap event:

Swap (index_topic_1 address sender, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out, index_topic_2 address to)View Source

Topics
0 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822

that 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822 corresponds to that Swap function and it is computed as

x = Web3.utils.sha3('Swap(address,uint256,uint256,uint256,uint256,address)')
=>
'0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822'
2 Likes

Alright i understand what you mean!

One last thing - after i have identified this : how can i see which tokens were involved in the swap. Again this is a problem with logs number since for some swaps the token addresses will be in log[0] and log[7] but for other that will be, for example, log[0] and log [8]. I need to figure out a way not to hardcode those.

For example here - https://testnet.bscscan.com/tx/0x742f7e4bd25bbb07a4a19b21cfaade9cba60300da43d4c3c17dc258702f98e7e#eventlog
The token we want to swap into (WBNB) is on the 6th log place top to bottom, whereas here -
https://testnet.bscscan.com/tx/0xc1f597fe8ba8263cabe861a7465481c1b3d44a275816186030ca8ddd6b07f016#eventlog
The token we want to swap into (DAI) is on the 9th log place top to bottom. This is extremely confusing. Thanks for your time to deal with me <3. Appreciate your time.

1 Like

it looks like there is a sequence of events of type Transfer, Sync, Swap

so in first case you have the Transfer at logs[6] and in second case you have a transfer on logs[6] and after that Sync on logs[7] and Swap on logs[8] and after that a new Transfer on logs[9] followed by Sync and Swap

2 Likes

Hey,

For a little test app of mine, I would like to recreate exactly this information about swaps you screenshoted. I read through this feed, but it isn’t 100% clear to me how to get this information from the logs. The differing log size makes a generic implementation complicated, I feel like. Did you manage to reconstruct it? I would really appreciate some advice.
Thanks already for the support here :slight_smile:

1 Like

Yes it works as cryptokid suggested. They all follow same pattern of transfer,sync,swap so no matter how many logs there are the tokenTo (bottom) in the swap will be logs[length-2] (aka the last transfer) and the first token aka tokenFrom(top) will be logs[0]

2 Likes