Skip to main content

mpl_utils/token/
cpi.rs

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
32/// TokenBurnParams
33pub struct TokenBurnParams<'a: 'b, 'b> {
34    /// mint
35    pub mint: AccountInfo<'a>,
36    /// source
37    pub source: AccountInfo<'a>,
38    /// amount
39    pub amount: u64,
40    /// authority
41    pub authority: AccountInfo<'a>,
42    /// authority_signer_seeds
43    pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
44    /// token_program
45    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
73/// TokenCloseParams
74pub struct TokenCloseParams<'a: 'b, 'b> {
75    /// Token account
76    pub account: AccountInfo<'a>,
77    /// Destination for redeemed SOL.
78    pub destination: AccountInfo<'a>,
79    /// Owner of the token account.
80    pub owner: AccountInfo<'a>,
81    /// authority_signer_seeds
82    pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
83    /// token_program
84    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
114/// TokenMintToParams
115pub struct TokenMintToParams<'a: 'b, 'b> {
116    /// mint
117    pub mint: AccountInfo<'a>,
118    /// destination
119    pub destination: AccountInfo<'a>,
120    /// amount
121    pub amount: u64,
122    /// authority
123    pub authority: AccountInfo<'a>,
124    /// authority_signer_seeds
125    pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
126    /// token_program
127    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/// TokenTransferParams
162#[derive(Debug)]
163pub struct TokenTransferParams<'a: 'b, 'b> {
164    /// mint
165    pub mint: AccountInfo<'a>,
166    /// source
167    pub source: AccountInfo<'a>,
168    /// destination
169    pub destination: AccountInfo<'a>,
170    /// amount
171    pub amount: u64,
172    /// authority
173    pub authority: AccountInfo<'a>,
174    /// authority_signer_seeds
175    pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
176    /// token_program
177    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/// TokenTransferParams
214#[derive(Debug)]
215pub struct TokenTransferCheckedParams<'a: 'b, 'b> {
216    /// mint
217    pub mint: AccountInfo<'a>,
218    /// source
219    pub source: AccountInfo<'a>,
220    /// destination
221    pub destination: AccountInfo<'a>,
222    /// amount
223    pub amount: u64,
224    /// authority
225    pub authority: AccountInfo<'a>,
226    /// authority_signer_seeds
227    pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
228    /// token_program
229    pub token_program: AccountInfo<'a>,
230    /// decimals
231    pub decimals: u8,
232}