tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
423 stars 271 forks source link

how to estimate bandwidth and get same value as on tronscan #437

Closed kricha closed 10 months ago

kricha commented 10 months ago

I have transaction where I'm triggering method of smart contract and need to understand how could I calculate bandwidth to get same value as on tronscan

{
  "constant_result": [
    ""
  ],
  "energy_used": 45778,
  "internal_transactions": [
    {
      "callValueInfo": [
        {}
      ],
      "caller_address": "41d7a70c2e4faee3df9cb77f34395735d51b15f8ea",
      "hash": "35c19e227ec392b693452dc4665f2b07c45a700e061847d21253a1657b2a3b5c",
      "note": "63616c6c",
      "transferTo_address": "41c30287de466e77abb21650e275f51fddb60a4eb8"
    },
    {
      "callValueInfo": [
        {}
      ],
      "caller_address": "41d7a70c2e4faee3df9cb77f34395735d51b15f8ea",
      "hash": "82c1f716a761245cf8be7eec80bdcea8607b3004c7db5733330126c74d15e313",
      "note": "63616c6c",
      "transferTo_address": "41c30287de466e77abb21650e275f51fddb60a4eb8"
    },
    {
      "callValueInfo": [
        {}
      ],
      "caller_address": "41d7a70c2e4faee3df9cb77f34395735d51b15f8ea",
      "hash": "3622cbdb913adad5ae2c3856112886afc8634f122ad40c0bb5a58f989abb8892",
      "note": "63616c6c",
      "transferTo_address": "41c30287de466e77abb21650e275f51fddb60a4eb8"
    },
    {
      "callValueInfo": [
        {}
      ],
      "caller_address": "41d7a70c2e4faee3df9cb77f34395735d51b15f8ea",
      "hash": "a087984feb42c9146ca4a56f513da722cc606f7b424513c5192ce94006fb2e56",
      "note": "63616c6c",
      "transferTo_address": "41c30287de466e77abb21650e275f51fddb60a4eb8"
    }
  ],
  "logs": [
    {
      "address": "c30287de466e77abb21650e275f51fddb60a4eb8",
      "data": "000000000000000000000000000000000000000000000000000000000287406a",
      "topics": [
        "8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
        "0000000000000000000000002ba7a980c15bb7c970997e2a22cb0458be17ccd0",
        "000000000000000000000000d7a70c2e4faee3df9cb77f34395735d51b15f8ea"
      ]
    },
    {
      "address": "c30287de466e77abb21650e275f51fddb60a4eb8",
      "data": "0000000000000000000000000000000000000000000000000000000000000001",
      "topics": [
        "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "0000000000000000000000002ba7a980c15bb7c970997e2a22cb0458be17ccd0",
        "0000000000000000000000003f026515ba44de4fe9c31c56d9bac14ef37b4065"
      ]
    },
    {
      "address": "c30287de466e77abb21650e275f51fddb60a4eb8",
      "data": "0000000000000000000000000000000000000000000000000000000002874069",
      "topics": [
        "8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
        "0000000000000000000000002ba7a980c15bb7c970997e2a22cb0458be17ccd0",
        "000000000000000000000000d7a70c2e4faee3df9cb77f34395735d51b15f8ea"
      ]
    },
    {
      "address": "c30287de466e77abb21650e275f51fddb60a4eb8",
      "data": "0000000000000000000000000000000000000000000000000000000000000001",
      "topics": [
        "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "0000000000000000000000002ba7a980c15bb7c970997e2a22cb0458be17ccd0",
        "00000000000000000000000000fa54a749e75812a4f4854947ed7270c76772e6"
      ]
    }
  ],
  "result": {
    "result": true
  },
  "transaction": {
    "raw_data": {
      "contract": [
        {
          "parameter": {
            "type_url": "type.googleapis.com/protocol.TriggerSmartContract",
            "value": {
              "contract_address": "41d7a70c2e4faee3df9cb77f34395735d51b15f8ea",
              "data": "7f10c18b000000000000000000000000c30287de466e77abb21650e275f51fddb60a4eb8000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000020000000000000000000000003f026515ba44de4fe9c31c56d9bac14ef37b406500000000000000000000000000fa54a749e75812a4f4854947ed7270c76772e6000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
              "owner_address": "412ba7a980c15bb7c970997e2a22cb0458be17ccd0"
            }
          },
          "type": "TriggerSmartContract"
        }
      ],
      "expiration": 1697795328000,
      "ref_block_bytes": "a451",
      "ref_block_hash": "e7c444f22559e0cf",
      "timestamp": 1697795268824
    },
    "raw_data_hex": "0a02a4512208e7c444f22559e0cf408090f3e3b4315a9003081f128b030a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412d5020a15412ba7a980c15bb7c970997e2a22cb0458be17ccd0121541d7a70c2e4faee3df9cb77f34395735d51b15f8ea22a4027f10c18b000000000000000000000000c30287de466e77abb21650e275f51fddb60a4eb8000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000020000000000000000000000003f026515ba44de4fe9c31c56d9bac14ef37b406500000000000000000000000000fa54a749e75812a4f4854947ed7270c76772e600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000170d8c1efe3b431",
    "ret": [
      {}
    ],
    "txID": "343d4bc3ea8329b13d587012002d3eddd7ccda49edfbf1eef4681b10bc7f62c3",
    "visible": false
  }
}

I've tried different operations to calculate this value, but can't get same.. this.chain.transactionBuilder

triggerConstantContract(
              massPayAddress,
              'sendTokenV1TransferFrom(address,address[],uint256[])',
              {},
              [
                { type: 'address', value: symbolContractAddress },
                { type: 'address[]', value: recipients },
                { type: 'uint256[]', value: amountsForCall },
              ],
              from
            )
            .then((estimateResult) => {
              this.logger.info('SSSSS', { estimateResult });
              const bytes = Buffer.from(estimateResult.transaction.raw_data_hex, 'hex');

              console.log(
                // estimateResult,
                JSON.stringify(estimateResult.transaction).length,
                JSON.stringify(estimateResult.transaction.raw_data).length,
                estimateResult.transaction.raw_data_hex.length,
                bytes.length
              );
            });

with this I'm getting 1960 966 862 431

maybe somebody could help me with this

svein1010 commented 10 months ago

In Java-tron, the consumed bandwidth of a transaction depends on its byte size after being serialized by protobuf. In frontend, you can use the following function to calculate the bandwidth with a signedTransaction roughly:

function estimateBandwidth(signedTxn) {
    const DATA_HEX_PROTOBUF_EXTRA = 3;
    const MAX_RESULT_SIZE_IN_TX = 64;
    const A_SIGNATURE = 67;

    const len = signedTxn.raw_data_hex.length /2 + DATA_HEX_PROTOBUF_EXTRA + MAX_RESULT_SIZE_IN_TX  ;
    const signatureListSize = signedTxn.signature.length || 0;
    for(let i=0;i<signatureListSize;i++)
    {
        len += A_SIGNATURE;
    }
    return len;
}
kricha commented 10 months ago

In Java-tron, the consumed bandwidth of a transaction depends on its byte size after being serialized by protobuf. In frontend, you can use the following function to calculate the bandwidth with a signedTransaction roughly:

function estimateBandwidth(signedTxn) {
    const DATA_HEX_PROTOBUF_EXTRA = 3;
    const MAX_RESULT_SIZE_IN_TX = 64;
    const A_SIGNATURE = 67;

    const len = signedTxn.raw_data_hex.length /2 + DATA_HEX_PROTOBUF_EXTRA + MAX_RESULT_SIZE_IN_TX  ;
    const signatureListSize = signedTxn.signature.length || 0;
    for(let i=0;i<signatureListSize;i++)
    {
        len += A_SIGNATURE;
    }
    return len;
}

before signing I can't calculate this?

svein1010 commented 10 months ago

You can calculate a unsigned transaction but the result is not the ultimate result. Only the signed transaction will be stored in the blockchain.

kricha commented 10 months ago

You can calculate a unsigned transaction but the result is not the ultimate result. Only the signed transaction will be stored in the blockchain.

thanks