-
-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * feat: encodeFunctionParams * format * fix: types
- Loading branch information
Showing
17 changed files
with
417 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"viem": patch | ||
--- | ||
|
||
Added `encodeFunctionParams`. |
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 |
---|---|---|
@@ -1,3 +1,146 @@ | ||
# encodeFunctionParams | ||
|
||
TODO | ||
Encodes the function name and parameters into an ABI encoded value (4byte selector & arguments). | ||
|
||
## Install | ||
|
||
```ts | ||
import { encodeFunctionParams } from 'viem' | ||
``` | ||
|
||
## Usage | ||
|
||
Below is a very basic example of how to encode a function to calldata. | ||
|
||
::: code-group | ||
|
||
```ts [example.ts] | ||
import { encodeFunctionParams } from 'viem' | ||
|
||
const data = encodeFunctionParams({ | ||
abi: wagmiAbi, | ||
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() | ||
}) | ||
``` | ||
|
||
::: | ||
|
||
### Passing Arguments | ||
|
||
If your function requires argument(s), you can pass them through with the `args` attribute. | ||
|
||
TypeScript types for `args` will be inferred from the function name & ABI, to guard you from inserting the wrong values. | ||
|
||
For example, the `balanceOf` function name below requires an **address** argument, and it is typed as `["0x${string}"]`. | ||
|
||
::: code-group | ||
|
||
```ts {8} [example.ts] | ||
import { encodeFunctionParams } from 'viem' | ||
import { publicClient } from './client' | ||
import { wagmiAbi } from './abi' | ||
|
||
const data = encodeFunctionParams({ | ||
abi: wagmiAbi, | ||
functionName: 'balanceOf', | ||
args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'] | ||
}) | ||
``` | ||
|
||
```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 | ||
|
||
`Hex` | ||
|
||
ABI encoded data (4byte function selector & arguments). | ||
|
||
## Parameters | ||
|
||
### abi | ||
|
||
- **Type:** [`Abi`](/docs/glossary/types#TODO) | ||
|
||
The contract's ABI. | ||
|
||
```ts | ||
const data = encodeFunctionParams({ | ||
abi: wagmiAbi, // [!code focus] | ||
functionName: 'totalSupply', | ||
}) | ||
``` | ||
|
||
### functionName | ||
|
||
- **Type:** `string` | ||
|
||
The function to encode from the ABI. | ||
|
||
```ts | ||
const data = encodeFunctionParams({ | ||
abi: wagmiAbi, | ||
functionName: 'totalSupply', // [!code focus] | ||
}) | ||
``` | ||
|
||
### args (optional) | ||
|
||
- **Type:** Inferred from ABI. | ||
|
||
Arguments to pass to function call. | ||
|
||
```ts | ||
const data = encodeFunctionParams({ | ||
abi: wagmiAbi, | ||
functionName: 'balanceOf', | ||
args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'] // [!code focus] | ||
}) | ||
``` |
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
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,140 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
import { encodeFunctionParams } from './encodeFunctionParams' | ||
|
||
test('foo()', () => { | ||
expect( | ||
encodeFunctionParams({ | ||
abi: [ | ||
{ | ||
inputs: [], | ||
name: 'foo', | ||
outputs: [], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const, | ||
functionName: 'foo', | ||
}), | ||
).toEqual('0xc2985578') | ||
expect( | ||
encodeFunctionParams({ | ||
// @ts-expect-error | ||
abi: [ | ||
{ | ||
name: 'foo', | ||
outputs: [], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const, | ||
functionName: 'foo', | ||
}), | ||
).toEqual('0xc2985578') | ||
}) | ||
|
||
test('bar(uint256)', () => { | ||
expect( | ||
encodeFunctionParams({ | ||
abi: [ | ||
{ | ||
inputs: [ | ||
{ | ||
internalType: 'uint256', | ||
name: 'a', | ||
type: 'uint256', | ||
}, | ||
], | ||
name: 'bar', | ||
outputs: [], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const, | ||
functionName: 'bar', | ||
args: [1n], | ||
}), | ||
).toEqual( | ||
'0x0423a1320000000000000000000000000000000000000000000000000000000000000001', | ||
) | ||
}) | ||
|
||
test('getVoter((uint256,bool,address,uint256))', () => { | ||
expect( | ||
encodeFunctionParams({ | ||
abi: [ | ||
{ | ||
inputs: [ | ||
{ | ||
components: [ | ||
{ | ||
internalType: 'uint256', | ||
name: 'weight', | ||
type: 'uint256', | ||
}, | ||
{ | ||
internalType: 'bool', | ||
name: 'voted', | ||
type: 'bool', | ||
}, | ||
{ | ||
internalType: 'address', | ||
name: 'delegate', | ||
type: 'address', | ||
}, | ||
{ | ||
internalType: 'uint256', | ||
name: 'vote', | ||
type: 'uint256', | ||
}, | ||
], | ||
internalType: 'struct Ballot.Voter', | ||
name: 'voter', | ||
type: 'tuple', | ||
}, | ||
], | ||
name: 'getVoter', | ||
outputs: [], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const, | ||
functionName: 'getVoter', | ||
args: [ | ||
{ | ||
delegate: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', | ||
vote: 41n, | ||
voted: true, | ||
weight: 69420n, | ||
}, | ||
], | ||
}), | ||
).toEqual( | ||
'0xf37414670000000000000000000000000000000000000000000000000000000000010f2c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000000029', | ||
) | ||
}) | ||
|
||
test("errors: function doesn't exist", () => { | ||
expect(() => | ||
encodeFunctionParams({ | ||
abi: [ | ||
{ | ||
inputs: [], | ||
name: 'foo', | ||
outputs: [], | ||
stateMutability: 'nonpayable', | ||
type: 'function', | ||
}, | ||
] as const, | ||
// @ts-expect-error | ||
functionName: 'bar', | ||
}), | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"Function \\"bar\\" not found on ABI. | ||
Make sure you are using the correct ABI and that the function exists on it. | ||
Docs: https://viem.sh/docs/contract/encodeFunctionParams | ||
Version: viem@1.0.2" | ||
`) | ||
}) |
Oops, something went wrong.
bb9e88a
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-git-main-wagmi-dev.vercel.app
viem-benchmark-wagmi-dev.vercel.app
viem-benchmark.vercel.app
bb9e88a
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-wagmi-dev.vercel.app
viem-site.vercel.app
viem-site-git-main-wagmi-dev.vercel.app
bb9e88a
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.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground-wagmi-dev.vercel.app