# encode_function_data

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

Canonical HTML: <https://determica.com/docs/functions/encode_function_data>

- Kind: scalar function
- Category: [Contracts and ABI](/docs/category/contracts-and-abi.md)
- Tags: ABI

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

## Overload 1: encode_function_data(JSON, VARCHAR, ...args)

```sql
encode_function_data(JSON, VARCHAR, ...args)
```

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

- Kind: scalar
- Execution context: Offline
- Behavior: Abi Calldata Encode
- Execution modes: Offline
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: Deterministic
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: None documented

### Risks

- product_semantics:single_function_selector_literal_not_validated

### Inputs

- `abi` (JSON; required; positional) — Contract ABI JSON containing the target function.
- `function_name` (VARCHAR; required; positional) — Function name, canonical signature, or 4-byte selector literal. Use a canonical signature when the ABI contains overloaded functions.
- `function_args` (ANY; positional; variadic) — One SQL value per selected function input, in ABI order. The ABI determines each accepted SQL cast and the encoded Solidity type.

### Returns

Selector-prefixed calldata

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

- `calldata` (BYTES)

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

### Examples

_Local SQL · Local_

```sql
-- Encode transfer(address,uint256) calldata with raw token units
WITH abi AS (
  SELECT '[{
    "type": "function",
    "name": "transfer",
    "inputs": [
      { "name": "to", "type": "address" },
      { "name": "value", "type": "uint256" }
    ]
  }]'::JSON AS abi_json
)
SELECT encode_function_data(
  abi.abi_json,
  'transfer(address,uint256)',
  '0x000000000000000000000000000000000000dEaD'::ADDRESS,
  parse_units('2500', 6)
) AS calldata
FROM abi;
```

## 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.

```sql
WITH abi AS (
  SELECT '[{
    "type": "function",
    "name": "transfer",
    "inputs": [
      { "name": "to", "type": "address" },
      { "name": "value", "type": "uint256" }
    ]
  }]'::JSON AS abi_json
)
SELECT encode_function_data(
  abi_json,
  'transfer(address,uint256)',
  '0x000000000000000000000000000000000000dEaD'::ADDRESS,
  parse_units('2500', 6)
) AS calldata
FROM abi;
```

### 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.

```sql
SELECT
  function_selector('transfer(address,uint256)') AS selector,
  encode_function_data(
    '[{"type":"function","name":"transfer","inputs":[{"type":"address"},{"type":"uint256"}]}]'::JSON,
    'transfer(address,uint256)',
    '0x000000000000000000000000000000000000dEaD'::ADDRESS,
    1::UINT256
  ) AS calldata;
```

## Related functions

- [raw_call](/docs/functions/raw_call.md) — Usually Before
- [decode_function_data](/docs/functions/decode_function_data.md) — See Also
- [encode_function_args](/docs/functions/encode_function_args.md) — See Also
- [abi_encode_packed](/docs/functions/abi_encode_packed.md) — See Also
- [evm_abi_word](/docs/functions/evm_abi_word.md) — Alternative
