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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use {
    crate::state::{Assembler, AssemblingAction, Creator, TokenStandard},
    anchor_lang::prelude::*,
    anchor_spl::token::{self, Mint, Token},
    hpl_events::HplEvents,
    hpl_hive_control::{
        program::HplHiveControl,
        state::{DelegateAuthority, Project},
    },
    hpl_utils::create_nft,
    mpl_token_metadata::state::AssetData,
};

/// Accounts used in the create assembler instruction
#[derive(Accounts)]
pub struct CreateAssembler<'info> {
    /// HIVE CONTROL
    #[account(mut)]
    pub project: Box<Account<'info, Project>>,

    /// Assembler state account
    #[account(
            init, payer = payer,
            space = Assembler::LEN ,
            seeds = [
              b"assembler".as_ref(),
              collection_mint.key().as_ref(),
            ],
            bump,
          )]
    pub assembler: Account<'info, Assembler>,

    /// Collection of the nfts generated by this assembler
    #[account(
      init,
      payer = payer,
      mint::decimals = 0,
      mint::authority = assembler,
      mint::freeze_authority = assembler,
    )]
    pub collection_mint: Box<Account<'info, Mint>>,

    /// Metadata account of the collection
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut)]
    pub collection_metadata: AccountInfo<'info>,

    /// Master Edition account of the collection
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut)]
    pub collection_master_edition: AccountInfo<'info>,

    #[account(has_one = authority)]
    pub delegate_authority: Option<Account<'info, DelegateAuthority>>,

    /// The wallet that holds the authority over the assembler
    /// CHECK: This is not dangerous because we don't read or write from this account
    pub authority: Signer<'info>,

    /// The wallet that pays for the rent
    #[account(mut)]
    pub payer: Signer<'info>,

    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut)]
    pub vault: AccountInfo<'info>,

    /// NATIVE SYSTEM PROGRAM
    pub system_program: Program<'info, System>,

    /// HIVE CONTROL PROGRAM
    pub hive_control: Program<'info, HplHiveControl>,

    /// SPL TOKEN PROGRAM
    #[account(address = token::ID)]
    pub token_program: Program<'info, Token>,

    /// METAPLEX TOKEN METADATA PROGRAM
    /// CHECK: This is not dangerous because we don't read or write from this account
    pub token_metadata_program: AccountInfo<'info>,

    /// HIVE CONTROL PROGRAM
    pub hpl_events: Program<'info, HplEvents>,

    /// SYSVAR CLOCK
    pub clock_sysvar: Sysvar<'info, Clock>,

    /// SYSVAR RENT
    pub rent_sysvar: Sysvar<'info, Rent>,

    /// NATIVE INSTRUCTIONS SYSVAR
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(address = anchor_lang::solana_program::sysvar::instructions::ID)]
    pub instructions_sysvar: AccountInfo<'info>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct CreateAssemblerArgs {
    assembling_action: AssemblingAction,
    collection_name: String,
    collection_symbol: String,
    collection_uri: String,
    collection_description: String,
    nft_base_uri: String,
    allow_duplicates: Option<bool>,
    default_royalty: Option<u16>,
    token_standard: Option<TokenStandard>,
    rule_set: Option<Pubkey>,
    default_creators: Vec<Creator>,
}

/// Create a new assembler
pub fn create_assembler(ctx: Context<CreateAssembler>, args: CreateAssemblerArgs) -> Result<()> {
    let assembler = &mut ctx.accounts.assembler;
    assembler.bump = ctx.bumps["assembler"];
    assembler.project = ctx.accounts.project.key();
    assembler.collection = ctx.accounts.collection_mint.key();
    assembler.collection_name = args.collection_name;
    assembler.collection_symbol = args.collection_symbol;
    assembler.collection_description = args.collection_description;
    assembler.nft_base_uri = args.nft_base_uri;
    assembler.assembling_action = args.assembling_action;
    assembler.nfts = 0;
    assembler.allow_duplicates = args.allow_duplicates.unwrap_or(false);
    assembler.default_royalty = args.default_royalty.unwrap_or(0);
    assembler.token_standard = args
        .token_standard
        .unwrap_or(crate::state::TokenStandard::NonFungible);
    assembler.rule_set = args.rule_set;
    assembler.default_creators = args.default_creators;

    let assembler_seeds = &[
        b"assembler".as_ref(),
        assembler.collection.as_ref(),
        &[assembler.bump],
    ];
    let assembler_signer = &[&assembler_seeds[..]];

    create_nft(
        AssetData {
            name: assembler.collection_name.clone(),
            symbol: assembler.collection_symbol.clone(),
            uri: args.collection_uri,
            seller_fee_basis_points: assembler.default_royalty,
            creators: Some(
                [
                    vec![mpl_token_metadata::state::Creator {
                        address: assembler.key(),
                        verified: true,
                        share: 0,
                    }],
                    assembler
                        .default_creators
                        .clone()
                        .iter()
                        .map(|c| mpl_token_metadata::state::Creator {
                            address: c.address,
                            verified: false,
                            share: c.share,
                        })
                        .collect(),
                ]
                .concat(),
            ),
            primary_sale_happened: false,
            is_mutable: true,
            token_standard: mpl_token_metadata::state::TokenStandard::NonFungible,
            collection: None,
            uses: None,
            collection_details: None,
            rule_set: assembler.rule_set,
        },
        false,
        true,
        ctx.accounts.collection_metadata.to_account_info(),
        ctx.accounts.collection_master_edition.to_account_info(),
        ctx.accounts.collection_mint.to_account_info(),
        assembler.to_account_info(),
        ctx.accounts.payer.to_account_info(),
        assembler.to_account_info(),
        ctx.accounts.system_program.to_account_info(),
        ctx.accounts.instructions_sysvar.to_account_info(),
        ctx.accounts.token_program.to_account_info(),
        Some(assembler_signer),
    )?;

    Ok(())
}

/// Accounts used in the create assembler instruction
#[derive(Accounts)]
pub struct UpdateAssembler<'info> {
    // HIVE CONTROL
    #[account()]
    pub project: Box<Account<'info, Project>>,

    /// Assembler state account
    #[account(mut, has_one = project)]
    pub assembler: Account<'info, Assembler>,

    #[account(has_one = authority)]
    pub delegate_authority: Option<Account<'info, DelegateAuthority>>,

    /// The wallet that holds the authority over the assembler
    pub authority: Signer<'info>,

    /// The wallet that pays for the rent
    pub payer: Signer<'info>,

    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut)]
    pub vault: AccountInfo<'info>,

    pub system_program: Program<'info, System>,

    /// HIVE CONTROL PROGRAM
    pub hive_control: Program<'info, HplHiveControl>,

    /// NATIVE INSTRUCTIONS SYSVAR
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(address = anchor_lang::solana_program::sysvar::instructions::ID)]
    pub instructions_sysvar: AccountInfo<'info>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct UpdateAssemblerArgs {
    assembling_action: AssemblingAction,
    nft_base_uri: String,
    allow_duplicates: Option<bool>,
    default_royalty: Option<u16>,
}

/// Update an assembler
pub fn update_assembler(ctx: Context<UpdateAssembler>, args: UpdateAssemblerArgs) -> Result<()> {
    let assembler = &mut ctx.accounts.assembler;
    assembler.nft_base_uri = args.nft_base_uri;
    assembler.assembling_action = args.assembling_action;
    assembler.allow_duplicates = args.allow_duplicates.unwrap_or(assembler.allow_duplicates);
    assembler.default_royalty = args.default_royalty.unwrap_or(assembler.default_royalty);
    Ok(())
}