Skip to content

Commit

Permalink
fix(common): route all actions through viem client
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 27, 2024
1 parent 0eb2556 commit 8f04325
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
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

0 comments on commit 8f04325

Please sign in to comment.