1use solana_program::{
2 account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed,
3};
4
5pub fn spl_token_burn(params: TokenBurnParams<'_, '_>) -> ProgramResult {
6 let TokenBurnParams {
7 mint,
8 source,
9 authority,
10 token_program,
11 amount,
12 authority_signer_seeds,
13 } = params;
14 let mut seeds: Vec<&[&[u8]]> = vec![];
15 if let Some(seed) = authority_signer_seeds {
16 seeds.push(seed);
17 }
18 invoke_signed(
19 &spl_token_2022::instruction::burn(
20 token_program.key,
21 source.key,
22 mint.key,
23 authority.key,
24 &[authority.key],
25 amount,
26 )?,
27 &[source, mint, authority],
28 seeds.as_slice(),
29 )
30}
31
32pub struct TokenBurnParams<'a: 'b, 'b> {
34 pub mint: AccountInfo<'a>,
36 pub source: AccountInfo<'a>,
38 pub amount: u64,
40 pub authority: AccountInfo<'a>,
42 pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
44 pub token_program: AccountInfo<'a>,
46}
47
48pub fn spl_token_close(params: TokenCloseParams<'_, '_>) -> ProgramResult {
49 let TokenCloseParams {
50 account,
51 destination,
52 owner,
53 authority_signer_seeds,
54 token_program,
55 } = params;
56 let mut seeds: Vec<&[&[u8]]> = vec![];
57 if let Some(seed) = authority_signer_seeds {
58 seeds.push(seed);
59 }
60 invoke_signed(
61 &spl_token_2022::instruction::close_account(
62 token_program.key,
63 account.key,
64 destination.key,
65 owner.key,
66 &[],
67 )?,
68 &[account, destination, owner, token_program],
69 seeds.as_slice(),
70 )
71}
72
73pub struct TokenCloseParams<'a: 'b, 'b> {
75 pub account: AccountInfo<'a>,
77 pub destination: AccountInfo<'a>,
79 pub owner: AccountInfo<'a>,
81 pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
83 pub token_program: AccountInfo<'a>,
85}
86
87pub fn spl_token_mint_to(params: TokenMintToParams<'_, '_>) -> ProgramResult {
88 let TokenMintToParams {
89 mint,
90 destination,
91 authority,
92 token_program,
93 amount,
94 authority_signer_seeds,
95 } = params;
96 let mut seeds: Vec<&[&[u8]]> = vec![];
97 if let Some(seed) = authority_signer_seeds {
98 seeds.push(seed);
99 }
100 invoke_signed(
101 &spl_token_2022::instruction::mint_to(
102 token_program.key,
103 mint.key,
104 destination.key,
105 authority.key,
106 &[],
107 amount,
108 )?,
109 &[mint, destination, authority, token_program],
110 seeds.as_slice(),
111 )
112}
113
114pub struct TokenMintToParams<'a: 'b, 'b> {
116 pub mint: AccountInfo<'a>,
118 pub destination: AccountInfo<'a>,
120 pub amount: u64,
122 pub authority: AccountInfo<'a>,
124 pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
126 pub token_program: AccountInfo<'a>,
128}
129
130#[allow(deprecated)]
131pub fn spl_token_transfer(params: TokenTransferParams<'_, '_>) -> ProgramResult {
132 let TokenTransferParams {
133 source,
134 destination,
135 amount,
136 authority,
137 token_program,
138 authority_signer_seeds,
139 ..
140 } = params;
141 let seeds = if let Some(seeds) = authority_signer_seeds {
142 seeds
143 } else {
144 &[]
145 };
146
147 invoke_signed(
148 &spl_token_2022::instruction::transfer(
149 token_program.key,
150 source.key,
151 destination.key,
152 authority.key,
153 &[authority.key],
154 amount,
155 )?,
156 &[source, destination, authority],
157 &[seeds],
158 )
159}
160
161#[derive(Debug)]
163pub struct TokenTransferParams<'a: 'b, 'b> {
164 pub mint: AccountInfo<'a>,
166 pub source: AccountInfo<'a>,
168 pub destination: AccountInfo<'a>,
170 pub amount: u64,
172 pub authority: AccountInfo<'a>,
174 pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
176 pub token_program: AccountInfo<'a>,
178}
179
180pub fn spl_token_transfer_checked(params: TokenTransferCheckedParams<'_, '_>) -> ProgramResult {
181 let TokenTransferCheckedParams {
182 mint,
183 source,
184 destination,
185 amount,
186 authority,
187 token_program,
188 authority_signer_seeds,
189 decimals,
190 } = params;
191 let seeds = if let Some(seeds) = authority_signer_seeds {
192 seeds
193 } else {
194 &[]
195 };
196
197 invoke_signed(
198 &spl_token_2022::instruction::transfer_checked(
199 token_program.key,
200 source.key,
201 mint.key,
202 destination.key,
203 authority.key,
204 &[authority.key],
205 amount,
206 decimals,
207 )?,
208 &[source, mint, destination, authority],
209 &[seeds],
210 )
211}
212
213#[derive(Debug)]
215pub struct TokenTransferCheckedParams<'a: 'b, 'b> {
216 pub mint: AccountInfo<'a>,
218 pub source: AccountInfo<'a>,
220 pub destination: AccountInfo<'a>,
222 pub amount: u64,
224 pub authority: AccountInfo<'a>,
226 pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
228 pub token_program: AccountInfo<'a>,
230 pub decimals: u8,
232}