Skip to content

[SDK] Fallback to onchain nonce in 7702 execution #7729

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

Merged
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/silly-bugs-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fallback to onchain nonce in 7702 execution
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function Eip7702SmartAccountPreview() {
setTxHash(null);
}}
onError={(error) => {
console.error("minting error", error);
alert(`Error: ${error.message}`);
}}
onTransactionSent={async (tx) => {
Expand Down
56 changes: 14 additions & 42 deletions packages/thirdweb/src/transaction/actions/estimate-gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as ox__Hex from "ox/Hex";
import { formatTransactionRequest } from "viem";
import { roundUpGas } from "../../gas/op-gas-fee-reducer.js";
import { getAddress } from "../../utils/address.js";
import { hexToBytes } from "../../utils/encoding/to-bytes.js";
import { resolvePromisedValue } from "../../utils/promise/resolve-promised-value.js";
import type { Prettify } from "../../utils/type-utils.js";
import type { Account } from "../../wallets/interfaces/wallet.js";
Expand Down Expand Up @@ -117,31 +116,20 @@ export async function estimateGas(

const rpcRequest = getRpcClient(options.transaction);
try {
let gas = await eth_estimateGas(
rpcRequest,
formatTransactionRequest({
authorizationList: authorizationList?.map((auth) => ({
...auth,
contractAddress: getAddress(auth.address),
nonce: Number(auth.nonce),
r: ox__Hex.fromNumber(auth.r),
s: ox__Hex.fromNumber(auth.s),
})),
data: encodedData,
from: fromAddress ? getAddress(fromAddress) : undefined,
to: toAddress ? getAddress(toAddress) : undefined,
value,
...(authorizationList && authorizationList?.length > 0
? {
gas:
minGas(
hexToBytes(encodedData),
BigInt(authorizationList?.length ?? 0),
) + 100_000n,
}
: {}),
}),
);
const formattedTx = formatTransactionRequest({
authorizationList: authorizationList?.map((auth) => ({
...auth,
contractAddress: getAddress(auth.address),
nonce: Number(auth.nonce),
r: ox__Hex.fromNumber(auth.r),
s: ox__Hex.fromNumber(auth.s),
})),
data: encodedData,
from: fromAddress ? getAddress(fromAddress) : undefined,
to: toAddress ? getAddress(toAddress) : undefined,
value,
});
let gas = await eth_estimateGas(rpcRequest, formattedTx);

if (options.transaction.chain.experimental?.increaseZeroByteCount) {
gas = roundUpGas(gas);
Expand All @@ -158,19 +146,3 @@ export async function estimateGas(
cache.set(txWithFrom, promise);
return promise;
}

// EIP-7623 + EIP-7702 floor calculation
const TxGas = 21_000n;
const TxCostFloorPerToken = 10n; // params.TxCostFloorPerToken
const TxTokenPerNonZero = 4n; // params.TxTokenPerNonZeroByte
const TxAuthTupleGas = 12_500n;

function minGas(data: Uint8Array, authCount = 0n) {
let nz = 0n;
for (const b of data) if (b !== 0) nz++;
const z = BigInt(data.length) - nz;
const tokens = nz * TxTokenPerNonZero + z;
const floor = TxGas + tokens * TxCostFloorPerToken;
const intrinsic = TxGas + authCount * TxAuthTupleGas;
return floor > intrinsic ? floor : intrinsic;
}
Loading
Loading