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): use pending block tag in tx queue #3073

Merged
merged 7 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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/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>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes align with viem's type signature (forgot to do this last version bump)

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);
Copy link
Member Author

@holic holic Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

viem types don't like blockTag here, but it does make it through to the eth_estimateGas RPC call, so I'm hoping to get this fixed upstream

},
{
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
Loading