spl_simplified/
simplespl.rs

1use anchor_lang::context::CpiContext;
2use anchor_lang::solana_program::account_info::AccountInfo;
3use anchor_lang::solana_program::pubkey::Pubkey;
4use anchor_lang::Key;
5use anchor_lang::{solana_program, Result};
6use mpl_token_metadata::types::{Creator, DataV2};
7use solana_program::program::invoke_signed;
8pub use spl_token::ID;
9
10use crate::metadata::{create_metadata_accounts_v3, CreateMetadataAccountsV3};
11
12/// Mints new SPL tokens with associated metadata.
13///
14/// This function creates metadata for a new token using the `mpl_token_metadata`
15/// program, and mints the specified number of tokens to the `to` account.
16///
17/// # Arguments
18///
19/// * `token_name` - The name of the token to be minted.
20/// * `token_symbol` - The symbol for the token.
21/// * `token_uri` - A URI pointing to the token's metadata (e.g., hosted image or metadata information).
22/// * `token_tax` - The seller fee basis points (bps) for token transactions.
23/// * `payer` - The account responsible for paying transaction fees.
24/// * `token_metadata_program` - The account for the token metadata program.
25/// * `update_authority` - The account authorized to update the token's metadata.
26/// * `metadata` - The metadata account for the token.
27/// * `mint_authority` - The account authorized to mint the tokens.
28/// * `system_program` - The system program account.
29/// * `rent` - The rent sysvar account.
30/// * `token_program` - The SPL token program account.
31/// * `mint` - The token mint account.
32/// * `to` - The account where the minted tokens will be transferred to.
33/// * `owner` - The owner of the `to` account.
34/// * `signer_seeds` - The seeds used to sign the transaction.
35/// * `amount` - The number of tokens to mint.
36///
37/// # Example
38///
39/// ```rust
40/// use simplespl::mint_simple;
41/// use anchor_lang::solana_program::account_info::AccountInfo;
42///
43/// let result = mint_simple(
44///     "TokenName".to_string(),
45///     "TKN".to_string(),
46///     "https://example.com/token-metadata".to_string(),
47///     500, // 5% seller fee
48///     payer_account_info,
49///     token_metadata_program_info,
50///     update_authority_info,
51///     metadata_account_info,
52///     mint_authority_info,
53///     system_program_info,
54///     rent_sysvar_info,
55///     token_program_info,
56///     mint_account_info,
57///     to_account_info,
58///     owner_account_info,
59///     &[&signer_seeds],
60///     1000 // Mint 1000 tokens
61/// ).unwrap();
62/// ```
63pub fn mint_simple<'info>(
64    token_name: String,
65    token_symbol: String,
66    token_uri: String,
67    token_tax: u16,
68    payer: AccountInfo<'info>,
69    token_metadata_program: AccountInfo<'info>,
70    update_authority: AccountInfo<'info>,
71    metadata: AccountInfo<'info>,
72    mint_authority: AccountInfo<'info>,
73    system_program: AccountInfo<'info>,
74    rent: AccountInfo<'info>,
75    token_program: AccountInfo<'info>,
76    mint: AccountInfo<'info>,
77    to: AccountInfo<'info>,
78    owner: AccountInfo<'info>,
79    signer_seeds: &[&[u8]],
80    amount: u64,
81) -> Result<()> {
82    metadata_thing(
83        token_name.clone(),
84        token_symbol.clone(),
85        token_uri.clone(),
86        token_tax.clone(),
87        payer,
88        token_metadata_program,
89        update_authority,
90        mint.clone(),
91        metadata,
92        mint_authority,
93        system_program,
94        rent,
95        &[&signer_seeds],
96    )?;
97
98    let ix = spl_token::instruction::mint_to(
99        &spl_token::ID,
100        &mint.key(),
101        &to.key(),
102        &owner.key(),
103        &[],
104        amount,
105    )?;
106
107    invoke_signed(
108        &ix,
109        &[mint.clone(), to.clone(), owner.clone(), token_program],
110        &[signer_seeds],
111    )
112    .map_err(Into::into)
113}
114
115/// Creates metadata for a token using the `mpl_token_metadata` program.
116///
117/// This function sets up the metadata for a token, including its name, symbol, URI,
118/// seller fee, and creator information. It also uses the provided `signer_seed` for
119/// signing the metadata creation transaction.
120///
121/// # Arguments
122///
123/// * `token_name` - The name of the token.
124/// * `token_symbol` - The symbol of the token.
125/// * `token_uri` - A URI that points to the token metadata.
126/// * `token_tax` - The seller fee in basis points (bps).
127/// * `payer` - The account paying for the transaction.
128/// * `token_metadata_program` - The token metadata program account.
129/// * `update_authority` - The account authorized to update the metadata.
130/// * `mint` - The mint account for the token.
131/// * `metadata` - The metadata account.
132/// * `mint_authority` - The account authorized to mint the tokens.
133/// * `system_program` - The system program account.
134/// * `rent` - The rent sysvar account.
135/// * `signer_seed` - A slice of slices of seeds for signing the metadata creation transaction.
136///
137/// # Example
138///
139/// ```rust
140/// use simplespl::metadata_thing;
141/// use anchor_lang::solana_program::account_info::AccountInfo;
142///
143/// metadata_thing(
144///     "TokenName".to_string(),
145///     "TKN".to_string(),
146///     "https://example.com/token-metadata".to_string(),
147///     500, // 5% seller fee
148///     payer_account_info,
149///     token_metadata_program_info,
150///     update_authority_info,
151///     mint_account_info,
152///     metadata_account_info,
153///     mint_authority_info,
154///     system_program_info,
155///     rent_sysvar_info,
156///     &[&signer_seed],
157/// ).unwrap();
158/// ```
159fn metadata_thing<'info>(
160    token_name: String,
161    token_symbol: String,
162    token_uri: String,
163    token_tax: u16,
164    payer: AccountInfo<'info>,
165    token_metadata_program: AccountInfo<'info>,
166    update_authority: AccountInfo<'info>,
167    mint: AccountInfo<'info>,
168    metadata: AccountInfo<'info>,
169    mint_authority: AccountInfo<'info>,
170    system_program: AccountInfo<'info>,
171    rent: AccountInfo<'info>,
172    signer_seed: &[&[&[u8]]],
173) -> Result<()> {
174    let mut creators: Vec<Creator> = Vec::<Creator>::new();
175    let creator: Creator = Creator {
176        address: mint.key(),
177        verified: true,
178        share: 100,
179    };
180
181    creators.push(creator);
182
183    let token_data: DataV2 = DataV2 {
184        name: token_name.clone(),
185        symbol: token_symbol,
186        uri: token_uri,
187        seller_fee_basis_points: token_tax,
188        creators: Some(creators),
189        collection: None,
190        uses: None,
191    };
192
193    let metadata_ctx = CpiContext::new_with_signer(
194        token_metadata_program,
195        CreateMetadataAccountsV3 {
196            payer,
197            update_authority,
198            mint,
199            metadata,
200            mint_authority,
201            system_program,
202            rent,
203        },
204        signer_seed,
205    );
206
207    create_metadata_accounts_v3(metadata_ctx, token_data, false, true, None).map_err(Into::into)
208}
209
210/// Transfers SPL tokens from one account to another.
211///
212/// This function facilitates the transfer of SPL tokens between accounts
213/// on the Solana blockchain. It uses the `spl_token::instruction::transfer` function
214/// to create the transfer instruction, and signs the transaction using the provided
215/// `signer_seeds`.
216///
217/// # Arguments
218///
219/// * `mint` - The mint account of the token.
220/// * `token_program_id` - The token program account (usually `spl_token`).
221/// * `source_pubkey` - The source account's public key from which tokens will be transferred.
222/// * `destination_pubkey` - The destination account's `AccountInfo`.
223/// * `authority_pubkey` - The authority account that will sign the transfer.
224/// * `amount` - The amount of tokens to transfer.
225/// * `signer_seeds` - A slice of slices of seeds for signing the transaction.
226///
227/// # Example
228///
229/// ```rust
230/// use simplespl::transfer_simple;
231/// use anchor_lang::solana_program::account_info::AccountInfo;
232///
233/// transfer_simple(
234///     mint_account_info,
235///     token_program_account_info,
236///     source_pubkey,
237///     destination_account_info,
238///     authority_account_info,
239///     500, // Transfer 500 tokens
240///     &[&signer_seeds],
241/// ).unwrap();
242/// ```
243pub fn transfer_simple<'info>(
244    mint: AccountInfo<'info>,
245    token_program_id: AccountInfo<'info>,
246    source_pubkey: Pubkey,
247    destination_pubkey: AccountInfo<'info>,
248    authority_pubkey: AccountInfo<'info>,
249    amount: u64,
250    signer_seeds: &[&[u8]],
251) -> Result<()> {
252    let ix = spl_token::instruction::transfer(
253        &token_program_id.key(),
254        &source_pubkey,
255        &destination_pubkey.key(),
256        &authority_pubkey.key(),
257        &[],
258        amount,
259    )?;
260
261    invoke_signed(
262        &ix,
263        &[mint, destination_pubkey, authority_pubkey, token_program_id],
264        &[signer_seeds],
265    )
266    .map_err(Into::into)
267}
268
269/// Burns SPL tokens from an account.
270///
271/// This function allows you to burn (destroy) a specified number of SPL tokens from
272/// a source account. The transaction is signed using the provided `signer_seeds`.
273///
274/// # Arguments
275///
276/// * `mint` - The mint account of the token.
277/// * `token_program_id` - The token program account (usually `spl_token`).
278/// * `source_pubkey` - The account from which tokens will be burned.
279/// * `authority_pubkey` - The account authorized to burn the tokens.
280/// * `amount` - The amount of tokens to burn.
281/// * `signer_seeds` - A slice of slices of seeds for signing the transaction.
282///
283/// # Example
284///
285/// ```rust
286/// use simplespl::burn_simple;
287/// use anchor_lang::solana_program::account_info::AccountInfo;
288///
289/// burn_simple(
290///     mint_account_info,
291///     token_program_id,
292///     source_account_info,
293///     authority_account_info,
294///     1000, // Burn 1000 tokens
295///     &[&signer_seeds],
296/// ).unwrap();
297/// ```
298pub fn burn_simple<'info>(
299    mint: AccountInfo<'info>,
300    token_program_id: AccountInfo<'info>,
301    source_pubkey: AccountInfo<'info>,
302    authority_pubkey: AccountInfo<'info>,
303    amount: u64,
304    signer_seeds: &[&[u8]],
305) -> Result<()> {
306    let ix = spl_token::instruction::burn(
307        &token_program_id.key(),
308        &source_pubkey.key(),
309        &mint.key(),
310        &authority_pubkey.key(),
311        &[],
312        amount,
313    )?;
314
315    invoke_signed(
316        &ix,
317        &[
318            source_pubkey.clone(),
319            mint.clone(),
320            authority_pubkey.clone(),
321        ],
322        &[signer_seeds],
323    )
324    .map_err(Into::into)
325}