|
1 | 1 | import type { Definition, TypedData } from "ox/TypedData";
|
2 | 2 | import type { Hex, SignableMessage } from "viem";
|
| 3 | +import type { Chain } from "../../../../chains/types.js"; |
3 | 4 | import { getCachedChain } from "../../../../chains/utils.js";
|
4 | 5 | import type { ThirdwebClient } from "../../../../client/client.js";
|
5 | 6 | import { getBytecode } from "../../../../contract/actions/get-bytecode.js";
|
|
8 | 9 | type ThirdwebContract,
|
9 | 10 | } from "../../../../contract/contract.js";
|
10 | 11 | import { execute } from "../../../../extensions/erc7702/__generated__/MinimalAccount/write/execute.js";
|
| 12 | +import { getRpcClient } from "../../../../rpc/rpc.js"; |
11 | 13 | import type { SignedAuthorization } from "../../../../transaction/actions/eip7702/authorization.js";
|
12 | 14 | import { toSerializableTransaction } from "../../../../transaction/actions/to-serializable-transaction.js";
|
13 | 15 | import type { SendTransactionResult } from "../../../../transaction/types.js";
|
@@ -49,9 +51,16 @@ export const create7702MinimalAccount = (args: {
|
49 | 51 | const isMinimalAccount = await is7702MinimalAccount(eoaContract);
|
50 | 52 | if (!isMinimalAccount) {
|
51 | 53 | // if not, sign authorization
|
52 |
| - const nonce = firstTx.nonce |
53 |
| - ? BigInt(firstTx.nonce) + (sponsorGas ? 0n : 1n) |
54 |
| - : 0n; // TODO (7702): get remote nonce if not provided, should be in the tx though |
| 54 | + let nonce = firstTx.nonce |
| 55 | + ? BigInt(firstTx.nonce) |
| 56 | + : BigInt( |
| 57 | + await getNonce({ |
| 58 | + client, |
| 59 | + address: adminAccount.address, |
| 60 | + chain: getCachedChain(firstTx.chainId), |
| 61 | + }), |
| 62 | + ); |
| 63 | + nonce += sponsorGas ? 0n : 1n; |
55 | 64 | const auth = await adminAccount.signAuthorization?.({
|
56 | 65 | address: MINIMAL_ACCOUNT_IMPLEMENTATION_ADDRESS,
|
57 | 66 | chainId: firstTx.chainId,
|
@@ -170,6 +179,27 @@ export const create7702MinimalAccount = (args: {
|
170 | 179 | return minimalAccount;
|
171 | 180 | };
|
172 | 181 |
|
| 182 | +async function getNonce(args: { |
| 183 | + client: ThirdwebClient; |
| 184 | + address: string; |
| 185 | + chain: Chain; |
| 186 | +}): Promise<number> { |
| 187 | + const { client, address, chain } = args; |
| 188 | + const rpcRequest = getRpcClient({ |
| 189 | + chain, |
| 190 | + client, |
| 191 | + }); |
| 192 | + const nonce = await import( |
| 193 | + "../../../../rpc/actions/eth_getTransactionCount.js" |
| 194 | + ).then(({ eth_getTransactionCount }) => |
| 195 | + eth_getTransactionCount(rpcRequest, { |
| 196 | + address, |
| 197 | + blockTag: "pending", |
| 198 | + }), |
| 199 | + ); |
| 200 | + return nonce; |
| 201 | +} |
| 202 | + |
173 | 203 | async function is7702MinimalAccount(
|
174 | 204 | eoaContract: ThirdwebContract,
|
175 | 205 | ): Promise<boolean> {
|
|
0 commit comments