|
1 | | -import { AccountWalletWithSecretKey, Contract, createPXEClient, waitForPXE, type FieldLike, type PXE } from "@aztec/aztec.js" |
2 | | - |
| 1 | +import { AccountWalletWithSecretKey, Contract, createAztecNodeClient, Fq, Fr, SponsoredFeePaymentMethod, waitForPXE, type FieldLike, type PXE } from "@aztec/aztec.js" |
| 2 | +import { getSponsoredFPCInstance } from "./sponsored_fpc.js"; |
| 3 | +import { SponsoredFPCContract } from "@aztec/noir-contracts.js/SponsoredFPC"; |
3 | 4 | export const PXE_URL = 'http://localhost:8080' |
4 | | -import { getInitialTestAccountsWallets } from '@aztec/accounts/testing' |
5 | 5 | import { ValueNotEqualContract, ValueNotEqualContractArtifact } from '../contract/artifacts/ValueNotEqual' |
6 | 6 | import data from '../data.json' |
| 7 | +import { createPXEService, getPXEServiceConfig } from "@aztec/pxe/server" |
| 8 | +import { createStore } from "@aztec/kv-store/lmdb" |
| 9 | +import { getSchnorrAccount } from "@aztec/accounts/schnorr" |
7 | 10 |
|
8 | | -export const setupSandbox = async (): Promise<PXE> => { |
9 | | - try { |
10 | | - console.log(`Setting up sandbox with PXE URL: ${PXE_URL}`) |
11 | | - const pxe = await createPXEClient(PXE_URL) |
12 | | - await waitForPXE(pxe) |
13 | | - console.log('PXE client created and connected successfully') |
14 | | - return pxe |
15 | | - } catch (error) { |
16 | | - console.error('Failed to setup sandbox:', error) |
17 | | - throw error |
18 | | - } |
19 | | -} |
20 | | -export interface TestWallets { |
21 | | - owner: AccountWalletWithSecretKey |
22 | | - user1: AccountWalletWithSecretKey |
23 | | - user2: AccountWalletWithSecretKey |
24 | | - user3: AccountWalletWithSecretKey |
25 | | -} |
| 11 | +const sponsoredFPC = await getSponsoredFPCInstance(); |
| 12 | +const sponsoredPaymentMethod = new SponsoredFeePaymentMethod(sponsoredFPC.address); |
26 | 13 |
|
27 | | -export const setupWallets = async (pxe: PXE): Promise<TestWallets> => { |
| 14 | +export const setupSandbox = async (): Promise<PXE> => { |
28 | 15 | try { |
29 | | - console.log('Setting up test wallets') |
30 | | - const wallets = await getInitialTestAccountsWallets(pxe) |
31 | | - |
32 | | - const testWallets: TestWallets = { |
33 | | - owner: wallets[0], |
34 | | - user1: wallets[1], |
35 | | - user2: wallets[2], |
36 | | - //? Here wallet[3] is always coming wallets[0], so please keep this in mind |
37 | | - user3: wallets[3] || wallets[0], // Fallback if not enough wallets |
| 16 | + const nodeUrl = 'http://localhost:8080'; |
| 17 | + const node = await createAztecNodeClient(nodeUrl); |
| 18 | + |
| 19 | + try { |
| 20 | + await node.getNodeInfo(); |
| 21 | + } catch (error) { |
| 22 | + throw new Error(`Cannot connect to node at ${nodeUrl}. ${nodeUrl.includes('localhost') ? 'Please run: aztec start --sandbox' : 'Check your connection.'}`); |
38 | 23 | } |
39 | 24 |
|
40 | | - console.log('Test wallets configured') |
41 | | - console.info('Owner address:', testWallets.owner.getAddress().toString()) |
42 | | - console.info('User1 address:', testWallets.user1.getAddress().toString()) |
43 | | - console.info('User2 address:', testWallets.user2.getAddress().toString()) |
| 25 | + const l1Contracts = await node.getL1ContractAddresses(); |
| 26 | + const config = getPXEServiceConfig(); |
| 27 | + const fullConfig = { |
| 28 | + ...config, |
| 29 | + l1Contracts, |
| 30 | + proverEnabled: true |
| 31 | + }; |
| 32 | + |
| 33 | + const store = await createStore('recursive_verification', { |
| 34 | + dataDirectory: 'store', |
| 35 | + dataStoreMapSizeKB: 1e6, |
| 36 | + }); |
44 | 37 |
|
45 | | - return testWallets |
| 38 | + const pxe = await createPXEService(node, fullConfig, { store }); |
| 39 | + await waitForPXE(pxe); |
| 40 | + await pxe.registerContract({ instance: sponsoredFPC, artifact: SponsoredFPCContract.artifact }); |
| 41 | + |
| 42 | + return pxe; |
46 | 43 | } catch (error) { |
47 | | - console.error('Failed to setup wallets:', error) |
| 44 | + console.error('Failed to setup sandbox:', error) |
48 | 45 | throw error |
49 | 46 | } |
50 | 47 | } |
51 | 48 |
|
| 49 | +export async function deployWallet(pxe: PXE): Promise<AccountWalletWithSecretKey> { |
| 50 | + let secretKey = Fr.random(); |
| 51 | + let signingKey = Fq.random(); |
| 52 | + let salt = Fr.random(); |
| 53 | + let schnorrAccount = await getSchnorrAccount(pxe, secretKey, signingKey, salt); |
| 54 | + let tx = await schnorrAccount.deploy({ fee: { paymentMethod: sponsoredPaymentMethod } }).wait({ timeout: 120000 }); |
| 55 | + let wallet = await schnorrAccount.getWallet(); |
| 56 | + return wallet |
| 57 | +} |
| 58 | + |
52 | 59 | async function main() { |
53 | 60 | const pxe = await setupSandbox(); |
54 | | - const wallets = await setupWallets(pxe) |
| 61 | + const wallet = await deployWallet(pxe) |
55 | 62 |
|
56 | | - const valueNotEqual = await Contract.deploy(wallets.owner, ValueNotEqualContractArtifact, [ |
57 | | - 10, wallets.owner.getAddress() |
58 | | - ], 'initialize').send({ from: wallets.owner.getAddress() }).deployed() as ValueNotEqualContract |
| 63 | + const valueNotEqual = await Contract.deploy(wallet, ValueNotEqualContractArtifact, [ |
| 64 | + 10, wallet.getAddress() |
| 65 | + ], 'initialize').send({ from: wallet.getAddress(), fee: { paymentMethod: sponsoredPaymentMethod } }).deployed() as ValueNotEqualContract |
59 | 66 |
|
60 | 67 | console.log("Contract Deployed at address", valueNotEqual.address.toString()) |
61 | 68 |
|
62 | | - const tx = await valueNotEqual.methods.increment(wallets.owner.getAddress(), data.vkAsFields as unknown as FieldLike[], data.proofAsFields as unknown as FieldLike[], data.publicInputs as unknown as FieldLike[]).send({ from: wallets.owner.getAddress() }).wait() |
| 69 | + const tx = await valueNotEqual.methods.increment(wallet.getAddress(), data.vkAsFields as unknown as FieldLike[], data.proofAsFields as unknown as FieldLike[], data.publicInputs as unknown as FieldLike[]).send({ from: wallet.getAddress(), fee: { paymentMethod: sponsoredPaymentMethod } }).wait() |
63 | 70 |
|
64 | 71 | console.log(`Tx hash: ${tx.txHash.toString()}`) |
65 | | - const counterValue = await valueNotEqual.methods.get_counter(wallets.owner.getAddress()).simulate({ from: wallets.owner.getAddress() }) |
| 72 | + const counterValue = await valueNotEqual.methods.get_counter(wallet.getAddress()).simulate({ from: wallet.getAddress() }) |
66 | 73 | console.log(`Counter value: ${counterValue}`) |
67 | 74 | } |
68 | 75 |
|
|
0 commit comments