tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
388 stars 259 forks source link

How to proper decode a smart contract input data? #518

Closed itxtoledo closed 2 days ago

itxtoledo commented 2 months ago

Im trying to listen for all blockchain transactions, i already have done receiving TRX, but im stuck on decode TRC20 transactions from block.transactions.

This is my code for decode data from "TriggerSmartContract.data" (from params.tx.raw_data.contract[0].parameter.value.data):

          const functionCalled = contract.decodeInput(
            contractEvent.parameter.value.data
          );

          if (functionCalled.name !== "transfer") {
            return null;
          }

          console.log("functionCalled", functionCalled);
          console.log("contractEvent.parameter.value.data", contractEvent.parameter.value.data);

And this is the output:

functionCalled { name: 'transfer', params: [ true ] }
contractEvent.parameter.value.data a9059cbb0000000000000000000000004e1f6e4bc98dd381eb0784c8eae2d50bd40d22a80000000000000000000000000000000000000000000000000000000000989680
functionCalled { name: 'transfer', params: [ true ] }
contractEvent.parameter.value.data a9059cbb000000000000000000000000af0932f8c32a6b9bbbe282d6b5193f74c50dc57300000000000000000000000000000000000000000000000000000000000000068e7780

In this example two "transfer" events, two parameters comes wrong, but the second in special has a data more longer than expected, why? And How to proper decode these datas?

ferdinand026 commented 2 months ago

@itxtoledo Here is an example code about how to decode parameters.

itxtoledo commented 2 months ago

@itxtoledo Here is an example code about how to decode parameters.

yes but in my example the second data will throw this: "The encoded string is not valid. Its length must be a multiple of 64."

start940315 commented 2 months ago

Where does contractEvent.parameter.value.data come from? And which version of TronWeb did you use?