11import { SponsoredFeePaymentMethod , FeeJuicePaymentMethodWithClaim } from "@aztec/aztec.js/fee" ;
2+ import type { FeePaymentMethod } from "@aztec/aztec.js/fee" ;
23import { getSponsoredFPCInstance } from "./sponsored_fpc.js" ;
34import { SponsoredFPCContractArtifact } from "@aztec/noir-contracts.js/SponsoredFPC" ;
5+ import { ProtocolContractAddress } from "@aztec/aztec.js/protocol" ;
46import { Fr } from "@aztec/aztec.js/fields" ;
57import { GrumpkinScalar } from "@aztec/foundation/curves/grumpkin" ;
68import { type Logger , createLogger } from "@aztec/foundation/log" ;
@@ -9,6 +11,7 @@ import { AztecAddress } from "@aztec/aztec.js/addresses";
911import { AccountManager } from "@aztec/aztec.js/wallet" ;
1012import { EmbeddedWallet } from "@aztec/wallets/embedded" ;
1113import { createAztecNodeClient } from "@aztec/aztec.js/node" ;
14+ import { deriveStorageSlotInMap } from "@aztec/stdlib/hash" ;
1215import configManager , { getAztecNodeUrl , getTimeouts } from "../../config/config.js" ;
1316import { bridgeL1FeeJuice } from "./bridge_fee_juice.js" ;
1417
@@ -36,15 +39,23 @@ export async function deploySchnorrAccount(wallet?: EmbeddedWallet): Promise<Acc
3639 const deployMethod = await account . getDeployMethod ( ) ;
3740 const timeouts = getTimeouts ( ) ;
3841
39- let paymentMethod ;
42+ let paymentMethod : FeePaymentMethod | undefined ;
4043
4144 if ( configManager . isTestnet ( ) ) {
42- // Testnet: bridge fee juice from L1 and use it to pay for deployment
43- logger . info ( '💰 Bridging fee juice from Sepolia L1 for account deployment...' ) ;
4445 const node = createAztecNodeClient ( getAztecNodeUrl ( ) ) ;
45- const claim = await bridgeL1FeeJuice ( node , account . address , FEE_JUICE_AMOUNT , logger ) ;
46- paymentMethod = new FeeJuicePaymentMethodWithClaim ( account . address , claim ) ;
47- logger . info ( '✅ Fee juice claim ready for account deployment' ) ;
46+
47+ // Check if the account already has fee juice on L2
48+ const balanceSlot = await deriveStorageSlotInMap ( new Fr ( 1 ) , account . address ) ;
49+ const balance = ( await node . getPublicStorageAt ( 'latest' , ProtocolContractAddress . FeeJuice , balanceSlot ) ) . toBigInt ( ) ;
50+
51+ if ( balance > 0n ) {
52+ logger . info ( `💰 Account already has ${ balance } fee juice on L2, skipping bridge` ) ;
53+ } else {
54+ logger . info ( '💰 No fee juice found, bridging from Sepolia L1...' ) ;
55+ const claim = await bridgeL1FeeJuice ( node , account . address , FEE_JUICE_AMOUNT , logger ) ;
56+ paymentMethod = new FeeJuicePaymentMethodWithClaim ( account . address , claim ) ;
57+ logger . info ( '✅ Fee juice claim ready for account deployment' ) ;
58+ }
4859 } else {
4960 // Devnet/local: use sponsored FPC
5061 logger . info ( '💰 Setting up sponsored fee payment for account deployment...' ) ;
@@ -64,11 +75,14 @@ export async function deploySchnorrAccount(wallet?: EmbeddedWallet): Promise<Acc
6475 logger . info ( '✅ Simulation successful, sending deployment transaction...' ) ;
6576
6677 // Deploy account
67- await deployMethod . send ( {
78+ const sendOpts : any = {
6879 from : AztecAddress . ZERO ,
69- fee : { paymentMethod } ,
7080 wait : { timeout : timeouts . deployTimeout } ,
71- } ) ;
81+ } ;
82+ if ( paymentMethod ) {
83+ sendOpts . fee = { paymentMethod } ;
84+ }
85+ await deployMethod . send ( sendOpts ) ;
7286
7387 logger . info ( `✅ Account deployment transaction successful!` ) ;
7488
0 commit comments