Skip to content

Commit

Permalink
feat: encodeFunctionResult
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jan 28, 2023
1 parent b4c1771 commit ee4d256
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-humans-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `encodeFunctionResult`.
4 changes: 2 additions & 2 deletions site/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ export const sidebar: DefaultTheme.Sidebar = {
link: '/docs/contract/encodeFunctionData',
},
{
text: 'encodeFunctionResult 🚧',
link: '/docs/contract/encodeFunctionData',
text: 'encodeFunctionResult',
link: '/docs/contract/encodeFunctionResult',
},
],
},
Expand Down
190 changes: 189 additions & 1 deletion site/docs/contract/encodeFunctionResult.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,191 @@
# encodeFunctionResult

TODO
Encodes structured return data into ABI encoded data. It is the opposite of [`decodeFunctionResult`](/docs/contract/decodeFunctionResult).

## Install

```ts
import { encodeFunctionResult } from 'viem';
```

## Usage

Given an ABI (`abi`) and a function (`functionName`), pass through the values (`values`) to encode:

::: code-group

```ts [example.ts]
import { encodeFunctionResult } from 'viem';

const data = encodeFunctionResult({
abi: wagmiAbi,
functionName: 'ownerOf',
value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'],
});
// '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac'
```

```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: 'tokenId', type: 'uint256' }],
name: 'ownerOf',
outputs: [{ name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
...
] as const;
```

```ts [client.ts]
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';

export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});
```

:::

### A more complex example

::: code-group

```ts [example.ts]
import { decodeFunctionResult } from 'viem'

const data = decodeFunctionResult({
abi: wagmiAbi,
functionName: 'getInfo',
value: [
{
foo: {
sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
x: 69420n,
y: true
},
sender: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
z: 69
}
]
})
// 0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000010f2c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000000045
```

```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [],
name: 'getInfo',
outputs: [
{
components: [
{
components: [
{
internalType: 'address',
name: 'sender',
type: 'address',
},
{
internalType: 'uint256',
name: 'x',
type: 'uint256',
},
{
internalType: 'bool',
name: 'y',
type: 'bool',
},
],
internalType: 'struct Example.Foo',
name: 'foo',
type: 'tuple',
},
{
internalType: 'address',
name: 'sender',
type: 'address',
},
{
internalType: 'uint32',
name: 'z',
type: 'uint32',
},
],
internalType: 'struct Example.Bar',
name: 'res',
type: 'tuple',
},
],
stateMutability: 'pure',
type: 'function',
},
...
] as const;
```

```ts [client.ts]
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';

export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});
```

:::

## Return Value

The decoded data. Type is inferred from the ABI.

## Parameters

### abi

- **Type:** [`Abi`](/docs/glossary/types#TODO)

The contract's ABI.

```ts
const data = encodeFunctionResult({
abi: wagmiAbi, // [!code focus]
functionName: 'ownerOf',
value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'],
});
```

### functionName

- **Type:** `string`

The function to encode from the ABI.

```ts
const data = encodeFunctionResult({
abi: wagmiAbi,
functionName: 'ownerOf', // [!code focus]
value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'],
});
```

### values

- **Type**: `Hex`

Return values to encode.

```ts
const data = encodeFunctionResult({
abi: wagmiAbi,
functionName: 'ownerOf',
value: ['0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac'], // [!code focus]
});
```
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test('exports actions', () => {
"encodeAbi": [Function],
"encodeBytes": [Function],
"encodeFunctionData": [Function],
"encodeFunctionResult": [Function],
"encodeHex": [Function],
"encodeRlp": [Function],
"estimateGas": [Function],
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export {
encodeAbi,
encodeBytes,
encodeFunctionData,
encodeFunctionResult,
encodeHex,
encodeRlp,
getAddress,
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type {
ExtractArgsFromAbi,
ExtractArgsFromEventDefinition,
ExtractArgsFromFunctionDefinition,
ExtractResultFromAbi,
} from './solidity'

export type {
Expand Down
25 changes: 25 additions & 0 deletions src/types/solidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ export type ExtractArgsFromAbi<
/** Arguments to pass contract method */ args: TArgs
}

export type ExtractResultFromAbi<
TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
TAbiFunction extends AbiFunction & { type: 'function' } = TAbi extends Abi
? ExtractAbiFunction<TAbi, TFunctionName>
: AbiFunction & { type: 'function' },
TArgs = AbiParametersToPrimitiveTypes<TAbiFunction['outputs']>,
FailedToParseArgs =
| ([TArgs] extends [never] ? true : false)
| (readonly unknown[] extends TArgs ? true : false),
> = true extends FailedToParseArgs
? {
/**
* Arguments to pass contract method
*
* Use a [const assertion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions) on {@link abi} for type inference.
*/
result?: readonly unknown[]
}
: TArgs extends readonly []
? { result?: never }
: {
/** Arguments to pass contract method */ result: TArgs
}

//////////////////////////////////////////////////////////////////////
// Event/Function Definitions

Expand Down
Loading

3 comments on commit ee4d256

@vercel
Copy link

@vercel vercel bot commented on ee4d256 Jan 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-benchmark – ./playgrounds/benchmark

viem-benchmark.vercel.app
viem-benchmark-git-main-wagmi-dev.vercel.app
viem-benchmark-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ee4d256 Jan 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-site – ./site

viem-site-git-main-wagmi-dev.vercel.app
viem-site.vercel.app
viem-site-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ee4d256 Jan 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-playground – ./playgrounds/dev

viem-playground-wagmi-dev.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground.vercel.app

Please sign in to comment.