# AbiFunction.encodeData

ABI-encodes function arguments (`inputs`), prefixed with the 4 byte function selector.

:::tip
This function is typically used to encode a contract function and its arguments for contract calls (e.g. `data` parameter of an `eth_call` or `eth_sendTransaction`).

See the [End-to-end Example](#end-to-end).
:::

## Imports

:::code-group
```ts [Named]
import { AbiFunction } from 'ox'
```

```ts [Entrypoint]
import * as AbiFunction from 'ox/AbiFunction'
```
:::

## Examples

```ts twoslash
import { AbiFunction } from 'ox'

const approve = AbiFunction.from(
  'function approve(address, uint256)'
)

const data = AbiFunction.encodeData(
  // [!code focus]
  approve, // [!code focus]
  ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n] // [!code focus]
) // [!code focus]
// @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c'
```

You can extract an ABI Function from a JSON ABI with [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi):

```ts twoslash
// @noErrors
import { Abi, AbiFunction } from 'ox'

const erc20Abi = Abi.from([...]) // [!code hl]
const approve = AbiFunction.fromAbi(erc20Abi, 'approve') // [!code hl]

const data = AbiFunction.encodeData(
  approve,
  ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
)
// @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c'
```

### ABI-shorthand

You can specify an entire ABI object and a function name as parameters to [`AbiFunction.encodeData`](/api/AbiFunction/encodeData):

```ts twoslash
// @noErrors
import { Abi, AbiFunction } from 'ox'

const erc20Abi = Abi.from([...])

const data = AbiFunction.encodeData(
  erc20Abi, // [!code focus]
  'approve', // [!code focus]
  ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
)
```

### End-to-end

Below is an end-to-end example of using `AbiFunction.encodeData` to encode the input of a `balanceOf` contract call on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2).

```ts twoslash
import 'ox/window'
import { Abi, AbiFunction } from 'ox'

// 1. Extract the Function from the Contract's ABI.
const abi = Abi.from([
  // ...
  {
    name: 'balanceOf',
    type: 'function',
    inputs: [{ name: 'account', type: 'address' }],
    outputs: [{ name: 'balance', type: 'uint256' }],
    stateMutability: 'view'
  }
  // ...
])
const balanceOf = AbiFunction.fromAbi(abi, 'balanceOf')

// 2. Encode the Function Input. // [!code focus]
const data = AbiFunction.encodeData(
  // [!code focus]
  balanceOf, // [!code focus]
  ['0xd2135CfB216b74109775236E36d4b433F1DF507B'] // [!code focus]
) // [!code focus]

// 3. Perform the Contract Call.
const response = await window.ethereum!.request({
  method: 'eth_call',
  params: [
    {
      data,
      to: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2'
    }
  ]
})

// 4. Decode the Function Output.
const balance = AbiFunction.decodeResult(
  balanceOf,
  response
)
```

:::note
For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface.
:::

## Definition

```ts
function encodeData<abi, name, args, abiFunction, allNames>(
  abi: abi | Abi.Abi | readonly unknown[],
  name: Hex.Hex | (name extends allNames ? name : never),
  args: encodeData.Args<abiFunction>,
): Hex.Hex
```

**Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L1095)

## Parameters

### abi

* **Type:** `abi | Abi.Abi | readonly unknown[]`

### name

* **Type:** `Hex.Hex | (name extends allNames ? name : never)`

### args

* **Type:** `encodeData.Args<abiFunction>`

Function arguments

## Return Type

ABI-encoded function name and arguments

`Hex.Hex`
