forked from QuarryProtocol/quarry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mintWrapper.spec.ts
290 lines (255 loc) · 8.83 KB
/
mintWrapper.spec.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
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
import * as anchor from "@project-serum/anchor";
import { assertTXSuccess, expectTX } from "@saberhq/chai-solana";
import type { Provider } from "@saberhq/solana-contrib";
import { PendingTransaction } from "@saberhq/solana-contrib";
import {
createInitMintInstructions,
getMintInfo,
Token,
TokenAmount,
u64,
} from "@saberhq/token-utils";
import type { PublicKey } from "@solana/web3.js";
import { Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
import * as assert from "assert";
import { expect } from "chai";
import type { MintWrapper, MintWrapperProgram, QuarrySDK } from "../src";
import { findMinterAddress } from "../src";
import { DEFAULT_DECIMALS, DEFAULT_HARD_CAP } from "./utils";
import { makeSDK } from "./workspace";
describe("MintWrapper", () => {
const { BN, web3 } = anchor;
let sdk: QuarrySDK;
let provider: Provider;
let mintWrapper: MintWrapper;
let MintWrapper: MintWrapperProgram;
before("Initialize SDK", () => {
sdk = makeSDK();
provider = sdk.provider;
mintWrapper = sdk.mintWrapper;
MintWrapper = sdk.programs.MintWrapper;
});
let rewardsMint: PublicKey;
let token: Token;
let mintWrapperKey: PublicKey;
let hardCap: TokenAmount;
beforeEach("Initialize mint", async () => {
const rewardsMintKP = Keypair.generate();
rewardsMint = rewardsMintKP.publicKey;
token = Token.fromMint(rewardsMint, DEFAULT_DECIMALS);
hardCap = TokenAmount.parse(token, DEFAULT_HARD_CAP.toString());
const { tx, mintWrapper: wrapperKey } = await mintWrapper.newWrapper({
hardcap: hardCap.toU64(),
tokenMint: rewardsMint,
});
await expectTX(
await createInitMintInstructions({
provider,
mintKP: rewardsMintKP,
decimals: DEFAULT_DECIMALS,
mintAuthority: wrapperKey,
freezeAuthority: wrapperKey,
})
).to.be.fulfilled;
mintWrapperKey = wrapperKey;
await expectTX(tx, "Initialize mint").to.be.fulfilled;
});
it("init mint wrapper v1", async () => {
const { tx } = await mintWrapper.newWrapperAndMintV1({
hardcap: hardCap.toU64(),
});
await assertTXSuccess(tx, "init mint wrapper v1");
});
it("Check MintWrapper", async () => {
const mintInfo = await getMintInfo(provider, rewardsMint);
assert.ok(mintInfo.mintAuthority?.equals(mintWrapperKey));
const mintWrapperState =
await mintWrapper.program.account.mintWrapper.fetch(mintWrapperKey);
expect(mintWrapperState.hardCap).to.bignumber.eq(hardCap.toU64());
expect(mintWrapperState.admin).to.eqAddress(provider.wallet.publicKey);
expect(mintWrapperState.tokenMint).to.eqAddress(rewardsMint);
});
describe("MintWrapper", () => {
it("Transfer admin authority and accept admin authority", async () => {
const newAuthority = web3.Keypair.generate();
await assert.doesNotReject(async () => {
await MintWrapper.rpc.transferAdmin({
accounts: {
mintWrapper: mintWrapperKey,
admin: provider.wallet.publicKey,
nextAdmin: newAuthority.publicKey,
},
});
});
let mintWrapperState =
await mintWrapper.program.account.mintWrapper.fetch(mintWrapperKey);
expect(mintWrapperState.admin).to.eqAddress(provider.wallet.publicKey);
expect(mintWrapperState.pendingAdmin).to.eqAddress(
newAuthority.publicKey
);
const ix = mintWrapper.program.instruction.acceptAdmin({
accounts: {
mintWrapper: mintWrapperKey,
pendingAdmin: newAuthority.publicKey,
},
});
let tx = sdk.newTx([ix], [newAuthority]);
await expectTX(tx, "transfer authority").to.be.fulfilled;
mintWrapperState = await mintWrapper.program.account.mintWrapper.fetch(
mintWrapperKey
);
expect(mintWrapperState.admin).to.eqAddress(newAuthority.publicKey);
expect(mintWrapperState.pendingAdmin).to.eqAddress(
web3.PublicKey.default.toString()
);
// Transfer back
const instructions = [];
instructions.push(
mintWrapper.program.instruction.transferAdmin({
accounts: {
mintWrapper: mintWrapperKey,
admin: newAuthority.publicKey,
nextAdmin: provider.wallet.publicKey,
},
})
);
instructions.push(
mintWrapper.program.instruction.acceptAdmin({
accounts: {
mintWrapper: mintWrapperKey,
pendingAdmin: provider.wallet.publicKey,
},
})
);
tx = sdk.newTx(instructions, [newAuthority]);
await expectTX(tx, "transfer authority back to original authority").to.be
.fulfilled;
mintWrapperState = await mintWrapper.program.account.mintWrapper.fetch(
mintWrapperKey
);
expect(mintWrapperState.admin).to.eqAddress(provider.wallet.publicKey);
expect(mintWrapperState.pendingAdmin).to.eqAddress(
web3.PublicKey.default
);
});
it("new minter v1", async () => {
const id = Keypair.generate().publicKey;
await expectTX(
mintWrapper.newMinterV1(mintWrapperKey, id),
"new minter v1"
).to.be.fulfilled;
});
it("Adds a Minter", async () => {
const allowance = new u64(1_000_000);
const id = Keypair.generate().publicKey;
expect(
(await mintWrapper.fetchMintWrapper(mintWrapperKey))?.numMinters,
"initial num minters"
).to.bignumber.eq(new BN(0));
await expectTX(
mintWrapper.newMinterWithAllowance(mintWrapperKey, id, allowance),
"add minter"
).to.be.fulfilled;
expect(
(await mintWrapper.fetchMinter(mintWrapperKey, id))?.allowance,
"allowance"
).to.bignumber.eq(allowance);
expect(
(await mintWrapper.fetchMintWrapper(mintWrapperKey))?.numMinters,
"final num minters"
).to.bignumber.eq(new BN(1));
});
it("Removes a Minter", async () => {
const allowance = new u64(1_000_000);
const id = Keypair.generate().publicKey;
await expectTX(
mintWrapper.newMinterWithAllowance(mintWrapperKey, id, allowance),
"add minter"
).to.be.fulfilled;
expect(
(await mintWrapper.fetchMinter(mintWrapperKey, id))?.allowance,
"allowance"
).to.bignumber.eq(allowance);
await expectTX(
mintWrapper.minterUpdate(mintWrapperKey, id, new u64(0)),
"remove minter"
).to.be.fulfilled;
expect(
(await mintWrapper.fetchMinter(mintWrapperKey, id))?.allowance,
"no more allowance"
).to.bignumber.zero;
});
it("Cannot mint past allowance", async () => {
const allowance = new u64(1_000_000);
const kp = Keypair.generate();
await expectTX(
new PendingTransaction(
provider.connection,
await provider.connection.requestAirdrop(
kp.publicKey,
LAMPORTS_PER_SOL
)
)
).to.be.fulfilled;
const id = kp.publicKey;
await expectTX(
mintWrapper.newMinterWithAllowance(mintWrapperKey, id, allowance),
"add minter"
).to.be.fulfilled;
expect(
(await mintWrapper.fetchMinter(mintWrapperKey, id))?.allowance,
"allowance"
).to.bignumber.eq(allowance);
const amount = new TokenAmount(token, new u64(1_000));
const minterSDK = sdk.withSigner(kp);
const [minterAddress] = await findMinterAddress(mintWrapperKey, id);
const minterData =
await minterSDK.programs.MintWrapper.account.minter.fetch(
minterAddress
);
const minterRaw = await provider.connection.getAccountInfo(minterAddress);
assert.ok(minterRaw);
await expectTX(
minterSDK.mintWrapper.performMint({
amount,
minter: {
accountId: minterAddress,
accountInfo: {
...minterRaw,
data: minterData,
},
},
}),
"mint"
).to.be.fulfilled;
const minterData2 = await mintWrapper.fetchMinter(mintWrapperKey, id);
const mwData2 = await mintWrapper.fetchMintWrapper(mintWrapperKey);
assert.ok(minterData2 && mwData2);
expect(minterData2.allowance, "allowance").to.bignumber.eq(
new BN(999_000)
);
expect(minterData2.totalMinted, "minter total minted").to.bignumber.eq(
new BN(1_000)
);
expect(mwData2.totalAllowance, "total allowance").to.bignumber.eq(
new BN(999_000)
);
expect(mwData2.totalMinted, "total minted").to.bignumber.eq(
new BN(1_000)
);
await expectTX(
minterSDK.mintWrapper.performMint({
amount: new TokenAmount(amount.token, allowance),
minter: {
accountId: minterAddress,
accountInfo: {
...minterRaw,
data: minterData,
},
},
}),
"mint"
).to.be.rejected;
});
});
});