-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
403 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
"viem": patch | ||
--- | ||
|
||
Added `encodeFunctionParams`. | ||
Added `encodeFunctionData`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"viem": patch | ||
--- | ||
|
||
- **Breaking**: Renamed `encodeFunctionParams` to `encodeFunctionData`. | ||
- Added `decodeFunctionData`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# decodeConstructorData | ||
|
||
TODO |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# decodeErrorData | ||
|
||
TODO |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# decodeFunctionData | ||
|
||
Decodes ABI encoded data (4byte selector & arguments) into a function name and arguments. | ||
|
||
The opposite of [`encodeFunctionData`](/docs/contract/encodeFunctionData). | ||
|
||
## Install | ||
|
||
```ts | ||
import { decodeFunctionData } from 'viem' | ||
``` | ||
|
||
## Usage | ||
|
||
Below is a very basic example of how to encode a function to calldata. | ||
|
||
::: code-group | ||
|
||
```ts [example.ts] | ||
import { decodeFunctionData } from 'viem' | ||
|
||
const { functionName } = decodeFunctionData({ | ||
abi: wagmiAbi, | ||
data: '0xc2985578' | ||
}) | ||
// { functionName: 'totalSupply' } | ||
``` | ||
|
||
```ts | ||
export const wagmiAbi = [ | ||
... | ||
{ | ||
inputs: [], | ||
name: "totalSupply", | ||
outputs: [{ name: "", type: "uint256" }], | ||
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() | ||
}) | ||
``` | ||
|
||
::: | ||
|
||
### Extracting Arguments | ||
|
||
If your calldata includes argument(s) after the 4byte function signature, you can extract them with the `args` return value. | ||
|
||
::: code-group | ||
|
||
```ts {7} [example.ts] | ||
import { decodeFunctionData } from 'viem' | ||
import { publicClient } from './client' | ||
import { wagmiAbi } from './abi' | ||
|
||
const { functionName, args } = decodeFunctionData({ | ||
abi: wagmiAbi, | ||
data: '0x0423a1320000000000000000000000000000000000000000000000000000000000000001' | ||
}) | ||
// { functionName: 'balanceOf', args: [1n] } | ||
``` | ||
|
||
```ts [abi.ts] | ||
export const wagmiAbi = [ | ||
... | ||
{ | ||
inputs: [{ name: "owner", type: "address" }], | ||
name: "balanceOf", | ||
outputs: [{ name: "", type: "uint256" }], | ||
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() | ||
}) | ||
``` | ||
|
||
::: | ||
|
||
## Return Value | ||
|
||
```ts | ||
{ | ||
functionName: string; | ||
args: unknown[] | undefined; | ||
} | ||
``` | ||
|
||
Decoded ABI function data. | ||
|
||
### functionName | ||
|
||
- **Type**: `string` | ||
|
||
The decoded function name. | ||
|
||
### args | ||
|
||
- **Type**: `unknown[] | undefined` | ||
|
||
The decoded function arguments. | ||
|
||
## Parameters | ||
|
||
### abi | ||
|
||
- **Type:** [`Abi`](/docs/glossary/types#TODO) | ||
|
||
The contract's ABI. | ||
|
||
```ts | ||
const { functionName } = decodeFunctionData({ | ||
abi: wagmiAbi, // [!code focus] | ||
data: '0xc2985578' | ||
}) | ||
``` | ||
|
||
### data | ||
|
||
- **Type:** `Hex` | ||
|
||
The encoded calldata. | ||
|
||
```ts | ||
const { functionName } = decodeFunctionData({ | ||
abi: wagmiAbi, | ||
data: '0xc2985578' // [!code focus] | ||
}) | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# encodeConstructorData | ||
|
||
TODO |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# encodeErrorData | ||
|
||
TODO |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
849653f
There was a problem hiding this comment.
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-git-main-wagmi-dev.vercel.app
viem-playground.vercel.app
viem-playground-wagmi-dev.vercel.app
849653f
There was a problem hiding this comment.
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.vercel.app
viem-site-git-main-wagmi-dev.vercel.app
viem-site-wagmi-dev.vercel.app
849653f
There was a problem hiding this comment.
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-wagmi-dev.vercel.app
viem-benchmark-git-main-wagmi-dev.vercel.app