testsvm-quarry 0.2.1

Quarry protocol testing utilities for Solana SVM testing framework
Documentation
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
use crate::{TestRewarder, quarry_mint_wrapper, tests::common::init_test_environment};
use anyhow::Result;
use testsvm::prelude::*;

#[test]
fn test_create_minter_incorrect_authority() -> Result<()> {
    let mut env = init_test_environment()?;

    // Create authority and unauthorized keypairs
    let authority = env.new_wallet("authority")?;
    let unauthorized = env.new_wallet("unauthorized")?;

    // Create rewarder with authority
    let rewarder = TestRewarder::new_rewarder(&mut env, "main", &authority)?;

    // Create a second rewarder that will try to be added as a minter
    let other_rewarder = TestRewarder::new_rewarder(&mut env, "other", &unauthorized)?;

    // Test 1: Unauthorized user cannot create a minter
    let minter_pda: AccountRef<quarry_mint_wrapper::accounts::Minter> = env.get_pda(
        "unauthorized_minter",
        &[
            b"MintWrapperMinter",
            rewarder.mint_wrapper.mint_wrapper.key.as_ref(),
            other_rewarder.rewarder.key.as_ref(),
        ],
        quarry_mint_wrapper::ID,
    )?;

    let create_minter_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::NewMinterV2 {
            new_minter_v2_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
                admin: unauthorized.pubkey(), // Wrong authority
            },
            new_minter_authority: other_rewarder.rewarder.key,
            minter: minter_pda.key,
            payer: env.default_fee_payer(),
            system_program: solana_sdk::system_program::ID,
        },
        quarry_mint_wrapper::client::args::NewMinterV2 {},
    );

    // Should fail because unauthorized is not the admin of the mint wrapper
    env.execute_ixs_with_signers(&[create_minter_ix], &[&unauthorized])
        .fails()?
        .with_anchor_error("Unauthorized")?;

    // Test 2: Authority can create a minter
    let new_minter_authority = env.new_wallet("new_minter_authority")?;
    let authorized_minter_pda: AccountRef<quarry_mint_wrapper::accounts::Minter> = env.get_pda(
        "authorized_minter",
        &[
            b"MintWrapperMinter",
            rewarder.mint_wrapper.mint_wrapper.key.as_ref(),
            new_minter_authority.pubkey().as_ref(),
        ],
        quarry_mint_wrapper::ID,
    )?;

    let create_authorized_minter_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::NewMinterV2 {
            new_minter_v2_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
                admin: authority.pubkey(), // Correct authority
            },
            new_minter_authority: new_minter_authority.pubkey(),
            minter: authorized_minter_pda.key,
            payer: env.default_fee_payer(),
            system_program: solana_sdk::system_program::ID,
        },
        quarry_mint_wrapper::client::args::NewMinterV2 {},
    );

    env.execute_ixs_with_signers(&[create_authorized_minter_ix], &[&authority])
        .succeeds()?;

    Ok(())
}

#[test]
fn test_update_minter_allowance_incorrect_authority() -> Result<()> {
    let mut env = init_test_environment()?;

    // Create authority and unauthorized keypairs
    let authority = env.new_wallet("authority")?;
    let unauthorized = env.new_wallet("unauthorized")?;

    // Create a new minter authority for this test
    let new_minter_authority = env.new_wallet("new_minter_authority")?;

    // Create rewarder with authority, but use a custom minter with lower initial allowance
    let mint_wrapper_base = env.new_wallet("mint_wrapper_base")?;
    let _rewarder_base = env.new_wallet("rewarder_base")?;

    // Calculate mint wrapper PDA
    let mint_wrapper: AccountRef<quarry_mint_wrapper::accounts::MintWrapper> = env.get_pda(
        "mint_wrapper",
        &[b"MintWrapper", mint_wrapper_base.pubkey().as_ref()],
        quarry_mint_wrapper::ID,
    )?;

    // Create reward token mint with mint wrapper as authority
    let reward_token_mint = env.create_mint("reward_token", 6, &mint_wrapper.key)?;

    let create_wrapper_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::NewWrapperV2 {
            base: mint_wrapper_base.pubkey(),
            mint_wrapper: mint_wrapper.key,
            admin: authority.pubkey(),
            token_mint: reward_token_mint.key,
            token_program: anchor_spl::token::ID,
            payer: env.default_fee_payer(),
            system_program: solana_sdk::system_program::ID,
        },
        quarry_mint_wrapper::client::args::NewWrapperV2 { hard_cap: u64::MAX },
    );

    env.execute_ixs_with_signers(&[create_wrapper_ix], &[&mint_wrapper_base])?;

    // Create minter with manageable initial allowance
    let minter: AccountRef<quarry_mint_wrapper::accounts::Minter> = env.get_pda(
        "minter",
        &[
            b"MintWrapperMinter",
            mint_wrapper.key.as_ref(),
            new_minter_authority.pubkey().as_ref(),
        ],
        quarry_mint_wrapper::ID,
    )?;

    let create_minter_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::NewMinterV2 {
            new_minter_v2_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: mint_wrapper.key,
                admin: authority.pubkey(),
            },
            new_minter_authority: new_minter_authority.pubkey(),
            minter: minter.key,
            payer: env.default_fee_payer(),
            system_program: solana_sdk::system_program::ID,
        },
        quarry_mint_wrapper::client::args::NewMinterV2 {},
    );

    let set_initial_allowance_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::MinterUpdate {
            minter: minter.key,
            minter_update_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: mint_wrapper.key,
                admin: authority.pubkey(),
            },
        },
        quarry_mint_wrapper::client::args::MinterUpdate {
            allowance: 1_000_000_000_000, // Set initial allowance to a manageable value
        },
    );

    env.execute_ixs_with_signers(&[create_minter_ix, set_initial_allowance_ix], &[&authority])?;

    // Test 1: Unauthorized user cannot update minter allowance
    let update_allowance_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::MinterUpdate {
            minter: minter.key,
            minter_update_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: mint_wrapper.key,
                admin: unauthorized.pubkey(), // Wrong authority
            },
        },
        quarry_mint_wrapper::client::args::MinterUpdate {
            allowance: 5_000_000_000_000,
        },
    );

    // Should fail because unauthorized is not the admin
    env.execute_ixs_with_signers(&[update_allowance_ix], &[&unauthorized])
        .fails()?;

    // Test 2: Authority can update minter allowance
    let authorized_update_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::MinterUpdate {
            minter: minter.key,
            minter_update_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: mint_wrapper.key,
                admin: authority.pubkey(), // Correct authority
            },
        },
        quarry_mint_wrapper::client::args::MinterUpdate {
            allowance: 10_000_000_000_000,
        },
    );

    env.execute_ixs_with_signers(&[authorized_update_ix], &[&authority])
        .succeeds()?;

    // Verify the allowance was updated
    let minter_data: quarry_mint_wrapper::accounts::Minter = minter.load(&env)?;
    assert_eq!(
        minter_data.allowance, 10_000_000_000_000,
        "Allowance should be updated"
    );

    Ok(())
}

#[test]
fn test_transfer_mint_wrapper_authority() -> Result<()> {
    let mut env = init_test_environment()?;

    // Create original authority and new authority
    let original_authority = env.new_wallet("original_authority")?;
    let new_authority = env.new_wallet("new_authority")?;
    let unauthorized = env.new_wallet("unauthorized")?;

    // Create rewarder with original authority
    let rewarder = TestRewarder::new_rewarder(&mut env, "main", &original_authority)?;

    // Verify original authority
    let wrapper_data = rewarder.fetch_mint_wrapper(&env)?;
    assert_eq!(
        wrapper_data.admin,
        original_authority.pubkey(),
        "Initial admin should be set"
    );

    // Test 1: Unauthorized user cannot transfer authority
    let unauthorized_transfer_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::TransferAdmin {
            mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
            admin: unauthorized.pubkey(), // Wrong current admin
            next_admin: new_authority.pubkey(),
        },
        quarry_mint_wrapper::client::args::TransferAdmin {},
    );

    env.execute_ixs_with_signers(&[unauthorized_transfer_ix], &[&unauthorized])
        .fails()?
        .with_anchor_error("KeyMismatch")?;

    // Test 2: Original authority can transfer to pending
    let transfer_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::TransferAdmin {
            mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
            admin: original_authority.pubkey(),
            next_admin: new_authority.pubkey(),
        },
        quarry_mint_wrapper::client::args::TransferAdmin {},
    );

    env.execute_ixs_with_signers(&[transfer_ix], &[&original_authority])
        .succeeds()?;

    // Verify pending authority was set
    let wrapper_data = rewarder.fetch_mint_wrapper(&env)?;
    assert_eq!(
        wrapper_data.pending_admin,
        new_authority.pubkey(),
        "Pending admin should be set"
    );
    assert_eq!(
        wrapper_data.admin,
        original_authority.pubkey(),
        "Current admin should remain unchanged"
    );

    // Test 3: Unauthorized user cannot accept authority transfer
    let unauthorized_accept_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::AcceptAdmin {
            mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
            pending_admin: unauthorized.pubkey(), // Wrong pending admin
        },
        quarry_mint_wrapper::client::args::AcceptAdmin {},
    );

    env.execute_ixs_with_signers(&[unauthorized_accept_ix], &[&unauthorized])
        .fails()?
        .with_anchor_error("KeyMismatch")?;

    // Test 4: New authority can accept the transfer
    let accept_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::AcceptAdmin {
            mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
            pending_admin: new_authority.pubkey(),
        },
        quarry_mint_wrapper::client::args::AcceptAdmin {},
    );

    env.execute_ixs_with_signers(&[accept_ix], &[&new_authority])
        .succeeds()?;

    // Verify new authority is active
    let wrapper_data = rewarder.fetch_mint_wrapper(&env)?;
    assert_eq!(
        wrapper_data.admin,
        new_authority.pubkey(),
        "Admin should be transferred"
    );
    assert_eq!(
        wrapper_data.pending_admin,
        Pubkey::default(),
        "Pending admin should be cleared"
    );

    // Test 5: Old authority can no longer make changes (creating a new minter)
    let test_minter_authority = env.new_wallet("test_minter_authority")?;
    let test_minter: AccountRef<quarry_mint_wrapper::accounts::Minter> = env.get_pda(
        "test_minter",
        &[
            b"MintWrapperMinter",
            rewarder.mint_wrapper.mint_wrapper.key.as_ref(),
            test_minter_authority.pubkey().as_ref(),
        ],
        quarry_mint_wrapper::ID,
    )?;

    let old_auth_create_minter_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::NewMinterV2 {
            new_minter_v2_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
                admin: original_authority.pubkey(), // Old authority
            },
            new_minter_authority: test_minter_authority.pubkey(),
            minter: test_minter.key,
            payer: env.default_fee_payer(),
            system_program: solana_sdk::system_program::ID,
        },
        quarry_mint_wrapper::client::args::NewMinterV2 {},
    );

    env.execute_ixs_with_signers(&[old_auth_create_minter_ix], &[&original_authority])
        .fails()?
        .with_anchor_error("Unauthorized")?;

    // Test 6: New authority can make changes (create a new minter)
    let new_auth_create_minter_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::NewMinterV2 {
            new_minter_v2_auth: quarry_mint_wrapper::client::accounts::NewMinterAuth {
                mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
                admin: new_authority.pubkey(), // New authority
            },
            new_minter_authority: test_minter_authority.pubkey(),
            minter: test_minter.key,
            payer: env.default_fee_payer(),
            system_program: solana_sdk::system_program::ID,
        },
        quarry_mint_wrapper::client::args::NewMinterV2 {},
    );

    env.execute_ixs_with_signers(&[new_auth_create_minter_ix], &[&new_authority])
        .succeeds()?;

    // Verify the new minter was created
    let test_minter_data: quarry_mint_wrapper::accounts::Minter = test_minter.load(&env)?;
    assert_eq!(
        test_minter_data.minter_authority,
        test_minter_authority.pubkey(),
        "New minter should be created with correct authority"
    );

    Ok(())
}

#[test]
fn test_perform_mint_incorrect_authority() -> Result<()> {
    let mut env = init_test_environment()?;

    // Create authority and unauthorized keypairs
    let authority = env.new_wallet("authority")?;
    let unauthorized = env.new_wallet("unauthorized")?;

    // Create rewarder with authority
    let rewarder = TestRewarder::new_rewarder(&mut env, "main", &authority)?;

    // Create a destination token account
    let destination_owner = env.new_wallet("destination_owner")?;
    let (create_ata_ix, destination) = env.create_ata_ix(
        "destination",
        &destination_owner.pubkey(),
        &rewarder.mint_wrapper.reward_token_mint.into(),
    )?;
    env.execute_ixs(&[create_ata_ix])?;

    // Test: Unauthorized minter authority cannot perform mint
    let mint_amount = 1_000_000_000u64; // 1000 tokens with 6 decimals
    let mint_ix = anchor_instruction(
        quarry_mint_wrapper::ID,
        quarry_mint_wrapper::client::accounts::PerformMint {
            mint_wrapper: rewarder.mint_wrapper.mint_wrapper.key,
            minter_authority: unauthorized.pubkey(), // Wrong minter authority
            token_mint: rewarder.mint_wrapper.reward_token_mint.key,
            destination: destination.into(),
            minter: rewarder.minter.key,
            token_program: anchor_spl::token::ID,
        },
        quarry_mint_wrapper::client::args::PerformMint {
            amount: mint_amount,
        },
    );

    // Should fail because unauthorized is not the minter authority
    env.execute_ixs_with_signers(&[mint_ix], &[&unauthorized])
        .fails()?
        .with_anchor_error("Unauthorized")?;

    // Verify no tokens were minted
    let destination_account: anchor_spl::token::TokenAccount = destination.load(&env)?;
    assert_eq!(
        destination_account.amount, 0,
        "No tokens should be minted for unauthorized mint attempt"
    );

    Ok(())
}