abi.encodeFunctionData <src>
Added in 6.4.0.
This method builds the calldata for a smart contract method: the 4-byte function
selector followed by the ABI-encoded parameters. It is the building block behind
contract calls, and is useful when you need the raw data field yourself (e.g. to pass
to triggerSmartContract / triggerConstantContract, or for an offline transaction).
For just the parameter encoding (without the selector), use abi.encodeParamsV2ByABI.
Usage
tronWeb.utils.abi.encodeFunctionData(funcABI, args);
Parameters
| Parameter | Description | Data Type |
|---|---|---|
| funcABI | A single function ABI fragment — { type: 'function', name, inputs, outputs? } — not the whole ABI array. | object (FunctionFragment) |
| args | Optional. The function arguments, in ABI order. Defaults to []. | array |
Notes
- Pass one function fragment, not the whole contract ABI array — extract the entry for the method (e.g.
abi.find((f) => f.name === 'transfer')). - The returned hex string has no
0xprefix. args.lengthmust equalfuncABI.inputs.length, otherwise it throwsInvalid arguments provided: expected N arguments, got M.- A non-
functionfragment (or one without aname) throwsInvalid function ABI fragment provided: only "function" type fragments with a name are supported. - TRON's
trcTokenis kept in the selector signature (TVM convention) but encoded asuint256in the parameters. Base58 addresses insideargsare converted to hex automatically.
Returns
hex string - The calldata: selector (8 hex chars) + ABI-encoded parameters, without a 0x prefix.
Example
// transfer(address,uint256)
const funcABI = {
name: 'transfer',
type: 'function',
inputs: [
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
outputs: [],
};
const data = tronWeb.utils.abi.encodeFunctionData(funcABI, ['TMVQGm1qAQYVdetCeGRRkTWYYrLXuHK2HC', 100]);
'a9059cbb0000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf0000000000000000000000000000000000000000000000000000000000000064'
// a function without parameters returns only the selector
tronWeb.utils.abi.encodeFunctionData({ name: 'totalSupply', type: 'function', inputs: [], outputs: [] });
'18160ddd'
// trcToken stays in the selector, value is encoded as uint256
tronWeb.utils.abi.encodeFunctionData(
{ name: 'transfer', type: 'function', inputs: [{ name: 'id', type: 'trcToken' }], outputs: [] },
[1000100]
);
'e7bf839c00000000000000000000000000000000000000000000000000000000000f42a4'