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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
pub mod assertions;
pub mod constants;
pub mod errors;
pub mod instructions;
pub mod state;

use {
    anchor_lang::prelude::*,
    instructions::*,
    state::{DelegateAuthority, Project, SerializableActions},
};

declare_id!("HivezrprVqHR6APKKQkkLHmUG8waZorXexEBRZWh5LRm");

fn profile_platform_gate<'info>(
    project: &Account<'info, Project>,
    signer_key: Pubkey,
    payer: AccountInfo<'info>,
    vault: AccountInfo<'info>,
    delegate_authority: &Option<Account<'info, DelegateAuthority>>,
    system_program: AccountInfo<'info>,
    instructions_sysvar: AccountInfo<'info>,
) -> Result<()> {
    if signer_key.eq(&project.driver) {
        msg!("Driver action");
        instructions::platform_gate_fn(
            SerializableActions::DriverAction,
            Some((0, Pubkey::default())),
            project,
            signer_key,
            payer,
            vault,
            delegate_authority,
            system_program,
            instructions_sysvar,
            ID,
        )
    } else {
        instructions::platform_gate_fn(
            SerializableActions::PublicLow,
            None,
            project,
            signer_key,
            payer,
            vault,
            delegate_authority,
            system_program,
            instructions_sysvar,
            ID,
        )
    }
}

#[program]
pub mod hpl_hive_control {
    use super::*;

    /// This function initializes the public information for the Honeycomb Protocol.
    /// It is called when the program is deployed and can be accessed by only the program authority.
    pub fn init_public_info(ctx: Context<InitPublicInfo>, env: String) -> Result<()> {
        assertions::assert_program_authority(
            ctx.accounts.program.to_account_info(),
            ctx.accounts.program_data.to_account_info(),
            ctx.accounts.authority.key,
        )?;
        instructions::init_public_info(ctx, env)
    }

    /// This function sets the public information for the Honeycomb Protocol.
    /// It is called when the program authority needs to update the public information.
    pub fn set_public_info(
        ctx: Context<SetPublicInfo>,
        env: String,
        args: SetPublicInfoArgs,
    ) -> Result<()> {
        assertions::assert_program_authority(
            ctx.accounts.program.to_account_info(),
            ctx.accounts.program_data.to_account_info(),
            ctx.accounts.authority.key,
        )?;
        instructions::set_public_info(ctx, env, args)
    }

    /// Sets the authentication driver with the provided environment string and arguments.
    ///
    /// This function sets the authentication driver for the Honeycomb Protocol.
    /// It is called when the program authority needs to update the authentication driver.
    pub fn set_auth_driver(
        ctx: Context<SetAuthDriver>,
        env: String,
        args: SetAuthDriverArgs,
    ) -> Result<()> {
        assertions::assert_program_authority(
            ctx.accounts.program.to_account_info(),
            ctx.accounts.program_data.to_account_info(),
            ctx.accounts.authority.key,
        )?;
        instructions::set_auth_driver(ctx, env, args)
    }

    pub fn platform_gate(ctx: Context<PlatformGate>, args: PlatformGateArgs) -> Result<()> {
        instructions::platform_gate(ctx, args)
    }

    /// This function creates a new project in the Honeycomb Protocol.
    pub fn create_project(ctx: Context<CreateProject>, args: CreateProjectArgs) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::CreateProject,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &None,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::create_project(ctx, args)
    }

    /// This function changes the driver authority for the specified project in the Honeycomb Protocol.
    pub fn change_driver(ctx: Context<ChangeDriver>) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageDelegateAuthority,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &ctx.accounts.delegate_authority,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::change_driver(ctx)
    }

    /// This function changes the driver authority for the specified project in the Honeycomb Protocol.
    pub fn add_remove_criteria(
        ctx: Context<AddRemoveCriteria>,
        args: AddRemoveCriteriaArgs,
    ) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageCriterias,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &ctx.accounts.delegate_authority,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::add_remove_criteria(ctx, args)
    }

    /// This function adds or removes a criteria to/from the specified project in the Honeycomb Protocol.
    pub fn add_remove_service(
        ctx: Context<AddRemoveService>,
        args: AddRemoveServiceArgs,
    ) -> Result<()> {
        instructions::platform_gate_fn(
            if args.remove.unwrap_or(false) {
                SerializableActions::RemoveService
            } else {
                SerializableActions::AddService
            },
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &ctx.accounts.delegate_authority,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::add_remove_service(ctx, args)
    }

    /// This function adds or removes a service to/from the specified project in the Honeycomb Protocol.
    pub fn add_remove_profile_data_config(
        ctx: Context<AddRemoveProfileDataConfig>,
        args: AddRemoveProfileDataConfigArgs,
    ) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageProfiles,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &ctx.accounts.delegate_authority,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::add_remove_profile_data_config(ctx, args)
    }

    /// This function clears the profile data configuration for the specified project in the Honeycomb Protocol.
    pub fn clear_profile_data_config(ctx: Context<ClearProfileDataConfig>) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageProfiles,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &ctx.accounts.delegate_authority,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::clear_profile_data_config(ctx)
    }

    /// This function adds an address container to the specified project in the Honeycomb Protocol.
    pub fn add_address_container_to_project(
        ctx: Context<AddAddressContainerToProject>,
        args: AddAddressContainerToProjectArgs,
    ) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageIndexing,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &ctx.accounts.delegate_authority,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::add_address_container_to_project(ctx, args)
    }

    /// This function adds multiple mint addresses to the specified address container in the Honeycomb Protocol.
    pub fn add_mint_addresses_to_address_container(
        ctx: Context<AddMintAddressesToAddressContainer>,
        args: AddMintAddressesToAddressContainerArgs,
    ) -> Result<()> {
        instructions::add_mint_addresses_to_address_container(ctx, args)
    }

    /// This function adds multiple mint addresses to the specified address container in the Honeycomb Protocol.
    pub fn create_delegate_authority(
        ctx: Context<CreateDelegateAuthority>,
        args: CreateDelegateAuthorityArgs,
    ) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageDelegateAuthority,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &None,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::create_delegate_authority(ctx, args)
    }

    /// This function adds or removes a delegation for the specified project in the Honeycomb Protocol.
    pub fn add_remove_delegation(
        ctx: Context<ModifyDelegate>,
        args: AddRemoveDelegationArgs,
    ) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::ManageDelegateAuthority,
            None,
            &ctx.accounts.project,
            ctx.accounts.authority.key(),
            ctx.accounts.payer.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &None,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::add_remove_delegation(ctx, args)
    }

    /// This function registers a new user in the Honeycomb Protocol.
    pub fn initialize_user(ctx: Context<InitializeUser>, args: InitializeUserArgs) -> Result<()> {
        instructions::initialize_user(ctx, args)
    }

    /// This function closes an existing user in the HPL Hive Control program.
    pub fn close_user(ctx: Context<CloseUser>) -> Result<()> {
        instructions::close_user(ctx)
    }

    /// This function updates user info in the HPL Hive Control program.
    pub fn update_user(ctx: Context<UpdateUser>, env: String, args: UpdateUserArgs) -> Result<()> {
        let assert_user = assertions::assert_user(&ctx.accounts.user, ctx.accounts.authority.key());
        if assert_user.is_err() {
            assertions::assert_auth_driver(&ctx.accounts.public_info, &ctx.accounts.authority)?;
        }

        instructions::update_user(ctx, env, args)
    }

    /// This function adds a new wallet to the user's profile in the HPL Hive Control program.
    pub fn add_wallet(ctx: Context<AddWallet>, env: String) -> Result<()> {
        let assert_user = assertions::assert_user(&ctx.accounts.user, ctx.accounts.authority.key());
        if assert_user.is_err() {
            assertions::assert_auth_driver(&ctx.accounts.public_info, &ctx.accounts.authority)?;
        }

        instructions::add_wallet(ctx, env)
    }

    /// This function deletes a wallet from the user's profile in the HPL Hive Control program.
    pub fn delete_wallet(ctx: Context<DeleteWallet>, env: String) -> Result<()> {
        let assert_user = assertions::assert_user(&ctx.accounts.user, ctx.accounts.authority.key());
        if assert_user.is_err() {
            assertions::assert_auth_driver(&ctx.accounts.public_info, &ctx.accounts.authority)?;
        }

        instructions::delete_wallet(ctx, env)
    }

    /// This function creates a new profile for qthe user in the HPL Hive Control program.
    pub fn create_profile(ctx: Context<CreateProfile>, args: CreateProfileArgs) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::PublicLow,
            None,
            &ctx.accounts.project,
            ctx.accounts.wallet.key(),
            ctx.accounts.wallet.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &None,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::create_profile(ctx, args)
    }

    /// This function deletes the user's profile in the HPL Hive Control program.
    pub fn delete_profile(ctx: Context<DeleteProfile>) -> Result<()> {
        instructions::platform_gate_fn(
            SerializableActions::PublicLow,
            None,
            &ctx.accounts.project,
            ctx.accounts.wallet.key(),
            ctx.accounts.wallet.to_account_info(),
            ctx.accounts.vault.to_account_info(),
            &None,
            ctx.accounts.system_program.to_account_info(),
            ctx.accounts.instructions_sysvar.to_account_info(),
            ID,
        )?;

        instructions::delete_profile(ctx)
    }

    /// This function adds profile data to the user's profile in the HPL Hive Control program.
    pub fn manage_profile_data(
        ctx: Context<ManageProfileData>,
        args: ManageProfileDataArgs,
    ) -> Result<()> {
        if args.value.is_some() {
            profile_platform_gate(
                &ctx.accounts.project,
                ctx.accounts.authority.key(),
                ctx.accounts.payer.to_account_info(),
                ctx.accounts.vault.to_account_info(),
                &ctx.accounts.delegate_authority,
                ctx.accounts.system_program.to_account_info(),
                ctx.accounts.instructions_sysvar.to_account_info(),
            )?;
        }

        instructions::manage_profile_data(ctx, args)
    }

    pub fn migrate_account(ctx: Context<MigrateAccount>, args: MigrateAccountArgs) -> Result<()> {
        instructions::migrate_account(ctx, args)
    }

    pub fn close_orphan_wallet_resolver(ctx: Context<CloseOrphanWalletResolver>) -> Result<()> {
        instructions::close_orphan_wallet_resolver(ctx)
    }
}