Skip to content

Commit

Permalink
fix(common): use pending block tag in tx queue (#3073)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Sep 1, 2024
1 parent b434e8d commit c0764a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-nails-appear.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 10 additions & 6 deletions packages/common/src/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Transport,
SendTransactionReturnType,
PublicClient,
SendTransactionRequest,
} from "viem";
import { sendTransaction as viem_sendTransaction } from "viem/actions";
import pRetry from "p-retry";
Expand Down Expand Up @@ -38,10 +39,11 @@ export type SendTransactionExtraOptions<chain extends Chain | undefined> = {
export async function sendTransaction<
chain extends Chain | undefined,
account extends Account | undefined,
chainOverride extends Chain | undefined,
const request extends SendTransactionRequest<chain, chainOverride>,
chainOverride extends Chain | undefined = undefined,
>(
client: Client<Transport, chain, account>,
request: SendTransactionParameters<chain, account, chainOverride>,
request: SendTransactionParameters<chain, account, chainOverride, request>,
opts: SendTransactionExtraOptions<chain> = {},
): Promise<SendTransactionReturnType> {
const rawAccount = request.account ?? client.account;
Expand All @@ -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,
});

Expand All @@ -70,13 +73,14 @@ export async function sendTransaction<
pRetry(
async () => {
const nonce = nonceManager.nextNonce();
const params: SendTransactionParameters<chain, account, chainOverride> = {
const params = {
blockTag,
...request,
nonce,
...feeRef.fees,
};
} as const satisfies SendTransactionParameters<chain, account, chainOverride, request>;
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,
Expand Down
12 changes: 7 additions & 5 deletions packages/common/src/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type WriteContractExtraOptions<chain extends Chain | undefined> = {
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<abi, "nonpayable" | "payable">,
args extends ContractFunctionArgs<abi, "nonpayable" | "payable", functionName>,
chainOverride extends Chain | undefined,
Expand All @@ -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,
});

Expand All @@ -76,13 +77,14 @@ export async function writeContract<
pRetry(
async () => {
const nonce = nonceManager.nextNonce();
const params: WriteContractParameters<abi, functionName, args, chain, account, chainOverride> = {
const params = {
blockTag,
...request,
nonce,
...feeRef.fees,
};
} as const satisfies WriteContractParameters<abi, functionName, args, chain, account, chainOverride>;
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,
Expand Down

0 comments on commit c0764a5

Please sign in to comment.