Skip to content

Commit

Permalink
refactor: rename encodeAbi & decodeAbi + add inferred return type…
Browse files Browse the repository at this point in the history
…s to `decodeAbiParameters` (#85)

* feat: rename encodeAbi/decodeAbi to encodeAbiParameters/decodeAbiParameters; refactor abi decode return type

* chore: changeset

* lint

* chore: update abitype

* fix

* fix

* fix

* docs: add getAbiItem

* docs: update
  • Loading branch information
jxom committed Feb 27, 2023
1 parent d664ff7 commit 2350d1a
Show file tree
Hide file tree
Showing 54 changed files with 2,668 additions and 2,278 deletions.
20 changes: 20 additions & 0 deletions .changeset/lazy-news-do.md
@@ -0,0 +1,20 @@
---
"viem": patch
---

**Breaking:** Renamed `encodeAbi` & `decodeAbi` to `encodeAbiParameters` & `decodeAbiParameters`, and modified API from named arguments to inplace arguments:

```diff
import {
- encodeAbi,
- decodeAbi,
+ encodeAbiParameters,
+ decodeAbiParameters,
} from 'viem'

-const result = encodeAbi({ params, values })
+const result = encodeAbiParameters(params, values)

-const result = decodeAbi({ params, data })
+const result = decodeAbiParameters(params, data)
```
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -21,6 +21,7 @@ bench
# @wagmi/cli
generated.ts

abi/**
chains/**
contract/**
ens/**
Expand Down
7 changes: 5 additions & 2 deletions .vscode/settings.json
Expand Up @@ -2,5 +2,8 @@
"editor.defaultFormatter": "rome.rome",
"editor.formatOnSave": true,
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
"typescript.enablePromptUseWorkspaceTsdk": true,
"[typescript]": {
"editor.defaultFormatter": "rome.rome"
}
}
9 changes: 8 additions & 1 deletion package.json
Expand Up @@ -32,6 +32,7 @@
"tsup": {
"entry": [
"src/index.ts",
"src/abi.ts",
"src/chains.ts",
"src/contract.ts",
"src/ens.ts",
Expand All @@ -43,6 +44,7 @@
]
},
"files": [
"/abi",
"/dist",
"/chains",
"/contract",
Expand All @@ -59,6 +61,11 @@
"module": "./dist/index.mts",
"default": "./dist/index.js"
},
"./abi": {
"types": "./dist/abi.d.ts",
"module": "./dist/abi.mts",
"default": "./dist/abi.js"
},
"./chains": {
"types": "./dist/chains.d.ts",
"module": "./dist/chains.mts",
Expand Down Expand Up @@ -108,7 +115,7 @@
"dependencies": {
"@noble/hashes": "^1.1.2",
"@wagmi/chains": "~0.2.8",
"abitype": "~0.3.0",
"abitype": "~0.5.0",
"idna-uts46-hx": "^4.1.2",
"isomorphic-ws": "^5.0.0",
"ws": "^8.12.0"
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 34 additions & 11 deletions site/.vitepress/sidebar.ts
Expand Up @@ -42,7 +42,6 @@ export const sidebar: DefaultTheme.Sidebar = {
},
{
text: 'Public Actions',
collapsible: true,
collapsed: true,
items: [
{ text: 'Introduction 🚧', link: '/docs/actions/public/introduction' },
Expand Down Expand Up @@ -179,7 +178,6 @@ export const sidebar: DefaultTheme.Sidebar = {
},
{
text: 'Wallet Actions',
collapsible: true,
collapsed: true,
items: [
{ text: 'Introduction', link: '/docs/actions/wallet/introduction' },
Expand Down Expand Up @@ -261,7 +259,6 @@ export const sidebar: DefaultTheme.Sidebar = {
},
{
text: 'Test Actions',
collapsible: true,
collapsed: true,
items: [
{ text: 'Introduction', link: '/docs/actions/test/introduction' },
Expand Down Expand Up @@ -378,7 +375,6 @@ export const sidebar: DefaultTheme.Sidebar = {
},
{
text: 'Contract',
collapsible: true,
collapsed: true,
items: [
{ text: 'Introduction', link: '/docs/contract/introduction' },
Expand Down Expand Up @@ -431,8 +427,8 @@ export const sidebar: DefaultTheme.Sidebar = {
text: 'Encoding',
items: [
{
text: 'decodeAbi',
link: '/docs/contract/decodeAbi',
text: 'decodeAbiParameters',
link: '/docs/contract/decodeAbiParameters',
},
{
text: 'decodeDeployData',
Expand All @@ -455,8 +451,8 @@ export const sidebar: DefaultTheme.Sidebar = {
link: '/docs/contract/decodeFunctionResult',
},
{
text: 'encodeAbi',
link: '/docs/contract/encodeAbi',
text: 'encodeAbiParameters',
link: '/docs/contract/encodeAbiParameters',
},
{
text: 'encodeDeployData',
Expand Down Expand Up @@ -484,7 +480,6 @@ export const sidebar: DefaultTheme.Sidebar = {
},
{
text: 'ENS',
collapsible: true,
collapsed: true,
items: [
{
Expand All @@ -508,9 +503,38 @@ export const sidebar: DefaultTheme.Sidebar = {
},
],
},
{
text: 'ABI',
collapsed: true,
items: [
{
text: 'decodeAbiParameters',
link: '/docs/abi/decodeAbiParameters',
},
{
text: 'encodeAbiParameters',
link: '/docs/abi/encodeAbiParameters',
},
{
text: 'getAbiItem',
link: '/docs/abi/getAbiItem'
},
{
text: 'parseAbiItem 🚧',
link: '/docs/abi/parseAbiItem'
},
{
text: 'parseAbiParameter 🚧',
link: '/docs/abi/parseAbiParameter'
},
{
text: 'parseAbiParameters 🚧',
link: '/docs/abi/parseAbiParameters'
}
],
},
{
text: 'Utilities',
collapsible: true,
collapsed: true,
items: [
{
Expand Down Expand Up @@ -666,7 +690,6 @@ export const sidebar: DefaultTheme.Sidebar = {
},
{
text: 'Glossary',
collapsible: true,
collapsed: true,
items: [
{ text: 'Terms', link: '/docs/glossary/terms' },
Expand Down
Expand Up @@ -2,7 +2,7 @@
head:
- - meta
- property: og:title
content: decodeAbi
content: decodeAbiParameters
- - meta
- name: description
content: Decodes ABI encoded data.
Expand All @@ -12,32 +12,32 @@ head:

---

# decodeAbi
# decodeAbiParameters

Decodes ABI encoded data using the [ABI specification](https://solidity.readthedocs.io/en/latest/abi-spec.html), given a set of ABI parameters (`inputs`/`outputs`) and the encoded ABI data.

The `decodeAbi` function is used by the other contract decoding utilities (ie. `decodeFunctionData`, `decodeEventLog`, etc).
The `decodeAbiParameters` function is used by the other contract decoding utilities (ie. `decodeFunctionData`, `decodeEventLog`, etc).

## Install

```ts
import { decodeAbi } from 'viem/contract'
import { decodeAbiParameters } from 'viem'
```

## Usage

The `decodeAbi` function accepts:
The `decodeAbiParameters` function takes in two parameters:

- a set of parameters (`params`), in the shape of the `inputs` or `outputs` attribute of an ABI event/function.
- a set of ABI Parameters (`params`), that can be in the shape of the `inputs` or `outputs` attribute of an ABI Item.
- the ABI encoded data (`data`) that correspond to the given `params`.

```ts
import { decodeAbi } from 'viem/contract'
import { decodeAbiParameters } from 'viem'

const values = decodeAbi({
data: '0x0000000000000000000000000000000000000000000000000000000000010f2c',
params: [{ name: 'x', type: 'uint32' }],
})
const values = decodeAbiParameters(
[{ name: 'x', type: 'uint32' }],
'0x0000000000000000000000000000000000000000000000000000000000010f2c',
)
// [69420]
```

Expand All @@ -47,32 +47,32 @@ The decoded data. Type is inferred from the ABI.

## Parameters

### data
### params

- **Type**: `Hex`
- **Type**: [`AbiParameter[]`](/TODO)

The ABI encoded data.
The set of ABI parameters to decode against `data`, in the shape of the `inputs` or `outputs` attribute of an ABI event/function.

These parameters must include valid [ABI types](https://docs.soliditylang.org/en/develop/abi-spec.html#types).

```ts
const values = decodeAbi({
data: '0x0000000000000000000000000000000000000000000000000000000000010f2c', // [!code focus]
params: [{ name: 'x', type: 'uint32' }],
})
const values = decodeAbiParameters(
[{ name: 'x', type: 'uint32' }], // [!code focus]
'0x0000000000000000000000000000000000000000000000000000000000010f2c',
)
```

### params

- **Type**: [`AbiParameter[]`](/TODO)
### data

The set of ABI parameters to decode against `data`, in the shape of the `inputs` or `outputs` attribute of an ABI event/function.
- **Type**: `Hex`

These parameters must include valid [ABI types](https://docs.soliditylang.org/en/develop/abi-spec.html#types).
The ABI encoded data.

```ts
const values = decodeAbi({
data: '0x0000000000000000000000000000000000000000000000000000000000010f2c',
params: [{ name: 'x', type: 'uint32' }], // [!code focus]
})
const values = decodeAbiParameters(
[{ name: 'x', type: 'uint32' }],
'0x0000000000000000000000000000000000000000000000000000000000010f2c', // [!code focus]
)
```

## More Examples
Expand All @@ -84,10 +84,10 @@ const values = decodeAbi({
```ts [example.ts]
import { abi } from './abi'

const values = decodeAbi({
data: '0x00000000000000000000000000000000000000000000000000000000000001a40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
params: abi[0].outputs
})
const values = decodeAbiParameters(
abi[0].outputs,
'0x00000000000000000000000000000000000000000000000000000000000001a40000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
)
// { x: 420n, y: true, z: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC' }
```

Expand Down

2 comments on commit 2350d1a

@vercel
Copy link

@vercel vercel bot commented on 2350d1a Feb 27, 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.vercel.app
viem.sh
www.viem.sh
viem-site-git-main-wagmi-dev.vercel.app
viem-site-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 2350d1a Feb 27, 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/browser

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

Please sign in to comment.