-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathminting.test.ts
138 lines (117 loc) · 5.3 KB
/
minting.test.ts
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
import { Fr, type TxHash, computeSecretHash } from '@aztec/aztec.js';
import { BITSIZE_TOO_BIG_ERROR, U128_OVERFLOW_ERROR } from '../fixtures/fixtures.js';
import { TokenContractTest } from './token_contract_test.js';
describe('e2e_token_contract minting', () => {
const t = new TokenContractTest('minting');
let { asset, accounts, tokenSim, wallets } = t;
beforeAll(async () => {
await t.applyBaseSnapshots();
await t.setup();
({ asset, accounts, tokenSim, wallets } = t);
});
afterAll(async () => {
await t.teardown();
});
afterEach(async () => {
await t.tokenSim.check();
});
describe('Public', () => {
it('as minter', async () => {
const amount = 10000n;
await asset.methods.mint_public(accounts[0].address, amount).send().wait();
tokenSim.mintPublic(accounts[0].address, amount);
expect(await asset.methods.balance_of_public(accounts[0].address).simulate()).toEqual(
tokenSim.balanceOfPublic(accounts[0].address),
);
expect(await asset.methods.total_supply().simulate()).toEqual(tokenSim.totalSupply);
});
describe('failure cases', () => {
it('as non-minter', async () => {
const amount = 10000n;
await expect(
asset.withWallet(wallets[1]).methods.mint_public(accounts[0].address, amount).simulate(),
).rejects.toThrow('Assertion failed: caller is not minter');
});
it('mint >u128 tokens to overflow', async () => {
const amount = 2n ** 128n; // U128::max() + 1;
await expect(asset.methods.mint_public(accounts[0].address, amount).simulate()).rejects.toThrow(
BITSIZE_TOO_BIG_ERROR,
);
});
it('mint <u128 but recipient balance >u128', async () => {
const amount = 2n ** 128n - tokenSim.balanceOfPublic(accounts[0].address);
await expect(asset.methods.mint_public(accounts[0].address, amount).simulate()).rejects.toThrow(
U128_OVERFLOW_ERROR,
);
});
it('mint <u128 but such that total supply >u128', async () => {
const amount = 2n ** 128n - tokenSim.balanceOfPublic(accounts[0].address);
await expect(asset.methods.mint_public(accounts[1].address, amount).simulate()).rejects.toThrow(
U128_OVERFLOW_ERROR,
);
});
});
});
describe('Private', () => {
const secret = Fr.random();
const amount = 10000n;
let secretHash: Fr;
let txHash: TxHash;
beforeAll(() => {
secretHash = computeSecretHash(secret);
});
describe('Mint flow', () => {
it('mint_private as minter', async () => {
const receipt = await asset.methods.mint_private(amount, secretHash).send().wait();
tokenSim.mintPrivate(amount);
txHash = receipt.txHash;
});
it('redeem as recipient', async () => {
await t.addPendingShieldNoteToPXE(0, amount, secretHash, txHash);
const txClaim = asset.methods.redeem_shield(accounts[0].address, amount, secret).send();
// docs:start:debug
const receiptClaim = await txClaim.wait({ debug: true });
// docs:end:debug
tokenSim.redeemShield(accounts[0].address, amount);
// 1 note should be created containing `amount` of tokens
const { visibleIncomingNotes } = receiptClaim.debugInfo!;
expect(visibleIncomingNotes.length).toBe(1);
expect(visibleIncomingNotes[0].note.items[0].toBigInt()).toBe(amount);
});
});
describe('failure cases', () => {
it('try to redeem as recipient (double-spend) [REVERTS]', async () => {
await expect(t.addPendingShieldNoteToPXE(0, amount, secretHash, txHash)).rejects.toThrow(
'The note has been destroyed.',
);
await expect(asset.methods.redeem_shield(accounts[0].address, amount, secret).simulate()).rejects.toThrow(
`Assertion failed: Attempted to read past end of BoundedVec`,
);
});
it('mint_private as non-minter', async () => {
await expect(asset.withWallet(wallets[1]).methods.mint_private(amount, secretHash).simulate()).rejects.toThrow(
'Assertion failed: caller is not minter',
);
});
it('mint_private as non-minter, bypassing account entrypoint', async () => {
const request = await asset.withWallet(wallets[1]).methods.mint_private(amount, secretHash).create();
await expect(wallets[1].simulateTx(request, true, accounts[0].address)).rejects.toThrow(
'Assertion failed: Users cannot set msg_sender in first call',
);
});
it('mint >u128 tokens to overflow', async () => {
const amount = 2n ** 128n; // U128::max() + 1;
await expect(asset.methods.mint_private(amount, secretHash).simulate()).rejects.toThrow(BITSIZE_TOO_BIG_ERROR);
});
it('mint <u128 but recipient balance >u128', async () => {
const amount = 2n ** 128n - tokenSim.balanceOfPrivate(accounts[0].address);
expect(amount).toBeLessThan(2n ** 128n);
await expect(asset.methods.mint_private(amount, secretHash).simulate()).rejects.toThrow(U128_OVERFLOW_ERROR);
});
it('mint <u128 but such that total supply >u128', async () => {
const amount = 2n ** 128n - tokenSim.totalSupply;
await expect(asset.methods.mint_private(amount, secretHash).simulate()).rejects.toThrow(U128_OVERFLOW_ERROR);
});
});
});
});