Ethereum BSC blockchain transaction data

0 votes

I managed to get the Price paid and tokens 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 to return me always 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, the 1st transfer and the last transfer in the transaction would always 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??

Apr 7 in Blockchain by Soham
• 8,730 points

1 answer to this question.

0 votes

The "Tokens Transferred" information comes from event logs. Most token standards define an event Transfer, so you can look for logs of this event in the transaction.

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

The "unindexed" values are then stored in the data property - ordered according to the order of their definition.

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',; 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('---'); } });


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

answered Apr 11 by Rahul
• 8,980 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
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,310 points
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,190 points
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
0 votes
1 answer
0 votes
0 answers

Error: Unexpected token o in JSON at position 1

I have been working with Interacting a ...READ MORE

Mar 6, 2019 in Blockchain by saeedi
• 120 points

edited Mar 6, 2019 by Omkar 1,621 views
0 votes
1 answer

Ethereum/BSC blockchain transaction data

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

answered Mar 24 in Blockchain by Rahul
• 8,980 points
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 in Blockchain by Rahul
• 8,980 points
Send OTP
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP