# AbiFunction.decodeResult

ABI-decodes a function's result according to the ABI Item's output types (`outputs`).

:::tip
This function is typically used to decode contract function return values (e.g. the response of an `eth_call` or the `input` property of a Transaction).

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 data =
  '0x000000000000000000000000000000000000000000000000000000000000002a'

const totalSupply = AbiFunction.from(
  'function totalSupply() returns (uint256)'
)

const output = AbiFunction.decodeResult(totalSupply, data)
// @log: 42n
```

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 data = '0x000000000000000000000000000000000000000000000000000000000000002a'

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

const output = AbiFunction.decodeResult(totalSupply, data)
// @log: 42n
```

### ABI-shorthand

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

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

const data = '0x000000000000000000000000000000000000000000000000000000000000002a'

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

const output = AbiFunction.decodeResult(
  erc20Abi, // [!code focus]
  'totalSupply', // [!code focus]
  data
)
// @log: 42n
```

### End-to-end

Below is an end-to-end example of using `AbiFunction.decodeResult` to decode the result 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.
const data = AbiFunction.encodeData(balanceOf, [
  '0xd2135CfB216b74109775236E36d4b433F1DF507B'
])

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

// 4. Decode the Function Output. // [!code focus]
const balance = AbiFunction.decodeResult(
  balanceOf,
  response
) // [!code focus]
// @log: 42n
```

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

## Definition

```ts
function decodeResult<abi, name, args, abiFunction, allNames, as>(
  abi: abi | Abi.Abi | readonly unknown[],
  name: Hex.Hex | (name extends allNames ? name : never),
  data: Hex.Hex,
  options?: decodeResult.Options<as>,
): decodeResult.ReturnType<abiFunction, as>
```

**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)`

### data

* **Type:** `Hex.Hex`

ABI-encoded function output

### options

* **Type:** `decodeResult.Options<as>`
* **Optional**

Decoding options

#### options.as

* **Type:** `"Array" | "Object" | as`
* **Optional**

Whether the decoded values should be returned as an `Object` or `Array`.

#### options.checksumAddress

* **Type:** `boolean`
* **Optional**

Whether decoded addresses should be checksummed.

## Return Type

Decoded function output

`decodeResult.ReturnType<abiFunction, as>`
