This repository was archived by the owner on Jul 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtoken.test.ts
More file actions
566 lines (464 loc) · 20.2 KB
/
Copy pathtoken.test.ts
File metadata and controls
566 lines (464 loc) · 20.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import {
ContractDeployer,
Fr,
TxStatus,
getContractInstanceFromDeployParams,
Contract,
AccountWalletWithSecretKey,
IntentAction,
Wallet,
} from '@aztec/aztec.js';
import { AMOUNT, deployTokenWithMinter, expectTokenBalances, expectUintNote, setupPXE, wad } from './utils.js';
import { PXE } from '@aztec/stdlib/interfaces/client';
import { AztecLmdbStore } from '@aztec/kv-store/lmdb';
import { getInitialTestAccountsManagers } from '@aztec/accounts/testing';
import { TokenContractArtifact, TokenContract } from '../../artifacts/Token.js';
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
export async function deployTokenWithInitialSupply(deployer: Wallet, options: any) {
const contract = await Contract.deploy(
deployer,
TokenContractArtifact,
['PrivateToken', 'PT', 18, 0, deployer.getAddress(), deployer.getAddress()],
'constructor_with_initial_supply',
)
.send(options)
.deployed();
return contract;
}
const setupTestSuite = async () => {
const { pxe, store } = await setupPXE();
const managers = await getInitialTestAccountsManagers(pxe);
const wallets = await Promise.all(managers.map((acc) => acc.register()));
const [deployer] = wallets;
return { pxe, deployer, wallets, store };
};
describe('Token - Single PXE', () => {
let pxe: PXE;
let store: AztecLmdbStore;
let wallets: AccountWalletWithSecretKey[];
let deployer: AccountWalletWithSecretKey;
let alice: AccountWalletWithSecretKey;
let bob: AccountWalletWithSecretKey;
let carl: AccountWalletWithSecretKey;
let token: TokenContract;
beforeAll(async () => {
({ pxe, deployer, wallets, store } = await setupTestSuite());
[alice, bob, carl] = wallets;
});
beforeEach(async () => {
token = (await deployTokenWithMinter(alice, {})) as TokenContract;
});
afterAll(async () => {
await store.delete();
});
it('deploys the contract with minter', async () => {
const salt = Fr.random();
const deployerWallet = alice;
const deploymentData = await getContractInstanceFromDeployParams(TokenContractArtifact, {
constructorArtifact: 'constructor_with_minter',
constructorArgs: ['PrivateToken', 'PT', 18, deployerWallet.getAddress(), deployerWallet.getAddress()],
salt,
deployer: deployerWallet.getAddress(),
});
const deployer = new ContractDeployer(TokenContractArtifact, deployerWallet, undefined, 'constructor_with_minter');
const tx = deployer
.deploy('PrivateToken', 'PT', 18, deployerWallet.getAddress(), deployerWallet.getAddress())
.send({
contractAddressSalt: salt,
});
const receipt = await tx.getReceipt();
expect(receipt).toEqual(
expect.objectContaining({
status: TxStatus.PENDING,
error: '',
}),
);
const receiptAfterMined = await tx.wait({ wallet: deployerWallet });
const contractMetadata = await pxe.getContractMetadata(deploymentData.address);
expect(contractMetadata).toBeDefined();
expect(contractMetadata.isContractPubliclyDeployed).toBeTruthy();
expect(receiptAfterMined).toEqual(
expect.objectContaining({
status: TxStatus.SUCCESS,
}),
);
expect(receiptAfterMined.contract.instance.address).toEqual(deploymentData.address);
}, 300_000);
it('deploys the contract with initial supply', async () => {
const salt = Fr.random();
const deployerWallet = alice; // using first account as deployer
const deploymentData = await getContractInstanceFromDeployParams(TokenContractArtifact, {
constructorArtifact: 'constructor_with_initial_supply',
constructorArgs: ['PrivateToken', 'PT', 18, 1, deployerWallet.getAddress(), deployerWallet.getAddress()],
salt,
deployer: deployerWallet.getAddress(),
});
const deployer = new ContractDeployer(
TokenContractArtifact,
deployerWallet,
undefined,
'constructor_with_initial_supply',
);
const tx = deployer
.deploy('PrivateToken', 'PT', 18, 1, deployerWallet.getAddress(), deployerWallet.getAddress())
.send({ contractAddressSalt: salt });
const receipt = await tx.getReceipt();
expect(receipt).toEqual(
expect.objectContaining({
status: TxStatus.PENDING,
error: '',
}),
);
const receiptAfterMined = await tx.wait({ wallet: deployerWallet });
const contractMetadata = await pxe.getContractMetadata(deploymentData.address);
expect(contractMetadata).toBeDefined();
expect(contractMetadata.isContractPubliclyDeployed).toBeTruthy();
expect(receiptAfterMined).toEqual(
expect.objectContaining({
status: TxStatus.SUCCESS,
}),
);
expect(receiptAfterMined.contract.instance.address).toEqual(deploymentData.address);
}, 300_000);
it('mints', async () => {
await token.withWallet(alice);
const tx = await token.methods.mint_to_public(bob.getAddress(), AMOUNT).send().wait();
const balance = await token.methods.balance_of_public(bob.getAddress()).simulate();
expect(balance).toBe(AMOUNT);
}, 300_000);
it('transfers tokens between public accounts', async () => {
// First mint 2 tokens to alice
await token
.withWallet(alice)
.methods.mint_to_public(alice.getAddress(), AMOUNT * 2n)
.send()
.wait();
// Transfer 1 token from alice to bob
await token
.withWallet(alice)
.methods.transfer_public_to_public(alice.getAddress(), bob.getAddress(), AMOUNT, 0)
.send()
.wait();
// Check balances are correct
const aliceBalance = await token.methods.balance_of_public(alice.getAddress()).simulate();
const bobBalance = await token.methods.balance_of_public(bob.getAddress()).simulate();
expect(aliceBalance).toBe(AMOUNT);
expect(bobBalance).toBe(AMOUNT);
}, 300_000);
// TODO(#29): burn was nuked because of this PR, re-enable it
// it('burns public tokens', async () => {
// // First mint 2 tokens to alice
// await token
// .withWallet(alice)
// .methods.mint_to_public(alice.getAddress(), AMOUNT * 2n)
// .send()
// .wait();
// // Burn 1 token from alice
// await token.withWallet(alice).methods.burn_public(alice.getAddress(), AMOUNT, 0).send().wait();
// // Check balance and total supply are reduced
// const aliceBalance = await token.methods.balance_of_public(alice.getAddress()).simulate();
// const totalSupply = await token.methods.total_supply().simulate();
// expect(aliceBalance).toBe(AMOUNT);
// expect(totalSupply).toBe(AMOUNT);
// }, 300_000);
it('transfers tokens from private to public balance', async () => {
// First mint to private 2 tokens to alice
await token
.withWallet(alice)
.methods.mint_to_private(alice.getAddress(), alice.getAddress(), AMOUNT * 2n)
.send()
.wait();
// Transfer 1 token from alice's private balance to public balance
await token
.withWallet(alice)
.methods.transfer_private_to_public(alice.getAddress(), alice.getAddress(), AMOUNT, 0)
.send()
.wait();
// Check public balance is correct
const alicePublicBalance = await token.methods.balance_of_public(alice.getAddress()).simulate();
expect(alicePublicBalance).toBe(AMOUNT);
// Check total supply hasn't changed
const totalSupply = await token.methods.total_supply().simulate();
expect(totalSupply).toBe(AMOUNT * 2n);
}, 300_000);
it.skip('fails when transferring more tokens than available in private balance', async () => {
// Mint 1 token privately to alice
await token.withWallet(alice).methods.mint_to_private(alice.getAddress(), alice.getAddress(), AMOUNT).send().wait();
// Try to transfer more tokens than available from private to public balance
// TODO(#29): fix "Invalid arguments size: expected 3, got 2" error handling
// await expect(
// token
// .withWallet(alice)
// .methods.transfer_private_to_public(alice.getAddress(), alice.getAddress(), AMOUNT + 1n, 0)
// .send()
// .wait(),
// ).rejects.toThrow(/Balance too low/);
}, 300_000);
it('can transfer tokens between private balances', async () => {
// Mint 2 tokens privately to alice
await token
.withWallet(alice)
.methods.mint_to_private(alice.getAddress(), alice.getAddress(), AMOUNT * 2n)
.send()
.wait();
// Transfer 1 token from alice to bob's private balance
await token
.withWallet(alice)
.methods.transfer_private_to_private(alice.getAddress(), bob.getAddress(), AMOUNT, 0)
.send()
.wait();
// Try to transfer more than available balance
// TODO(#29): fix "Invalid arguments size: expected 3, got 2" error handling
// await expect(
// token
// .withWallet(alice)
// .methods.transfer_private_to_private(alice.getAddress(), bob.getAddress(), AMOUNT + 1n, 0)
// .send()
// .wait(),
// ).rejects.toThrow(/Balance too low/);
// Check total supply hasn't changed
const totalSupply = await token.methods.total_supply().simulate();
expect(totalSupply).toBe(AMOUNT * 2n);
}, 300_000);
it('can mint tokens to private balance', async () => {
// Mint 2 tokens privately to alice
await token
.withWallet(alice)
.methods.mint_to_private(alice.getAddress(), alice.getAddress(), AMOUNT * 2n)
.send()
.wait();
// Check total supply increased
const totalSupply = await token.methods.total_supply().simulate();
expect(totalSupply).toBe(AMOUNT * 2n);
// Public balance should be 0 since we minted privately
const alicePublicBalance = await token.methods.balance_of_public(alice.getAddress()).simulate();
expect(alicePublicBalance).toBe(0n);
}, 300_000);
it('can burn tokens from private balance', async () => {
// Mint 2 tokens privately to alice
await token
.withWallet(alice)
.methods.mint_to_private(alice.getAddress(), alice.getAddress(), AMOUNT * 2n)
.send()
.wait();
// Burn 1 token from alice's private balance
await token.withWallet(alice).methods.burn_private(alice.getAddress(), AMOUNT, 0).send().wait();
// Try to burn more than available balance
await expect(
token
.withWallet(alice)
.methods.burn_private(alice.getAddress(), AMOUNT * 2n, 0)
.send()
.wait(),
).rejects.toThrow(/Balance too low/);
// Check total supply decreased
const totalSupply = await token.methods.total_supply().simulate();
expect(totalSupply).toBe(AMOUNT);
// Public balance should still be 0
const alicePublicBalance = await token.methods.balance_of_public(alice.getAddress()).simulate();
expect(alicePublicBalance).toBe(0n);
}, 300_000);
it('can transfer tokens from public to private balance', async () => {
// Mint 2 tokens publicly to alice
await token
.withWallet(alice)
.methods.mint_to_public(alice.getAddress(), AMOUNT * 2n)
.send()
.wait();
// Transfer 1 token from alice's public balance to private balance
await token
.withWallet(alice)
.methods.transfer_public_to_private(alice.getAddress(), alice.getAddress(), AMOUNT, 0)
.send()
.wait();
// Try to transfer more than available public balance
// TODO(#29): fix "Invalid arguments size: expected 3, got 2" error handling
// await expect(
// token
// .withWallet(alice)
// .methods.transfer_public_to_private(alice.getAddress(), alice.getAddress(), AMOUNT * 2n, 0)
// .send()
// .wait(),
// ).rejects.toThrow(/attempt to subtract with underflow/);
// Check total supply stayed the same
const totalSupply = await token.methods.total_supply().simulate();
expect(totalSupply).toBe(AMOUNT * 2n);
// Public balance should be reduced by transferred amount
const alicePublicBalance = await token.methods.balance_of_public(alice.getAddress()).simulate();
expect(alicePublicBalance).toBe(AMOUNT);
}, 300_000);
it.skip('mint in public, prepare partial note and finalize it', async () => {
await token.withWallet(alice);
await token.methods.mint_to_public(alice.getAddress(), AMOUNT).send().wait();
// alice has tokens in public
expect(await token.methods.balance_of_public(alice.getAddress()).simulate()).toBe(AMOUNT);
expect(await token.methods.balance_of_private(alice.getAddress()).simulate()).toBe(0n);
// bob has 0 tokens
expect(await token.methods.balance_of_private(bob.getAddress()).simulate()).toBe(0n);
expect(await token.methods.balance_of_private(bob.getAddress()).simulate()).toBe(0n);
expect(await token.methods.total_supply().simulate()).toBe(AMOUNT);
// alice prepares partial note for bob
await token.methods
.initialize_transfer_commitment(bob.getAddress(), alice.getAddress(), bob.getAddress())
.send()
.wait();
// alice still has tokens in public
expect(await token.methods.balance_of_public(alice.getAddress()).simulate()).toBe(AMOUNT);
// finalize partial note passing the commitment slot
// await token.methods.transfer_public_to_commitment(AMOUNT, latestEvent.hiding_point_slot).send().wait();
// alice now has no tokens
// expect(await token.methods.balance_of_public(alice.getAddress()).simulate()).toBe(0n);
// // bob has tokens in private
// expect(await token.methods.balance_of_public(bob.getAddress()).simulate()).toBe(0n);
// expect(await token.methods.balance_of_private(bob.getAddress()).simulate()).toBe(AMOUNT);
// // total supply is still the same
// expect(await token.methods.total_supply().simulate()).toBe(AMOUNT);
}, 300_000);
// TODO: Can't figure out why this is failing
// Assertion failed: unauthorized 'true, authorized'
it.skip('public transfer with authwitness', async () => {
// Mint tokens to Alice in public
await token.withWallet(alice).methods.mint_to_public(alice.getAddress(), AMOUNT).send().wait();
// build transfer public to public call
const nonce = Fr.random();
const action = token
.withWallet(carl)
.methods.transfer_public_to_public(alice.getAddress(), bob.getAddress(), AMOUNT, nonce);
// define intent
const intent: IntentAction = {
caller: carl.getAddress(),
action,
};
// alice creates authwitness
const authWitness = await alice.createAuthWit(intent);
// alice authorizes the public authwit
await (await alice.setPublicAuthWit(intent, true)).send().wait();
// check validity of alice's authwit
const validity = await carl.lookupValidity(alice.getAddress(), intent, authWitness);
expect(validity.isValidInPrivate).toBeTruthy();
expect(validity.isValidInPublic).toBeTruthy();
// Carl submits the action, using alice's authwit
await action.send({ authWitnesses: [authWitness] }).wait();
// Check balances, alice to should 0
expect(await token.methods.balance_of_public(alice.getAddress()).simulate()).toBe(0n);
// Bob should have the a non-zero amount
expect(await token.methods.balance_of_public(bob.getAddress()).simulate()).toBe(AMOUNT);
}, 300_000);
it('private transfer with authwitness', async () => {
// setup balances
await token.withWallet(alice).methods.mint_to_public(alice.getAddress(), AMOUNT).send().wait();
await token
.withWallet(alice)
.methods.transfer_public_to_private(alice.getAddress(), alice.getAddress(), AMOUNT, 0)
.send()
.wait();
expect(await token.methods.balance_of_private(alice.getAddress()).simulate()).toBe(AMOUNT);
// prepare action
const nonce = Fr.random();
const action = token
.withWallet(carl)
.methods.transfer_private_to_private(alice.getAddress(), bob.getAddress(), AMOUNT, nonce);
const intent: IntentAction = {
caller: carl.getAddress(),
action,
};
const witness = await alice.createAuthWit(intent);
const validity = await alice.lookupValidity(alice.getAddress(), intent, witness);
expect(validity.isValidInPrivate).toBeTruthy();
expect(validity.isValidInPublic).toBeFalsy();
await action.send({ authWitnesses: [witness] }).wait();
expect(await token.methods.balance_of_private(alice.getAddress()).simulate()).toBe(0n);
expect(await token.methods.balance_of_private(bob.getAddress()).simulate()).toBe(AMOUNT);
}, 300_000);
});
describe.skip('Token - Multi PXE', () => {
let pxe: PXE;
let store: AztecLmdbStore;
let wallets: AccountWalletWithSecretKey[];
let deployer: AccountWalletWithSecretKey;
let alice: AccountWalletWithSecretKey;
let bob: AccountWalletWithSecretKey;
let carl: AccountWalletWithSecretKey;
let token: TokenContract;
let alicePXE: PXE;
let bobPXE: PXE;
beforeAll(async () => {
({ pxe, deployer, wallets, store } = await setupTestSuite());
[alice, bob, carl] = wallets;
// TODO: use different PXE instances.
alicePXE = pxe;
bobPXE = pxe;
});
afterAll(async () => {
await store.delete();
});
beforeEach(async () => {
token = (await deployTokenWithMinter(alice)) as TokenContract;
await bobPXE.registerContract(token);
// alice knows bob
// TODO: review this, alice shouldn't need to register bob's **secrets**!
await alicePXE.registerAccount(bob.getSecretKey(), bob.getCompleteAddress().partialAddress);
await alicePXE.registerSender(bob.getAddress());
// bob knows alice
await bobPXE.registerAccount(alice.getSecretKey(), alice.getCompleteAddress().partialAddress);
await bobPXE.registerSender(alice.getAddress());
});
it('transfers', async () => {
let events, notes;
// mint initial amount to alice
await token.withWallet(alice).methods.mint_to_public(alice.getAddress(), wad(10)).send().wait();
// self-transfer 5 public tokens to private
const aliceShieldTx = await token
.withWallet(alice)
.methods.transfer_public_to_private(alice.getAddress(), alice.getAddress(), wad(5), 0)
.send()
.wait();
await token.withWallet(alice).methods.sync_private_state().simulate({});
// assert balances
await expectTokenBalances(expect, token, alice.getAddress(), wad(5), wad(5));
// retrieve notes from last tx
notes = await alicePXE.getNotes({ txHash: aliceShieldTx.txHash });
expect(notes.length).toBe(1);
expectUintNote(expect, notes[0], wad(5), alice.getAddress());
// transfer some private tokens to bob
const fundBobTx = await token
.withWallet(alice)
.methods.transfer_public_to_private(alice.getAddress(), bob.getAddress(), wad(5), 0)
.send()
.wait();
await token.withWallet(alice).methods.sync_private_state().simulate({});
await token.withWallet(bob).methods.sync_private_state().simulate({});
notes = await alicePXE.getNotes({ txHash: fundBobTx.txHash });
expect(notes.length).toBe(1);
expectUintNote(expect, notes[0], wad(5), bob.getAddress());
// TODO: Bob is not receiving notes
// notes = await bob.getNotes({ txHash: fundBobTx.txHash });
// expect(notes.length).toBe(1);
// expectUintNote(expect, notes[0], wad(5), bob.getAddress());
// fund bob again
const fundBobTx2 = await token
.withWallet(alice)
.methods.transfer_private_to_private(alice.getAddress(), bob.getAddress(), wad(5), 0)
.send()
.wait();
await token.withWallet(alice).methods.sync_private_state().simulate({});
await token.withWallet(bob).methods.sync_private_state().simulate({});
// assert balances
await expectTokenBalances(expect, token, alice.getAddress(), wad(0), wad(0));
await expectTokenBalances(expect, token, bob.getAddress(), wad(0), wad(10));
// Alice shouldn't have any notes because it not a sender/registered account in her PXE
// (but she has because I gave her access to Bob's notes)
notes = await alicePXE.getNotes({ txHash: fundBobTx2.txHash });
expect(notes.length).toBe(1);
expectUintNote(expect, notes[0], wad(5), bob.getAddress());
// TODO: Bob is not receiving notes
// Bob should have a note
// notes = await bob.getNotes({txHash: fundBobTx2.txHash});
// expect(notes.length).toBe(1);
// expectUintNote(expect, notes[0], wad(5), bob.getAddress());
// assert alice's balances again
await expectTokenBalances(expect, token, alice.getAddress(), wad(0), wad(0));
// assert bob's balances
await expectTokenBalances(expect, token, bob.getAddress(), wad(0), wad(10));
}, 300_000);
});