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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use anchor_client::solana_sdk::{
    pubkey::Pubkey,
    signature::{Keypair, Signature, Signer},
    system_instruction, system_program,
};
use anyhow::Result;
use mpl_candy_machine_core::{
    accounts as nft_accounts, instruction as nft_instruction, CandyMachineData, ConfigLineSettings,
    Creator as CandyCreator,
};
pub use mpl_token_metadata::state::{
    MAX_CREATOR_LIMIT, MAX_NAME_LENGTH, MAX_SYMBOL_LENGTH, MAX_URI_LENGTH,
};
use mpl_token_metadata::{
    instruction::MetadataDelegateRole, pda::find_metadata_delegate_record_account,
    state::TokenStandard,
};
use solana_program::native_token::LAMPORTS_PER_SOL;

use crate::{
    common::*,
    config::data::*,
    deploy::errors::*,
    pdas::{find_candy_machine_creator_pda, find_master_edition_pda, find_metadata_pda},
};

/// Create the candy machine data struct.
pub fn create_candy_machine_data(
    _client: &Client,
    config: &ConfigData,
    cache: &Cache,
) -> Result<CandyMachineData> {
    let mut creators: Vec<CandyCreator> = Vec::new();
    let mut share = 0u32;

    for creator in &config.creators {
        let c = creator.to_candy_format()?;
        share += c.percentage_share as u32;

        creators.push(c);
    }

    if creators.is_empty() || creators.len() > (MAX_CREATOR_LIMIT - 1) {
        return Err(anyhow!(
            "The number of creators must be between 1 and {}.",
            MAX_CREATOR_LIMIT - 1,
        ));
    }

    if share != 100 {
        return Err(anyhow!(
            "Creator(s) share must add up to 100, current total {}.",
            share,
        ));
    }

    let config_line_settings = if config.hidden_settings.is_some() {
        None
    } else {
        // CMv3 allows the specification of a common prefix for both name and uri
        // therefore we need to determine the largest common prefix and the len of
        // the remaining parts of the name and uri

        // (shortest, largest, len largest) name
        let mut name_pair = [String::new(), String::new(), String::new()];
        // (shortest, largest, len largest) uri
        let mut uri_pair = [String::new(), String::new(), String::new()];
        // compares a value against a pair of (shorter, larger)
        let compare_pair = |value: &String, pair: &mut [String; 3]| {
            // lexicographic smaller
            if pair[0].is_empty() || value < &pair[0] {
                pair[0] = value.to_string();
            }
            // lexicographic larger
            if value > &pair[1] {
                pair[1] = value.to_string();
            }
            // lengthwise larger
            if value.len() > pair[2].len() {
                pair[2] = value.to_string();
            }
        };
        let common_prefix = |value1: &str, value2: &str| {
            let bytes1 = value1.as_bytes();
            let bytes2 = value2.as_bytes();
            let mut index = 0;

            while (index < bytes1.len() && index < bytes2.len()) && bytes1[index] == bytes2[index] {
                index += 1;
            }

            value1[..index].to_string()
        };

        for (index, item) in cache.items.iter() {
            if i64::from_str(index)? > -1 {
                compare_pair(&item.name, &mut name_pair);
                compare_pair(&item.metadata_link, &mut uri_pair);
            }
        }

        let name_prefix = common_prefix(&name_pair[0], &name_pair[1]);
        let uri_prefix = common_prefix(&uri_pair[0], &uri_pair[1]);

        Some(ConfigLineSettings {
            name_length: (name_pair[2].len() - name_prefix.len()) as u32,
            prefix_name: name_prefix,
            uri_length: (uri_pair[2].len() - uri_prefix.len()) as u32,
            prefix_uri: uri_prefix,
            is_sequential: config.is_sequential,
        })
    };

    let hidden_settings = config.hidden_settings.as_ref().map(|s| s.to_candy_format());

    let data = CandyMachineData {
        items_available: config.number,
        symbol: config.symbol.clone(),
        seller_fee_basis_points: config.seller_fee_basis_points,
        max_supply: 0,
        is_mutable: config.is_mutable,
        creators,
        config_line_settings,
        hidden_settings,
    };

    Ok(data)
}

/// Send the `initialize_candy_machine` instruction to the candy machine program.
pub fn initialize_candy_machine(
    config_data: &ConfigData,

    candy_account: &Keypair,
    candy_machine_data: CandyMachineData,
    collection_mint: Pubkey,
    collection_update_authority: Pubkey,
    program: Program,
) -> Result<Signature> {
    let payer = program.payer();
    let candy_account_size = candy_machine_data.get_space_for_candy()?;

    info!(
        "Initializing candy machine with account size of: {} and address of: {}",
        candy_account_size,
        candy_account.pubkey().to_string()
    );

    let lamports = program
        .rpc()
        .get_minimum_balance_for_rent_exemption(candy_account_size)?;

    let balance = program.rpc().get_account(&payer)?.lamports;

    if lamports > balance {
        return Err(DeployError::BalanceTooLow(
            format!("{:.3}", (balance as f64 / LAMPORTS_PER_SOL as f64)),
            format!("{:.3}", (lamports as f64 / LAMPORTS_PER_SOL as f64)),
        )
        .into());
    }

    // required PDAs

    let (authority_pda, _) = find_candy_machine_creator_pda(&candy_account.pubkey());

    let collection_metadata = find_metadata_pda(&collection_mint);
    let collection_master_edition = find_master_edition_pda(&collection_mint);
    let (collection_delegate_record, _) = find_metadata_delegate_record_account(
        &collection_mint,
        MetadataDelegateRole::Collection,
        &collection_update_authority,
        &authority_pda,
    );

    let tx = program
        .request()
        .instruction(system_instruction::create_account(
            &payer,
            &candy_account.pubkey(),
            lamports,
            candy_account_size as u64,
            &program.id(),
        ))
        .signer(candy_account)
        .accounts(nft_accounts::InitializeV2 {
            candy_machine: candy_account.pubkey(),
            authority: payer,
            authority_pda,
            payer,
            collection_metadata,
            collection_mint,
            collection_master_edition,
            collection_update_authority,
            collection_delegate_record,
            rule_set: config_data.rule_set,
            token_metadata_program: mpl_token_metadata::ID,
            system_program: system_program::id(),
            sysvar_instructions: sysvar::instructions::ID,
            authorization_rules_program: None,
            authorization_rules: None,
        })
        .args(nft_instruction::InitializeV2 {
            data: candy_machine_data,
            token_standard: <crate::config::data::TokenStandard as std::convert::Into<
                TokenStandard,
            >>::into(config_data.token_standard) as u8,
        });

    let sig = tx.send()?;

    Ok(sig)
}