Skip to content

Commit

Permalink
Merge branch 'alpha' of https://github.com/wagmi-dev/viem into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 7, 2023
2 parents 609765f + 4004a75 commit 6d85bb7
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-feet-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `estimateFeesPerGas` and `estimateMaxPriorityFeePerGas` exports to `viem/actions`.
5 changes: 0 additions & 5 deletions .changeset/lemon-eels-approve.md

This file was deleted.

20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# viem

## 1.10.4

### Patch Changes

- [#1153](https://github.com/wagmi-dev/viem/pull/1153) [`040dda64`](https://github.com/wagmi-dev/viem/commit/040dda64340fd70602d951efea1454afe9e7c198) Thanks [@vmichalik](https://github.com/vmichalik)! - Updated Polygon Mumbai RPC URL.

## 1.10.3

### Patch Changes

- [#1147](https://github.com/wagmi-dev/viem/pull/1147) [`5001e9f7`](https://github.com/wagmi-dev/viem/commit/5001e9f72cada8be8fed8c858c37d59d1bea8425) Thanks [@holic](https://github.com/holic)! - Fixed `encodePacked` for empty arrays.

## 1.10.2

### Patch Changes

- [#1145](https://github.com/wagmi-dev/viem/pull/1145) [`eac50a51`](https://github.com/wagmi-dev/viem/commit/eac50a5138f9e2078e99abda02a9c3468bcd195b) Thanks [@jxom](https://github.com/jxom)! - Support function/event selectors as `name` in `getAbiItem`.

* [#1146](https://github.com/wagmi-dev/viem/pull/1146) [`4875a2af`](https://github.com/wagmi-dev/viem/commit/4875a2aff987709f3f94287d9f77895ec99d9590) Thanks [@darrenvechain](https://github.com/darrenvechain)! - Added Vechain chain.

## 1.10.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "viem",
"description": "TypeScript Interface for Ethereum",
"version": "1.10.1",
"version": "1.10.4",
"scripts": {
"bench": "vitest bench",
"bench:ci": "CI=true vitest bench",
Expand Down
9 changes: 9 additions & 0 deletions site/docs/abi/getAbiItem.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ const encodedData = getAbiItem({
})
```

You can also provide the ABI item's 4byte selector:

```ts
const encodedData = getAbiItem({
abi: [...],
name: '0x70a08231', // [!code focus]
})
```

### args (optional)

- **Type:** Inferred.
Expand Down
4 changes: 2 additions & 2 deletions site/docs/actions/public/watchBlocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Polling frequency (in ms). Defaults to the Client's `pollingInterval` config.
const unwatch = publicClient.watchBlocks(
{
onBlock: block => console.log(block),
pollingInterval: true, // [!code focus]
pollingInterval: 1_000, // [!code focus]
}
)
```
Expand All @@ -192,4 +192,4 @@ Check out the usage of `watchBlocks` in the live [Watch Blocks Example](https://
## JSON-RPC Methods

- When `poll: true`, calls [`eth_getBlockByNumber`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getBlockByNumber) on a polling interval.
- When `poll: false` & WebSocket Transport, uses a WebSocket subscription via [`eth_subscribe`](https://docs.alchemy.com/reference/eth-subscribe-polygon) and the `"newHeads"` event.
- When `poll: false` & WebSocket Transport, uses a WebSocket subscription via [`eth_subscribe`](https://docs.alchemy.com/reference/eth-subscribe-polygon) and the `"newHeads"` event.
32 changes: 24 additions & 8 deletions site/docs/contract/simulateContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ import { mainnet } from 'viem/chains'

// JSON-RPC Account
export const [account] = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
// Local Account
// Local Account
export const account = privateKeyToAccount(...)

export const publicClient = createPublicClient({
Expand Down Expand Up @@ -187,7 +187,7 @@ export const publicClient = createPublicClient({

### Handling Custom Errors

In the example below, we are **catching** a [custom error](https://blog.soliditylang.org/2021/04/21/custom-errors/) thrown by the `simulateContract`. It is important to include the custom error item in the contract `abi`.
In the example below, we are **catching** a [custom error](https://blog.soliditylang.org/2021/04/21/custom-errors/) thrown by the `simulateContract`. It is important to include the custom error item in the contract `abi`.

You can access the custom error through the `data` attribute of the error:

Expand Down Expand Up @@ -228,10 +228,10 @@ export const wagmiAbi = [
type: "function",
},
// Custom solidity error
{
type: 'error',
inputs: [],
name: 'MintIsDisabled'
{
type: 'error',
inputs: [],
name: 'MintIsDisabled'
},
...
] as const;
Expand Down Expand Up @@ -359,7 +359,7 @@ const { result } = await publicClient.simulateContract({
accessList: [{ // [!code focus:4]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
storageKeys: ['0x1'],
}],
}],
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
})
```
Expand All @@ -385,7 +385,7 @@ const { result } = await publicClient.simulateContract({

- **Type:** `Hex`

Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).
Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).

```ts
const { result } = await publicClient.simulateContract({
Expand All @@ -397,6 +397,22 @@ const { result } = await publicClient.simulateContract({
})
```

### gas (optional)

- **Type:** `bigint`

The gas limit for the transaction.

```ts
await walletClient.writeContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
gas: 69420n, // [!code focus]
})
```

### gasPrice (optional)

- **Type:** `bigint`
Expand Down
24 changes: 20 additions & 4 deletions site/docs/contract/writeContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ head:

Executes a write function on a contract.

A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a [Transaction](/docs/glossary/terms) is needed to be broadcast in order to change the state.
A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a [Transaction](/docs/glossary/terms) is needed to be broadcast in order to change the state.

Internally, `writeContract` uses a [Wallet Client](/docs/clients/wallet) to call the [`sendTransaction` action](/docs/actions/wallet/sendTransaction) with [ABI-encoded `data`](/docs/contract/encodeFunctionData).

::: warning

The `writeContract` internally sends a transaction – it **does not** validate if the contract write will succeed (the contract may throw an error). It is highly recommended to [simulate the contract write with `simulateContract`](#usage) before you execute it.
The `writeContract` internally sends a transaction – it **does not** validate if the contract write will succeed (the contract may throw an error). It is highly recommended to [simulate the contract write with `simulateContract`](#usage) before you execute it.

:::

Expand Down Expand Up @@ -289,7 +289,7 @@ await walletClient.writeContract({
accessList: [{ // [!code focus:4]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
storageKeys: ['0x1'],
}],
}],
})
```

Expand Down Expand Up @@ -333,7 +333,7 @@ await walletClient.writeContract({

- **Type:** `Hex`

Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).
Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f).

```ts
await walletClient.writeContract({
Expand All @@ -345,6 +345,22 @@ await walletClient.writeContract({
})
```

### gas (optional)

- **Type:** `bigint`

The gas limit for the transaction. Note that passing a gas limit also skips the gas estimation step.

```ts
await walletClient.writeContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
gas: 69420n, // [!code focus]
})
```

### gasPrice (optional)

- **Type:** `bigint`
Expand Down
10 changes: 10 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ export {
type EstimateContractGasReturnType,
estimateContractGas,
} from './public/estimateContractGas.js'
export {
type EstimateFeesPerGasParameters,
type EstimateFeesPerGasReturnType,
estimateFeesPerGas,
} from './public/estimateFeesPerGas.js'
export {
type EstimateMaxPriorityFeePerGasParameters,
type EstimateMaxPriorityFeePerGasReturnType,
estimateMaxPriorityFeePerGas,
} from './public/estimateMaxPriorityFeePerGas.js'
export {
type EstimateGasParameters,
type EstimateGasReturnType,
Expand Down
4 changes: 2 additions & 2 deletions src/chains/definitions/polygonMumbai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const polygonMumbai = /*#__PURE__*/ defineChain({
webSocket: ['wss://polygon-mumbai.infura.io/ws/v3'],
},
default: {
http: ['https://matic-mumbai.chainstacklabs.com'],
http: ['https://rpc-mumbai.matic.today'],
},
public: {
http: ['https://matic-mumbai.chainstacklabs.com'],
http: ['https://rpc-mumbai.matic.today'],
},
},
blockExplorers: {
Expand Down
2 changes: 1 addition & 1 deletion src/errors/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '1.10.1'
export const version = '1.10.2'
5 changes: 5 additions & 0 deletions src/utils/abi/encodePacked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { address } from '../../_test/constants.js'
import { encodePacked } from './encodePacked.js'

test.each([
{
types: [],
values: [],
expected: '0x',
},
{
types: ['address'],
values: [address.vitalik],
Expand Down
6 changes: 3 additions & 3 deletions src/utils/abi/encodePacked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { InvalidAddressError } from '../../errors/address.js'
import type { Hex } from '../../types/misc.js'
import { isAddress } from '../address/isAddress.js'
import { concat } from '../data/concat.js'
import { concatHex } from '../data/concat.js'
import { pad } from '../data/pad.js'
import { boolToHex, numberToHex, stringToHex } from '../encoding/toHex.js'
import { arrayRegex, bytesRegex, integerRegex } from '../regex.js'
Expand Down Expand Up @@ -54,7 +54,7 @@ export function encodePacked<
const value = values[i]
data.push(encode(type, value))
}
return concat(data)
return concatHex(data)
}

function encode<const TPackedAbiType extends PackedAbiType | unknown>(
Expand Down Expand Up @@ -103,7 +103,7 @@ function encode<const TPackedAbiType extends PackedAbiType | unknown>(
data.push(encode(childType, value[i], true))
}
if (data.length === 0) return '0x'
return concat(data)
return concatHex(data)
}

throw new UnsupportedPackedAbiType(type)
Expand Down
61 changes: 61 additions & 0 deletions src/utils/abi/getAbiItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,67 @@ test('default', () => {
`)
})

describe('selector', () => {
test('function', () => {
expect(
getAbiItem({
abi: wagmiContractConfig.abi,
name: '0x70a08231',
}),
).toMatchInlineSnapshot(`
{
"inputs": [
{
"name": "owner",
"type": "address",
},
],
"name": "balanceOf",
"outputs": [
{
"name": "",
"type": "uint256",
},
],
"stateMutability": "view",
"type": "function",
}
`)
})

test('event', () => {
expect(
getAbiItem({
abi: wagmiContractConfig.abi,
name: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
}),
).toMatchInlineSnapshot(`
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address",
},
{
"indexed": true,
"name": "to",
"type": "address",
},
{
"indexed": true,
"name": "tokenId",
"type": "uint256",
},
],
"name": "Transfer",
"type": "event",
}
`)
})
})

test('no matching name', () => {
expect(
getAbiItem({
Expand Down
2 changes: 2 additions & 0 deletions src/utils/abi/getAbiItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ExtractAbiItemForArgs,
Widen,
} from '../../types/contract.js'
import type { Hex } from '../../types/misc.js'
import type { UnionEvaluate } from '../../types/utils.js'
import { isAddress } from '../address/isAddress.js'

Expand All @@ -22,6 +23,7 @@ export type GetAbiItemParameters<
name:
| allNames // show all options
| (name extends allNames ? name : never) // infer value
| Hex // function selector
} & UnionEvaluate<
readonly [] extends allArgs
? {
Expand Down

0 comments on commit 6d85bb7

Please sign in to comment.