From 8f043250e1cf36ff1ddcb2a079fc7ad77c504bc3 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 27 Aug 2024 14:28:55 +0100 Subject: [PATCH 1/4] fix(common): route all actions through viem client --- packages/common/src/createFeeRef.ts | 3 ++- packages/common/src/createNonceManager.ts | 3 ++- packages/common/src/getFeeRef.ts | 4 +++- packages/common/src/getNonceManagerId.ts | 3 ++- packages/common/src/sendTransaction.ts | 3 ++- packages/common/src/writeContract.ts | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/common/src/createFeeRef.ts b/packages/common/src/createFeeRef.ts index 5fa7f7691d..8f0b0dae33 100644 --- a/packages/common/src/createFeeRef.ts +++ b/packages/common/src/createFeeRef.ts @@ -1,5 +1,6 @@ import { EstimateFeesPerGasParameters, Client, EstimateFeesPerGasReturnType } from "viem"; import { estimateFeesPerGas } from "viem/actions"; +import { getAction } from "viem/utils"; export type CreateFeeRefOptions = { client: Client; @@ -17,7 +18,7 @@ export async function createFeeRef({ client, args, refreshInterval }: CreateFeeR const feeRef: FeeRef = { fees: {}, lastUpdatedTimestamp: 0 }; async function updateFees(): Promise { - const fees = await estimateFeesPerGas(client, args); + const fees = await getAction(client, estimateFeesPerGas, "estimateFeesPerGas")(args); feeRef.fees = fees; feeRef.lastUpdatedTimestamp = Date.now(); } diff --git a/packages/common/src/createNonceManager.ts b/packages/common/src/createNonceManager.ts index 3c48eb72eb..1a1ac59ded 100644 --- a/packages/common/src/createNonceManager.ts +++ b/packages/common/src/createNonceManager.ts @@ -3,6 +3,7 @@ import { debug as parentDebug } from "./debug"; import { getNonceManagerId } from "./getNonceManagerId"; import { getTransactionCount } from "viem/actions"; import PQueue from "p-queue"; +import { getAction } from "viem/utils"; const debug = parentDebug.extend("createNonceManager"); @@ -66,7 +67,7 @@ export function createNonceManager({ async function resetNonce(): Promise { ref.noncePromise ??= (async (): Promise => { - ref.nonce = await getTransactionCount(client, { address, blockTag }); + ref.nonce = await getAction(client, getTransactionCount, "getTransactionCount")({ address, blockTag }); ref.noncePromise = null; channel?.postMessage(JSON.stringify(ref.nonce)); debug("reset nonce to", ref.nonce); diff --git a/packages/common/src/getFeeRef.ts b/packages/common/src/getFeeRef.ts index 3110cc875e..04c0baf5c6 100644 --- a/packages/common/src/getFeeRef.ts +++ b/packages/common/src/getFeeRef.ts @@ -1,10 +1,12 @@ import { getChainId } from "viem/actions"; import { CreateFeeRefOptions, FeeRef, createFeeRef } from "./createFeeRef"; +import { getAction } from "viem/utils"; const feeRefs = new Map(); export async function getFeeRef(opts: CreateFeeRefOptions): Promise { - const chainId = opts.args?.chain?.id ?? opts.client.chain?.id ?? (await getChainId(opts.client)); + const chainId = + opts.args?.chain?.id ?? opts.client.chain?.id ?? (await getAction(opts.client, getChainId, "getChainId")({})); const existingFeeRef = feeRefs.get(chainId); if (existingFeeRef) { diff --git a/packages/common/src/getNonceManagerId.ts b/packages/common/src/getNonceManagerId.ts index 950e9fbcfe..d8dcecc799 100644 --- a/packages/common/src/getNonceManagerId.ts +++ b/packages/common/src/getNonceManagerId.ts @@ -1,5 +1,6 @@ import { BlockTag, Client, Hex, getAddress } from "viem"; import { getChainId } from "viem/actions"; +import { getAction } from "viem/utils"; export async function getNonceManagerId({ client, @@ -11,6 +12,6 @@ export async function getNonceManagerId({ blockTag: BlockTag; }): Promise { // TODO: improve this so we don't have to call getChainId every time - const chainId = client.chain?.id ?? (await getChainId(client)); + const chainId = client.chain?.id ?? (await getAction(client, getChainId, "getChainId")({})); return `mud:createNonceManager:${chainId}:${getAddress(address)}:${blockTag}`; } diff --git a/packages/common/src/sendTransaction.ts b/packages/common/src/sendTransaction.ts index 349a68cd1a..45d526c9b5 100644 --- a/packages/common/src/sendTransaction.ts +++ b/packages/common/src/sendTransaction.ts @@ -13,6 +13,7 @@ import { debug as parentDebug } from "./debug"; import { getNonceManager } from "./getNonceManager"; import { parseAccount } from "viem/accounts"; import { getFeeRef } from "./getFeeRef"; +import { getAction } from "viem/utils"; const debug = parentDebug.extend("sendTransaction"); @@ -75,7 +76,7 @@ export async function sendTransaction< ...feeRef.fees, }; debug("sending tx to", request.to, "with nonce", nonce); - return await viem_sendTransaction(client, params); + return await getAction(client, viem_sendTransaction, "sendTransaction")(params); }, { retries: 3, diff --git a/packages/common/src/writeContract.ts b/packages/common/src/writeContract.ts index 4dd2e2d283..5cd0ed19e9 100644 --- a/packages/common/src/writeContract.ts +++ b/packages/common/src/writeContract.ts @@ -82,7 +82,7 @@ export async function writeContract< ...feeRef.fees, }; debug("calling", params.functionName, "at", params.address, "with nonce", nonce); - return await getAction(client, viem_writeContract, "writeContract")(params); + return await getAction(client, viem_writeContract, "writeContract")({ ...params }); }, { retries: 3, From d9b1e509be6d201914185ea7dee46e138f6fb124 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 27 Aug 2024 15:32:22 +0100 Subject: [PATCH 2/4] fix(common): use pending block tag in tx queue --- packages/common/src/sendTransaction.ts | 12 ++++++++---- packages/common/src/writeContract.ts | 12 +++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/common/src/sendTransaction.ts b/packages/common/src/sendTransaction.ts index 45d526c9b5..5247e97046 100644 --- a/packages/common/src/sendTransaction.ts +++ b/packages/common/src/sendTransaction.ts @@ -6,6 +6,7 @@ import { Transport, SendTransactionReturnType, PublicClient, + SendTransactionRequest, } from "viem"; import { sendTransaction as viem_sendTransaction } from "viem/actions"; import pRetry from "p-retry"; @@ -38,7 +39,8 @@ export type SendTransactionExtraOptions = { export async function sendTransaction< chain extends Chain | undefined, account extends Account | undefined, - chainOverride extends Chain | undefined, + const request extends SendTransactionRequest, + chainOverride extends Chain | undefined = undefined, >( client: Client, request: SendTransactionParameters, @@ -52,10 +54,11 @@ export async function sendTransaction< const account = parseAccount(rawAccount); const chain = client.chain; + const blockTag = "pending"; const nonceManager = await getNonceManager({ client: opts.publicClient ?? client, address: account.address, - blockTag: "pending", + blockTag, queueConcurrency: opts.queueConcurrency, }); @@ -70,11 +73,12 @@ export async function sendTransaction< pRetry( async () => { const nonce = nonceManager.nextNonce(); - const params: SendTransactionParameters = { + const params = { + blockTag, ...request, nonce, ...feeRef.fees, - }; + } as const satisfies SendTransactionParameters; debug("sending tx to", request.to, "with nonce", nonce); return await getAction(client, viem_sendTransaction, "sendTransaction")(params); }, diff --git a/packages/common/src/writeContract.ts b/packages/common/src/writeContract.ts index 5cd0ed19e9..b23ab4f1dd 100644 --- a/packages/common/src/writeContract.ts +++ b/packages/common/src/writeContract.ts @@ -41,7 +41,7 @@ export type WriteContractExtraOptions = { export async function writeContract< chain extends Chain | undefined, account extends Account | undefined, - abi extends Abi | readonly unknown[], + const abi extends Abi | readonly unknown[], functionName extends ContractFunctionName, args extends ContractFunctionArgs, chainOverride extends Chain | undefined, @@ -58,10 +58,11 @@ export async function writeContract< const account = parseAccount(rawAccount); const chain = client.chain; + const blockTag = "pending"; const nonceManager = await getNonceManager({ client: opts.publicClient ?? client, address: account.address, - blockTag: "pending", + blockTag, queueConcurrency: opts.queueConcurrency, }); @@ -76,13 +77,14 @@ export async function writeContract< pRetry( async () => { const nonce = nonceManager.nextNonce(); - const params: WriteContractParameters = { + const params = { + blockTag, ...request, nonce, ...feeRef.fees, - }; + } as const satisfies WriteContractParameters; debug("calling", params.functionName, "at", params.address, "with nonce", nonce); - return await getAction(client, viem_writeContract, "writeContract")({ ...params }); + return await getAction(client, viem_writeContract, "writeContract")(params as never); }, { retries: 3, From effd7fc8814dc4649ad099977ca46aa1a9b79031 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 27 Aug 2024 15:37:52 +0100 Subject: [PATCH 3/4] maybe fix types --- packages/common/src/sendTransaction.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/src/sendTransaction.ts b/packages/common/src/sendTransaction.ts index 5247e97046..6e2775367e 100644 --- a/packages/common/src/sendTransaction.ts +++ b/packages/common/src/sendTransaction.ts @@ -43,7 +43,7 @@ export async function sendTransaction< chainOverride extends Chain | undefined = undefined, >( client: Client, - request: SendTransactionParameters, + request: SendTransactionParameters, opts: SendTransactionExtraOptions = {}, ): Promise { const rawAccount = request.account ?? client.account; @@ -80,7 +80,7 @@ export async function sendTransaction< ...feeRef.fees, } as const satisfies SendTransactionParameters; debug("sending tx to", request.to, "with nonce", nonce); - return await getAction(client, viem_sendTransaction, "sendTransaction")(params); + return await getAction(client, viem_sendTransaction, "sendTransaction")(params as never); }, { retries: 3, From 6858a5abafe01e56dc6eac8bc3f882ad3cb81925 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 27 Aug 2024 07:58:16 -0700 Subject: [PATCH 4/4] Create poor-nails-appear.md --- .changeset/poor-nails-appear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/poor-nails-appear.md diff --git a/.changeset/poor-nails-appear.md b/.changeset/poor-nails-appear.md new file mode 100644 index 0000000000..9ee7e6acc7 --- /dev/null +++ b/.changeset/poor-nails-appear.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/common": patch +--- + +`writeContract` and `sendTransaction` actions now use `pending` block tag when estimating gas. This aligns with previous behavior before changes in the last version.