-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSol_Purchase.rs
More file actions
864 lines (747 loc) · 30.9 KB
/
Copy pathSol_Purchase.rs
File metadata and controls
864 lines (747 loc) · 30.9 KB
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
use pyth_sdk::PriceFeed;
use pyth_sdk_solana::load_price_feed_from_account_info;
use arrayref::array_ref;
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Mint, Token, TokenAccount, Transfer};
declare_id!("BU9M4GMAjMCLiwd7opAqmZz12VtqHE3YRjbkx5MkTWq2");
#[program]
pub mod fam_presale_contract {
use super::*;
// Initialize presale and vesting parameters
pub fn initialize(
ctx: Context<Initialize>,
presale_start: i64,
presale_end: i64,
public_sale_start: i64,
price: u64,
max_allocation: u64,
cliff_period: i64,
vesting_period: i64,
vesting_interval: i64,
airdrop_percentages: Vec<u64>, // Accept airdrop percentages as input
max_airdrop_elements: u8 // Accept maximum airdrop elements as input
) -> ProgramResult {
// Cap the size of the airdrop_percentages vector (e.g., max 12 elements)
if airdrop_percentages.len() > max_airdrop_elements.into() {
return Err(ErrorCode::AirdropConfigurationError.into());
}
let total_percentage: u64 = airdrop_percentages.iter().sum();
if total_percentage > 100 {
return Err(ErrorCode::AirdropConfigurationError.into());
}
if vesting_period == 0 || vesting_interval == 0 {
return Err(ErrorCode::InvalidVestingParameters.into());
}
if vesting_interval > vesting_period {
return Err(ErrorCode::InvalidVestingParameters.into());
}
if cliff_period > vesting_period {
return Err(ErrorCode::InvalidVestingParameters.into());
}
if presale_start >= presale_end {
return Err(ErrorCode::InvalidPresaleTiming.into());
}
if public_sale_start <= presale_end {
return Err(ErrorCode::InvalidPresaleTiming.into());
}
if airdrop_percentages.is_empty() {
return Err(ErrorCode::AirdropConfigurationError.into());
}
if airdrop_percentages.iter().any(|&x| x == 0) {
return Err(ErrorCode::AirdropConfigurationError.into());
}
let presale_account = &mut ctx.accounts.presale_account;
presale_account.presale_start = presale_start;
presale_account.presale_end = presale_end;
presale_account.public_sale_start = public_sale_start;
presale_account.price = price;
presale_account.max_allocation = max_allocation;
presale_account.cliff_period = cliff_period;
presale_account.vesting_period = vesting_period;
presale_account.vesting_interval = vesting_interval;
presale_account.airdrop_percentages = airdrop_percentages;
Ok(())
}
pub fn update_presale_params(
ctx: Context<UpdatePresaleParams>,
new_price: Option<u64>,
new_min_buy_amount: Option<u64>,
new_max_buy_amount: Option<u64>,
new_hard_cap: Option<u64>,
) -> ProgramResult {
let presale_account = &mut ctx.accounts.presale_account;
// Ensure caller is the authorized admin
if ctx.accounts.authority.key() != presale_account.authority {
return Err(ErrorCode::UnauthorizedAccess.into());
}
// Update parameters if provided
if let Some(price) = new_price {
presale_account.price = price;
}
if let Some(min_buy) = new_min_buy_amount {
presale_account.min_buy_amount_sol = min_buy;
}
if let Some(max_buy) = new_max_buy_amount {
presale_account.max_buy_amount_sol = max_buy;
}
if let Some(hard_cap) = new_hard_cap {
presale_account.hard_cap_sol = hard_cap;
}
// Emit event with updated parameters
let clock = Clock::get()?;
emit!(PresaleParamsUpdated {
new_price,
new_min_buy_amount,
new_max_buy_amount,
new_hard_cap,
timestamp: clock.unix_timestamp,
});
Ok(())
}
#[account]
pub struct UserVesting {
pub total_amount: u64, // Total tokens purchased
pub claimed_amount: u64, // Tokens already claimed
pub start_time: i64, // Presale end time
pub airdrops_completed: u8, // Number of airdrops already distributed
pub total_purchased_sol: u64, // Total SOL equivalent purchased by this user
}
// Define the PurchaseEvent at the top of your contract
#[event]
pub struct PurchaseEvent {
pub buyer: Pubkey, // Buyer's wallet public key
pub amount: u64, // Number of tokens purchased
pub cost_in_sol: Option<u64>, // Cost in SOL equivalent (in lamports, if applicable)
pub timestamp: i64,
}
pub fn purchase(ctx: Context<Purchase>, amount: u64) -> ProgramResult {
let presale_account = &mut ctx.accounts.presale_account;
let user_vesting = &mut ctx.accounts.user_vesting;
// Fetch current timestamp
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
// Ensure presale is not paused
if presale_account.paused {
return Err(ErrorCode::PresalePaused.into());
}
// Ensure presale is active
if !(current_time >= presale_account.presale_start && current_time <= presale_account.presale_end) {
return Err(ErrorCode::SaleNotActive.into());
}
// Fetch SOL/USD price using fallback logic
let sol_price_in_usd = get_price_from_oracle(
&ctx.accounts.sol_to_usd_oracle,
presale_account.manual_price_override,
)?;
// Calculate the token price in SOL
let token_price_in_sol = presale_account
.price
.checked_mul(10u64.pow(9)) // Convert price from USD cents to lamports
.ok_or(ErrorCode::MathOverflow)?
.checked_div(sol_price_in_usd)
.ok_or(ErrorCode::MathOverflow)?;
// Calculate total cost in SOL (lamports)
let total_cost_in_sol = amount
.checked_mul(token_price_in_sol)
.ok_or(ErrorCode::MathOverflow)?;
// Ensure the buyer has enough SOL
if **ctx.accounts.buyer.to_account_info().lamports.borrow() < total_cost_in_sol {
return Err(ErrorCode::InsufficientFunds.into());
}
// Ensure the purchase is within allowed limits
if total_cost_in_sol < presale_account.min_buy_amount_sol {
return Err(ErrorCode::BelowMinimumPurchase.into());
}
if user_vesting
.total_purchased_sol
.checked_add(total_cost_in_sol)
.ok_or(ErrorCode::MathOverflow)?
> presale_account.max_buy_amount_sol
{
return Err(ErrorCode::ExceedsMaximumPurchase.into());
}
// Check if the total sold exceeds the global hard cap
if presale_account
.total_sold_sol
.checked_add(total_cost_in_sol)
.ok_or(ErrorCode::MathOverflow)?
> presale_account.hard_cap_sol
{
return Err(ErrorCode::HardCapReached.into());
}
// --- STATE UPDATES ---
presale_account.total_sold_sol = presale_account
.total_sold_sol
.checked_add(total_cost_in_sol)
.ok_or(ErrorCode::MathOverflow)?;
user_vesting.total_amount = user_vesting
.total_amount
.checked_add(amount)
.ok_or(ErrorCode::MathOverflow)?;
user_vesting.total_purchased_sol = user_vesting
.total_purchased_sol
.checked_add(total_cost_in_sol)
.ok_or(ErrorCode::MathOverflow)?;
// Ensure the amount is non-zero
if amount == 0 || total_cost_in_sol < presale_account.min_buy_amount_sol {
return Err(ErrorCode::BelowMinimumPurchase.into());
}
// Derive a minimum token purchase based on `min_buy_amount_sol` and token price
let min_token_purchase = presale_account
.min_buy_amount_sol
.checked_mul(sol_price_in_usd)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(presale_account.price)
.ok_or(ErrorCode::MathOverflow)?;
if amount < min_token_purchase {
return Err(ErrorCode::BelowMinimumPurchase.into());
}
// --- EXTERNAL CALL ---
let program_pda = ctx.accounts.presale_account.to_account_info().key;
**ctx.accounts.buyer.to_account_info().try_borrow_mut_lamports()? -= total_cost_in_sol;
**program_pda.try_borrow_mut_lamports()? += total_cost_in_sol;
// Emit event
emit!(PurchaseEvent {
buyer: ctx.accounts.buyer.key(),
amount,
cost_in_sol: total_cost_in_sol,
timestamp: current_time,
});
Ok(())
}
// Utility function to derive the program's PDA
fn derive_presale_pda(program_id: &Pubkey, seed: &[u8]) -> Pubkey {
Pubkey::find_program_address(&[seed], program_id).0
}
#[event]
pub struct PauseStateChanged {
pub paused: bool, // Whether the presale is paused
pub timestamp: i64,
}
pub fn set_pause_state(ctx: Context<SetPauseState>, paused: bool) -> ProgramResult {
let presale_account = &mut ctx.accounts.presale_account;
// Ensure caller is the authorized admin
if ctx.accounts.authority.key() != presale_account.authority {
return Err(ErrorCode::UnauthorizedAccess.into());
}
// Update pause state
presale_account.paused = paused;
// Emit pause state change event
let clock = Clock::get()?;
emit!(PauseStateChanged {
paused,
timestamp: clock.unix_timestamp,
});
Ok(())
}
// Batch airdrop distribution to save compute units
const MAX_BATCH_SIZE: usize = 50; // Set a limit for batch size
pub fn distribute_airdrops_batch(
ctx: Context<BatchDistributeAirdrops>,
users: Vec<UserDistribution>,
) -> ProgramResult {
const MAX_BATCH_SIZE: usize = 50; // Set a limit for batch size
let presale_account = &ctx.accounts.presale_account;
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
// Ensure presale has ended
if current_time < presale_account.presale_end {
return Err(ErrorCode::PresaleNotEnded.into());
}
// Ensure batch size does not exceed MAX_BATCH_SIZE
if users.len() > MAX_BATCH_SIZE {
return Err(ErrorCode::BatchTooLarge.into());
}
// Iterate over users and process airdrops
for user in users.iter() {
// Validate that the user_vesting_index is within bounds
if user.user_vesting_index >= ctx.remaining_accounts.len() {
return Err(ErrorCode::InvalidUserAccountIndex.into());
}
// Safely load the user vesting account
let user_vesting_account = &mut Account::<UserVesting>::try_from(
&ctx.remaining_accounts[user.user_vesting_index],
)?;
// Skip users who have completed all their airdrops
if user_vesting_account.airdrops_completed >= presale_account.total_airdrop_periods {
continue;
}
// Ensure airdrop index is valid
let airdrop_percentage = presale_account
.airdrop_percentages
.get(user.airdrop_index as usize)
.ok_or(ErrorCode::AirdropConfigurationError)?;
// Calculate the airdrop amount
let airdrop_amount = user_vesting_account
.total_amount
.checked_mul(*airdrop_percentage as u64)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(100)
.ok_or(ErrorCode::MathOverflow)?;
// Transfer the airdrop tokens
token::transfer(
ctx.accounts.into_transfer_context(user_vesting_account),
airdrop_amount,
)?;
if airdrop_amount == 0 {
continue; // Avoid unnecessary transfers or updates
}
// Update user vesting account
user_vesting_account.claimed_amount = user_vesting_account
.claimed_amount
.checked_add(airdrop_amount)
.ok_or(ErrorCode::MathOverflow)?;
user_vesting_account.airdrops_completed = user_vesting_account
.airdrops_completed
.checked_add(1)
.ok_or(ErrorCode::MathOverflow)?;
}
Ok(())
}
pub fn refund(ctx: Context<Refund>, refund_amount: u64) -> ProgramResult {
let presale_account = &mut ctx.accounts.presale_account;
let user_vesting = &mut ctx.accounts.user_vesting;
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
// Ensure the refund period is active
if current_time < presale_account.presale_end {
return Err(ErrorCode::RefundNotAvailable.into());
}
// Calculate claimable and refundable tokens
let claimable_tokens = calculate_vested_amount(
user_vesting.total_amount,
user_vesting.start_time,
presale_account.vesting_period,
presale_account.vesting_interval,
current_time,
)
.saturating_sub(user_vesting.claimed_amount);
let refundable_tokens = user_vesting
.total_amount
.saturating_sub(claimable_tokens)
.saturating_sub(user_vesting.claimed_amount);
if refund_amount == 0 || refundable_tokens == 0 {
return Err(ErrorCode::InsufficientRefundBalance.into());
}
// Fetch SOL/USD price using fallback logic
let sol_price_in_usd = get_price_from_oracle(
&ctx.accounts.sol_to_usd_oracle,
presale_account.manual_price_override,
)?;
// Calculate refund amount in SOL
let refund_sol = calculate_sol_price(refund_amount, presale_account.price, sol_price_in_usd)?;
// Ensure the program PDA has enough SOL for the refund
let program_pda = ctx.accounts.presale_account.to_account_info();
if **program_pda.lamports.borrow() < refund_sol {
return Err(ErrorCode::InsufficientProgramBalance.into());
}
// Process the refund
**program_pda.try_borrow_mut_lamports()? -= refund_sol;
**ctx.accounts.buyer.to_account_info().try_borrow_mut_lamports()? += refund_sol;
// Update metrics
user_vesting.total_amount = user_vesting
.total_amount
.checked_sub(refund_amount)
.ok_or(ErrorCode::MathOverflow)?;
presale_account.total_sold_sol = presale_account
.total_sold_sol
.checked_sub(refund_amount)
.ok_or(ErrorCode::MathOverflow)?;
// Emit event
emit!(RefundEvent {
buyer: ctx.accounts.buyer.key(),
refund_amount,
refund_sol,
remaining_tokens: refundable_tokens - refund_amount,
total_refund_tokens: user_vesting.total_amount,
total_refunded_sol: presale_account.total_sold_sol,
});
Ok(())
}
fn get_price_from_oracle(
oracle_account: &AccountInfo,
manual_price_override: Option<u64>,
) -> Result<u64, ProgramError> {
// Attempt to load the price feed from the oracle
if let Ok(price_feed) = load_price_feed_from_account_info(oracle_account) {
if let Some(price_data) = price_feed.get_current_price() {
if price_data.price > 0 {
return Ok(price_data.price as u64); // Return valid oracle price
}
}
}
// Fallback to manual price override if oracle fails or returns invalid data
if let Some(price) = manual_price_override {
if price == 0 || price > 1_000_000 { // Validate fallback price
return Err(ErrorCode::InvalidPrice.into());
}
return Ok(price);
}
Err(ErrorCode::PriceFeedUnavailable.into())
}
pub fn update_manual_price_override(
ctx: Context<UpdateManualPriceOverride>,
new_price: Option<u64>,
) -> ProgramResult {
let presale_account = &mut ctx.accounts.presale_account;
// Ensure caller is the authorized admin
if ctx.accounts.authority.key() != presale_account.authority {
return Err(ErrorCode::UnauthorizedAccess.into());
}
// Validate the manual price override
if let Some(price) = new_price {
if price == 0 || price > 1_000_000 { // Ensure price is non-zero and within reasonable bounds
return Err(ErrorCode::InvalidPrice.into());
}
}
// Update the manual price override
presale_account.manual_price_override = new_price;
// Emit event
let clock = Clock::get()?;
emit!(ManualPriceOverrideUpdated {
new_price,
timestamp: clock.unix_timestamp,
});
Ok(())
}
fn calculate_sol_price(
amount: u64,
price: u64,
sol_price_in_usd: u64,
) -> Result<u64, ProgramError> {
amount
.checked_mul(price)
.ok_or(ErrorCode::MathOverflow)?
.checked_mul(10u64.pow(9)) // Convert USD cents to lamports
.ok_or(ErrorCode::MathOverflow)?
.checked_div(sol_price_in_usd)
.ok_or(ErrorCode::MathOverflow)
}
#[event]
pub struct RefundEvent {
pub buyer: Pubkey, // User's wallet public key
pub refund_amount: u64, // Number of tokens refunded
pub refund_sol: u64, // Amount of SOL refunded
pub remaining_tokens: u64, // Remaining refundable tokens
pub total_refund_tokens: u64, // Total tokens refunded so far
pub total_refunded_sol: u64, // Total SOL refunded so far
}
#[derive(Accounts)]
pub struct Refund<'info> {
#[account(mut)]
pub presale_account: Account<'info, PresaleAccount>, // Presale account storing presale details
#[account(mut)]
pub user_vesting: Account<'info, UserVesting>, // User's vesting account
#[account(mut)]
pub buyer: Signer<'info>, // User requesting the refund
pub sol_to_usd_oracle: AccountInfo<'info>, // Oracle account for SOL/USD price
pub system_program: Program<'info, System>, // System program for SOL transfers
}
#[derive(Accounts)]
pub struct SetPauseState<'info> {
#[account(mut, has_one = authority)]
pub presale_account: Account<'info, PresaleAccount>,
pub authority: Signer<'info>, // Admin account
}
#[derive(AnchorDeserialize, AnchorSerialize, Clone)]
pub struct UserDistribution {
pub user_vesting_index: usize, // Index in the remaining accounts array
pub airdrop_index: u8, // Index of the current airdrop percentage
}
#[event]
pub struct ClaimEvent {
pub user: Pubkey, // User's public key
pub amount: u64, // Amount of tokens claimed
pub total_claimed: u64, // Total claimed tokens after this transaction
}
#[event]
pub struct ManualPriceOverrideUpdated {
pub new_price: Option<u64>, // Updated manual price
pub timestamp: i64, // Time of the update
}
pub fn claim(ctx: Context<Claim>) -> ProgramResult {
let user_vesting = &mut ctx.accounts.user_vesting;
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
// Calculate vested tokens
let vested_amount = calculate_vested_amount(
user_vesting.total_amount,
user_vesting.start_time,
ctx.accounts.presale_account.vesting_period,
ctx.accounts.presale_account.vesting_interval,
current_time,
);
let claimable_amount = vested_amount.saturating_sub(user_vesting.claimed_amount);
if claimable_amount > 0 {
token::transfer(
ctx.accounts.into_transfer_context(),
claimable_amount,
)?;
// Update claimed amount
user_vesting.claimed_amount += claimable_amount;
// Emit event
emit!(ClaimEvent {
user: ctx.accounts.buyer.key(),
amount: claimable_amount,
total_claimed: user_vesting.claimed_amount,
});
Ok(())
} else {
Err(ErrorCode::NoTokensToClaim.into())
}
}
// Helper to calculate vested amount
fn calculate_vested_amount(
total_amount: u64,
start_time: i64,
vesting_period: i64,
vesting_interval: i64,
current_time: i64,
) -> u64 {
if current_time < start_time {
return 0;
}
let elapsed_time = current_time - start_time;
if vesting_period == 0 || vesting_interval == 0 {
return 0;
}
let vested_intervals = elapsed_time / vesting_interval;
let total_intervals = vesting_period / vesting_interval;
if total_intervals == 0 {
return 0;
}
// Calculate the proportion of vested tokens
let vested_amount = (total_amount as u128 * vested_intervals as u128 / total_intervals as u128) as u64;
vested_amount
}
pub fn calculate_claimable(ctx: Context<CalculateClaimable>) -> Result<u64> {
let user_vesting = &ctx.accounts.user_vesting;
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
let vested_amount = calculate_vested_amount(
user_vesting.total_amount,
user_vesting.start_time,
ctx.accounts.presale_account.vesting_period,
ctx.accounts.presale_account.vesting_interval,
current_time,
);
// Return the amount of claimable tokens
Ok(vested_amount.saturating_sub(user_vesting.claimed_amount))
}
#[derive(Accounts)]
pub struct CalculateClaimable<'info> {
#[account(mut)]
pub user_vesting: Account<'info, UserVesting>,
pub presale_account: Account<'info, PresaleAccount>,
}
#[account]
pub struct PresaleAccount {
pub presale_start: i64,
pub presale_end: i64,
pub public_sale_start: i64,
pub price: u64, // Price per token in USD cents
pub max_allocation: u64,
pub cliff_period: i64,
pub vesting_period: i64,
pub vesting_interval: i64,
pub total_airdrop_periods: u8,
pub airdrop_percentages: Vec<u8>,
pub total_sold_sol: u64, // Total tokens sold
pub min_buy_amount_sol: u64, // Minimum SOL amount per purchase
pub max_buy_amount_sol: u64, // Maximum SOL amount per user
pub hard_cap_sol: u64, // Maximum SOL for the entire presale
pub authority: Pubkey, // Admin authority key
pub manual_price_override: Option<u64>, // Optional manual price in USD cents
pub paused: bool, // Whether the presale is paused
}
impl<'info> Claim<'info> {
fn into_transfer_context(&self) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> {
CpiContext::new(
self.token_program.to_account_info(),
Transfer {
from: self.user_vesting.to_account_info(),
to: self.buyer.to_account_info(),
authority: self.buyer.to_account_info(),
},
)
}
}
pub fn update_presale_discount(
ctx: Context<UpdatePresaleParams>,
new_price: Option<u64>,
) -> ProgramResult {
let presale_account = &mut ctx.accounts.presale_account;
// Validate and update price directly
if let Some(price) = new_price {
if price == 0 {
return Err(ErrorCode::InvalidPrice.into());
}
presale_account.price = price;
}
Ok(())
}
pub fn distribute_initial_airdrop(ctx: Context<DistributeAirdrop>) -> ProgramResult {
let user_vesting = &mut ctx.accounts.user_vesting;
let presale_account = &ctx.accounts.presale_account;
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
// Ensure presale has ended
if current_time < presale_account.presale_end {
return Err(ErrorCode::PresaleNotEnded.into());
}
// Calculate initial airdrop percentage
let initial_percentage = *presale_account.airdrop_percentages.get(0).ok_or(ErrorCode::AirdropConfigurationError)?;
let initial_airdrop = user_vesting
.total_amount
.checked_mul(initial_percentage)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(100)
.ok_or(ErrorCode::MathOverflow)?;
// Transfer initial airdrop
token::transfer(
ctx.accounts.into_transfer_context(),
initial_airdrop,
)?;
// Update claimed amount and airdrop count
user_vesting.claimed_amount += initial_airdrop;
user_vesting.airdrops_completed = 1;
Ok(())
}
#[derive(Accounts)]
pub struct UpdateManualPriceOverride<'info> {
#[account(mut, has_one = authority)]
pub presale_account: Account<'info, PresaleAccount>,
pub authority: Signer<'info>, // Admin account
}
#[derive(Accounts)]
pub struct UpdatePresaleParams<'info> {
#[account(mut, has_one = authority)]
pub presale_account: Account<'info, PresaleAccount>,
pub authority: Signer<'info>, // Admin account
}
// Define the `Claim` context for claiming tokens
#[derive(Accounts)]
pub struct Claim<'info> {
#[account(mut)]
pub user_vesting: Account<'info, UserVesting>,
#[account(mut)]
pub presale_account: Account<'info, PresaleAccount>, // Added
#[account(mut)]
pub buyer: Signer<'info>,
pub token_program: Program<'info, Token>,
}
#[event]
pub struct PresaleParamsUpdated {
pub new_price: Option<u64>,
pub new_min_buy_amount: Option<u64>,
pub new_max_buy_amount: Option<u64>,
pub new_hard_cap: Option<u64>,
pub timestamp: i64,
}
#[derive(Accounts)]
pub struct DistributeAirdrop<'info> {
#[account(mut)]
pub user_vesting: Account<'info, UserVesting>,
#[account(mut)]
pub presale_account: Account<'info, PresaleAccount>,
pub token_program: Program<'info, Token>,
}
#[derive(Accounts)]
pub struct Purchase<'info> {
#[account(mut)]
pub presale_account: Account<'info, PresaleAccount>,
#[account(mut)]
pub user_vesting: Account<'info, UserVesting>,
#[account(mut)]
pub buyer: Signer<'info>,
pub sol_to_usd_oracle: AccountInfo<'info>, // Oracle account for SOL/USD price
pub system_program: Program<'info, System>,
}
pub fn distribute_monthly_airdrop(ctx: Context<DistributeAirdrop>) -> ProgramResult {
let user_vesting = &mut ctx.accounts.user_vesting;
let presale_account = &ctx.accounts.presale_account;
let clock = Clock::get()?;
let current_time = clock.unix_timestamp;
// Ensure at least one month has passed since the last airdrop
let months_elapsed = (current_time - user_vesting.start_time) / presale_account.vesting_interval;
if months_elapsed as u8 <= user_vesting.airdrops_completed {
return Err(ErrorCode::AirdropNotDue.into());
}
// Ensure airdrops do not exceed total periods
if user_vesting.airdrops_completed >= presale_account.total_airdrop_periods {
return Err(ErrorCode::AirdropCompleted.into());
}
// Get the percentage for the current airdrop
let current_percentage = *presale_account
.airdrop_percentages
.get(user_vesting.airdrops_completed as usize)
.ok_or(ErrorCode::AirdropConfigurationError)?;
// Calculate the airdrop amount
let airdrop_amount = user_vesting
.total_amount
.checked_mul(current_percentage)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(100)
.ok_or(ErrorCode::MathOverflow)?;
// Transfer the airdrop amount
token::transfer(
ctx.accounts.into_transfer_context(),
airdrop_amount,
)?;
// Update claimed amount and airdrop count
user_vesting.claimed_amount += airdrop_amount;
user_vesting.airdrops_completed += 1;
Ok(())
}
#[error_code]
pub enum ErrorCode {
#[msg("The sale is not currently active.")]
SaleNotActive,
#[msg("No tokens available for claiming.")]
NoTokensToClaim,
#[msg("The presale has not ended.")]
PresaleNotEnded,
#[msg("Refunds are not available.")]
RefundNotAvailable,
#[msg("Insufficient unclaimed tokens for refund.")]
InsufficientRefundBalance,
#[msg("Program does not have enough SOL for the refund.")]
InsufficientProgramBalance,
#[msg("All airdrops have been completed.")]
AirdropCompleted,
#[msg("Invalid vesting parameters.")]
InvalidVestingParameters,
#[msg("Purchase amount exceeds maximum allocation.")]
AllocationExceeded,
#[msg("Insufficient funds for purchase.")]
InsufficientFunds,
#[msg("Math overflow occurred.")]
MathOverflow,
#[msg("Invalid payment method.")]
InvalidPaymentMethod,
#[msg("Price feed is unavailable.")]
PriceFeedUnavailable,
#[msg("Airdrop configuration error.")]
AirdropConfigurationError,
#[msg("Purchase amount is below the minimum buy amount.")]
BelowMinimumPurchase,
#[msg("Purchase amount exceeds the maximum allowed for this user.")]
ExceedsMaximumPurchase,
#[msg("Presale hard cap has been reached.")]
HardCapReached,
#[msg("Unauthorized access.")]
UnauthorizedAccess,
#[msg("Invalid parameter value.")]
InvalidParameterValue,
#[msg("Invalid discount percentage. Must be <= 100.")]
InvalidDiscountPercentage,
#[msg("Invalid price. Price must be greater than zero.")]
InvalidPrice,
#[msg("Batch size exceeds the maximum limit.")]
BatchTooLarge,
#[msg("Invalid user account index.")]
InvalidUserAccountIndex,
#[msg("Presale is currently paused.")]
PresalePaused,
}
}