Skip to content

Commit 58dbe90

Browse files
[SDK] Fallback to onchain nonce in 7702 execution (#7729)
1 parent 0c6d75d commit 58dbe90

File tree

4 files changed

+650
-46
lines changed

4 files changed

+650
-46
lines changed

.changeset/silly-bugs-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fallback to onchain nonce in 7702 execution

apps/playground-web/src/components/account-abstraction/7702-smart-account.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export function Eip7702SmartAccountPreview() {
101101
setTxHash(null);
102102
}}
103103
onError={(error) => {
104+
console.error("minting error", error);
104105
alert(`Error: ${error.message}`);
105106
}}
106107
onTransactionSent={async (tx) => {

packages/thirdweb/src/transaction/actions/estimate-gas.ts

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as ox__Hex from "ox/Hex";
22
import { formatTransactionRequest } from "viem";
33
import { roundUpGas } from "../../gas/op-gas-fee-reducer.js";
44
import { getAddress } from "../../utils/address.js";
5-
import { hexToBytes } from "../../utils/encoding/to-bytes.js";
65
import { resolvePromisedValue } from "../../utils/promise/resolve-promised-value.js";
76
import type { Prettify } from "../../utils/type-utils.js";
87
import type { Account } from "../../wallets/interfaces/wallet.js";
@@ -117,31 +116,20 @@ export async function estimateGas(
117116

118117
const rpcRequest = getRpcClient(options.transaction);
119118
try {
120-
let gas = await eth_estimateGas(
121-
rpcRequest,
122-
formatTransactionRequest({
123-
authorizationList: authorizationList?.map((auth) => ({
124-
...auth,
125-
contractAddress: getAddress(auth.address),
126-
nonce: Number(auth.nonce),
127-
r: ox__Hex.fromNumber(auth.r),
128-
s: ox__Hex.fromNumber(auth.s),
129-
})),
130-
data: encodedData,
131-
from: fromAddress ? getAddress(fromAddress) : undefined,
132-
to: toAddress ? getAddress(toAddress) : undefined,
133-
value,
134-
...(authorizationList && authorizationList?.length > 0
135-
? {
136-
gas:
137-
minGas(
138-
hexToBytes(encodedData),
139-
BigInt(authorizationList?.length ?? 0),
140-
) + 100_000n,
141-
}
142-
: {}),
143-
}),
144-
);
119+
const formattedTx = formatTransactionRequest({
120+
authorizationList: authorizationList?.map((auth) => ({
121+
...auth,
122+
contractAddress: getAddress(auth.address),
123+
nonce: Number(auth.nonce),
124+
r: ox__Hex.fromNumber(auth.r),
125+
s: ox__Hex.fromNumber(auth.s),
126+
})),
127+
data: encodedData,
128+
from: fromAddress ? getAddress(fromAddress) : undefined,
129+
to: toAddress ? getAddress(toAddress) : undefined,
130+
value,
131+
});
132+
let gas = await eth_estimateGas(rpcRequest, formattedTx);
145133

146134
if (options.transaction.chain.experimental?.increaseZeroByteCount) {
147135
gas = roundUpGas(gas);
@@ -158,19 +146,3 @@ export async function estimateGas(
158146
cache.set(txWithFrom, promise);
159147
return promise;
160148
}
161-
162-
// EIP-7623 + EIP-7702 floor calculation
163-
const TxGas = 21_000n;
164-
const TxCostFloorPerToken = 10n; // params.TxCostFloorPerToken
165-
const TxTokenPerNonZero = 4n; // params.TxTokenPerNonZeroByte
166-
const TxAuthTupleGas = 12_500n;
167-
168-
function minGas(data: Uint8Array, authCount = 0n) {
169-
let nz = 0n;
170-
for (const b of data) if (b !== 0) nz++;
171-
const z = BigInt(data.length) - nz;
172-
const tokens = nz * TxTokenPerNonZero + z;
173-
const floor = TxGas + tokens * TxCostFloorPerToken;
174-
const intrinsic = TxGas + authCount * TxAuthTupleGas;
175-
return floor > intrinsic ? floor : intrinsic;
176-
}

0 commit comments

Comments
 (0)