spl_simplified/
simplespl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use anchor_lang::solana_program::account_info::AccountInfo;
use anchor_lang::solana_program::pubkey::Pubkey;
use anchor_lang::Key;
use anchor_lang::{context::CpiContext, Accounts};
use anchor_lang::{solana_program, Result};
use mpl_token_metadata::types::{Creator, DataV2};
use solana_program::program::invoke_signed;
pub use spl_token::ID;

use crate::metadata::{create_metadata_accounts_v3, CreateMetadataAccountsV3};

pub fn mint_to<'info>(
    ctx: CpiContext<'_, '_, '_, 'info, MintTo<'info>>,
    amount: u64,
) -> Result<()> {
    let ix = spl_token::instruction::mint_to(
        &spl_token::ID,
        ctx.accounts.mint.key,
        ctx.accounts.to.key,
        ctx.accounts.authority.key,
        &[],
        amount,
    )?;
    invoke_signed(
        &ix,
        &[ctx.accounts.to, ctx.accounts.mint, ctx.accounts.authority],
        ctx.signer_seeds,
    )
    .map_err(Into::into)
}

pub fn mint_simple<'info>(
    token_name: String,
    token_symbol: String,
    token_uri: String,
    token_tax: u16,
    payer: AccountInfo<'info>,
    token_metadata_program: AccountInfo<'info>,
    update_authority: AccountInfo<'info>,
    metadata: AccountInfo<'info>,
    mint_authority: AccountInfo<'info>,
    system_program: AccountInfo<'info>,
    rent: AccountInfo<'info>,
    token_program: AccountInfo<'info>,
    mint: AccountInfo<'info>,
    to: AccountInfo<'info>,
    owner: AccountInfo<'info>,
    signer_seeds: &[&[u8]],
    amount: u64,
) -> Result<()> {
    metadata_thing(
        token_name.clone(),
        token_symbol.clone(),
        token_uri.clone(),
        token_tax.clone(),
        payer,
        token_metadata_program,
        update_authority,
        mint.clone(),
        metadata,
        mint_authority,
        system_program,
        rent,
        &[&signer_seeds],
    )?;

    let ix = spl_token::instruction::mint_to(
        &spl_token::ID,
        &mint.key(),
        &to.key(),
        &owner.key(),
        &[],
        amount,
    )?;

    invoke_signed(
        &ix,
        &[mint.clone(), to.clone(), owner.clone(), token_program],
        &[signer_seeds],
    )?;

    Ok(())
}

fn metadata_thing<'info>(
    token_name: String,
    token_symbol: String,
    token_uri: String,
    token_tax: u16,
    payer: AccountInfo<'info>,
    token_metadata_program: AccountInfo<'info>,
    update_authority: AccountInfo<'info>,
    mint: AccountInfo<'info>,
    metadata: AccountInfo<'info>,
    mint_authority: AccountInfo<'info>,
    system_program: AccountInfo<'info>,
    rent: AccountInfo<'info>,
    signer_seed: &[&[&[u8]]],
) -> Result<()> {
    let mut creators: Vec<Creator> = Vec::<Creator>::new();
    let creator: Creator = Creator {
        address: mint.key(),
        verified: true,
        share: 100,
    };

    creators.push(creator);

    let token_data: DataV2 = DataV2 {
        name: token_name.clone(),
        symbol: token_symbol,
        uri: token_uri,
        seller_fee_basis_points: token_tax,
        creators: Some(creators),
        collection: None,
        uses: None,
    };

    let metadata_ctx = CpiContext::new_with_signer(
        token_metadata_program,
        CreateMetadataAccountsV3 {
            payer,
            update_authority,
            mint,
            metadata,
            mint_authority,
            system_program,
            rent,
        },
        signer_seed,
    );

    create_metadata_accounts_v3(metadata_ctx, token_data, false, true, None)?;

    Ok(())
}

#[derive(Accounts)]
pub struct MintTo<'info> {
    pub mint: AccountInfo<'info>,
    pub to: AccountInfo<'info>,
    pub authority: AccountInfo<'info>,
}