encode_function_data

Encodes function arguments as contract calldata: the function's 4-byte selector followed by standard ABI argument bytes.

Example

Local

1
-- Encode transfer(address,uint256) calldata with raw token units
2
WITH abi AS (
3
SELECT '[{
4
"type": "function",
5
"name": "transfer",
6
"inputs": [
7
{ "name": "to", "type": "address" },
8
{ "name": "value", "type": "uint256" }
9
]
10
}]'::JSON AS abi_json
11
)
12
SELECT encode_function_data(
13
abi.abi_json,
14
'transfer(address,uint256)',
15
'0x000000000000000000000000000000000000dEaD'::ADDRESS,
16
parse_units('2500', 6)
17
) AS calldata
18
FROM abi;
Notebook ready in readonly mode.

API reference

Exact signatures with descriptions, requirements, inputs, returns, and examples.

encode_function_data(JSON, VARCHAR, ...args) #

Selects one ABI function by name, canonical signature, or selector literal and returns selector-prefixed standard ABI calldata.

Note: A single function-selector literal is not validated.

Inputs

Name Type Use
abi JSON

Contract ABI JSON containing the target function.

required positional
function_name VARCHAR

Function name, canonical signature, or 4-byte selector literal. Use a canonical signature when the ABI contains overloaded functions.

required positional
...args dynamic ANY

One SQL value per selected function input, in ABI order. The ABI determines each accepted SQL cast and the encoded Solidity type.

positional

Returns

Name Type
calldata BYTES

Selector-prefixed calldata. Returns BYTES containing the 4-byte function selector followed by ABI-encoded arguments.

Guidance

Build contract calldata

Use encode_function_data for simulation inputs, transaction preparation, or raw contract calls. Convert display amounts to the raw integer units required by the contract before encoding.

  • ERC-20 transfer calldata expects transfer(address,uint256).
  • Use explicit ADDRESS casts for address inputs.
  • Use parse_units('2500', 6) or another raw UINT256 expression for token amounts.
  • Use the canonical signature when an ABI has overloaded functions.

Local SQL

1
WITH abi AS (
2
SELECT '[{
3
"type": "function",
4
"name": "transfer",
5
"inputs": [
6
{ "name": "to", "type": "address" },
7
{ "name": "value", "type": "uint256" }
8
]
9
}]'::JSON AS abi_json
10
)
11
SELECT encode_function_data(
12
abi_json,
13
'transfer(address,uint256)',
14
'0x000000000000000000000000000000000000dEaD'::ADDRESS,
15
parse_units('2500', 6)
16
) AS calldata
17
FROM abi;
Notebook ready in readonly mode.

Selector literals

A selector literal can select the only function in a single-function ABI, but the current implementation does not compare that literal with the function's derived selector.

Use a function name or canonical signature when selector validation is required.

Choose the right ABI primitive

Choose by byte framing and direction: selector construction, standard arguments, calldata, packed bytes, raw words, calldata decoding, and return-data decoding are distinct operations.

  • function_selector hashes already-canonical signature text exactly; function_selector_json builds a canonical signature from one JSON ABI fragment first.
  • encode_function_args emits selector-free standard ABI arguments; encode_function_data prefixes those same bytes with the 4-byte selector.
  • abi_encode_packed concatenates compact scalar encodings without standard ABI offsets, lengths, or 32-byte padding; evm_abi_word always emits exactly one padded 32-byte word.
  • decode_function_data consumes and validates selector-prefixed calldata inputs; call_decode consumes selector-free function return bytes.

Local SQL

1
SELECT
2
function_selector('transfer(address,uint256)') AS selector,
3
encode_function_data(
4
'[{"type":"function","name":"transfer","inputs":[{"type":"address"},{"type":"uint256"}]}]'::JSON,
5
'transfer(address,uint256)',
6
'0x000000000000000000000000000000000000dEaD'::ADDRESS,
7
1::UINT256
8
) AS calldata;
Notebook ready in readonly mode.

Related functions

Category and tags

Tag
ABI