Repository navigation
Expand file tree
/
Copy pathAxiom.txt
More file actions
2138 lines (1779 loc) · 60.8 KB
/
Copy pathAxiom.txt
File metadata and controls
2138 lines (1779 loc) · 60.8 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
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
// Source: https://github.com/nisargpatel7042lva/Axiom (branch: master)
// Merged on: 2026-05-04T10:40:43.379Z
/*
Axiom/
└─ spectra-vault
└─ src
├─ instructions
│ ├─ approve_operation.rs
│ ├─ cancel_operation.rs
│ ├─ collect_fee.rs
│ ├─ deposit.rs
│ ├─ execute_operation.rs
│ ├─ execute_strategy_change.rs
│ ├─ initialize_multisig.rs
│ ├─ initialize_strategy.rs
│ ├─ initialize.rs
│ ├─ mod.rs
│ ├─ pause.rs
│ ├─ propose_operation.rs
│ ├─ propose_strategy_change.rs
│ ├─ sync_nav.rs
│ ├─ vote_on_strategy.rs
│ └─ withdraw.rs
├─ ix_accounts
│ ├─ collect_fee.rs
│ ├─ deposit.rs
│ ├─ initialize_strategy.rs
│ ├─ initialize.rs
│ ├─ mod.rs
│ ├─ multisig.rs
│ ├─ pause.rs
│ ├─ sync_nav.rs
│ └─ withdraw.rs
├─ constants.rs
├─ errors.rs
├─ events.rs
├─ lib.rs
└─ state.rs
*/
// File: Axiom/programs/spectra-vault/src/constants.rs
//! Values shared across account constraints and instruction logic.
/// Share token mint decimals (Token-2022).
pub const SHARE_DECIMALS: u8 = 9;
/// Initial high-water mark: 1.000000 USDC per share (6-decimal fixed-point style PPS).
pub const INITIAL_HWM: u64 = 1_000_000;
/// 10^(share_decimals - asset_decimals) for typical USDC (6) vs shares (9).
pub const DECIMAL_ADJUSTMENT: u64 = 1_000;
/// Bridges share-decimals / asset-decimals gap in PPS calculations.
pub const PPS_PRECISION: u128 = 1_000_000_000;
pub const BPS_DENOMINATOR: u128 = 10_000;
// File: Axiom/programs/spectra-vault/src/errors.rs
use anchor_lang::prelude::*;
#[error_code]
pub enum SpectraError {
#[msg("Signer is not the vault authority")]
Unauthorized,
#[msg("Vault is currently paused")]
VaultPaused,
#[msg("Vault is not paused")]
VaultNotPaused,
#[msg("Deposit amount must be greater than zero")]
InvalidAmount,
#[msg("Insufficient shares balance for withdrawal")]
InsufficientShares,
#[msg("Arithmetic overflow")]
MathOverflow,
#[msg("Invalid strategy type (must be 0, 1, or 2)")]
InvalidStrategyType,
#[msg("No performance fee to collect — PPS is at or below high-water mark")]
NoFeeToCollect,
#[msg("min_probability must be less than or equal to max_probability")]
InvalidProbabilityRange,
#[msg("Vault reserves insufficient to cover withdrawal")]
InsufficientVaultBalance,
#[msg("Too many categories (max 8)")]
TooManyCategories,
#[msg("Category string too long (max 32 bytes)")]
CategoryTooLong,
#[msg("Performance fee basis points out of range (max 5000)")]
InvalidFeeBps,
// Multisig errors
#[msg("Signer is not authorized in multisig")]
UnauthorizedSigner,
#[msg("Threshold must be between 1 and MAX_MULTISIG_THRESHOLD")]
InvalidThreshold,
#[msg("Too many signers (max 5)")]
TooManySigners,
#[msg("Operation already approved by this signer")]
AlreadyApproved,
#[msg("Operation not found or already executed/cancelled")]
OperationNotFound,
#[msg("Timelock has not expired yet")]
TimelockActive,
#[msg("Threshold not met - more approvals required")]
ThresholdNotMet,
#[msg("Duplicate signer in list")]
DuplicateSigner,
// Governance errors
#[msg("Voting period has ended")]
VotingEnded,
#[msg("Voting period is still active")]
VotingActive,
#[msg("User already voted on this proposal")]
AlreadyVoted,
#[msg("Quorum not reached")]
QuorumNotReached,
#[msg("Proposal already executed")]
AlreadyExecuted,
#[msg("Proposal was rejected")]
ProposalRejected,
#[msg("Invalid strategy change type")]
InvalidChangeType,
#[msg("User has no shares to vote with")]
NoSharesToVote,
}
// File: Axiom/programs/spectra-vault/src/events.rs
use anchor_lang::prelude::*;
#[event]
pub struct DepositEvent {
pub vault: Pubkey,
pub user: Pubkey,
pub amount: u64,
pub shares_minted: u64,
pub timestamp: i64,
}
#[event]
pub struct WithdrawEvent {
pub vault: Pubkey,
pub user: Pubkey,
pub shares_burned: u64,
pub amount_returned: u64,
pub timestamp: i64,
}
#[event]
pub struct NavSyncEvent {
pub vault: Pubkey,
pub old_total_assets: u64,
pub new_total_assets: u64,
pub pps: u64,
pub timestamp: i64,
}
#[event]
pub struct StrategyUpdateEvent {
pub vault: Pubkey,
pub strategy_type: u8,
pub timestamp: i64,
}
// Multisig events
#[event]
pub struct MultisigInitializedEvent {
pub vault: Pubkey,
pub signers: Vec<Pubkey>,
pub threshold: u8,
pub timestamp: i64,
}
#[event]
pub struct OperationProposedEvent {
pub vault: Pubkey,
pub operation_id: u64,
pub operation_type: u8,
pub proposed_at: i64,
pub executable_at: i64,
pub proposer: Pubkey,
}
#[event]
pub struct OperationApprovedEvent {
pub vault: Pubkey,
pub operation_id: u64,
pub approver: Pubkey,
pub approvals_count: u8,
pub threshold: u8,
}
#[event]
pub struct OperationExecutedEvent {
pub vault: Pubkey,
pub operation_id: u64,
pub operation_type: u8,
pub executed_at: i64,
}
#[event]
pub struct OperationCancelledEvent {
pub vault: Pubkey,
pub operation_id: u64,
pub cancelled_by: Pubkey,
pub timestamp: i64,
}
// Governance events
#[event]
pub struct StrategyProposalCreatedEvent {
pub vault: Pubkey,
pub proposal_id: u64,
pub change_type: u8,
pub new_value: u64,
pub voting_ends_at: i64,
}
#[event]
pub struct VoteCastEvent {
pub vault: Pubkey,
pub proposal_id: u64,
pub voter: Pubkey,
pub voted_for: bool,
pub vote_weight: u64,
}
#[event]
pub struct StrategyProposalExecutedEvent {
pub vault: Pubkey,
pub proposal_id: u64,
pub change_type: u8,
pub new_value: u64,
pub votes_for: u64,
pub votes_against: u64,
}
// File: Axiom/programs/spectra-vault/src/instructions/approve_operation.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::OperationApprovedEvent;
use crate::state::{MultisigConfig, PendingOperation};
use crate::ix_accounts::ApproveOperation;
pub fn handler(ctx: Context<ApproveOperation>) -> Result<()> {
let multisig = &ctx.accounts.multisig_config;
let pending_op = &mut ctx.accounts.pending_operation;
let approver = &ctx.accounts.approver;
// Verify operation is still pending
require!(
!pending_op.is_executed && !pending_op.is_cancelled,
SpectraError::OperationNotFound
);
// Verify approver is an authorized signer
require!(
multisig.signers.contains(&approver.key()),
SpectraError::UnauthorizedSigner
);
// Check if already approved
require!(
!pending_op.approvals.contains(&approver.key()),
SpectraError::AlreadyApproved
);
// Add approval
pending_op.approvals.push(approver.key());
let approvals_count = pending_op.approvals.len() as u8;
emit!(OperationApprovedEvent {
vault: pending_op.vault,
operation_id: pending_op.operation_id,
approver: approver.key(),
approvals_count,
threshold: multisig.threshold,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/cancel_operation.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::OperationCancelledEvent;
use crate::state::{MultisigConfig, PendingOperation};
use crate::ix_accounts::CancelOperation;
pub fn handler(ctx: Context<CancelOperation>) -> Result<()> {
let multisig = &ctx.accounts.multisig_config;
let pending_op = &mut ctx.accounts.pending_operation;
let canceller = &ctx.accounts.canceller;
// Verify operation is still pending
require!(
!pending_op.is_executed && !pending_op.is_cancelled,
SpectraError::OperationNotFound
);
// Verify canceller is an authorized signer (any signer can cancel)
require!(
multisig.signers.contains(&canceller.key()),
SpectraError::UnauthorizedSigner
);
pending_op.is_cancelled = true;
emit!(OperationCancelledEvent {
vault: pending_op.vault,
operation_id: pending_op.operation_id,
cancelled_by: canceller.key(),
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/collect_fee.rs
use anchor_lang::prelude::*;
use anchor_spl::token_interface::{self, MintTo};
use crate::constants::{BPS_DENOMINATOR, PPS_PRECISION};
use crate::errors::SpectraError;
use crate::ix_accounts::CollectPerformanceFee;
pub fn handler(ctx: Context<CollectPerformanceFee>) -> Result<()> {
let vault = &ctx.accounts.vault;
require!(vault.total_shares > 0, SpectraError::NoFeeToCollect);
let total_assets = vault.total_assets as u128;
let total_shares = vault.total_shares as u128;
let hwm = vault.high_water_mark as u128;
let fee_bps = vault.performance_fee_bps as u128;
// PPS = total_assets * 10^9 / total_shares (6-decimal fixed-point)
let current_pps = total_assets
.checked_mul(PPS_PRECISION)
.ok_or(SpectraError::MathOverflow)?
.checked_div(total_shares)
.ok_or(SpectraError::MathOverflow)?;
require!(current_pps > hwm, SpectraError::NoFeeToCollect);
// profit_per_share (in 6-decimal fixed-point)
let profit_per_share = current_pps
.checked_sub(hwm)
.ok_or(SpectraError::MathOverflow)?;
// Total profit in asset base units
let total_profit = profit_per_share
.checked_mul(total_shares)
.ok_or(SpectraError::MathOverflow)?
.checked_div(PPS_PRECISION)
.ok_or(SpectraError::MathOverflow)?;
// Fee amount in asset base units
let fee_amount = total_profit
.checked_mul(fee_bps)
.ok_or(SpectraError::MathOverflow)?
.checked_div(BPS_DENOMINATOR)
.ok_or(SpectraError::MathOverflow)?;
require!(fee_amount > 0, SpectraError::NoFeeToCollect);
// fee_shares = fee_amount * total_shares / (total_assets - fee_amount)
// This formula ensures the minted shares are worth exactly fee_amount after dilution
let denominator = total_assets
.checked_sub(fee_amount)
.ok_or(SpectraError::MathOverflow)?;
let fee_shares_u128 = fee_amount
.checked_mul(total_shares)
.ok_or(SpectraError::MathOverflow)?
.checked_div(denominator)
.ok_or(SpectraError::MathOverflow)?;
let fee_shares = u64::try_from(fee_shares_u128).map_err(|_| SpectraError::MathOverflow)?;
require!(fee_shares > 0, SpectraError::NoFeeToCollect);
// Mint fee shares to authority (Token-2022)
let vault_id_bytes = ctx.accounts.vault.vault_id.to_le_bytes();
let seeds: &[&[u8]] = &[b"vault", vault_id_bytes.as_ref(), &[ctx.accounts.vault.bump]];
let signer_seeds: &[&[&[u8]]] = &[seeds];
let mint_ctx = CpiContext::new_with_signer(
ctx.accounts.token_2022_program.to_account_info(),
MintTo {
mint: ctx.accounts.shares_mint.to_account_info(),
to: ctx.accounts.authority_shares_account.to_account_info(),
authority: ctx.accounts.vault.to_account_info(),
},
signer_seeds,
);
token_interface::mint_to(mint_ctx, fee_shares)?;
// Update state
let vault = &mut ctx.accounts.vault;
vault.total_shares = vault
.total_shares
.checked_add(fee_shares)
.ok_or(SpectraError::MathOverflow)?;
// Set HWM to current PPS (post-dilution PPS equals pre-dilution PPS by construction)
let new_pps = (vault.total_assets as u128)
.checked_mul(PPS_PRECISION)
.ok_or(SpectraError::MathOverflow)?
.checked_div(vault.total_shares as u128)
.ok_or(SpectraError::MathOverflow)?;
vault.high_water_mark = u64::try_from(new_pps).map_err(|_| SpectraError::MathOverflow)?;
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/deposit.rs
use anchor_lang::prelude::*;
use anchor_spl::token_interface::{self, MintTo, TransferChecked};
use crate::constants::DECIMAL_ADJUSTMENT;
use crate::errors::SpectraError;
use crate::events::DepositEvent;
use crate::ix_accounts::Deposit;
pub fn handler(ctx: Context<Deposit>, amount: u64) -> Result<()> {
require!(amount > 0, SpectraError::InvalidAmount);
let vault = &ctx.accounts.vault;
let total_assets = vault.total_assets;
let total_shares = vault.total_shares;
let shares_to_mint: u64 = if total_shares == 0 {
amount
.checked_mul(DECIMAL_ADJUSTMENT)
.ok_or(SpectraError::MathOverflow)?
} else {
let numerator = (amount as u128)
.checked_mul(total_shares as u128)
.ok_or(SpectraError::MathOverflow)?;
let result = numerator
.checked_div(total_assets as u128)
.ok_or(SpectraError::MathOverflow)?;
u64::try_from(result).map_err(|_| SpectraError::MathOverflow)?
};
require!(shares_to_mint > 0, SpectraError::InvalidAmount);
// Transfer USDC from user → asset_vault
let transfer_ctx = CpiContext::new(
ctx.accounts.token_program.to_account_info(),
TransferChecked {
from: ctx.accounts.user_asset_account.to_account_info(),
mint: ctx.accounts.asset_mint.to_account_info(),
to: ctx.accounts.asset_vault.to_account_info(),
authority: ctx.accounts.user.to_account_info(),
},
);
token_interface::transfer_checked(transfer_ctx, amount, ctx.accounts.asset_mint.decimals)?;
// Mint vault shares to user (Token-2022)
let vault_id_bytes = ctx.accounts.vault.vault_id.to_le_bytes();
let seeds: &[&[u8]] = &[b"vault", vault_id_bytes.as_ref(), &[ctx.accounts.vault.bump]];
let signer_seeds: &[&[&[u8]]] = &[seeds];
let mint_ctx = CpiContext::new_with_signer(
ctx.accounts.token_2022_program.to_account_info(),
MintTo {
mint: ctx.accounts.shares_mint.to_account_info(),
to: ctx.accounts.user_shares_account.to_account_info(),
authority: ctx.accounts.vault.to_account_info(),
},
signer_seeds,
);
token_interface::mint_to(mint_ctx, shares_to_mint)?;
// Update vault state
let vault = &mut ctx.accounts.vault;
vault.total_assets = vault
.total_assets
.checked_add(amount)
.ok_or(SpectraError::MathOverflow)?;
vault.total_shares = vault
.total_shares
.checked_add(shares_to_mint)
.ok_or(SpectraError::MathOverflow)?;
emit!(DepositEvent {
vault: vault.key(),
user: ctx.accounts.user.key(),
amount,
shares_minted: shares_to_mint,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/execute_operation.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::{OperationExecutedEvent, NavSyncEvent};
use crate::state::{MultisigConfig, PendingOperation, VaultState, OperationType};
use crate::constants::PPS_PRECISION;
use crate::ix_accounts::ExecuteOperation;
pub fn handler(ctx: Context<ExecuteOperation>) -> Result<()> {
let multisig = &ctx.accounts.multisig_config;
let pending_op = &mut ctx.accounts.pending_operation;
let vault = &mut ctx.accounts.vault;
// Verify operation is still pending
require!(
!pending_op.is_executed && !pending_op.is_cancelled,
SpectraError::OperationNotFound
);
// Check threshold met
require!(
pending_op.approvals.len() >= multisig.threshold as usize,
SpectraError::ThresholdNotMet
);
// Check timelock expired
let current_time = Clock::get()?.unix_timestamp;
require!(
current_time >= pending_op.executable_at,
SpectraError::TimelockActive
);
// Execute based on operation type
match pending_op.operation_type {
OperationType::SyncNav => {
// For SyncNav, new_total_assets is stored in operation_id
let new_total_assets = pending_op.operation_id; // Using operation_id as a workaround for data
execute_sync_nav(vault, new_total_assets)?;
},
OperationType::Pause => {
vault.is_paused = true;
},
OperationType::Unpause => {
vault.is_paused = false;
},
OperationType::CollectFee => {
// Collect fee logic would go here
// For now, we just mark as executed
},
OperationType::EmergencyWithdraw => {
// Emergency withdrawal logic
// Would require additional accounts
},
}
pending_op.is_executed = true;
emit!(OperationExecutedEvent {
vault: vault.key(),
operation_id: pending_op.operation_id,
operation_type: pending_op.operation_type as u8,
executed_at: current_time,
});
Ok(())
}
fn execute_sync_nav(vault: &mut Account<VaultState>, new_total_assets: u64) -> Result<()> {
let old_total_assets = vault.total_assets;
vault.total_assets = new_total_assets;
let pps: u64 = if vault.total_shares > 0 {
let num = (new_total_assets as u128)
.checked_mul(PPS_PRECISION)
.ok_or(SpectraError::MathOverflow)?;
let result = num
.checked_div(vault.total_shares as u128)
.ok_or(SpectraError::MathOverflow)?;
u64::try_from(result).map_err(|_| SpectraError::MathOverflow)?
} else {
0
};
emit!(NavSyncEvent {
vault: vault.key(),
old_total_assets,
new_total_assets,
pps,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/execute_strategy_change.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::StrategyProposalExecutedEvent;
use crate::state::{StrategyProposal, StrategyConfig, VaultState, StrategyChangeType, GOVERNANCE_QUORUM_BPS};
use crate::ix_accounts::ExecuteStrategyChange;
pub fn handler(ctx: Context<ExecuteStrategyChange>) -> Result<()> {
let proposal = &mut ctx.accounts.strategy_proposal;
let strategy_config = &mut ctx.accounts.strategy_config;
let vault = &ctx.accounts.vault;
// Check voting period has ended
let current_time = Clock::get()?.unix_timestamp;
require!(
current_time >= proposal.voting_ends_at,
SpectraError::VotingActive
);
// Check proposal not already decided
require!(!proposal.is_executed, SpectraError::AlreadyExecuted);
require!(!proposal.is_rejected, SpectraError::ProposalRejected);
// Check quorum (minimum 20% of total supply must vote)
let total_votes = proposal.votes_for.saturating_add(proposal.votes_against);
// In a real implementation, check against total shares outstanding
// For now, we require more votes for than against
require!(
proposal.votes_for > proposal.votes_against,
SpectraError::QuorumNotReached
);
// Apply the strategy change
match proposal.change_type {
StrategyChangeType::MaxPositionSize => {
strategy_config.max_position_pct = proposal.new_value as u16;
},
StrategyChangeType::LendingAllocation => {
strategy_config.lend_allocation_pct = proposal.new_value as u16;
},
StrategyChangeType::ProbabilityRange => {
// Would need two values for min/max
// For now, just update max
strategy_config.max_probability = proposal.new_value as u16;
},
StrategyChangeType::Categories => {
// Would require more complex handling
// Not implemented in this simplified version
},
}
proposal.is_executed = true;
emit!(StrategyProposalExecutedEvent {
vault: vault.key(),
proposal_id: proposal.proposal_id,
change_type: proposal.change_type as u8,
new_value: proposal.new_value,
votes_for: proposal.votes_for,
votes_against: proposal.votes_against,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/initialize.rs
use anchor_lang::prelude::*;
use crate::constants::INITIAL_HWM;
use crate::errors::SpectraError;
use crate::ix_accounts::{BootstrapVault, CreateAssetVault};
/// Step 1/2: vault state + Token-2022 shares mint. Does not include legacy
/// `token_program` on the account struct to keep BPF stack under 4KiB.
pub fn bootstrap_vault_handler(
ctx: Context<BootstrapVault>,
vault_id: u64,
strategy_type: u8,
performance_fee_bps: u16,
) -> Result<()> {
require!(strategy_type <= 2, SpectraError::InvalidStrategyType);
require!(performance_fee_bps <= 5000, SpectraError::InvalidFeeBps);
let vault = &mut ctx.accounts.vault;
let (asset_vault_pda, _) = Pubkey::find_program_address(
&[b"asset_vault", vault.key().as_ref()],
ctx.program_id,
);
vault.authority = ctx.accounts.admin.key();
vault.asset_mint = ctx.accounts.asset_mint.key();
vault.shares_mint = ctx.accounts.shares_mint.key();
vault.asset_vault = asset_vault_pda;
vault.total_assets = 0;
vault.total_shares = 0;
vault.vault_id = vault_id;
vault.strategy_type = strategy_type;
vault.high_water_mark = INITIAL_HWM;
vault.performance_fee_bps = performance_fee_bps;
vault.is_paused = false;
vault.bump = ctx.bumps.vault;
Ok(())
}
/// Step 2/2: SPL token account holding USDC reserves (legacy Token program).
pub fn create_asset_vault_handler(ctx: Context<CreateAssetVault>) -> Result<()> {
let _vault = &ctx.accounts.vault;
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/initialize_multisig.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::MultisigInitializedEvent;
use crate::state::{MultisigConfig, VaultState, MAX_MULTISIG_SIGNERS, MAX_MULTISIG_THRESHOLD};
use crate::ix_accounts::InitializeMultisig;
pub fn handler(
ctx: Context<InitializeMultisig>,
signers: Vec<Pubkey>,
threshold: u8,
) -> Result<()> {
// Validate inputs
require!(
signers.len() <= MAX_MULTISIG_SIGNERS,
SpectraError::TooManySigners
);
require!(
threshold > 0 && threshold <= MAX_MULTISIG_THRESHOLD,
SpectraError::InvalidThreshold
);
require!(
threshold as usize <= signers.len(),
SpectraError::InvalidThreshold
);
// Check for duplicates
let mut unique_signers = signers.clone();
unique_signers.sort();
unique_signers.dedup();
require!(
unique_signers.len() == signers.len(),
SpectraError::DuplicateSigner
);
let vault = &ctx.accounts.vault;
let multisig = &mut ctx.accounts.multisig_config;
multisig.vault = vault.key();
multisig.signers = signers.clone();
multisig.threshold = threshold;
multisig.bump = ctx.bumps.multisig_config;
emit!(MultisigInitializedEvent {
vault: vault.key(),
signers,
threshold,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/initialize_strategy.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::StrategyUpdateEvent;
use crate::ix_accounts::InitializeStrategyConfig;
use crate::state::StrategyConfig;
pub fn handler(
ctx: Context<InitializeStrategyConfig>,
min_probability: u16,
max_probability: u16,
max_position_pct: u16,
lend_allocation_pct: u16,
categories: Vec<String>,
) -> Result<()> {
require!(
min_probability <= max_probability,
SpectraError::InvalidProbabilityRange
);
require!(
categories.len() <= StrategyConfig::MAX_CATEGORIES,
SpectraError::TooManyCategories
);
for cat in &categories {
require!(
cat.len() <= StrategyConfig::MAX_CATEGORY_LEN,
SpectraError::CategoryTooLong
);
}
let vault = &ctx.accounts.vault;
let strategy = &mut ctx.accounts.strategy_config;
strategy.vault = vault.key();
strategy.min_probability = min_probability;
strategy.max_probability = max_probability;
strategy.max_position_pct = max_position_pct;
strategy.lend_allocation_pct = lend_allocation_pct;
strategy.categories = categories;
strategy.is_active = true;
strategy.bump = ctx.bumps.strategy_config;
emit!(StrategyUpdateEvent {
vault: vault.key(),
strategy_type: vault.strategy_type,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/mod.rs
pub mod approve_operation;
pub mod cancel_operation;
pub mod collect_fee;
pub mod deposit;
pub mod execute_operation;
pub mod execute_strategy_change;
pub mod initialize;
pub mod initialize_multisig;
pub mod initialize_strategy;
pub mod pause;
pub mod propose_operation;
pub mod propose_strategy_change;
pub mod sync_nav;
pub mod vote_on_strategy;
pub mod withdraw;
// File: Axiom/programs/spectra-vault/src/instructions/pause.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::ix_accounts::{Pause, Unpause};
pub fn handler(ctx: Context<Pause>) -> Result<()> {
let vault = &mut ctx.accounts.vault;
require!(!vault.is_paused, SpectraError::VaultPaused);
vault.is_paused = true;
Ok(())
}
pub fn unpause_handler(ctx: Context<Unpause>) -> Result<()> {
let vault = &mut ctx.accounts.vault;
require!(vault.is_paused, SpectraError::VaultNotPaused);
vault.is_paused = false;
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/propose_operation.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::OperationProposedEvent;
use crate::state::{MultisigConfig, OperationType, PendingOperation, VaultState, TIMELOCK_DURATION};
use crate::ix_accounts::ProposeOperation;
pub fn handler(
ctx: Context<ProposeOperation>,
operation_id: u64,
operation_type: OperationType,
) -> Result<()> {
let vault = &ctx.accounts.vault;
let multisig = &ctx.accounts.multisig_config;
let proposer = &ctx.accounts.proposer;
let pending_op = &mut ctx.accounts.pending_operation;
// Verify proposer is an authorized signer
require!(
multisig.signers.contains(&proposer.key()),
SpectraError::UnauthorizedSigner
);
let current_time = Clock::get()?.unix_timestamp;
// Initialize the pending operation
pending_op.vault = vault.key();
pending_op.operation_id = operation_id;
pending_op.operation_type = operation_type;
pending_op.proposed_at = current_time;
pending_op.executable_at = current_time + TIMELOCK_DURATION;
pending_op.is_executed = false;
pending_op.is_cancelled = false;
pending_op.approvals = vec![proposer.key()]; // Proposer automatically approves
pending_op.bump = ctx.bumps.pending_operation;
let operation_type_u8 = operation_type as u8;
emit!(OperationProposedEvent {
vault: vault.key(),
operation_id,
operation_type: operation_type_u8,
proposed_at: current_time,
executable_at: pending_op.executable_at,
proposer: proposer.key(),
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/propose_strategy_change.rs
use anchor_lang::prelude::*;
use crate::errors::SpectraError;
use crate::events::StrategyProposalCreatedEvent;
use crate::state::{StrategyProposal, VaultState, StrategyChangeType, GOVERNANCE_QUORUM_BPS};
use crate::ix_accounts::ProposeStrategyChange;
pub fn handler(
ctx: Context<ProposeStrategyChange>,
proposal_id: u64,
change_type: StrategyChangeType,
new_value: u64,
voting_duration_seconds: i64,
) -> Result<()> {
require!(
voting_duration_seconds >= 24 * 60 * 60, // Minimum 24 hour voting period
SpectraError::InvalidChangeType
);
let vault = &ctx.accounts.vault;
let proposal = &mut ctx.accounts.strategy_proposal;
let current_time = Clock::get()?.unix_timestamp;
proposal.vault = vault.key();
proposal.proposal_id = proposal_id;
proposal.change_type = change_type;
proposal.new_value = new_value;
proposal.voting_ends_at = current_time + voting_duration_seconds;
proposal.votes_for = 0;
proposal.votes_against = 0;
proposal.is_executed = false;
proposal.is_rejected = false;
proposal.bump = ctx.bumps.strategy_proposal;
emit!(StrategyProposalCreatedEvent {
vault: vault.key(),
proposal_id,
change_type: change_type as u8,
new_value,
voting_ends_at: proposal.voting_ends_at,
});
Ok(())
}
// File: Axiom/programs/spectra-vault/src/instructions/sync_nav.rs
use anchor_lang::prelude::*;
use crate::constants::PPS_PRECISION;
use crate::errors::SpectraError;
use crate::events::NavSyncEvent;
use crate::ix_accounts::SyncNav;
pub fn handler(ctx: Context<SyncNav>, new_total_assets: u64) -> Result<()> {
let vault = &mut ctx.accounts.vault;
let old_total_assets = vault.total_assets;
vault.total_assets = new_total_assets;
let pps: u64 = if vault.total_shares > 0 {
let num = (new_total_assets as u128)