spl_managed_token/
token.rs

1use solana_program::{
2    account_info::AccountInfo,
3    entrypoint::ProgramResult,
4    program::{invoke, invoke_signed},
5    pubkey::Pubkey,
6};
7
8pub(crate) fn initialize_mint<'a, 'b>(
9    freeze_authority: &Pubkey,
10    mint_authority: &Pubkey,
11    mint: &'a AccountInfo<'b>,
12    token_program: &'a AccountInfo<'b>,
13    decimals: u8,
14) -> ProgramResult {
15    invoke(
16        &spl_token::instruction::initialize_mint2(
17            token_program.key,
18            mint.key,
19            mint_authority,
20            Some(freeze_authority),
21            decimals,
22        )?,
23        &[token_program.clone(), mint.clone()],
24    )
25}
26
27pub(crate) fn thaw<'a, 'b>(
28    freeze_authority: &'a AccountInfo<'b>,
29    mint: &'a AccountInfo<'b>,
30    target: &'a AccountInfo<'b>,
31    token_program: &'a AccountInfo<'b>,
32    seeds: &[Vec<u8>],
33) -> ProgramResult {
34    invoke_signed(
35        &spl_token::instruction::thaw_account(
36            token_program.key,
37            target.key,
38            mint.key,
39            freeze_authority.key,
40            &[],
41        )?,
42        &[
43            token_program.clone(),
44            mint.clone(),
45            freeze_authority.clone(),
46            target.clone(),
47        ],
48        &[&seeds.iter().map(|s| s.as_slice()).collect::<Vec<&[u8]>>()],
49    )
50}
51
52pub(crate) fn freeze<'a, 'b>(
53    freeze_authority: &'a AccountInfo<'b>,
54    mint: &'a AccountInfo<'b>,
55    target: &'a AccountInfo<'b>,
56    token_program: &'a AccountInfo<'b>,
57    seeds: &[Vec<u8>],
58) -> ProgramResult {
59    invoke_signed(
60        &spl_token::instruction::freeze_account(
61            token_program.key,
62            target.key,
63            mint.key,
64            freeze_authority.key,
65            &[],
66        )?,
67        &[
68            token_program.clone(),
69            mint.clone(),
70            freeze_authority.clone(),
71            target.clone(),
72        ],
73        &[&seeds.iter().map(|s| s.as_slice()).collect::<Vec<&[u8]>>()],
74    )
75}
76
77pub(crate) fn transfer<'a, 'b>(
78    src: &'a AccountInfo<'b>,
79    dst: &'a AccountInfo<'b>,
80    owner: &'a AccountInfo<'b>,
81    token_program: &'a AccountInfo<'b>,
82    amount: u64,
83) -> ProgramResult {
84    invoke(
85        &spl_token::instruction::transfer(
86            token_program.key,
87            src.key,
88            dst.key,
89            owner.key,
90            &[],
91            amount,
92        )?,
93        &[
94            token_program.clone(),
95            src.clone(),
96            dst.clone(),
97            owner.clone(),
98        ],
99    )
100}
101
102pub(crate) fn mint_to<'a, 'b>(
103    mint: &'a AccountInfo<'b>,
104    account: &'a AccountInfo<'b>,
105    owner: &'a AccountInfo<'b>,
106    token_program: &'a AccountInfo<'b>,
107    amount: u64,
108    seeds: &[Vec<u8>],
109) -> ProgramResult {
110    invoke_signed(
111        &spl_token::instruction::mint_to(
112            token_program.key,
113            mint.key,
114            account.key,
115            owner.key,
116            &[],
117            amount,
118        )?,
119        &[
120            token_program.clone(),
121            mint.clone(),
122            account.clone(),
123            owner.clone(),
124        ],
125        &[&seeds.iter().map(|s| s.as_slice()).collect::<Vec<&[u8]>>()],
126    )
127}
128
129pub(crate) fn burn<'a, 'b>(
130    mint: &'a AccountInfo<'b>,
131    account: &'a AccountInfo<'b>,
132    owner: &'a AccountInfo<'b>,
133    token_program: &'a AccountInfo<'b>,
134    amount: u64,
135) -> ProgramResult {
136    invoke(
137        &spl_token::instruction::burn(
138            token_program.key,
139            account.key,
140            mint.key,
141            owner.key,
142            &[],
143            amount,
144        )?,
145        &[
146            token_program.clone(),
147            mint.clone(),
148            account.clone(),
149            owner.clone(),
150        ],
151    )
152}
153
154pub(crate) fn approve<'a, 'b>(
155    account: &'a AccountInfo<'b>,
156    owner: &'a AccountInfo<'b>,
157    delegate: &'a AccountInfo<'b>,
158    token_program: &'a AccountInfo<'b>,
159    amount: u64,
160) -> ProgramResult {
161    invoke(
162        &spl_token::instruction::approve(
163            token_program.key,
164            account.key,
165            delegate.key,
166            owner.key,
167            &[],
168            amount,
169        )?,
170        &[
171            token_program.clone(),
172            account.clone(),
173            delegate.clone(),
174            owner.clone(),
175        ],
176    )
177}
178
179pub(crate) fn revoke<'a, 'b>(
180    account: &'a AccountInfo<'b>,
181    owner: &'a AccountInfo<'b>,
182    token_program: &'a AccountInfo<'b>,
183) -> ProgramResult {
184    invoke(
185        &spl_token::instruction::revoke(token_program.key, account.key, owner.key, &[])?,
186        &[token_program.clone(), account.clone(), owner.clone()],
187    )
188}
189
190pub(crate) fn close<'a, 'b>(
191    account: &'a AccountInfo<'b>,
192    destination: &'a AccountInfo<'b>,
193    owner: &'a AccountInfo<'b>,
194    token_program: &'a AccountInfo<'b>,
195) -> ProgramResult {
196    invoke(
197        &spl_token::instruction::close_account(
198            token_program.key,
199            account.key,
200            destination.key,
201            owner.key,
202            &[],
203        )?,
204        &[
205            token_program.clone(),
206            destination.clone(),
207            account.clone(),
208            owner.clone(),
209        ],
210    )
211}