|
1 | 1 | # encodeFunctionResult |
2 | 2 |
|
3 | | -TODO |
| 3 | +Encodes structured return data into ABI encoded data. It is the opposite of [`decodeFunctionResult`](/docs/contract/decodeFunctionResult). |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```ts |
| 8 | +import { encodeFunctionResult } from 'viem'; |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +Given an ABI (`abi`) and a function (`functionName`), pass through the values (`values`) to encode: |
| 14 | + |
| 15 | +::: code-group |
| 16 | + |
| 17 | +```ts [example.ts] |
| 18 | +import { encodeFunctionResult } from 'viem'; |
| 19 | + |
| 20 | +const data = encodeFunctionResult({ |
| 21 | + abi: wagmiAbi, |
| 22 | + functionName: 'ownerOf', |
| 23 | + value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'], |
| 24 | +}); |
| 25 | +// '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' |
| 26 | +``` |
| 27 | + |
| 28 | +```ts [abi.ts] |
| 29 | +export const wagmiAbi = [ |
| 30 | + ... |
| 31 | + { |
| 32 | + inputs: [{ name: 'tokenId', type: 'uint256' }], |
| 33 | + name: 'ownerOf', |
| 34 | + outputs: [{ name: '', type: 'address' }], |
| 35 | + stateMutability: 'view', |
| 36 | + type: 'function', |
| 37 | + }, |
| 38 | + ... |
| 39 | +] as const; |
| 40 | +``` |
| 41 | + |
| 42 | +```ts [client.ts] |
| 43 | +import { createPublicClient, http } from 'viem'; |
| 44 | +import { mainnet } from 'viem/chains'; |
| 45 | + |
| 46 | +export const publicClient = createPublicClient({ |
| 47 | + chain: mainnet, |
| 48 | + transport: http(), |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +::: |
| 53 | + |
| 54 | +### A more complex example |
| 55 | + |
| 56 | +::: code-group |
| 57 | + |
| 58 | +```ts [example.ts] |
| 59 | +import { decodeFunctionResult } from 'viem' |
| 60 | + |
| 61 | +const data = decodeFunctionResult({ |
| 62 | + abi: wagmiAbi, |
| 63 | + functionName: 'getInfo', |
| 64 | + value: [ |
| 65 | + { |
| 66 | + foo: { |
| 67 | + sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', |
| 68 | + x: 69420n, |
| 69 | + y: true |
| 70 | + }, |
| 71 | + sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', |
| 72 | + z: 69 |
| 73 | + } |
| 74 | + ] |
| 75 | +}) |
| 76 | +// 0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000010f2c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000000045 |
| 77 | +``` |
| 78 | + |
| 79 | +```ts [abi.ts] |
| 80 | +export const wagmiAbi = [ |
| 81 | + ... |
| 82 | + { |
| 83 | + inputs: [], |
| 84 | + name: 'getInfo', |
| 85 | + outputs: [ |
| 86 | + { |
| 87 | + components: [ |
| 88 | + { |
| 89 | + components: [ |
| 90 | + { |
| 91 | + internalType: 'address', |
| 92 | + name: 'sender', |
| 93 | + type: 'address', |
| 94 | + }, |
| 95 | + { |
| 96 | + internalType: 'uint256', |
| 97 | + name: 'x', |
| 98 | + type: 'uint256', |
| 99 | + }, |
| 100 | + { |
| 101 | + internalType: 'bool', |
| 102 | + name: 'y', |
| 103 | + type: 'bool', |
| 104 | + }, |
| 105 | + ], |
| 106 | + internalType: 'struct Example.Foo', |
| 107 | + name: 'foo', |
| 108 | + type: 'tuple', |
| 109 | + }, |
| 110 | + { |
| 111 | + internalType: 'address', |
| 112 | + name: 'sender', |
| 113 | + type: 'address', |
| 114 | + }, |
| 115 | + { |
| 116 | + internalType: 'uint32', |
| 117 | + name: 'z', |
| 118 | + type: 'uint32', |
| 119 | + }, |
| 120 | + ], |
| 121 | + internalType: 'struct Example.Bar', |
| 122 | + name: 'res', |
| 123 | + type: 'tuple', |
| 124 | + }, |
| 125 | + ], |
| 126 | + stateMutability: 'pure', |
| 127 | + type: 'function', |
| 128 | + }, |
| 129 | + ... |
| 130 | +] as const; |
| 131 | +``` |
| 132 | + |
| 133 | +```ts [client.ts] |
| 134 | +import { createPublicClient, http } from 'viem'; |
| 135 | +import { mainnet } from 'viem/chains'; |
| 136 | + |
| 137 | +export const publicClient = createPublicClient({ |
| 138 | + chain: mainnet, |
| 139 | + transport: http(), |
| 140 | +}); |
| 141 | +``` |
| 142 | + |
| 143 | +::: |
| 144 | + |
| 145 | +## Return Value |
| 146 | + |
| 147 | +The decoded data. Type is inferred from the ABI. |
| 148 | + |
| 149 | +## Parameters |
| 150 | + |
| 151 | +### abi |
| 152 | + |
| 153 | +- **Type:** [`Abi`](/docs/glossary/types#TODO) |
| 154 | + |
| 155 | +The contract's ABI. |
| 156 | + |
| 157 | +```ts |
| 158 | +const data = encodeFunctionResult({ |
| 159 | + abi: wagmiAbi, // [!code focus] |
| 160 | + functionName: 'ownerOf', |
| 161 | + value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'], |
| 162 | +}); |
| 163 | +``` |
| 164 | + |
| 165 | +### functionName |
| 166 | + |
| 167 | +- **Type:** `string` |
| 168 | + |
| 169 | +The function to encode from the ABI. |
| 170 | + |
| 171 | +```ts |
| 172 | +const data = encodeFunctionResult({ |
| 173 | + abi: wagmiAbi, |
| 174 | + functionName: 'ownerOf', // [!code focus] |
| 175 | + value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'], |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +### values |
| 180 | + |
| 181 | +- **Type**: `Hex` |
| 182 | + |
| 183 | +Return values to encode. |
| 184 | + |
| 185 | +```ts |
| 186 | +const data = encodeFunctionResult({ |
| 187 | + abi: wagmiAbi, |
| 188 | + functionName: 'ownerOf', |
| 189 | + value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'], // [!code focus] |
| 190 | +}); |
| 191 | +``` |
0 commit comments