-
Notifications
You must be signed in to change notification settings - Fork 511
/
lib.rs
1490 lines (1303 loc) · 45.4 KB
/
lib.rs
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(clippy::result_large_err)]
//! Program for distributing tokens efficiently via uploading a Merkle root.
use anchor_lang::{
prelude::*,
solana_program::{
instruction::{AccountMeta, Instruction},
program::{invoke, invoke_signed},
system_instruction, sysvar,
},
};
use anchor_spl::token::{self, Token, TokenAccount};
use std::io::Write;
pub mod merkle_proof;
declare_id!("gdrpGjVffourzkdDRrQmySw4aTHr8a3xmQzzxSwFD1a");
pub const CANDY_MACHINE_V1_PROGRAM_ID: Pubkey = Pubkey::new_from_array([
0x09, 0x2a, 0xee, 0x40, 0xbb, 0xdd, 0x63, 0x1e, 0xef, 0xfb, 0x7c, 0x96, 0xf6, 0x15, 0x65, 0x76,
0x84, 0x65, 0xf3, 0xc1, 0x9c, 0xf9, 0x90, 0xcd, 0x7f, 0x74, 0x8c, 0x8d, 0x79, 0x95, 0x08, 0x20,
]);
pub const CANDY_MACHINE_V2_PROGRAM_ID: Pubkey = Pubkey::new_from_array([
0x09, 0x2a, 0xee, 0x3d, 0xfc, 0x2d, 0x0e, 0x55, 0x78, 0x23, 0x13, 0x83, 0x79, 0x69, 0xea, 0xf5,
0x21, 0x51, 0xc0, 0x96, 0xc0, 0x6b, 0x5c, 0x2a, 0x82, 0xf0, 0x86, 0xa5, 0x03, 0xe8, 0x2c, 0x34,
]);
fn verify_candy(candy_machine_program_account: &Pubkey) -> Result<()> {
if candy_machine_program_account != &CANDY_MACHINE_V1_PROGRAM_ID
&& candy_machine_program_account != &CANDY_MACHINE_V2_PROGRAM_ID
{
return Err(GumdropError::MustUseOfficialCandyMachine.into());
}
Ok(())
}
const CLAIM_COUNT: &[u8] = b"ClaimCount";
const CLAIM_STATUS: &[u8] = b"ClaimStatus";
fn verify_temporal<'a>(
distributor: &Account<'a, MerkleDistributor>,
temporal: &Signer<'a>,
claimant_secret: Pubkey,
) -> Result<()> {
require!(
// got the OTP auth from the signer specified by the creator
temporal.key() == distributor.temporal
// the secret used in the hash was a Pubkey (wallet) so proof-of-ownership is achieved by
// signing for this transaction
|| temporal.key() == claimant_secret
// the creator decided not to use a temporal signer
|| distributor.temporal == Pubkey::default(),
GumdropError::TemporalMismatch
);
Ok(())
}
fn verify_claim_bump<'a>(
claim_account: &AccountInfo<'a>,
claim_prefix: &[u8],
claim_bump: u8,
index: u64,
distributor: &Account<'a, MerkleDistributor>,
) -> Result<()> {
require!(
claim_prefix == CLAIM_COUNT || claim_prefix == CLAIM_STATUS,
GumdropError::InvalidClaimBump,
);
let (claim_account_key, claim_account_bump) = Pubkey::find_program_address(
&[
claim_prefix,
&index.to_le_bytes(),
&distributor.key().to_bytes(),
],
&ID,
);
require!(
claim_account_key == *claim_account.key && claim_account_bump == claim_bump,
GumdropError::InvalidClaimBump,
);
Ok(())
}
fn get_or_create_claim_count<'a>(
distributor: &Account<'a, MerkleDistributor>,
claim_count: &AccountInfo<'a>,
temporal: &Signer<'a>,
payer: &Signer<'a>,
system_program: &Program<'a, System>,
claim_bump: u8,
index: u64,
claimant_secret: Pubkey,
) -> Result<Account<'a, ClaimCount>> {
let rent = &Rent::get()?;
let space = 8 + ClaimCount::default().try_to_vec().unwrap().len();
verify_claim_bump(claim_count, CLAIM_COUNT, claim_bump, index, distributor)?;
let create_claim_state = claim_count.lamports() == 0; // TODO: support initial lamports?
if create_claim_state {
let lamports = rent.minimum_balance(space);
let claim_count_seeds = [
CLAIM_COUNT,
&index.to_le_bytes(),
&distributor.key().to_bytes(),
&[claim_bump],
];
invoke_signed(
&system_instruction::create_account(
&payer.key(),
claim_count.key,
lamports,
space as u64,
&ID,
),
&[
payer.to_account_info().clone(),
claim_count.clone(),
system_program.to_account_info().clone(),
],
&[&claim_count_seeds],
)?;
let mut data = claim_count.try_borrow_mut_data()?;
let dst: &mut [u8] = &mut data;
let mut cursor = std::io::Cursor::new(dst);
cursor
.write_all(&<ClaimCount as anchor_lang::Discriminator>::discriminator())
.unwrap();
}
// anchor_lang::Account::try_from(&claim_count)?;
let mut pa: Account<ClaimCount> = Account::try_from(claim_count)?;
if create_claim_state {
verify_temporal(distributor, temporal, claimant_secret)?;
pa.claimant = payer.key();
} else {
require!(pa.claimant == payer.key(), GumdropError::OwnerMismatch);
}
Ok(pa)
}
/// The [gumdrop] program.
#[program]
pub mod gumdrop {
use super::*;
/// Creates a new [MerkleDistributor].
/// After creating this [MerkleDistributor], the account should be seeded with tokens via
/// delegates
pub fn new_distributor(
ctx: Context<NewDistributor>,
_bump: u8,
root: [u8; 32],
temporal: Pubkey,
) -> Result<()> {
let distributor = &mut ctx.accounts.distributor;
distributor.base = ctx.accounts.base.key();
distributor.bump = *ctx
.bumps
.get("distributor")
.ok_or(GumdropError::BumpSeedNotInHashMap)?;
distributor.root = root;
distributor.temporal = temporal;
Ok(())
}
/// Closes distributor-owned token accounts. Normal tokens should just use a delegate but we
/// need to transfer ownership for edition minting ATM.
pub fn close_distributor_token_account(
ctx: Context<CloseDistributorTokenAccount>,
_bump: u8,
) -> Result<()> {
let distributor = &ctx.accounts.distributor;
// should be implicit in the PDA
require!(
distributor.base == ctx.accounts.base.key(),
GumdropError::Unauthorized
);
let seeds = [
b"MerkleDistributor".as_ref(),
&distributor.base.to_bytes(),
&[ctx.accounts.distributor.bump],
];
token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
token::Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
authority: ctx.accounts.distributor.to_account_info(),
},
)
.with_signer(&[&seeds[..]]),
ctx.accounts.from.amount,
)?;
token::close_account(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
token::CloseAccount {
account: ctx.accounts.from.to_account_info(),
destination: ctx.accounts.receiver.to_account_info(),
authority: ctx.accounts.distributor.to_account_info(),
},
)
.with_signer(&[&seeds[..]]),
)?;
Ok(())
}
/// Closes an existing [MerkleDistributor].
/// Moves all tokens from the [MerkleDistributor] to the specified account and closes
/// distributor accounts.
/// Must `close_distributor_token_account` first
pub fn close_distributor<'info>(
ctx: Context<'_, '_, '_, 'info, CloseDistributor<'info>>,
_bump: u8,
_wallet_bump: u8,
) -> Result<()> {
let distributor = &ctx.accounts.distributor;
// should be implicit in the PDA
require!(
distributor.base == ctx.accounts.base.key(),
GumdropError::Unauthorized
);
let wallet_seeds = [
b"Wallet".as_ref(),
&distributor.key().to_bytes(),
&[_wallet_bump],
];
if !ctx.remaining_accounts.is_empty() {
// transfer authority out
let candy_machine_info = &ctx.remaining_accounts[0];
let candy_machine_program_info = &ctx.remaining_accounts[1];
verify_candy(candy_machine_program_info.key)?;
// TODO. global::update_authority instruction...
let mut data = vec![0x20, 0x2e, 0x40, 0x1c, 0x95, 0x4b, 0xf3, 0x58];
data.push(0x01);
data.extend_from_slice(&ctx.accounts.receiver.key.to_bytes());
invoke_signed(
&Instruction {
program_id: *candy_machine_program_info.key,
accounts: vec![
AccountMeta::new(*candy_machine_info.key, false),
AccountMeta::new(*ctx.accounts.distributor_wallet.key, true),
],
data,
},
&[
candy_machine_info.clone(),
ctx.accounts.distributor_wallet.clone(),
],
&[&wallet_seeds],
)?;
}
invoke_signed(
&system_instruction::transfer(
ctx.accounts.distributor_wallet.key,
ctx.accounts.receiver.key,
ctx.accounts.distributor_wallet.lamports(),
),
&[
ctx.accounts.distributor_wallet.clone(),
ctx.accounts.receiver.clone(),
ctx.accounts.system_program.to_account_info().clone(),
],
&[&wallet_seeds],
)?;
Ok(())
}
pub fn prove_claim(
ctx: Context<ProveClaim>,
claim_prefix: Vec<u8>,
claim_bump: u8,
index: u64,
amount: u64,
claimant_secret: Pubkey,
resource: Pubkey,
resource_nonce: Vec<u8>,
proof: Vec<[u8; 32]>,
) -> Result<()> {
// The logic here is that we will allow the proof to be whichever prefix matches the claim
// type. The ClaimProof will live at the same place as V1 ClaimCount and V1 ClaimStatus so
// that users can't claim with both endpoints but also maintain some backwards
// compatibility. The account is created wherever this prefix points to and since the
// resource is unique per gumdrop, if this is messed up, they shouldn't be able to claim
// extra resources.
require!(
claim_prefix.as_slice() == CLAIM_COUNT || claim_prefix.as_slice() == CLAIM_STATUS,
GumdropError::InvalidProof,
);
let claim_proof = &mut ctx.accounts.claim_proof;
let distributor = &ctx.accounts.distributor;
verify_claim_bump(
&claim_proof.to_account_info(),
claim_prefix.as_slice(),
claim_bump,
index,
distributor,
)?;
// Verify the merkle proof.
let node = if resource_nonce.is_empty() {
solana_program::keccak::hashv(&[
&[0x00],
&index.to_le_bytes(),
&claimant_secret.to_bytes(),
&resource.to_bytes(),
&amount.to_le_bytes(),
])
} else {
solana_program::keccak::hashv(&[
&[0x00],
&index.to_le_bytes(),
&claimant_secret.to_bytes(),
&resource.to_bytes(),
&amount.to_le_bytes(),
resource_nonce.as_slice(),
])
};
require!(
merkle_proof::verify(proof, distributor.root, node.0),
GumdropError::InvalidProof,
);
verify_temporal(distributor, &ctx.accounts.temporal, claimant_secret)?;
claim_proof.amount = amount;
claim_proof.count = 0;
claim_proof.claimant = ctx.accounts.payer.key();
claim_proof.resource = resource;
claim_proof.resource_nonce = resource_nonce;
Ok(())
}
/// Claims tokens from the [MerkleDistributor].
pub fn claim(
ctx: Context<Claim>,
claim_bump: u8,
index: u64,
amount: u64,
claimant_secret: Pubkey,
proof: Vec<[u8; 32]>,
) -> Result<()> {
let claim_status = &mut ctx.accounts.claim_status;
require!(
*claim_status.to_account_info().owner == ID,
GumdropError::OwnerMismatch
);
require!(
// This check is redudant, we should not be able to initialize a claim status account at the same key.
!claim_status.is_claimed && claim_status.claimed_at == 0,
GumdropError::DropAlreadyClaimed
);
let distributor = &ctx.accounts.distributor;
let mint = ctx.accounts.from.mint;
verify_claim_bump(
&claim_status.to_account_info(),
CLAIM_STATUS,
claim_bump,
index,
distributor,
)?;
// Verify the merkle proof.
let node = solana_program::keccak::hashv(&[
&[0x00],
&index.to_le_bytes(),
&claimant_secret.to_bytes(),
&mint.to_bytes(),
&amount.to_le_bytes(),
]);
require!(
merkle_proof::verify(proof, distributor.root, node.0),
GumdropError::InvalidProof
);
// Mark it claimed and send the tokens.
claim_status.amount = amount;
claim_status.is_claimed = true;
let clock = Clock::get()?;
claim_status.claimed_at = clock.unix_timestamp;
claim_status.claimant = ctx.accounts.payer.key();
let seeds = [
b"MerkleDistributor".as_ref(),
&distributor.base.to_bytes(),
&[ctx.accounts.distributor.bump],
];
verify_temporal(distributor, &ctx.accounts.temporal, claimant_secret)?;
token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
token::Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
authority: ctx.accounts.distributor.to_account_info(),
},
)
.with_signer(&[&seeds[..]]),
amount,
)?;
emit!(ClaimedEvent {
index,
claimant: ctx.accounts.payer.key(),
amount
});
Ok(())
}
/// Claims NFTs directly from the candy machine through the [MerkleDistributor].
pub fn claim_candy<'info>(
ctx: Context<'_, '_, '_, 'info, ClaimCandy<'info>>,
wallet_bump: u8,
claim_bump: u8,
index: u64,
amount: u64,
claimant_secret: Pubkey,
proof: Vec<[u8; 32]>,
) -> Result<()> {
let distributor = &ctx.accounts.distributor;
let mut claim_count = get_or_create_claim_count(
&ctx.accounts.distributor,
&ctx.accounts.claim_count,
&ctx.accounts.temporal,
&ctx.accounts.payer,
&ctx.accounts.system_program,
claim_bump,
index,
claimant_secret,
)?;
require!(
*claim_count.to_account_info().owner == ID,
GumdropError::OwnerMismatch
);
// TODO: this is a bit weird but we verify elsewhere that the candy_machine_config is
// actually a config thing and not a mint
// Verify the merkle proof.
let node = solana_program::keccak::hashv(&[
&[0x00],
&index.to_le_bytes(),
&claimant_secret.to_bytes(),
&ctx.accounts.candy_machine_config.key.to_bytes(),
&amount.to_le_bytes(),
]);
require!(
merkle_proof::verify(proof, distributor.root, node.0),
GumdropError::InvalidProof
);
// This user is whitelisted to mint at most `amount` NFTs from the candy machine
require!(claim_count.count < amount, GumdropError::DropAlreadyClaimed);
// Mark it claimed
claim_count.count = claim_count
.count
.checked_add(1)
.ok_or(GumdropError::NumericalOverflow)?;
issue_mint_nft(
distributor,
&ctx.accounts.distributor_wallet,
&ctx.accounts.payer,
&ctx.accounts.candy_machine_config,
&ctx.accounts.candy_machine,
&ctx.accounts.candy_machine_wallet,
&ctx.accounts.candy_machine_mint,
&ctx.accounts.candy_machine_metadata,
&ctx.accounts.candy_machine_master_edition,
&ctx.accounts.system_program,
&ctx.accounts.token_program,
&ctx.accounts.token_metadata_program,
&ctx.accounts.candy_machine_program,
&ctx.accounts.rent,
&ctx.accounts.clock,
ctx.remaining_accounts,
wallet_bump,
)?;
// reserialize claim_count
{
let mut claim_count_data: &mut [u8] =
&mut ctx.accounts.claim_count.try_borrow_mut_data()?;
claim_count.try_serialize(&mut claim_count_data)?;
}
Ok(())
}
/// Claims NFTs by calling MintNewEditionFromMasterEditionViaToken
pub fn claim_edition(
ctx: Context<ClaimEdition>,
claim_bump: u8,
index: u64,
amount: u64,
edition: u64,
claimant_secret: Pubkey,
proof: Vec<[u8; 32]>,
) -> Result<()> {
let distributor = &ctx.accounts.distributor;
let mut claim_count = get_or_create_claim_count(
&ctx.accounts.distributor,
&ctx.accounts.claim_count,
&ctx.accounts.temporal,
&ctx.accounts.payer,
&ctx.accounts.system_program,
claim_bump,
index,
claimant_secret,
)?;
require!(
*claim_count.to_account_info().owner == ID,
GumdropError::OwnerMismatch
);
// TODO: master_edition or something else? should we has the edition here also?
let node = solana_program::keccak::hashv(&[
&[0x00],
&index.to_le_bytes(),
&claimant_secret.to_bytes(),
&ctx.accounts.metadata_master_mint.key.to_bytes(),
&amount.to_le_bytes(),
&edition.to_le_bytes(),
]);
require!(
merkle_proof::verify(proof, distributor.root, node.0),
GumdropError::InvalidProof
);
// This user is whitelisted to mint at most `amount` NFTs from the candy machine
require!(claim_count.count < amount, GumdropError::DropAlreadyClaimed);
// Mark it claimed
claim_count.count = claim_count
.count
.checked_add(1)
.ok_or(GumdropError::NumericalOverflow)?;
let seeds = [
b"MerkleDistributor".as_ref(),
&distributor.base.to_bytes(),
&[ctx.accounts.distributor.bump],
];
let metadata_infos = [
ctx.accounts.token_metadata_program.clone(),
ctx.accounts.metadata_new_metadata.clone(),
ctx.accounts.metadata_new_edition.clone(),
ctx.accounts.metadata_master_edition.clone(),
ctx.accounts.metadata_new_mint.clone(),
ctx.accounts.metadata_edition_mark_pda.clone(),
ctx.accounts
.metadata_new_mint_authority
.to_account_info()
.clone(),
ctx.accounts.payer.to_account_info().clone(),
ctx.accounts.distributor.to_account_info().clone(),
ctx.accounts.metadata_master_token_account.clone(),
ctx.accounts.metadata_new_update_authority.clone(),
ctx.accounts.metadata_master_metadata.clone(),
ctx.accounts.metadata_master_mint.clone(),
ctx.accounts.rent.to_account_info().clone(),
];
invoke_signed(
&mpl_token_metadata::instruction::mint_new_edition_from_master_edition_via_token(
*ctx.accounts.token_metadata_program.key,
*ctx.accounts.metadata_new_metadata.key,
*ctx.accounts.metadata_new_edition.key,
*ctx.accounts.metadata_master_edition.key,
*ctx.accounts.metadata_new_mint.key,
*ctx.accounts.metadata_new_mint_authority.key,
*ctx.accounts.payer.key,
ctx.accounts.distributor.key(),
*ctx.accounts.metadata_master_token_account.key,
*ctx.accounts.metadata_new_update_authority.key,
*ctx.accounts.metadata_master_metadata.key,
*ctx.accounts.metadata_master_mint.key,
edition,
),
&metadata_infos,
&[&seeds],
)?;
// reserialize claim_count
{
let mut claim_count_data: &mut [u8] =
&mut ctx.accounts.claim_count.try_borrow_mut_data()?;
claim_count.try_serialize(&mut claim_count_data)?;
}
Ok(())
}
pub fn claim_candy_proven<'info>(
ctx: Context<'_, '_, '_, 'info, ClaimCandyProven<'info>>,
wallet_bump: u8,
_claim_bump: u8, // proof is not created
_index: u64,
) -> Result<()> {
let claim_proof = &mut ctx.accounts.claim_proof;
let distributor = &ctx.accounts.distributor;
require!(
claim_proof.claimant == ctx.accounts.payer.key(),
GumdropError::InvalidProof,
);
require!(
claim_proof.resource == *ctx.accounts.candy_machine_config.key,
GumdropError::InvalidProof,
);
// At least 1 remaining
require!(
claim_proof.count < claim_proof.amount,
GumdropError::DropAlreadyClaimed,
);
// Mark it claimed
claim_proof.count = claim_proof
.count
.checked_add(1)
.ok_or(GumdropError::NumericalOverflow)?;
issue_mint_nft(
distributor,
&ctx.accounts.distributor_wallet,
&ctx.accounts.payer,
&ctx.accounts.candy_machine_config,
&ctx.accounts.candy_machine,
&ctx.accounts.candy_machine_wallet,
&ctx.accounts.candy_machine_mint,
&ctx.accounts.candy_machine_metadata,
&ctx.accounts.candy_machine_master_edition,
&ctx.accounts.system_program,
&ctx.accounts.token_program,
&ctx.accounts.token_metadata_program,
&ctx.accounts.candy_machine_program,
&ctx.accounts.rent,
&ctx.accounts.clock,
ctx.remaining_accounts,
wallet_bump,
)?;
Ok(())
}
pub fn recover_update_authority(
ctx: Context<RecoverUpdateAuthority>,
_bump: u8,
wallet_bump: u8,
) -> Result<()> {
let wallet_seeds = [
b"Wallet".as_ref(),
&ctx.accounts.distributor.key().to_bytes(),
&[wallet_bump],
];
invoke_signed(
&mpl_token_metadata::instruction::update_metadata_accounts_v2(
*ctx.accounts.token_metadata_program.key,
*ctx.accounts.metadata.key,
*ctx.accounts.distributor_wallet.key,
Some(*ctx.accounts.new_update_authority.key),
None,
None,
None,
),
&[
ctx.accounts.token_metadata_program.to_account_info(),
ctx.accounts.metadata.to_account_info(),
ctx.accounts.distributor_wallet.to_account_info(),
],
&[&wallet_seeds],
)?;
Ok(())
}
}
fn issue_mint_nft<'info>(
distributor: &Account<'info, MerkleDistributor>,
distributor_wallet: &AccountInfo<'info>,
payer: &Signer<'info>,
candy_machine_config: &AccountInfo<'info>,
candy_machine: &AccountInfo<'info>,
candy_machine_wallet: &AccountInfo<'info>,
candy_machine_mint: &AccountInfo<'info>,
candy_machine_metadata: &AccountInfo<'info>,
candy_machine_master_edition: &AccountInfo<'info>,
system_program: &Program<'info, System>,
token_program: &Program<'info, Token>,
token_metadata_program: &AccountInfo<'info>,
candy_machine_program: &AccountInfo<'info>,
rent: &Sysvar<'info, Rent>,
clock: &Sysvar<'info, Clock>,
claim_remaining_accounts: &[AccountInfo<'info>],
wallet_bump: u8,
) -> Result<()> {
// Transfer the required SOL from the payer
let required_lamports;
let remaining_accounts;
{
let rent = &Rent::get()?;
let mut candy_machine_data: &[u8] = &candy_machine.try_borrow_data()?;
verify_candy(candy_machine_program.key)?;
let candy_machine = CandyMachine::try_deserialize(&mut candy_machine_data)?;
let required_rent = rent.minimum_balance(mpl_token_metadata::state::MAX_METADATA_LEN)
+ rent.minimum_balance(mpl_token_metadata::state::MAX_MASTER_EDITION_LEN);
if candy_machine.token_mint.is_some() {
required_lamports = required_rent;
// checked by candy machine
let token_account_info = &claim_remaining_accounts[0];
let transfer_authority_info = &claim_remaining_accounts[1];
remaining_accounts = vec![token_account_info.clone(), transfer_authority_info.clone()];
} else {
required_lamports = candy_machine.data.price + required_rent;
remaining_accounts = vec![];
}
}
msg!(
"Transferring {} lamports to distributor wallet for candy machine mint",
required_lamports,
);
invoke(
&system_instruction::transfer(payer.key, distributor_wallet.key, required_lamports),
&[
payer.to_account_info().clone(),
distributor_wallet.clone(),
system_program.to_account_info().clone(),
],
)?;
let wallet_seeds = [
b"Wallet".as_ref(),
&distributor.key().to_bytes(),
&[wallet_bump],
];
let mut account_metas = vec![
AccountMeta::new_readonly(candy_machine_config.key(), false),
AccountMeta::new(candy_machine.key(), false),
AccountMeta::new(distributor_wallet.key(), true),
AccountMeta::new(candy_machine_wallet.key(), false),
AccountMeta::new(candy_machine_metadata.key(), false),
AccountMeta::new(candy_machine_mint.key(), false),
AccountMeta::new_readonly(payer.key(), true),
AccountMeta::new_readonly(payer.key(), true),
AccountMeta::new(candy_machine_master_edition.key(), false),
AccountMeta::new_readonly(token_metadata_program.key(), false),
AccountMeta::new_readonly(token_program.key(), false),
AccountMeta::new_readonly(system_program.key(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
];
for a in &remaining_accounts {
account_metas.push(AccountMeta::new(a.key(), false));
}
let mut candy_machine_infos = vec![
candy_machine_config.clone(),
candy_machine.to_account_info().clone(),
distributor_wallet.clone(),
candy_machine_wallet.clone(),
candy_machine_metadata.clone(),
candy_machine_mint.clone(),
payer.to_account_info().clone(),
candy_machine_master_edition.clone(),
token_metadata_program.clone(),
token_program.to_account_info().clone(),
system_program.to_account_info().clone(),
rent.to_account_info().clone(),
clock.to_account_info().clone(),
];
candy_machine_infos.extend(remaining_accounts);
invoke_signed(
&Instruction {
program_id: candy_machine_program.key(),
accounts: account_metas,
// TODO. global::mint_nft instruction...
data: vec![0xd3, 0x39, 0x06, 0xa7, 0x0f, 0xdb, 0x23, 0xfb],
},
&candy_machine_infos,
&[&wallet_seeds],
)?;
// point back to the gumdrop authority
let mut cm_config_data: &[u8] = &candy_machine_config.try_borrow_data()?;
let cm_config = Config::try_deserialize(&mut cm_config_data)?;
if cm_config.data.retain_authority {
invoke_signed(
&mpl_token_metadata::instruction::update_metadata_accounts_v2(
*token_metadata_program.key,
*candy_machine_metadata.key,
*distributor_wallet.key,
Some(distributor.base),
None,
None,
None,
),
&[
token_metadata_program.to_account_info(),
candy_machine_metadata.to_account_info(),
distributor_wallet.to_account_info(),
],
&[&wallet_seeds],
)?;
}
Ok(())
}
/// Accounts for [gumdrop::new_distributor].
#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct NewDistributor<'info> {
/// Base key of the distributor.
pub base: Signer<'info>,
/// [MerkleDistributor].
#[account(
init,
seeds = [
b"MerkleDistributor".as_ref(),
base.key().to_bytes().as_ref()
],
space = 8+97,
bump,
payer = payer
)]
pub distributor: Account<'info, MerkleDistributor>,
/// Payer to create the distributor.
#[account(mut)]
pub payer: Signer<'info>,
/// The [System] program.
pub system_program: Program<'info, System>,
}
/// [gumdrop::close_distributor_token_acconut] accounts.
#[derive(Accounts)]
#[instruction(_bump: u8)]
pub struct CloseDistributorTokenAccount<'info> {
/// Base key of the distributor.
pub base: Signer<'info>,
/// [MerkleDistributor].
#[account(
seeds = [
b"MerkleDistributor".as_ref(),
base.key().to_bytes().as_ref()
],
bump = _bump,
)]
pub distributor: Account<'info, MerkleDistributor>,
/// Distributor containing the tokens to distribute.
#[account(mut)]
pub from: Account<'info, TokenAccount>,
/// Account to send the claimed tokens to.
#[account(mut)]
pub to: Account<'info, TokenAccount>,
/// Who is receiving the remaining rent allocation.
#[account(mut)]
/// CHECK: just a destination
pub receiver: AccountInfo<'info>,
/// The [System] program.
pub system_program: Program<'info, System>,
/// SPL [Token] program.
pub token_program: Program<'info, Token>,
}
/// [gumdrop::close_distributor] accounts.
#[derive(Accounts)]
#[instruction(_bump: u8, _wallet_bump: u8)]
pub struct CloseDistributor<'info> {
/// Base key of the distributor.
pub base: Signer<'info>,
/// [MerkleDistributor].
#[account(
seeds = [
b"MerkleDistributor".as_ref(),
base.key().to_bytes().as_ref()
],
bump = _bump,
mut,
close = receiver,
)]
pub distributor: Account<'info, MerkleDistributor>,
#[account(
seeds = [
b"Wallet".as_ref(),
distributor.key().to_bytes().as_ref()
],
bump = _wallet_bump,
mut,
)]
/// CHECK: PDA Checked
pub distributor_wallet: AccountInfo<'info>,
/// Who is receiving the remaining tokens and rent allocations.
/// CHECK: just a destination
pub receiver: AccountInfo<'info>,
/// The [System] program.
pub system_program: Program<'info, System>,
/// SPL [Token] program.
pub token_program: Program<'info, Token>,
}
/// [gumdrop::prove_claim] accounts.
#[derive(Accounts)]
#[instruction(
claim_prefix: Vec < u8 >,
claim_bump: u8,
index: u64,
_amount: u64,
_claimant_secret: Pubkey,
_resource: Pubkey,
resource_nonce: Vec < u8 >,
)]
pub struct ProveClaim<'info> {
/// The [MerkleDistributor].
#[account(mut)]
pub distributor: Account<'info, MerkleDistributor>,
/// Status of the claim.
#[account(
init,
seeds = [
claim_prefix.as_slice(),
index.to_le_bytes().as_ref(),
distributor.key().to_bytes().as_ref()
],
bump,
payer = payer,
space = 8 // discriminator
+ 8 // amount
+ 8 // count
+ 32 // claimant
+ 32 // resource
+ 4 + resource_nonce.len() // resource_nonce vec
)]
pub claim_proof: Account<'info, ClaimProof>,
/// Extra signer expected for claims
pub temporal: Signer<'info>,
/// Payer of the claim.
#[account(mut)]
pub payer: Signer<'info>,
/// The [System] program.
pub system_program: Program<'info, System>,
}
/// [gumdrop::claim] accounts.
#[derive(Accounts)]
#[instruction(claim_bump: u8, index: u64)]
pub struct Claim<'info> {