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, PlaceBet},
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 {
75 let user_ata = associated_usdc_ata(user, &token_program);
76 let treasury_address = Treasury::pda().0;
77 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
78 let user_pda = User::pda(&user).0;
79 let fee_collector_ata = fee_collector_usdc_ata_with_token_program(&token_program);
80 Instruction {
81 program_id: crate::ID,
82 accounts: vec![
83 AccountMeta::new(user, true),
84 AccountMeta::new(user_ata, false),
85 AccountMeta::new(treasury_address, false),
86 AccountMeta::new(treasury_ata, false),
87 AccountMeta::new(user_pda, false),
88 AccountMeta::new(fee_collector_ata, false),
89 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
90 AccountMeta::new_readonly(token_program, false),
91 AccountMeta::new_readonly(system_program::ID, false),
92 ],
93 data: BuyTicket {
94 amount: amount.to_le_bytes(),
95 }
96 .to_bytes(),
97 }
98}
99
100pub fn place_bet(user: Pubkey, side: u8, series_id: u16, period: u64) -> Instruction {
105 let user_pda = User::pda(&user).0;
106 Instruction {
107 program_id: crate::ID,
108 accounts: vec![
109 AccountMeta::new(user, true),
110 AccountMeta::new(user_pda, false),
111 ],
112 data: PlaceBet {
113 side,
114 _pad: [0; 1],
115 series_id: series_id.to_le_bytes(),
116 _pad2: [0; 4],
117 period: period.to_le_bytes(),
118 }
119 .to_bytes(),
120 }
121}
122
123pub fn route_fees(executor: Pubkey, amount: u64, token_program: Pubkey) -> Instruction {
125 let treasury_address = Treasury::pda().0;
126 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
127 let executor_ata = associated_usdc_ata(executor, &token_program);
128 let fee_collector_ata = fee_collector_usdc_ata_with_token_program(&token_program);
129 Instruction {
130 program_id: crate::ID,
131 accounts: vec![
132 AccountMeta::new(executor, true),
133 AccountMeta::new(executor_ata, false),
134 AccountMeta::new(treasury_address, false),
135 AccountMeta::new(treasury_ata, false),
136 AccountMeta::new(fee_collector_ata, false),
137 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
138 AccountMeta::new_readonly(token_program, false),
139 ],
140 data: AdminRouteFees {
141 amount: amount.to_le_bytes(),
142 }
143 .to_bytes(),
144 }
145}
146
147pub fn route_fees_default_executor(amount: u64, token_program: Pubkey) -> Instruction {
149 route_fees(EXECUTOR_ADDRESS, amount, token_program)
150}
151
152pub fn payout(
154 executor: Pubkey,
155 recipient_ata: Pubkey,
156 amount: u64,
157 series_id: u16,
158 period: u64,
159 token_program: Pubkey,
160) -> Instruction {
161 let treasury_address = Treasury::pda().0;
162 let treasury_ata = treasury_usdc_ata_with_token_program(&token_program);
163 Instruction {
164 program_id: crate::ID,
165 accounts: vec![
166 AccountMeta::new(executor, true),
167 AccountMeta::new(treasury_address, false),
168 AccountMeta::new(treasury_ata, false),
169 AccountMeta::new(recipient_ata, false),
170 AccountMeta::new_readonly(USDC_MAINNET_MINT, false),
171 AccountMeta::new_readonly(token_program, false),
172 ],
173 data: AdminPayout {
174 amount: amount.to_le_bytes(),
175 series_id: series_id.to_le_bytes(),
176 _pad_ix: [0; 6],
177 period: period.to_le_bytes(),
178 }
179 .to_bytes(),
180 }
181}