-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathminting.test.ts
147 lines (123 loc) · 5.46 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
139
140
141
142
143
144
145
146
147
import { Fr, type TxHash, computeSecretHash } from '@aztec/aztec.js';
import { BITSIZE_TOO_BIG_ERROR, U128_OVERFLOW_ERROR } from '../fixtures/index.js';
import { BlacklistTokenContractTest } from './blacklist_token_contract_test.js';
describe('e2e_blacklist_token_contract mint', () => {
const t = new BlacklistTokenContractTest('mint');
let { asset, tokenSim, wallets, blacklisted } = t;
beforeAll(async () => {
await t.applyBaseSnapshots();
// Beware that we are adding the admin as minter here, which is very slow because it needs multiple blocks.
await t.applyMintSnapshot();
await t.setup();
// Have to destructure again to ensure we have latest refs.
({ asset, tokenSim, wallets, blacklisted } = t);
}, 600_000);
afterAll(async () => {
await t.teardown();
});
beforeEach(async () => {
await t.tokenSim.check();
});
afterEach(async () => {
await t.tokenSim.check();
});
describe('Public', () => {
it('as minter', async () => {
const amount = 10000n;
tokenSim.mintPublic(wallets[0].getAddress(), amount);
await asset.methods.mint_public(wallets[0].getAddress(), amount).send().wait();
});
describe('failure cases', () => {
it('as non-minter', async () => {
const amount = 10000n;
await expect(
asset.withWallet(wallets[1]).methods.mint_public(wallets[0].getAddress(), amount).prove(),
).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(wallets[0].getAddress(), amount).prove()).rejects.toThrow(
BITSIZE_TOO_BIG_ERROR,
);
});
it('mint <u128 but recipient balance >u128', async () => {
const amount = 2n ** 128n - tokenSim.balanceOfPublic(wallets[0].getAddress());
await expect(asset.methods.mint_public(wallets[0].getAddress(), amount).prove()).rejects.toThrow(
U128_OVERFLOW_ERROR,
);
});
it('mint <u128 but such that total supply >u128', async () => {
const amount = 2n ** 128n - tokenSim.balanceOfPublic(wallets[0].getAddress());
await expect(asset.methods.mint_public(wallets[1].getAddress(), amount).prove()).rejects.toThrow(
U128_OVERFLOW_ERROR,
);
});
it('mint to blacklisted entity', async () => {
await expect(
asset.withWallet(wallets[1]).methods.mint_public(blacklisted.getAddress(), 1n).prove(),
).rejects.toThrow(/Assertion failed: Blacklisted: Recipient/);
});
});
});
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 receiptClaim = await asset.methods
.redeem_shield(wallets[0].getAddress(), amount, secret)
.send()
.wait({ debug: true });
tokenSim.redeemShield(wallets[0].getAddress(), 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(wallets[0].getAddress(), amount, secret).prove()).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).prove()).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_private(amount, secretHash).prove()).rejects.toThrow(BITSIZE_TOO_BIG_ERROR);
});
it('mint <u128 but recipient balance >u128', async () => {
const amount = 2n ** 128n - tokenSim.balanceOfPrivate(wallets[0].getAddress());
expect(amount).toBeLessThan(2n ** 128n);
await expect(asset.methods.mint_private(amount, secretHash).prove()).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).prove()).rejects.toThrow(U128_OVERFLOW_ERROR);
});
it('mint and try to redeem at blacklist', async () => {
await expect(asset.methods.redeem_shield(blacklisted.getAddress(), amount, secret).prove()).rejects.toThrow(
/Assertion failed: Blacklisted: Recipient .*/,
);
});
});
});
});