-
Notifications
You must be signed in to change notification settings - Fork 21
/
wen_transfer_guard.test.ts
467 lines (425 loc) · 13.1 KB
/
wen_transfer_guard.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
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
import * as anchor from "@coral-xyz/anchor";
import { BankrunProvider } from "anchor-bankrun";
import { Program, web3 } from "@coral-xyz/anchor";
import { WenTransferGuard } from "../target/types/wen_transfer_guard";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
ExtensionType,
TOKEN_2022_PROGRAM_ID,
createAssociatedTokenAccountInstruction,
createInitializeMintInstruction,
createInitializeTransferHookInstruction,
createMintToInstruction,
createTransferCheckedWithTransferHookInstruction,
getAssociatedTokenAddressSync,
getExtraAccountMetaAddress,
getMintLen,
} from "@solana/spl-token";
import { ProgramTestContext, startAnchor } from "solana-bankrun";
import { expect } from "chai";
const sendSignedVtx = async (
provider: BankrunProvider,
payer: web3.PublicKey,
signers: web3.Signer[],
...ixs: web3.TransactionInstruction[]
) =>
provider.sendAndConfirm(
new web3.VersionedTransaction(
new web3.TransactionMessage({
payerKey: payer,
instructions: ixs,
recentBlockhash: provider.context.lastBlockhash,
}).compileToV0Message(),
),
signers,
);
const kMint = {
keypair: web3.Keypair.generate(),
decimals: 9,
mintAmount: 1e9,
mintAuthority: web3.Keypair.generate(),
transferHookAuthority: web3.Keypair.generate(),
};
const kSourceAuthority = web3.Keypair.generate();
const kDestinationAuthority = web3.Keypair.generate();
const createMint = async (
programId: web3.PublicKey,
payer: web3.Signer,
provider: BankrunProvider,
mint = kMint,
sourceAuthority = kSourceAuthority,
destinationAuthority = kDestinationAuthority,
) => {
const extensions = [ExtensionType.TransferHook];
const mintLen = getMintLen(extensions);
const lamports = (
await provider.context.banksClient.getRent()
).minimumBalance(BigInt(mintLen));
const sourceAta = getAssociatedTokenAddressSync(
mint.keypair.publicKey,
sourceAuthority.publicKey,
false,
TOKEN_2022_PROGRAM_ID,
);
const destinationAta = getAssociatedTokenAddressSync(
mint.keypair.publicKey,
destinationAuthority.publicKey,
false,
TOKEN_2022_PROGRAM_ID,
);
const ixs = [
// TX: Allocate mint account
web3.SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: mint.keypair.publicKey,
space: mintLen,
lamports: Number(lamports),
programId: TOKEN_2022_PROGRAM_ID,
}),
// TX: Init transfer hook on mint
createInitializeTransferHookInstruction(
mint.keypair.publicKey,
mint.transferHookAuthority.publicKey,
programId,
TOKEN_2022_PROGRAM_ID,
),
// TX: Init mint
createInitializeMintInstruction(
mint.keypair.publicKey,
mint.decimals,
mint.mintAuthority.publicKey,
mint.mintAuthority.publicKey,
TOKEN_2022_PROGRAM_ID,
),
// TX: Create Source ATA
createAssociatedTokenAccountInstruction(
payer.publicKey,
sourceAta,
sourceAuthority.publicKey,
mint.keypair.publicKey,
TOKEN_2022_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
),
// TX: Create Destination ATA
createAssociatedTokenAccountInstruction(
payer.publicKey,
destinationAta,
destinationAuthority.publicKey,
mint.keypair.publicKey,
TOKEN_2022_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
),
// TX: Mint to Source ATA some tokens
createMintToInstruction(
mint.keypair.publicKey,
sourceAta,
mint.mintAuthority.publicKey,
mint.mintAmount,
undefined,
TOKEN_2022_PROGRAM_ID,
),
];
const txId = await sendSignedVtx(
provider,
payer.publicKey,
[payer, mint.keypair, mint.mintAuthority],
...ixs,
);
return {
sourceAta,
destinationAta,
txId,
};
};
describe("[wen_transfer_guard] - Solana Bankrun test suite", () => {
// Anchor + Bankrun Tooling
let context: ProgramTestContext;
let provider: BankrunProvider;
let program: anchor.Program<WenTransferGuard>;
let kSourceAta: web3.PublicKey | null = null;
let kDestinationAta: web3.PublicKey | null = null;
let kExtraMetasAddress: web3.PublicKey | null = null;
let kGuard: web3.PublicKey | null = null;
let kGuardOwner = web3.Keypair.generate();
let kGuardMint = web3.Keypair.generate();
let kGuardMetadata = {
name: "Test Guard",
symbol: "TG",
uri: "https://bafkreiewmldtian6gxfyn354xlpuvj5zkcpvwasjzexirgdtpj44l32z44.ipfs.nftstorage.link/",
};
let kGuardOriginalCpiRule: { deny: { 0: anchor.web3.PublicKey[] } };
let kGuardUpdatedCpiRule: { deny: { 0: anchor.web3.PublicKey[] } };
const kGuardDenyNonCpiTransfersRule = {
deny: { 0: [TOKEN_2022_PROGRAM_ID] },
};
before(async () => {
context = await startAnchor(
"./",
[],
[
{
// Guard owner starts with 1 sol for gas.
address: kGuardOwner.publicKey,
info: {
lamports: 1e9,
data: Buffer.alloc(0),
executable: false,
owner: web3.SystemProgram.programId,
},
},
// Mint authority starts with 1 sol for gas.
{
address: kMint.mintAuthority.publicKey,
info: {
lamports: 1e9,
data: Buffer.alloc(0),
executable: false,
owner: web3.SystemProgram.programId,
},
},
],
);
provider = new BankrunProvider(context);
program = new Program<WenTransferGuard>(
require("../target/idl/wen_transfer_guard.json"),
provider,
);
const extraMetasAddress = getExtraAccountMetaAddress(
kMint.keypair.publicKey,
program.programId,
);
// Create a mint
const { sourceAta, destinationAta } = await createMint(
program.programId,
context.payer,
provider,
);
kSourceAta = sourceAta;
kDestinationAta = destinationAta;
kExtraMetasAddress = extraMetasAddress;
const [guardAddress] = web3.PublicKey.findProgramAddressSync(
[
Buffer.from("wen_token_transfer_guard"),
Buffer.from("guard_v1"),
kGuardMint.publicKey.toBuffer(),
],
program.programId,
);
kGuard = guardAddress;
kGuardOriginalCpiRule = {
deny: { 0: [program.programId, web3.PublicKey.default] },
};
kGuardUpdatedCpiRule = { deny: { 0: [program.programId] } };
});
it("[Transfer Guards] - Initializes a transfer guard.", async () => {
const ix = await program.methods
.createGuard({
...kGuardMetadata,
additionalFieldsRule: [],
transferAmountRule: null,
cpiRule: kGuardOriginalCpiRule,
})
.accounts({
mint: kGuardMint.publicKey,
guardAuthority: kGuardOwner.publicKey,
payer: context.payer.publicKey,
})
.instruction();
await sendSignedVtx(
provider,
context.payer.publicKey,
[context.payer, kGuardOwner, kGuardMint],
ix,
);
const guard = await program.account.guardV1.fetch(kGuard);
expect(guard.mint.toString()).to.be.eq(kGuardMint.publicKey.toString());
const denyRules = guard.cpiRule.deny[0];
expect(denyRules[0].toString()).to.be.eq(program.programId.toString());
expect(denyRules[1].toString()).to.be.eq(web3.PublicKey.default.toString());
});
it("[Transfer Guards] - Updates a transfer guard.", async () => {
const ix = await program.methods
.updateGuard({
additionalFieldsRule: [],
transferAmountRule: null,
cpiRule: kGuardUpdatedCpiRule,
})
.accounts({
mint: kGuardMint.publicKey,
guardAuthority: kGuardOwner.publicKey,
})
.instruction();
await sendSignedVtx(provider, context.payer.publicKey, [kGuardOwner], ix);
const guard = await program.account.guardV1.fetch(kGuard);
const denyRules = guard.cpiRule.deny[0];
expect(denyRules.length).to.be.eq(1);
expect(denyRules[0].toString()).to.be.eq(program.programId.toString());
});
it("[Transfer Hook] - Assigns guard to mint via init.", async () => {
const ix = await program.methods
.initialize()
.accountsStrict({
guard: kGuard,
mint: kMint.keypair.publicKey,
transferHookAuthority: kMint.transferHookAuthority.publicKey,
payer: context.payer.publicKey,
extraMetasAccount: kExtraMetasAddress,
systemProgram: web3.SystemProgram.programId,
})
.instruction();
await sendSignedVtx(
provider,
context.payer.publicKey,
[context.payer, kMint.transferHookAuthority],
ix,
);
});
it("[Transfer Hook] - Executes correctly during transfer.", async () => {
let ix = await createTransferCheckedWithTransferHookInstruction(
provider.connection,
kSourceAta,
kMint.keypair.publicKey,
kDestinationAta,
kSourceAuthority.publicKey,
BigInt(1e8),
kMint.decimals,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID,
);
await sendSignedVtx(
provider,
context.payer.publicKey,
[kSourceAuthority, context.payer],
ix,
);
});
it("[Transfer Guards] - Adds TOKEN_2022_PROGRAM_ID to deny list, non-cpi calls should fail.", async () => {
await sendSignedVtx(
provider,
context.payer.publicKey,
[kGuardOwner],
await program.methods
.updateGuard({
additionalFieldsRule: [],
transferAmountRule: null,
cpiRule: kGuardDenyNonCpiTransfersRule,
})
.accounts({
mint: kGuardMint.publicKey,
guardAuthority: kGuardOwner.publicKey,
})
.instruction(),
);
const guard = await program.account.guardV1.fetch(kGuard);
const denyRules = guard.cpiRule.deny[0];
expect(denyRules.length).to.be.eq(1);
expect(denyRules[0].toString()).to.be.eq(TOKEN_2022_PROGRAM_ID.toString());
let ix = await createTransferCheckedWithTransferHookInstruction(
provider.connection,
kSourceAta,
kMint.keypair.publicKey,
kDestinationAta,
kSourceAuthority.publicKey,
BigInt(2e8),
kMint.decimals,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID,
);
try {
await sendSignedVtx(
provider,
context.payer.publicKey,
[kSourceAuthority, context.payer],
ix,
);
} catch (error) {
expect(error.message).to.include("undefined");
}
});
it("[Transfer Guards] - Shouldn't initialize with a mint using a wrong transfer guard program id.", async () => {
const wrongProgramId = web3.Keypair.generate();
const wrongProgramMint = {
keypair: web3.Keypair.generate(),
decimals: 9,
mintAmount: 1e9,
mintAuthority: web3.Keypair.generate(),
transferHookAuthority: web3.Keypair.generate(),
};
const extraMetasAddress = getExtraAccountMetaAddress(
wrongProgramMint.keypair.publicKey,
program.programId, // Purposefully using right program id here to simulate wrong sdk usage.
);
await createMint(
wrongProgramId.publicKey,
context.payer,
provider,
wrongProgramMint,
);
const ix = await program.methods
.initialize()
.accountsStrict({
guard: kGuard, // Reuse the same guard.
mint: wrongProgramMint.keypair.publicKey,
transferHookAuthority: wrongProgramMint.transferHookAuthority.publicKey,
payer: context.payer.publicKey,
extraMetasAccount: extraMetasAddress,
systemProgram: web3.SystemProgram.programId,
})
.instruction();
try {
await sendSignedVtx(
provider,
context.payer.publicKey,
[context.payer, wrongProgramMint.transferHookAuthority],
ix,
);
} catch (error) {
console.log({ error });
expect(error.message).to.include("undefined");
}
});
it("[Transfer Guards] - Shouldn't initialize with a mint using a wrong transfer guard program id.", async () => {
const wrongProgramId = web3.Keypair.generate();
const wrongProgramMint = {
keypair: web3.Keypair.generate(),
decimals: 9,
mintAmount: 1e9,
mintAuthority: web3.Keypair.generate(),
transferHookAuthority: web3.Keypair.generate(),
};
const extraMetasAddress = getExtraAccountMetaAddress(
wrongProgramMint.keypair.publicKey,
program.programId, // Purposefully using right program id here to simulate wrong sdk usage.
);
await createMint(
wrongProgramId.publicKey,
context.payer,
provider,
wrongProgramMint,
);
const ix = await program.methods
.initialize()
.accountsStrict({
guard: kGuard, // Reuse the same guard.
mint: wrongProgramMint.keypair.publicKey,
transferHookAuthority: wrongProgramMint.transferHookAuthority.publicKey,
payer: context.payer.publicKey,
extraMetasAccount: extraMetasAddress,
systemProgram: web3.SystemProgram.programId,
})
.instruction();
try {
await sendSignedVtx(
provider,
context.payer.publicKey,
[context.payer, wrongProgramMint.transferHookAuthority],
ix,
);
} catch (error) {
expect(error.message).to.include("undefined");
}
});
// TODO: Add additional tests for the rest of the guard rules.
});