1- import { AccountWallet , CompleteAddress , createLogger , FeeJuicePaymentMethodWithClaim , Fr , L1FeeJuicePortalManager , PXE , waitForPXE , createPXEClient , Logger , FeeJuicePaymentMethod , PrivateFeePaymentMethod , PublicFeePaymentMethod } from "@aztec/aztec.js" ;
2- import { getInitialTestAccountsWallets } from "@aztec/accounts/testing" ;
31import {
4- createPublicClient ,
52 createWalletClient ,
63 http ,
74} from 'viem' ;
8- import { getSchnorrAccount } from '@aztec/accounts/schnorr' ;
9- import { deriveSigningKey } from '@aztec/stdlib/keys' ;
105import { foundry } from 'viem/chains'
116import { mnemonicToAccount } from 'viem/accounts' ;
127import { FeeJuiceContract } from "@aztec/noir-contracts.js/FeeJuice" ;
@@ -15,46 +10,46 @@ import { EasyPrivateVotingContract } from "../src/artifacts/EasyPrivateVoting.js
1510import { TokenContract } from "@aztec/noir-contracts.js/Token" ;
1611// TODO: replace with import from aztec.js when published
1712import { SponsoredFeePaymentMethod } from '@aztec/aztec.js/fee/testing'
18- import { getDeployedSponsoredFPCAddress } from "../src/utils/sponsored_fpc.js" ;
13+ import { getDeployedSponsoredFPCAddress , getSponsoredFPCInstance } from "../src/utils/sponsored_fpc.js" ;
1914import { createEthereumChain , createExtendedL1Client } from "@aztec/ethereum" ;
20-
21- const setupSandbox = async ( ) => {
22- const { PXE_URL = 'http://localhost:8080' } = process . env ;
23- const pxe = await createPXEClient ( PXE_URL ) ;
24- await waitForPXE ( pxe ) ;
25- return pxe ;
26- } ;
15+ import { deploySchnorrAccount } from "../src/utils/deploy_account.js" ;
16+ import { setupPXE } from "../src/utils/setup_pxe.js" ;
17+ import { createLogger , FeeJuicePaymentMethod , FeeJuicePaymentMethodWithClaim , Fr , L1FeeJuicePortalManager , Logger , PrivateFeePaymentMethod , PublicFeePaymentMethod , PXE } from '@aztec/aztec.js' ;
18+ import { SponsoredFPCContract } from '@aztec/noir-contracts.js/SponsoredFPC' ;
19+ import { getCanonicalFeeJuice } from '@aztec/protocol-contracts/fee-juice' ;
20+ import { getSchnorrAccount } from '@aztec/accounts/schnorr' ;
21+ import { deriveSigningKey } from '@aztec/stdlib/keys' ;
2722
2823const MNEMONIC = 'test test test test test test test test test test test junk' ;
2924const FEE_FUNDING_FOR_TESTER_ACCOUNT = 1000000000000000000n ;
3025
3126let walletClient = getL1WalletClient ( foundry . rpcUrls . default . http [ 0 ] , 0 ) ;
3227const ownerEthAddress = walletClient . account . address ;
3328
34-
3529async function main ( ) {
3630
3731 let pxe : PXE ;
38- let wallets : AccountWallet [ ] = [ ] ;
3932 let logger : Logger ;
4033
4134 logger = createLogger ( 'aztec:aztec-starter' ) ;
4235
43- pxe = await setupSandbox ( ) ;
44- wallets = await getInitialTestAccountsWallets ( pxe ) ;
36+ pxe = await setupPXE ( ) ;
37+ // wallets = await getInitialTestAccountsWallets(pxe);
4538 const nodeInfo = ( await pxe . getNodeInfo ( ) )
4639
4740 const chain = createEthereumChain ( [ 'http://localhost:8545' ] , nodeInfo . l1ChainId ) ;
4841 const l1Client = createExtendedL1Client ( chain . rpcUrls , MNEMONIC , chain . chainInfo ) ;
4942
5043 // Setup Schnorr AccountManager
5144
45+ const account1 = await deploySchnorrAccount ( pxe ) ;
46+ const wallet1 = await account1 . getWallet ( ) ;
47+
5248 let secretKey = Fr . random ( ) ;
5349 let salt = Fr . random ( ) ;
54- let schnorrAccount = await getSchnorrAccount ( pxe , secretKey , deriveSigningKey ( secretKey ) , salt ) ;
55-
56- const newWallet = await schnorrAccount . getWallet ( )
57- const feeJuiceReceipient = schnorrAccount . getAddress ( )
50+ let account2 = await getSchnorrAccount ( pxe , secretKey , deriveSigningKey ( secretKey ) , salt ) ;
51+ const wallet2 = await account2 . getWallet ( ) ;
52+ const feeJuiceRecipient = account2 . getAddress ( ) ;
5853
5954 // Setup and bridge fee asset to L2 to get fee juice
6055
@@ -65,26 +60,30 @@ async function main() {
6560 logger ,
6661 ) ;
6762
68- const claim = await feeJuicePortalManager . bridgeTokensPublic ( feeJuiceReceipient , FEE_FUNDING_FOR_TESTER_ACCOUNT , true ) ;
63+ const claim = await feeJuicePortalManager . bridgeTokensPublic ( feeJuiceRecipient , FEE_FUNDING_FOR_TESTER_ACCOUNT , true ) ;
64+
65+ logger . info ( `Fee Juice minted to ${ feeJuiceRecipient } on L2.` )
6966
70- const feeJuice = await FeeJuiceContract . at ( nodeInfo . protocolContractAddresses . feeJuice , wallets [ 0 ] )
71- logger . info ( `Fee Juice minted to ${ feeJuiceReceipient } on L2.` )
67+ // set up sponsored fee payments
68+ const sponseredFPC = await getSponsoredFPCInstance ( ) ;
69+ await pxe . registerContract ( { instance : sponseredFPC , artifact : SponsoredFPCContract . artifact } ) ;
70+ const paymentMethod = new SponsoredFeePaymentMethod ( sponseredFPC . address ) ;
7271
7372 // Two arbitraty txs to make the L1 message available on L2
74- const votingContract = await EasyPrivateVotingContract . deploy ( wallets [ 0 ] , wallets [ 0 ] . getAddress ( ) ) . send ( ) . deployed ( ) ;
75- const bananaCoin = await TokenContract . deploy ( wallets [ 0 ] , wallets [ 0 ] . getAddress ( ) , "bananaCoin" , "BNC" , 18 ) . send ( ) . deployed ( )
73+ const votingContract = await EasyPrivateVotingContract . deploy ( wallet1 , wallet1 . getAddress ( ) ) . send ( { fee : { paymentMethod } } ) . deployed ( ) ;
74+ const bananaCoin = await TokenContract . deploy ( wallet1 , wallet1 . getAddress ( ) , "bananaCoin" , "BNC" , 18 ) . send ( { fee : { paymentMethod } } ) . deployed ( )
7675
7776 // Claim Fee Juice & Pay Fees yourself
7877
79- const claimAndPay = new FeeJuicePaymentMethodWithClaim ( newWallet , claim )
80- await schnorrAccount . deploy ( { fee : { paymentMethod : claimAndPay } } ) . wait ( )
81- logger . info ( `New account at ${ newWallet . getAddress ( ) } deployed using claimed funds for fees.` )
78+ const claimAndPay = new FeeJuicePaymentMethodWithClaim ( wallet2 , claim )
79+ await account2 . deploy ( { fee : { paymentMethod : claimAndPay } } ) . wait ( )
80+ logger . info ( `New account at ${ account2 . getAddress ( ) } deployed using claimed funds for fees.` )
8281
8382 // Pay fees yourself
8483
8584 // Create a new voting contract instance, interacting from the newWallet
86- const useFeeJuice = new FeeJuicePaymentMethod ( newWallet . getAddress ( ) )
87- await votingContract . withWallet ( newWallet ) . methods . cast_vote ( wallets [ 0 ] . getAddress ( ) ) . send ( { fee : { paymentMethod : useFeeJuice } } ) . wait ( )
85+ const useFeeJuice = new FeeJuicePaymentMethod ( account2 . getAddress ( ) )
86+ await votingContract . withWallet ( wallet2 ) . methods . cast_vote ( wallet1 . getAddress ( ) ) . send ( { fee : { paymentMethod : useFeeJuice } } ) . wait ( )
8887 logger . info ( `Vote cast from new account, paying fees via newWallet.` )
8988
9089 // Private Fee Payments via FPC
@@ -93,38 +92,40 @@ async function main() {
9392 // Need to deploy an FPC to use Private Fee payment methods
9493
9594 // This uses bananaCoin as the fee paying asset that will be exchanged for fee juice
96- const fpc = await FPCContract . deploy ( wallets [ 0 ] , bananaCoin . address , wallets [ 0 ] . getAddress ( ) ) . send ( ) . deployed ( )
95+ const fpc = await FPCContract . deploy ( wallet1 , bananaCoin . address , wallet1 . getAddress ( ) ) . send ( { fee : { paymentMethod } } ) . deployed ( )
9796 const fpcClaim = await feeJuicePortalManager . bridgeTokensPublic ( fpc . address , FEE_FUNDING_FOR_TESTER_ACCOUNT , true ) ;
9897 // 2 public txs to make the bridged fee juice available
9998 // Mint some bananaCoin and send to the newWallet to pay fees privately
100- await bananaCoin . methods . mint_to_private ( wallets [ 0 ] . getAddress ( ) , newWallet . getAddress ( ) , FEE_FUNDING_FOR_TESTER_ACCOUNT ) . send ( ) . wait ( )
99+ await bananaCoin . methods . mint_to_private ( wallet1 . getAddress ( ) , wallet2 . getAddress ( ) , FEE_FUNDING_FOR_TESTER_ACCOUNT ) . send ( { fee : { paymentMethod } } ) . wait ( )
101100 // mint some public bananaCoin to the newWallet to pay fees publicly
102- await bananaCoin . methods . mint_to_public ( newWallet . getAddress ( ) , FEE_FUNDING_FOR_TESTER_ACCOUNT ) . send ( ) . wait ( )
103- const bananaBalance = await bananaCoin . withWallet ( newWallet ) . methods . balance_of_private ( newWallet . getAddress ( ) ) . simulate ( )
101+ await bananaCoin . methods . mint_to_public ( wallet2 . getAddress ( ) , FEE_FUNDING_FOR_TESTER_ACCOUNT ) . send ( { fee : { paymentMethod } } ) . wait ( )
102+ const bananaBalance = await bananaCoin . withWallet ( wallet2 ) . methods . balance_of_private ( wallet2 . getAddress ( ) ) . simulate ( )
104103
105104 logger . info ( `BananaCoin balance of newWallet is ${ bananaBalance } ` )
106105
106+ const feeJuiceInstance = await getCanonicalFeeJuice ( ) ;
107+ const feeJuice = await FeeJuiceContract . at ( feeJuiceInstance . address , wallet2 )
107108 await feeJuice . methods . claim ( fpc . address , fpcClaim . claimAmount , fpcClaim . claimSecret , fpcClaim . messageLeafIndex ) . send ( ) . wait ( )
108109
109110 logger . info ( `Fpc fee juice balance ${ await feeJuice . methods . balance_of_public ( fpc . address ) . simulate ( ) } ` )
110111
111- const privateFee = new PrivateFeePaymentMethod ( fpc . address , newWallet )
112- await bananaCoin . withWallet ( newWallet ) . methods . transfer_in_private ( newWallet . getAddress ( ) , wallets [ 0 ] . getAddress ( ) , 10 , 0 ) . send ( { fee : { paymentMethod : privateFee } } ) . wait ( )
113-
112+ const privateFee = new PrivateFeePaymentMethod ( fpc . address , wallet2 )
113+ await bananaCoin . withWallet ( wallet2 ) . methods . transfer_in_private ( wallet2 . getAddress ( ) , wallet1 . getAddress ( ) , 10 , 0 ) . send ( { fee : { paymentMethod : privateFee } } ) . wait ( )
114+
114115 logger . info ( `Transfer paid with fees via the FPC, privately.` )
115116
116117 // Public Fee Payments via FPC
117118
118- const publicFee = new PublicFeePaymentMethod ( fpc . address , newWallet )
119- await bananaCoin . withWallet ( newWallet ) . methods . transfer_in_private ( newWallet . getAddress ( ) , wallets [ 0 ] . getAddress ( ) , 10 , 0 ) . send ( { fee : { paymentMethod : publicFee } } ) . wait ( )
119+ const publicFee = new PublicFeePaymentMethod ( fpc . address , wallet2 )
120+ await bananaCoin . withWallet ( wallet2 ) . methods . transfer_in_private ( wallet2 . getAddress ( ) , wallet1 . getAddress ( ) , 10 , 0 ) . send ( { fee : { paymentMethod : publicFee } } ) . wait ( )
120121 logger . info ( `Transfer paid with fees via the FPC, publicly.` )
121122
122123 // Sponsored Fee Payment
123124
124125 // This method will only work in environments where there is a sponsored fee contract deployed
125126 const deployedSponseredFPC = await getDeployedSponsoredFPCAddress ( pxe ) ;
126127 const sponsoredPaymentMethod = new SponsoredFeePaymentMethod ( deployedSponseredFPC ) ;
127- await bananaCoin . withWallet ( newWallet ) . methods . transfer_in_private ( newWallet . getAddress ( ) , wallets [ 0 ] . getAddress ( ) , 10 , 0 ) . send ( { fee : { paymentMethod : sponsoredPaymentMethod } } ) . wait ( )
128+ await bananaCoin . withWallet ( wallet2 ) . methods . transfer_in_private ( wallet2 . getAddress ( ) , wallet1 . getAddress ( ) , 10 , 0 ) . send ( { fee : { paymentMethod : sponsoredPaymentMethod } } ) . wait ( )
128129 logger . info ( `Transfer paid with fees from Sponsored FPC.` )
129130}
130131
@@ -138,4 +139,5 @@ function getL1WalletClient(rpcUrl: string, index: number) {
138139 chain : foundry ,
139140 transport : http ( rpcUrl ) ,
140141 } ) ;
141- }
142+ }
143+
0 commit comments