Skip to content

Commit

Permalink
feat: add dataSuffix to estimateContractGas (#3115)
Browse files Browse the repository at this point in the history
* Add dataSuffix arg to estimateContractGas

* Add changeset

* Update fluffy-bears-kick.md

---------

Co-authored-by: jxom <7336481+jxom@users.noreply.github.com>
  • Loading branch information
rkalis and jxom authored Dec 12, 2024
1 parent 83ae3e1 commit a948622
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-bears-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `dataSuffix` parameter to `estimateContractGas`.
46 changes: 31 additions & 15 deletions site/pages/docs/contract/estimateContractGas.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: Estimates the gas required to successfully execute a contract write function call.
description: Estimates the gas required to successfully execute a contract write function call.
---

# estimateContractGas
Expand Down Expand Up @@ -132,7 +132,7 @@ The gas estimate.
The contract address.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -147,7 +147,7 @@ const { result } = await publicClient.estimateContractGas({
The contract's ABI.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi, // [!code focus]
functionName: 'mint',
Expand All @@ -162,7 +162,7 @@ const { result } = await publicClient.estimateContractGas({
A function to extract from the ABI.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint', // [!code focus]
Expand All @@ -179,7 +179,7 @@ The Account to estimate gas from.
Accepts a [JSON-RPC Account](/docs/clients/wallet#json-rpc-accounts) or [Local Account (Private Key, etc)](/docs/clients/wallet#local-accounts-private-key-mnemonic-etc).

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -194,15 +194,15 @@ const { result } = await publicClient.estimateContractGas({
The access list.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
accessList: [{ // [!code focus:4]
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
storageKeys: ['0x1'],
}],
}],
account,
})
```
Expand All @@ -214,7 +214,7 @@ const { result } = await publicClient.estimateContractGas({
Arguments to pass to function call.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
abi: wagmiAbi,
functionName: 'balanceOf',
Expand All @@ -223,14 +223,30 @@ const { result } = await publicClient.estimateContractGas({
})
```

### dataSuffix (optional)

- **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).

```ts
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
args: [69420],
dataSuffix: '0xdeadbeef' // [!code focus]
})
```

### gasPrice (optional)

- **Type:** `bigint`

The price (in wei) to pay per gas. Only applies to [Legacy Transactions](/docs/glossary/terms#legacy-transaction).

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -247,7 +263,7 @@ const { result } = await publicClient.estimateContractGas({
Total fee per gas (in wei), inclusive of `maxPriorityFeePerGas`. Only applies to [EIP-1559 Transactions](/docs/glossary/terms#eip-1559-transaction)

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -264,7 +280,7 @@ const { result } = await publicClient.estimateContractGas({
Max priority fee per gas (in wei). Only applies to [EIP-1559 Transactions](/docs/glossary/terms#eip-1559-transaction)

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -282,7 +298,7 @@ const { result } = await publicClient.estimateContractGas({
Unique number identifying this transaction.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -299,7 +315,7 @@ const { result } = await publicClient.estimateContractGas({
Value in wei sent with this transaction.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -316,7 +332,7 @@ const { result } = await publicClient.estimateContractGas({
The block number to perform the read against.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand All @@ -333,7 +349,7 @@ const { result } = await publicClient.estimateContractGas({
The block tag to perform the read against.

```ts
const { result } = await publicClient.estimateContractGas({
const gas = await publicClient.estimateContractGas({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: wagmiAbi,
functionName: 'mint',
Expand Down
32 changes: 30 additions & 2 deletions src/actions/public/estimateContractGas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* - Custom chain types
* - Custom nonce
*/
import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'

import { ErrorsExample } from '~contracts/generated.js'
import { baycContractConfig, wagmiContractConfig } from '~test/src/abis.js'
Expand All @@ -17,9 +17,10 @@ import { sendTransaction } from '../wallet/sendTransaction.js'

import { anvilMainnet } from '../../../test/src/anvil.js'

import { publicActions } from '../../index.js'
import { estimateContractGas } from './estimateContractGas.js'

const client = anvilMainnet.getClient()
const client = anvilMainnet.getClient().extend(publicActions)

describe('wagmi', () => {
test('default', async () => {
Expand Down Expand Up @@ -121,6 +122,33 @@ describe('wagmi', () => {
Version: viem@x.y.z]
`)
})

test('args: dataSuffix', async () => {
const spy = vi.spyOn(client, 'estimateGas')

const gasWithDataSuffix = await estimateContractGas(client, {
abi: wagmiContractConfig.abi,
address: wagmiContractConfig.address,
account: accounts[0].address,
functionName: 'mint',
dataSuffix: '0x12345678',
})

expect(spy).toHaveBeenCalledWith({
account: accounts[0].address,
data: '0x1249c58b12345678',
to: wagmiContractConfig.address,
})

const gasWithoutDataSuffix = await estimateContractGas(client, {
abi: wagmiContractConfig.abi,
address: wagmiContractConfig.address,
account: accounts[0].address,
functionName: 'mint',
})

expect(gasWithDataSuffix).toBeGreaterThan(gasWithoutDataSuffix)
})
})

describe('BAYC', () => {
Expand Down
10 changes: 7 additions & 3 deletions src/actions/public/estimateContractGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
ContractFunctionParameters,
GetValue,
} from '../../types/contract.js'
import type { Hex } from '../../types/misc.js'
import type { UnionOmit } from '../../types/utils.js'
import {
type EncodeFunctionDataErrorType,
Expand Down Expand Up @@ -57,7 +58,10 @@ export type EstimateContractGasParameters<
EstimateGasParameters<chain> extends EstimateGasParameters
? EstimateGasParameters<chain>['value']
: EstimateGasParameters['value']
>
> & {
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
dataSuffix?: Hex | undefined
}

export type EstimateContractGasReturnType = bigint

Expand Down Expand Up @@ -102,7 +106,7 @@ export async function estimateContractGas<
client: Client<Transport, chain, account>,
parameters: EstimateContractGasParameters<abi, functionName, args, chain>,
): Promise<EstimateContractGasReturnType> {
const { abi, address, args, functionName, ...request } =
const { abi, address, args, functionName, dataSuffix, ...request } =
parameters as EstimateContractGasParameters
const data = encodeFunctionData({
abi,
Expand All @@ -115,7 +119,7 @@ export async function estimateContractGas<
estimateGas,
'estimateGas',
)({
data,
data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
to: address,
...request,
} as unknown as EstimateGasParameters)
Expand Down

0 comments on commit a948622

Please sign in to comment.