-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathmultiple_pxe.ts
More file actions
121 lines (96 loc) · 4.52 KB
/
multiple_pxe.ts
File metadata and controls
121 lines (96 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { waitForPXE, getContractInstanceFromDeployParams, Fr, ContractInstanceWithAddress, AztecAddress, SponsoredFeePaymentMethod, createAztecNodeClient } from "@aztec/aztec.js";
import { TokenContract } from "@aztec/noir-contracts.js/Token"
import { getSponsoredFPCInstance } from "../src/utils/sponsored_fpc.js";
import { deriveSigningKey } from "@aztec/stdlib/keys";
import { getSchnorrAccount } from "@aztec/accounts/schnorr";
import { SponsoredFPCContract } from "@aztec/noir-contracts.js/SponsoredFPC";
import { createPXEService, getPXEServiceConfig } from '@aztec/pxe/server';
import { createStore } from "@aztec/kv-store/lmdb"
const { NODE_URL = 'http://localhost:8080' } = process.env;
const node = createAztecNodeClient(NODE_URL)
const l1Contracts = await node.getL1ContractAddresses();
const config = getPXEServiceConfig()
const fullConfig = { ...config, l1Contracts }
fullConfig.proverEnabled = false;
const store1 = await createStore('pxe1', {
dataDirectory: 'store',
dataStoreMapSizeKB: 1e6,
});
const store2 = await createStore('pxe2', {
dataDirectory: 'store',
dataStoreMapSizeKB: 1e6,
});
const setupPxe1 = async () => {
const pxe = await createPXEService(node, fullConfig, {store: store1});
await waitForPXE(pxe);
return pxe;
};
const setupPxe2 = async () => {
const pxe = await createPXEService(node, fullConfig, {store: store2});
await waitForPXE(pxe);
return pxe;
};
const L2_TOKEN_CONTRACT_SALT = Fr.random();
export async function getL2TokenContractInstance(deployerAddress: any, ownerAztecAddress: AztecAddress): Promise<ContractInstanceWithAddress> {
return await getContractInstanceFromDeployParams(
TokenContract.artifact,
{
salt: L2_TOKEN_CONTRACT_SALT,
deployer: deployerAddress,
constructorArgs: [
ownerAztecAddress,
'Clean USDC',
'USDC',
6
]
}
)
}
async function main() {
const pxe1 = await setupPxe1();
const pxe2 = await setupPxe2();
const sponseredFPC = await getSponsoredFPCInstance();
await pxe1.registerContract({ instance: sponseredFPC, artifact: SponsoredFPCContract.artifact });
await pxe2.registerContract({ instance: sponseredFPC, artifact: SponsoredFPCContract.artifact });
const paymentMethod = new SponsoredFeePaymentMethod(sponseredFPC.address);
// deploy token contract
let secretKey = Fr.random();
let salt = Fr.random();
let schnorrAccount = await getSchnorrAccount(pxe1, secretKey, deriveSigningKey(secretKey), salt);
let tx = await schnorrAccount.deploy({ fee: { paymentMethod } }).wait();
let ownerWallet = await schnorrAccount.getWallet();
let ownerAddress = ownerWallet.getAddress();
const token = await TokenContract.deploy(ownerWallet, ownerAddress, 'Clean USDC', 'USDC', 6).send({ contractAddressSalt: L2_TOKEN_CONTRACT_SALT, fee: { paymentMethod } }).wait()
// setup account on 2nd pxe
pxe2.registerSender(ownerAddress)
let secretKey2 = Fr.random();
let salt2 = Fr.random();
let schnorrAccount2 = await getSchnorrAccount(pxe2, secretKey2, deriveSigningKey(secretKey2), salt2);
// deploy account on 2nd pxe
let tx2 = await schnorrAccount2.deploy({ fee: { paymentMethod } }).wait();
let wallet2 = await schnorrAccount2.getWallet();
wallet2.registerSender(ownerAddress)
// mint to account on 2nd pxe
const private_mint_tx = await token.contract.methods.mint_to_private(ownerAddress, schnorrAccount2.getAddress(), 100).send({ fee: { paymentMethod } }).wait()
console.log(await pxe1.getTxEffect(private_mint_tx.txHash))
await token.contract.methods.mint_to_public(schnorrAccount2.getAddress(), 100).send({ fee: { paymentMethod } }).wait()
// setup token on 2nd pxe
const l2TokenContractInstance = await getL2TokenContractInstance(ownerAddress, ownerAddress)
await wallet2.registerContract({
instance: l2TokenContractInstance,
artifact: TokenContract.artifact
})
const l2TokenContract = await TokenContract.at(
l2TokenContractInstance.address,
wallet2
)
await l2TokenContract.methods.sync_private_state().simulate()
const notes = await pxe2.getNotes({ txHash: private_mint_tx.txHash });
console.log(notes)
// returns 0n
const balance = await l2TokenContract.methods.balance_of_private(wallet2.getAddress()).simulate()
console.log("private balance should be 100", balance)
// errors
await l2TokenContract.methods.balance_of_public(wallet2.getAddress()).simulate()
}
main();