diff --git a/.changeset/clean-worms-walk.md b/.changeset/clean-worms-walk.md new file mode 100644 index 0000000000..409a44a0f9 --- /dev/null +++ b/.changeset/clean-worms-walk.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/common": patch +--- + +Updated all custom Viem actions to properly call other actions via `getAction` so they can be composed. 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,