abi_encode_packed

Encodes scalar values with Solidity's packed ABI rules. The result has no selector, offsets, lengths, or 32-byte padding.

Example

Local

1
-- Encode a Uniswap V3-style path segment: tokenIn + fee + tokenOut
2
SELECT abi_encode_packed(
3
'["address","uint24","address"]'::JSON,
4
'0x0000000000000000000000000000000000000001'::ADDRESS,
5
500,
6
'0x0000000000000000000000000000000000000002'::ADDRESS
7
);
Notebook ready in readonly mode.

API reference

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

abi_encode_packed(JSON, ...args) #

Uses a constant JSON type list to encode the corresponding scalar values in order. A literal NULL type list returns NULL; arrays and tuples are unsupported.

Note: The type list must be constant at planning time. Only supported scalar types can be encoded. Packed encoding can be ambiguous when distinct inputs produce the same bytes. Arrays and tuples are not supported. A null type list returns null.

Inputs

Name Type Use
types JSON

Constant JSON array of Solidity ABI scalar types, with one type for each value.

required positional
...args dynamic ANY

One SQL value per type in the types array. Supported types are address, bool, uintN, intN, bytesN, bytes, and string; arrays and tuples are not supported.

positional

Returns

Name Type
packed_bytes BYTES

Packed ABI bytes. Returns the Solidity packed encoding as BYTES.

Guidance

Use packed encoding safely

Packed values are not self-describing. Different type and value sequences can produce identical bytes, so only hash packed data when the protocol defines an unambiguous layout.

  • Array and tuple packed encoding is currently unsupported.
  • Use abi_encode_packed for Uniswap V3 path segments: tokenIn + uint24 fee + tokenOut.
  • Use keccak256(abi_encode_packed(...)) for Solidity-style packed hashing.
  • Do not use packed encoding for normal contract calldata; use encode_function_data instead.

Local SQL

1
SELECT abi_encode_packed(
2
'["address","uint24","address"]'::JSON,
3
'0x0000000000000000000000000000000000000001'::ADDRESS,
4
500,
5
'0x0000000000000000000000000000000000000002'::ADDRESS
6
) AS v3_path_segment;
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