decode_function_data

Validates and decodes selector-prefixed function calldata into typed SQL columns defined by the function's ABI inputs.

Example

Local

1
-- Decode ERC20 transfer calldata
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
), payload AS (
12
SELECT encode_function_data(
13
abi_json,
14
'transfer(address,uint256)',
15
'0x000000000000000000000000000000000000dEaD'::ADDRESS,
16
parse_units('2500', 6)
17
) AS calldata
18
FROM abi
19
)
20
SELECT decoded.*
21
FROM payload
22
CROSS JOIN LATERAL decode_function_data(
23
'[{
24
"type": "function",
25
"name": "transfer",
26
"inputs": [
27
{ "name": "to", "type": "address" },
28
{ "name": "value", "type": "uint256" }
29
]
30
}]'::JSON,
31
'transfer(address,uint256)',
32
payload.calldata
33
) AS decoded;
Notebook ready in readonly mode.

API reference

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

decode_function_data(JSON, VARCHAR, BYTES) #

The ABI and function selection must be constant because they define the result columns; calldata may vary by input row.

Note: The ABI must be constant at planning time. The function selection must be constant at planning time. Calldata must include its function selector.

Inputs

Name Type Use
abi JSON

Contract ABI JSON containing the selected function inputs.

required positional
function_or_selector VARCHAR

Constant name, canonical signature, or 4-byte selector identifying one ABI function.

required positional
calldata BYTES

Raw calldata beginning with the 4-byte function selector followed by standard ABI argument bytes.

required positional

Input relation columns

Name Type
abi JSON

Foldable ABI JSON column that defines the input schema. Required

function_or_selector VARCHAR

Foldable function name, canonical signature, or 4-byte selector column. Required

calldata BYTES

Row-varying selector-prefixed calldata. Required

The ABI and function-selection columns must be constant across the relation. calldata may vary by row.

Returns

Name Type
calldata_selector BYTES4

The validated 4-byte selector read from calldata.

function_name VARCHAR

Selected ABI function name.

function_signature VARCHAR

Canonical signature of the selected ABI function.

...

ABI-derived input columns

After calldata_selector, function_name, and function_signature, returns one column per selected function input.

Columns
No fixed columns
Source
abi + function_or_selector
Names
ABI input names, or input{index} for unnamed inputs
Types
Mapped from ABI input types

Guidance

Inspect function calldata

Use decode_function_data to inspect calldata before simulation, signing, or policy evaluation. The function validates the calldata selector before decoding inputs.

  • Use a canonical signature such as transfer(address,uint256) when overloads are possible.
  • Decoded ERC-20 amount columns are raw token units; format them separately for display.

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
), payload AS (
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
18
)
19
SELECT decoded.*
20
FROM payload
21
CROSS JOIN LATERAL decode_function_data(
22
'[{
23
"type": "function",
24
"name": "transfer",
25
"inputs": [
26
{ "name": "to", "type": "address" },
27
{ "name": "value", "type": "uint256" }
28
]
29
}]'::JSON,
30
'transfer(address,uint256)',
31
payload.calldata
32
) AS decoded;
Notebook ready in readonly mode.

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