solagent_plugin_solana/
deploy_token.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
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2025 zTgx
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::DeployedData;
use mpl_token_metadata::{
    accounts::Metadata,
    instructions::{CreateV1, CreateV1InstructionArgs},
    types::{PrintSupply, TokenStandard},
};
use solagent_core::{
    solana_client::{client_error::ClientError, rpc_config::RpcSendTransactionConfig},
    solana_program,
    solana_sdk::{
        program_pack::Pack,
        signature::{Keypair, Signer},
        system_instruction, system_program,
        {commitment_config::CommitmentConfig, transaction::Transaction},
    },
    SolanaAgentKit,
};
use spl_associated_token_account::get_associated_token_address;
use spl_token::instruction as spl_token_instruction;

/// Deploys a new SPL token.
///
/// # Parameters
///
/// - `agent`: An instance of `SolanaAgentKit`.
/// - `name`: Name of the token.
/// - `uri`: URI for the token metadata.
/// - `symbol`: Symbol of the token.
/// - `decimals`: Number of decimals for the token (default: 9).
/// - `initial_supply`: Initial supply to mint (optional).
///
/// # Returns
///
/// An object containing the token mint address.
pub async fn deploy_token(
    agent: &SolanaAgentKit,
    name: String,
    uri: String,
    symbol: String,
    decimals: u8,
    initial_supply: Option<u64>,
) -> Result<DeployedData, ClientError> {
    let mint = Keypair::new();
    let mint_pubkey = mint.pubkey();

    // Create token mint account
    let min_rent = agent.connection.get_minimum_balance_for_rent_exemption(spl_token::state::Mint::LEN)?;

    let create_mint_account_ix = system_instruction::create_account(
        &agent.wallet.address,
        &mint_pubkey,
        min_rent,
        spl_token::state::Mint::LEN as u64,
        &spl_token::id(),
    );

    let initialize_mint_ix = spl_token_instruction::initialize_mint(
        &spl_token::id(),
        &mint_pubkey,
        &agent.wallet.address,
        Some(&agent.wallet.address),
        decimals,
    )
    .expect("initialize_mint");

    // Create metadata account
    let (metadata, _x) = Metadata::find_pda(&mint_pubkey);
    // instruction args
    let args = CreateV1InstructionArgs {
        name,
        symbol,
        uri,
        seller_fee_basis_points: 500,
        primary_sale_happened: false,
        is_mutable: true,
        token_standard: TokenStandard::Fungible,
        collection: None,
        uses: None,
        collection_details: None,
        creators: None,
        rule_set: None,
        decimals: Some(18),
        print_supply: Some(PrintSupply::Zero),
    };

    // instruction accounts
    let create_ix = CreateV1 {
        metadata,
        master_edition: None,
        mint: (mint_pubkey, true),
        authority: agent.wallet.address,
        payer: agent.wallet.address,
        update_authority: (agent.wallet.address, true),
        system_program: system_program::ID,
        sysvar_instructions: solana_program::sysvar::instructions::ID,
        spl_token_program: Some(spl_token::ID),
    };
    let create_metadata_ix = create_ix.instruction(args);

    let mut instructions = vec![create_mint_account_ix, initialize_mint_ix, create_metadata_ix];

    if let Some(supply) = initial_supply {
        let associated_token_account = get_associated_token_address(&agent.wallet.address, &mint_pubkey);

        let create_associated_token_account_ix =
            spl_associated_token_account::instruction::create_associated_token_account(
                &agent.wallet.address,
                &agent.wallet.address,
                &mint_pubkey,
                &spl_token::id(),
            );

        let mint_to_ix = spl_token_instruction::mint_to(
            &spl_token::id(),
            &mint_pubkey,
            &associated_token_account,
            &agent.wallet.address,
            &[&agent.wallet.address],
            supply,
        )
        .expect("mint_to");

        instructions.push(create_associated_token_account_ix);
        instructions.push(mint_to_ix);
    }

    let recent_blockhash = agent.connection.get_latest_blockhash()?;
    let transaction = Transaction::new_signed_with_payer(
        &instructions,
        Some(&agent.wallet.address),
        &[&agent.wallet.wallet, &mint],
        recent_blockhash,
    );

    let signature = agent.connection.send_and_confirm_transaction_with_spinner_and_config(
        &transaction,
        CommitmentConfig::finalized(),
        RpcSendTransactionConfig { skip_preflight: true, ..Default::default() },
    )?;

    Ok(DeployedData::new(mint_pubkey.to_string(), signature.to_string()))
}