From 6071163f70599384c5034dd772ef6fc7cdae9983 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 9 Aug 2023 07:24:10 -0700 Subject: [PATCH] feat(common): move zero gas fee override to `createContract` (#1266) --- .changeset/tricky-kangaroos-love.md | 6 ++++ packages/common/src/chains/mudFoundry.ts | 17 ++------- packages/common/src/createBurnerAccount.ts | 14 +------- packages/common/src/createContract.ts | 41 ++++++++++++++++++---- packages/common/src/type-utils/common.ts | 2 ++ 5 files changed, 47 insertions(+), 33 deletions(-) create mode 100644 .changeset/tricky-kangaroos-love.md diff --git a/.changeset/tricky-kangaroos-love.md b/.changeset/tricky-kangaroos-love.md new file mode 100644 index 0000000000..c97a573e2d --- /dev/null +++ b/.changeset/tricky-kangaroos-love.md @@ -0,0 +1,6 @@ +--- +"@latticexyz/common": minor +--- + +- Moves zero gas fee override to `createContract` until https://github.com/wagmi-dev/viem/pull/963 or similar feature lands +- Skip simulation if `gas` is provided diff --git a/packages/common/src/chains/mudFoundry.ts b/packages/common/src/chains/mudFoundry.ts index e885b4dd1e..f3d0b62501 100644 --- a/packages/common/src/chains/mudFoundry.ts +++ b/packages/common/src/chains/mudFoundry.ts @@ -1,18 +1,7 @@ -import { defineTransactionRequest } from "viem"; import { foundry } from "viem/chains"; +import { MUDChain } from "./types"; export const mudFoundry = { ...foundry, - formatters: { - transactionRequest: defineTransactionRequest({ - format() { - // Temporarily override base fee for MUD's anvil config - // TODO: remove once https://github.com/wagmi-dev/viem/pull/963 is fixed - return { - maxFeePerGas: 0n, - maxPriorityFeePerGas: 0n, - }; - }, - }), - }, -}; + // We may override chain settings here +} as const satisfies MUDChain; diff --git a/packages/common/src/createBurnerAccount.ts b/packages/common/src/createBurnerAccount.ts index 08bf3c763b..8c0effda9d 100644 --- a/packages/common/src/createBurnerAccount.ts +++ b/packages/common/src/createBurnerAccount.ts @@ -3,20 +3,8 @@ import { privateKeyToAccount } from "viem/accounts"; export function createBurnerAccount(privateKey: Hex): Account { const account = privateKeyToAccount(privateKey); - - // Temporarily override base fee for MUD's anvil config - // TODO: remove once https://github.com/wagmi-dev/viem/pull/963 is fixed - const signTransaction: typeof account.signTransaction = async (transaction, ...args) => { - // TODO: refine check for mud anvil (0 base fee) - if (transaction.chainId === 31337) { - transaction.maxFeePerGas = 0n; - transaction.maxPriorityFeePerGas = 0n; - } - return account.signTransaction(transaction, ...args); - }; - + // We may override account features here return { ...account, - signTransaction, }; } diff --git a/packages/common/src/createContract.ts b/packages/common/src/createContract.ts index 336fb75aef..b22483f0f0 100644 --- a/packages/common/src/createContract.ts +++ b/packages/common/src/createContract.ts @@ -17,10 +17,11 @@ import pQueue from "p-queue"; import pRetry from "p-retry"; import { createNonceManager } from "./createNonceManager"; import { debug as parentDebug } from "./debug"; +import { UnionOmit } from "./type-utils/common"; const debug = parentDebug.extend("createContract"); -// copied from viem because it isn't exported +// copied from viem because this isn't exported // TODO: import from viem? function getFunctionParameters(values: [args?: readonly unknown[], options?: object]): { args: readonly unknown[]; @@ -72,31 +73,59 @@ export function createContract< { get(_, functionName: string): GetContractReturnType["write"][string] { return async (...parameters) => { - const { args, options } = getFunctionParameters(parameters as any); + const { args, options } = < + { + args: unknown[]; + options: UnionOmit; + } + >getFunctionParameters(parameters as any); async function write(): Promise { if (!nonceManager.hasNonce()) { await nonceManager.resetNonce(); } + // Temporarily override base fee for our default anvil config + // TODO: remove once https://github.com/wagmi-dev/viem/pull/963 is fixed + // TODO: more specific mud foundry check? or can we safely assume anvil+mud will be block fee zero for now? + if ( + walletClient.chain.id === 31337 && + options.maxFeePerGas == null && + options.maxPriorityFeePerGas == null + ) { + options.maxFeePerGas = 0n; + options.maxPriorityFeePerGas = 0n; + } + + if (options.gas) { + const nonce = nonceManager.nextNonce(); + debug("gas provided, skipping simulate and calling write function with nonce", nonce, options); + return await walletClient.writeContract({ + address, + abi, + functionName, + args, + nonce, + ...options, + } as unknown as WriteContractParameters); + } + debug("simulating write", functionName, args, options); const { request } = await publicClient.simulateContract({ - account: walletClient.account, address, abi, functionName, args, ...options, + account: options.account ?? walletClient.account, } as unknown as SimulateContractParameters); const nonce = nonceManager.nextNonce(); debug("calling write function with nonce", nonce, request); - const result = await walletClient.writeContract({ + return await walletClient.writeContract({ nonce, ...request, } as unknown as WriteContractParameters); - - return result; } return await queue.add( diff --git a/packages/common/src/type-utils/common.ts b/packages/common/src/type-utils/common.ts index f32301b6fe..2e7d66225c 100644 --- a/packages/common/src/type-utils/common.ts +++ b/packages/common/src/type-utils/common.ts @@ -17,3 +17,5 @@ export type OrDefault = T extends undefined ? Default : T; export type OrDefaults = { [key in keyof Defaults]: key extends keyof T ? OrDefault : Defaults[key]; }; + +export type UnionOmit = T extends any ? Omit : never;