Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default priority fee per-chain #1006

Merged
merged 15 commits into from Aug 10, 2023
21 changes: 21 additions & 0 deletions .changeset/sweet-lemons-explain.md
@@ -0,0 +1,21 @@
---
"viem": minor
---

Added `fees` to `chain` config that includes a `defaultPriorityFee` for setting a default priority fee (`maxFeePerGas`) for a chain.

```ts
import type { Chain } from 'viem'

export const example = {
// ...
fees: {
defaultPriorityFee: 1_000_000n, // 0.001 gwei
// or
async defaultPriorityFee() {
// ... some async behavior to derive the fee.
}
},
// ...
} as const satifies Chain
```
13 changes: 11 additions & 2 deletions src/actions/public/estimateGas.ts
Expand Up @@ -20,7 +20,10 @@ import {
type AssertRequestParameters,
assertRequest,
} from '../../utils/transaction/assertRequest.js'
import { prepareRequest } from '../../utils/transaction/prepareRequest.js'
import {
type PrepareRequestParameters,
prepareRequest,
} from '../../utils/transaction/prepareRequest.js'

export type FormattedEstimateGas<
TChain extends Chain | undefined = Chain | undefined,
Expand Down Expand Up @@ -102,7 +105,13 @@ export async function estimateGas<
to,
value,
...rest
} = account.type === 'local' ? await prepareRequest(client, args) : args
} =
account.type === 'local'
? ((await prepareRequest(
client,
args as PrepareRequestParameters,
)) as EstimateGasParameters)
: args

const blockNumberHex = blockNumber ? numberToHex(blockNumber) : undefined
const block = blockNumberHex || blockTag
Expand Down
4 changes: 2 additions & 2 deletions src/actions/public/simulateContract.ts
Expand Up @@ -29,7 +29,7 @@ export type SimulateContractParameters<
TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = any,
TChain extends Chain | undefined = Chain | undefined,
TChainOverride extends Chain | undefined = undefined,
TChainOverride extends Chain | undefined = Chain | undefined,
> = {
chain?: TChainOverride
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
Expand All @@ -51,7 +51,7 @@ export type SimulateContractReturnType<
TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = string,
TChain extends Chain | undefined = Chain | undefined,
TChainOverride extends Chain | undefined = undefined,
TChainOverride extends Chain | undefined = Chain | undefined,
> = {
result: ContractFunctionResult<TAbi, TFunctionName>
request: UnionOmit<
Expand Down
2 changes: 1 addition & 1 deletion src/actions/wallet/deployContract.ts
Expand Up @@ -19,7 +19,7 @@ export type DeployContractParameters<
TAbi extends Abi | readonly unknown[] = Abi,
TChain extends Chain | undefined = Chain | undefined,
TAccount extends Account | undefined = Account | undefined,
TChainOverride extends Chain | undefined = undefined,
TChainOverride extends Chain | undefined = Chain | undefined,
> = UnionOmit<
SendTransactionParameters<TChain, TAccount, TChainOverride>,
'accessList' | 'chain' | 'to' | 'data'
Expand Down
15 changes: 9 additions & 6 deletions src/actions/wallet/sendTransaction.ts
Expand Up @@ -11,25 +11,28 @@ import type {
TransactionRequest,
TransactionSerializable,
} from '../../types/transaction.js'
import type { IsUndefined, UnionOmit } from '../../types/utils.js'
import type { UnionOmit } from '../../types/utils.js'
import { assertCurrentChain } from '../../utils/chain.js'
import { getTransactionError } from '../../utils/errors/getTransactionError.js'
import { extract } from '../../utils/formatters/extract.js'
import {
type FormattedTransactionRequest,
formatTransactionRequest,
} from '../../utils/formatters/transactionRequest.js'
import { assertRequest } from '../../utils/transaction/assertRequest.js'
import {
type AssertRequestParameters,
assertRequest,
} from '../../utils/transaction/assertRequest.js'
import { prepareRequest } from '../../utils/transaction/prepareRequest.js'
import { getChainId } from '../public/getChainId.js'

export type SendTransactionParameters<
TChain extends Chain | undefined = Chain | undefined,
TAccount extends Account | undefined = Account | undefined,
TChainOverride extends Chain | undefined = Chain,
TChainOverride extends Chain | undefined = Chain | undefined,
> = UnionOmit<
FormattedTransactionRequest<
IsUndefined<TChain> extends true ? TChainOverride : TChain
TChainOverride extends Chain ? TChainOverride : TChain
>,
'from'
> &
Expand Down Expand Up @@ -113,7 +116,7 @@ export async function sendTransaction<
const account = parseAccount(account_)

try {
assertRequest(args)
assertRequest(args as AssertRequestParameters)

let chainId
if (chain !== null) {
Expand All @@ -139,7 +142,7 @@ export async function sendTransaction<
to,
value,
...rest,
})
} as any)

if (!chainId) chainId = await getChainId(client)

Expand Down
18 changes: 11 additions & 7 deletions src/actions/wallet/writeContract.ts
Expand Up @@ -3,6 +3,7 @@ import type { Abi } from 'abitype'
import type { Account } from '../../accounts/types.js'
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import type { GetAccountParameter } from '../../types/account.js'
import type { Chain, GetChain } from '../../types/chain.js'
import type { ContractFunctionConfig, GetValue } from '../../types/contract.js'
import type { Hex } from '../../types/misc.js'
Expand All @@ -11,7 +12,7 @@ import {
type EncodeFunctionDataParameters,
encodeFunctionData,
} from '../../utils/abi/encodeFunctionData.js'

import type { FormattedTransactionRequest } from '../../utils/formatters/transactionRequest.js'
import {
type SendTransactionParameters,
type SendTransactionReturnType,
Expand All @@ -22,14 +23,17 @@ export type WriteContractParameters<
TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = string,
TChain extends Chain | undefined = Chain,
TAccount extends Account | undefined = undefined,
TChainOverride extends Chain | undefined = undefined,
TAccount extends Account | undefined = Account | undefined,
TChainOverride extends Chain | undefined = Chain | undefined,
> = ContractFunctionConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'> &
GetAccountParameter<TAccount> &
GetChain<TChain, TChainOverride> &
UnionOmit<
SendTransactionParameters<TChain, TAccount, TChainOverride>,
'chain' | 'to' | 'data' | 'value'
FormattedTransactionRequest<
TChainOverride extends Chain ? TChainOverride : TChain
>,
'from' | 'to' | 'data' | 'value'
> &
GetChain<TChain, TChainOverride> &
GetValue<
TAbi,
TFunctionName,
Expand Down Expand Up @@ -102,7 +106,7 @@ export async function writeContract<
TAccount extends Account | undefined,
TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
TChainOverride extends Chain | undefined = undefined,
TChainOverride extends Chain | undefined,
>(
client: Client<Transport, TChain, TAccount>,
{
Expand Down
4 changes: 2 additions & 2 deletions src/chains/celo/formatters.ts
@@ -1,4 +1,4 @@
import { type Formatters } from '../../types/formatter.js'
import { type ChainFormatters } from '../../types/chain.js'
import type { Hash } from '../../types/misc.js'
import { hexToBigInt } from '../../utils/encoding/fromHex.js'
import { numberToHex } from '../../utils/encoding/toHex.js'
Expand Down Expand Up @@ -82,4 +82,4 @@ export const formattersCelo = {
}
},
}),
} as const satisfies Formatters
} as const satisfies ChainFormatters
4 changes: 2 additions & 2 deletions src/chains/celo/serializers.ts
Expand Up @@ -4,9 +4,9 @@ import { InvalidAddressError } from '../../errors/address.js'
import { BaseError } from '../../errors/base.js'
import { InvalidChainIdError } from '../../errors/chain.js'
import { FeeCapTooHighError, TipAboveFeeCapError } from '../../errors/node.js'
import type { ChainSerializers } from '../../types/chain.js'
import type { FeeValuesEIP1559 } from '../../types/fee.js'
import type { Signature } from '../../types/misc.js'
import type { Serializers } from '../../types/serializer.js'
import type {
AccessList,
TransactionSerializable,
Expand Down Expand Up @@ -39,7 +39,7 @@ export const serializeTransactionCelo: SerializeTransactionFn<

export const serializersCelo = {
transaction: serializeTransactionCelo,
} as const satisfies Serializers
} as const satisfies ChainSerializers

//////////////////////////////////////////////////////////////////////////////
// Types
Expand Down
23 changes: 19 additions & 4 deletions src/chains/index.ts
Expand Up @@ -3,6 +3,7 @@ import * as chains from '@wagmi/chains'
import { defineChain } from '../utils/chain.js'
import { formattersCelo } from './celo/formatters.js'
import { serializersCelo } from './celo/serializers.js'
import { feesOptimism } from './optimism/fees.js'
import { formattersOptimism } from './optimism/formatters.js'

export const arbitrum = /*#__PURE__*/ defineChain(chains.arbitrum)
Expand All @@ -11,8 +12,14 @@ export const aurora = /*#__PURE__*/ defineChain(chains.aurora)
export const auroraTestnet = /*#__PURE__*/ defineChain(chains.auroraTestnet)
export const avalanche = /*#__PURE__*/ defineChain(chains.avalanche)
export const avalancheFuji = /*#__PURE__*/ defineChain(chains.avalancheFuji)
export const base = /*#__PURE__*/ defineChain(chains.base)
export const baseGoerli = /*#__PURE__*/ defineChain(chains.baseGoerli)
export const base = /*#__PURE__*/ defineChain(chains.base, {
fees: feesOptimism,
formatters: formattersOptimism,
})
export const baseGoerli = /*#__PURE__*/ defineChain(chains.baseGoerli, {
fees: feesOptimism,
formatters: formattersOptimism,
})
export const boba = /*#__PURE__*/ defineChain(chains.boba)
export const bronos = /*#__PURE__*/ defineChain(chains.bronos)
export const bronosTestnet = /*#__PURE__*/ defineChain(chains.bronosTestnet)
Expand Down Expand Up @@ -72,9 +79,11 @@ export const moonriver = /*#__PURE__*/ defineChain(chains.moonriver)
export const nexi = /*#__PURE__*/ defineChain(chains.nexi)
export const okc = /*#__PURE__*/ defineChain(chains.okc)
export const optimism = /*#__PURE__*/ defineChain(chains.optimism, {
fees: feesOptimism,
formatters: formattersOptimism,
})
export const optimismGoerli = /*#__PURE__*/ defineChain(chains.optimismGoerli, {
fees: feesOptimism,
formatters: formattersOptimism,
})
export const polygon = /*#__PURE__*/ defineChain(chains.polygon)
Expand Down Expand Up @@ -136,7 +145,13 @@ export const xdcTestnet = /*#__PURE__*/ defineChain(chains.xdcTestnet)
export const zhejiang = /*#__PURE__*/ defineChain(chains.zhejiang)
export const zkSync = /*#__PURE__*/ defineChain(chains.zkSync)
export const zkSyncTestnet = /*#__PURE__*/ defineChain(chains.zkSyncTestnet)
export const zora = /*#__PURE__*/ defineChain(chains.zora)
export const zoraTestnet = /*#__PURE__*/ defineChain(chains.zoraTestnet)
export const zora = /*#__PURE__*/ defineChain(chains.zora, {
fees: feesOptimism,
formatters: formattersOptimism,
})
export const zoraTestnet = /*#__PURE__*/ defineChain(chains.zoraTestnet, {
fees: feesOptimism,
formatters: formattersOptimism,
})

export type { Chain } from '../types/chain.js'
5 changes: 5 additions & 0 deletions src/chains/optimism/fees.ts
@@ -0,0 +1,5 @@
import type { ChainFees } from '../../types/chain.js'

export const feesOptimism = {
defaultPriorityFee: 1_000_000n, // 0.001 gwei
} as const satisfies ChainFees
4 changes: 2 additions & 2 deletions src/chains/optimism/formatters.ts
@@ -1,4 +1,4 @@
import { type Formatters } from '../../types/formatter.js'
import { type ChainFormatters } from '../../types/chain.js'
import type { Hash } from '../../types/misc.js'
import { type RpcTransaction } from '../../types/rpc.js'
import { hexToBigInt } from '../../utils/encoding/fromHex.js'
Expand Down Expand Up @@ -71,4 +71,4 @@ export const formattersOptimism = {
}
},
}),
} as const satisfies Formatters
} as const satisfies ChainFormatters
3 changes: 3 additions & 0 deletions src/clients/createClient.test.ts
Expand Up @@ -64,6 +64,7 @@ describe('transports', () => {
"batch": undefined,
"cacheTime": 4000,
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -120,6 +121,7 @@ describe('transports', () => {
"batch": undefined,
"cacheTime": 4000,
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -422,6 +424,7 @@ describe('extends', () => {
"cacheTime": 4000,
"call": [Function],
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down
3 changes: 3 additions & 0 deletions src/clients/createPublicClient.test.ts
Expand Up @@ -143,6 +143,7 @@ describe('transports', () => {
"cacheTime": 4000,
"call": [Function],
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -239,6 +240,7 @@ describe('transports', () => {
"cacheTime": 4000,
"call": [Function],
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -410,6 +412,7 @@ test('extend', () => {
"cacheTime": 4000,
"call": [Function],
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down
4 changes: 4 additions & 0 deletions src/clients/createTestClient.test.ts
Expand Up @@ -33,6 +33,7 @@ test('creates', () => {
"batch": undefined,
"cacheTime": 4000,
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -119,6 +120,7 @@ describe('transports', () => {
"batch": undefined,
"cacheTime": 4000,
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -205,6 +207,7 @@ describe('transports', () => {
"batch": undefined,
"cacheTime": 4000,
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -300,6 +303,7 @@ test('extend', () => {
"cacheTime": 4000,
"call": [Function],
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down
2 changes: 2 additions & 0 deletions src/clients/createWalletClient.test.ts
Expand Up @@ -280,6 +280,7 @@ describe('args: transport', () => {
"batch": undefined,
"cacheTime": 4000,
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down Expand Up @@ -357,6 +358,7 @@ test('extend', () => {
"cacheTime": 4000,
"call": [Function],
"chain": {
"fees": undefined,
"formatters": undefined,
"id": 1337,
"name": "Localhost",
Expand Down
6 changes: 3 additions & 3 deletions src/clients/decorators/public.ts
Expand Up @@ -1206,9 +1206,9 @@ export type PublicActions<
* })
*/
simulateContract: <
TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = any,
TChainOverride extends Chain | undefined = undefined,
TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
TChainOverride extends Chain | undefined,
>(
args: SimulateContractParameters<
TAbi,
Expand Down