Ethereum BSC blockchain transaction data

0 votes

I'm trying to play with web3js over the Binance Smart Chain blockchain and I hit a wall understanding the transaction data. I don't understand what determines the number of transfers for a single transaction. And how to retrieve that data using web3js.

I would like to know the amount of BNB paid and the amount of the Tokens received in that transaction and vice versa if the transaction was about selling the tokens instead of buying.

I managed to get the Price paid and token amount but only for transactions where there are 2 Token transfers. But if there are 3 or more I can't manage to get this information.

web3.eth.getTransaction('0x899e7f3c2138d051eb5246850ded99d519ab65eba58e5f806245cf346ab40e83').then((result) => { console.log(result) console.log(web3.utils.fromWei(result.value)) let tx_data = result.input; let input_data = '0x' + tx_data.slice(10); // get only data without function selector let params = web3.eth.abi.decodeParameters([ { indexed: false, internalType: 'uint256', name: 'value', type: 'uint256' }, { indexed: false, internalType: 'uint256', name: 'ethReceived', type: 'uint256' }, ] , input_data); console.log(params) })


This portion of the code gives me data only for 2 token transfers. How to make it return the amount of paid/received cash/tokens no matter how many transfers there are in the transactions?? Is it possible?? From what I can see, always the 1st transfer and the last transfer in the transaction would be the values that I'm interested in. IS there an easy way to get those? I'm struggling with understanding this and getting work with the ABIs for decoding. Can they be somewhat generic?

Mar 11, 2022 in Blockchain by Soham
• 9,710 points
939 views

1 answer to this question.

0 votes

The "Tokens Transferred" information comes from event logs. Most token standards define an event Transfer(address indexed from, address indexed to, uint256 value), so you can look for logs of this event in the transaction.

Event logs are available in getTransactionReceipt(), not the regular getTransaction().

The indexed modifier in the event definition means that the value is going to be available in the topics property (topics[0] is the keccak256 hash of the event signature, following the indexed values).

const transferEventSignature = web3.utils.keccak256('Transfer(address,address,uint256)'); // 0xddf252... const jsonAbi = [{ "constant" :true, "inputs": [], "name": "decimals", "outputs": [{"name":"","type":"uint8"}], "type": "function" }]; // simplified JSON abi that is only able to read decimals web3.eth.getTransactionReceipt('0x899e7f3c2138d051eb5246850ded99d519ab65eba58e5f806245cf346ab40e83').then(async (result) => { for (const log of result.logs) { if (log.topics[0] !== transferEventSignature) { continue; // only interested in Transfer events } const from = web3.eth.abi.decodeParameter('address', log.topics[1]); const to = web3.eth.abi.decodeParameter('address', log.topics[2]); const value = web3.eth.abi.decodeParameter('uint256', log.data); const tokenContractAddress = log.address; const contractInstance = new web3.eth.Contract(jsonAbi, tokenContractAddress); const decimals = await contractInstance.methods.decimals().call(); console.log('From: ', from); console.log('To: ', to); console.log('Value: ', value); console.log('Token contract: ', tokenContractAddress); console.log('Token decimals: ', decimals); console.log('---'); } });

Output:

From: 0xC6A93610eCa5509E66f9B2a95A5ed1d576cC9b7d To: 0xE437fFf464c6FF2AA5aD5c15B4CCAD98DF38cF52 Value: 31596864050517135 Token contract: 0x78F1A99238109C4B834Ac100d1dfCf14e3fC321C Token decimals: 9 --- From: 0xE437fFf464c6FF2AA5aD5c15B4CCAD98DF38cF52 To: 0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16 Value: 4064578781674512 Token contract: 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c Token decimals: 18 --- From: 0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16 To: 0xC6A93610eCa5509E66f9B2a95A5ed1d576cC9b7d Value: 2552379452401563824 Token contract: 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56 Token decimals: 18

answered Mar 24, 2022 by Rahul
• 9,680 points

Related Questions In Blockchain

+1 vote
1 answer

How to store state data in Ethereum blockchain?

You won't have to overwrite the whole ...READ MORE

answered Apr 25, 2018 in Blockchain by Shashank
• 10,400 points
972 views
0 votes
1 answer

How to extract Ethereum Blockchain data?

You are using web3.eth.getTransaction(txHash) web3.eth.getTransaction(txHash) will only return info ...READ MORE

answered Jul 30, 2018 in Blockchain by slayer
• 29,370 points
1,703 views
0 votes
1 answer
0 votes
1 answer

How to set the hex-encoded data field in a Web3j Ethereum transaction?

You can use the "data" field of ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
3,005 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,428 views
0 votes
1 answer

.Ethereum/BSC blockchain transaction data

The "Tokens Transferred" information comes from event ...READ MORE

answered Apr 11, 2022 in Blockchain by Rahul
• 9,680 points
975 views
0 votes
1 answer

What is blockchain and Ethereum? Where is it used?

A blockchain is an immutable, sequential chain of ...READ MORE

answered Apr 11, 2022 in Blockchain by Rahul
• 9,680 points
500 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP