1use solana_program::pubkey::Pubkey;
4use spl_associated_token_account::{
5 get_associated_token_address, get_associated_token_address_with_program_id,
6};
7use steel::*;
8
9use crate::{
10 consts::{EXECUTOR_ADDRESS, FEE_COLLECTOR, USDC_MAINNET_MINT},
11 instruction::{AdminPayout, AdminRouteFees, BuyTicket, Initialize},
12 state::{Treasury, User},
13};
14
15#[inline(always)]
16pub fn program_id() -> Pubkey {
17 crate::ID
18}
19
20pub fn treasury_usdc_ata() -> Pubkey {
23 get_associated_token_address(&Treasury::pda().0, &USDC_MAINNET_MINT)
24}
25
26pub fn treasury_usdc_ata_with_token_program(token_program: &Pubkey) -> Pubkey {
27 get_associated_token_address_with_program_id(
28 &Treasury::pda().0,
29 &USDC_MAINNET_MINT,
30 token_program,
31 )
32}
33
34pub fn associated_usdc_ata(owner: Pubkey, token_program: &Pubkey) -> Pubkey {
35 get_associated_token_address_with_program_id(&owner, &USDC_MAINNET_MINT, token_program)
36}
37
38pub fn usdc_ata(owner: Pubkey) -> Pubkey {
39 get_associated_token_address(&owner, &USDC_MAINNET_MINT)
40}
41
42pub fn fee_collector_usdc_ata_with_token_program(token_program: &Pubkey) -> Pubkey {
43 get_associated_token_address_with_program_id(&FEE_COLLECTOR, &USDC_MAINNET_MINT, token_program)
44}
45
46pub fn initialize(payer: Pubkey, token_program: Pubkey) -> Instruction {
50 let treasury_address = Treasury::pda().0;
51 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
52 Instruction {
53 program_id: crate::ID,
54 accounts: vec![
55 AccountMeta::new(payer, true),
56 AccountMeta::new(treasury_address, false),
57 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
58 AccountMeta::new(treasury_ata, false),
59 AccountMeta::new_readonly(system_program::ID, false),
60 AccountMeta::new_readonly(token_program, false),
61 AccountMeta::new_readonly(spl_associated_token_account::ID, false),
62 ],
63 data: Initialize {}.to_bytes(),
64 }
65}
66
67pub fn buy_ticket(user: Pubkey, amount: u64, token_program: Pubkey) -> Instruction {
74 let user_ata = associated_usdc_ata(user, &token_program);
75 let treasury_address = Treasury::pda().0;
76 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
77 let fee_collector_ata = fee_collector_usdc_ata_with_token_program(&token_program);
78 let credits = User::pda(&user).0;
79 Instruction {
80 program_id: crate::ID,
81 accounts: vec![
82 AccountMeta::new(user, true),
83 AccountMeta::new(user_ata, false),
84 AccountMeta::new(treasury_address, false),
85 AccountMeta::new(treasury_ata, false),
86 AccountMeta::new(credits, false),
87 AccountMeta::new(fee_collector_ata, false),
88 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
89 AccountMeta::new_readonly(token_program, false),
90 AccountMeta::new_readonly(system_program::ID, false),
91 ],
92 data: BuyTicket {
93 amount: amount.to_le_bytes(),
94 }
95 .to_bytes(),
96 }
97}
98
99pub fn route_fees(executor: Pubkey, amount: u64, token_program: Pubkey) -> Instruction {
101 let treasury_address = Treasury::pda().0;
102 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
103 let executor_ata = associated_usdc_ata(executor, &token_program);
104 let fee_collector_ata = fee_collector_usdc_ata_with_token_program(&token_program);
105 Instruction {
106 program_id: crate::ID,
107 accounts: vec![
108 AccountMeta::new(executor, true),
109 AccountMeta::new(executor_ata, false),
110 AccountMeta::new(treasury_address, false),
111 AccountMeta::new(treasury_ata, false),
112 AccountMeta::new(fee_collector_ata, false),
113 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
114 AccountMeta::new_readonly(token_program, false),
115 ],
116 data: AdminRouteFees {
117 amount: amount.to_le_bytes(),
118 }
119 .to_bytes(),
120 }
121}
122
123pub fn route_fees_default_executor(amount: u64, token_program: Pubkey) -> Instruction {
125 route_fees(EXECUTOR_ADDRESS, amount, token_program)
126}
127
128pub fn payout(
130 executor: Pubkey,
131 recipient_ata: Pubkey,
132 amount: u64,
133 series_id: u16,
134 period: u64,
135 token_program: Pubkey,
136) -> Instruction {
137 let treasury_address = Treasury::pda().0;
138 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
139 Instruction {
140 program_id: crate::ID,
141 accounts: vec![
142 AccountMeta::new(executor, true),
143 AccountMeta::new(treasury_address, false),
144 AccountMeta::new(treasury_ata, false),
145 AccountMeta::new(recipient_ata, false),
146 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
147 AccountMeta::new_readonly(token_program, false),
148 ],
149 data: AdminPayout {
150 amount: amount.to_le_bytes(),
151 series_id: series_id.to_le_bytes(),
152 _pad_ix: [0; 6],
153 period: period.to_le_bytes(),
154 }
155 .to_bytes(),
156 }
157}