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

fix(common): route all actions through viem client #3071

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-worms-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/common": patch
---

Updated all custom Viem actions to properly call other actions via `getAction` so they can be composed.
3 changes: 2 additions & 1 deletion packages/common/src/createFeeRef.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +18,7 @@ export async function createFeeRef({ client, args, refreshInterval }: CreateFeeR
const feeRef: FeeRef = { fees: {}, lastUpdatedTimestamp: 0 };

async function updateFees(): Promise<void> {
const fees = await estimateFeesPerGas(client, args);
const fees = await getAction(client, estimateFeesPerGas, "estimateFeesPerGas")(args);
feeRef.fees = fees;
feeRef.lastUpdatedTimestamp = Date.now();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/createNonceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -66,7 +67,7 @@ export function createNonceManager({

async function resetNonce(): Promise<void> {
ref.noncePromise ??= (async (): Promise<void> => {
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);
Expand Down
4 changes: 3 additions & 1 deletion packages/common/src/getFeeRef.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { getChainId } from "viem/actions";
import { CreateFeeRefOptions, FeeRef, createFeeRef } from "./createFeeRef";
import { getAction } from "viem/utils";

const feeRefs = new Map<number, FeeRef>();

export async function getFeeRef(opts: CreateFeeRefOptions): Promise<FeeRef> {
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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/getNonceManagerId.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,6 +12,6 @@ export async function getNonceManagerId({
blockTag: BlockTag;
}): Promise<string> {
// 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}`;
}
3 changes: 2 additions & 1 deletion packages/common/src/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading