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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use std::fmt::Display;

use borsh::BorshSerialize;
use mpl_token_auth_rules::utils::get_latest_revision;
use mpl_utils::{assert_signer, create_or_allocate_account_raw};
use solana_program::{
    account_info::AccountInfo, entrypoint::ProgramResult, program::invoke, program_pack::Pack,
    pubkey::Pubkey, system_program, sysvar,
};
use spl_token::state::Account;

use crate::{
    assertions::{
        assert_derivation, assert_keys_equal, assert_owned_by,
        metadata::assert_update_authority_is_correct,
    },
    error::MetadataError,
    instruction::{Context, Delegate, DelegateArgs, MetadataDelegateRole},
    pda::{find_token_record_account, PREFIX},
    processor::AuthorizationData,
    state::{
        Metadata, MetadataDelegateRecord, Operation, ProgrammableConfig, Resizable,
        TokenDelegateRole, TokenMetadataAccount, TokenRecord, TokenStandard, TokenState,
    },
    utils::{auth_rules_validate, freeze, thaw, AuthRulesValidateParams},
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DelegateScenario {
    Metadata(MetadataDelegateRole),
    Token(TokenDelegateRole),
}

impl Display for DelegateScenario {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            Self::Metadata(role) => match role {
                MetadataDelegateRole::Authority => "Authority".to_string(),
                MetadataDelegateRole::Collection => "Collection".to_string(),
                MetadataDelegateRole::Use => "Use".to_string(),
                MetadataDelegateRole::Update => "Update".to_string(),
            },
            Self::Token(role) => match role {
                TokenDelegateRole::Sale => "Sale".to_string(),
                TokenDelegateRole::Transfer => "Transfer".to_string(),
                TokenDelegateRole::LockedTransfer => "LockedTransfer".to_string(),
                TokenDelegateRole::Utility => "Utility".to_string(),
                TokenDelegateRole::Staking => "Staking".to_string(),
                _ => panic!("Invalid delegate role"),
            },
        };

        write!(f, "{message}")
    }
}

/// Delegates an action over an asset to a specific account.
pub fn delegate<'a>(
    program_id: &Pubkey,
    accounts: &'a [AccountInfo<'a>],
    args: DelegateArgs,
) -> ProgramResult {
    let context = Delegate::to_context(accounts)?;

    // checks if it is a TokenDelegate creation
    let delegate_args = match &args {
        // Sale
        DelegateArgs::SaleV1 {
            amount,
            authorization_data,
        } => Some((TokenDelegateRole::Sale, amount, authorization_data)),
        // Transfer
        DelegateArgs::TransferV1 {
            amount,
            authorization_data,
        } => Some((TokenDelegateRole::Transfer, amount, authorization_data)),
        // LockedTransfer
        DelegateArgs::LockedTransferV1 {
            amount,
            authorization_data,
            ..
        } => Some((
            TokenDelegateRole::LockedTransfer,
            amount,
            authorization_data,
        )),
        // Utility
        DelegateArgs::UtilityV1 {
            amount,
            authorization_data,
        } => Some((TokenDelegateRole::Utility, amount, authorization_data)),
        // Staking
        DelegateArgs::StakingV1 {
            amount,
            authorization_data,
        } => Some((TokenDelegateRole::Staking, amount, authorization_data)),
        // Standard
        DelegateArgs::StandardV1 { amount } => Some((TokenDelegateRole::Standard, amount, &None)),
        // we don't need to fail if did not find a match at this point
        _ => None,
    };

    if let Some((role, amount, authorization_data)) = delegate_args {
        // proceed with the delegate creation if we have a match
        return create_persistent_delegate_v1(
            program_id,
            context,
            &args,
            role,
            *amount,
            authorization_data,
        );
    }

    // checks if it is a MetadataDelegate creation
    let delegate_args = match &args {
        DelegateArgs::CollectionV1 { authorization_data } => {
            Some((MetadataDelegateRole::Collection, authorization_data))
        }
        DelegateArgs::UpdateV1 { authorization_data } => {
            Some((MetadataDelegateRole::Update, authorization_data))
        }
        // we don't need to fail if did not find a match at this point
        _ => None,
    };

    if let Some((role, _authorization_data)) = delegate_args {
        return create_delegate_v1(program_id, context, args, role);
    }

    // this only happens if we did not find a match
    Err(MetadataError::InvalidDelegateArgs.into())
}

/// Creates a `DelegateRole::Collection` delegate.
///
/// There can be multiple collections delegates set at any time.
fn create_delegate_v1(
    program_id: &Pubkey,
    ctx: Context<Delegate>,
    _args: DelegateArgs,
    role: MetadataDelegateRole,
) -> ProgramResult {
    // signers

    assert_signer(ctx.accounts.payer_info)?;
    assert_signer(ctx.accounts.authority_info)?;

    // ownership

    assert_owned_by(ctx.accounts.metadata_info, program_id)?;
    assert_owned_by(ctx.accounts.mint_info, &spl_token::id())?;

    // key match

    assert_keys_equal(ctx.accounts.system_program_info.key, &system_program::ID)?;
    assert_keys_equal(
        ctx.accounts.sysvar_instructions_info.key,
        &sysvar::instructions::ID,
    )?;

    // account relationships

    let metadata = Metadata::from_account_info(ctx.accounts.metadata_info)?;
    // authority must match update authority
    assert_update_authority_is_correct(&metadata, ctx.accounts.authority_info)?;

    if metadata.mint != *ctx.accounts.mint_info.key {
        return Err(MetadataError::MintMismatch.into());
    }

    let delegate_record_info = match ctx.accounts.delegate_record_info {
        Some(delegate_record_info) => delegate_record_info,
        None => {
            return Err(MetadataError::MissingTokenAccount.into());
        }
    };

    // process the delegation creation (the derivation is checked
    // by the create helper)

    let delegate_role = role.to_string();

    create_pda_account(
        program_id,
        delegate_record_info,
        ctx.accounts.delegate_info,
        ctx.accounts.mint_info,
        ctx.accounts.authority_info,
        ctx.accounts.payer_info,
        ctx.accounts.system_program_info,
        &delegate_role,
    )
}

/// Creates a presistent delegate. For non-programmable assets, this is just a wrapper over
/// spl-token 'approve' delegate.
///
/// Note that `DelegateRole::Sale` is only available for programmable assets.
fn create_persistent_delegate_v1(
    program_id: &Pubkey,
    ctx: Context<Delegate>,
    args: &DelegateArgs,
    role: TokenDelegateRole,
    amount: u64,
    authorization_data: &Option<AuthorizationData>,
) -> ProgramResult {
    // retrieving required optional accounts

    let token_info = match ctx.accounts.token_info {
        Some(token_info) => token_info,
        None => {
            return Err(MetadataError::MissingTokenAccount.into());
        }
    };

    let spl_token_program_info = match ctx.accounts.spl_token_program_info {
        Some(spl_token_program_info) => spl_token_program_info,
        None => {
            return Err(MetadataError::MissingSplTokenProgram.into());
        }
    };

    // signers

    assert_signer(ctx.accounts.payer_info)?;
    assert_signer(ctx.accounts.authority_info)?;

    // ownership

    assert_owned_by(ctx.accounts.metadata_info, program_id)?;
    assert_owned_by(ctx.accounts.mint_info, &spl_token::id())?;
    assert_owned_by(token_info, &spl_token::id())?;

    // key match

    assert_keys_equal(ctx.accounts.system_program_info.key, &system_program::ID)?;
    assert_keys_equal(
        ctx.accounts.sysvar_instructions_info.key,
        &sysvar::instructions::ID,
    )?;
    assert_keys_equal(spl_token_program_info.key, &spl_token::ID)?;

    // account relationships

    let metadata = Metadata::from_account_info(ctx.accounts.metadata_info)?;
    if metadata.mint != *ctx.accounts.mint_info.key {
        return Err(MetadataError::MintMismatch.into());
    }

    // authority must be the owner of the token account: spl-token required the
    // token owner to set a delegate
    let token = Account::unpack(&token_info.try_borrow_data()?).unwrap();
    if token.owner != *ctx.accounts.authority_info.key {
        return Err(MetadataError::IncorrectOwner.into());
    }

    // process the delegation

    // programmables assets can have delegates from any role apart from `Standard`
    match metadata.token_standard {
        Some(TokenStandard::ProgrammableNonFungible) => {
            if matches!(role, TokenDelegateRole::Standard) {
                return Err(MetadataError::InvalidDelegateRole.into());
            }

            let (mut token_record, token_record_info) = match ctx.accounts.token_record_info {
                Some(token_record_info) => {
                    let (pda_key, _) =
                        find_token_record_account(ctx.accounts.mint_info.key, token_info.key);

                    assert_keys_equal(&pda_key, token_record_info.key)?;
                    assert_owned_by(token_record_info, &crate::ID)?;

                    (
                        TokenRecord::from_account_info(token_record_info)?,
                        token_record_info,
                    )
                }
                None => {
                    // token record is required for programmable assets
                    return Err(MetadataError::MissingTokenRecord.into());
                }
            };

            // we cannot replace an existing delegate, it must be revoked first
            if token_record.delegate.is_some() {
                return Err(MetadataError::DelegateAlreadyExists.into());
            }

            // if we have a rule set, we need to store its revision; at this point,
            // we will validate that we have the correct auth rules PDA
            if let Some(ProgrammableConfig::V1 {
                rule_set: Some(rule_set),
            }) = metadata.programmable_config
            {
                // valudates that we got the correct rule set
                let authorization_rules_info = ctx
                    .accounts
                    .authorization_rules_info
                    .ok_or(MetadataError::MissingAuthorizationRules)?;
                assert_keys_equal(authorization_rules_info.key, &rule_set)?;
                assert_owned_by(authorization_rules_info, &mpl_token_auth_rules::ID)?;

                // validates auth rules program
                let authorization_rules_program_info = ctx
                    .accounts
                    .authorization_rules_program_info
                    .ok_or(MetadataError::MissingAuthorizationRulesProgram)?;
                assert_keys_equal(
                    authorization_rules_program_info.key,
                    &mpl_token_auth_rules::ID,
                )?;

                let auth_rules_validate_params = AuthRulesValidateParams {
                    mint_info: ctx.accounts.mint_info,
                    owner_info: None,
                    authority_info: None,
                    source_info: None,
                    destination_info: Some(ctx.accounts.delegate_info),
                    programmable_config: metadata.programmable_config,
                    amount,
                    auth_data: authorization_data.clone(),
                    auth_rules_info: ctx.accounts.authorization_rules_info,
                    operation: Operation::Delegate {
                        scenario: DelegateScenario::Token(role),
                    },
                    is_wallet_to_wallet: false,
                    rule_set_revision: token_record
                        .rule_set_revision
                        .map(|revision| revision as usize),
                };

                auth_rules_validate(auth_rules_validate_params)?;

                // stores the latest rule set revision
                token_record.rule_set_revision =
                    get_latest_revision(authorization_rules_info)?.map(|revision| revision as u64);
            }

            token_record.state = if matches!(role, TokenDelegateRole::Sale) {
                // when a 'Sale' delegate is set, the token state is 'Listed'
                // to restrict holder transfers
                TokenState::Listed
            } else {
                TokenState::Unlocked
            };

            token_record.locked_transfer = if matches!(role, TokenDelegateRole::LockedTransfer) {
                if let DelegateArgs::LockedTransferV1 { locked_address, .. } = args {
                    Some(*locked_address)
                } else {
                    return Err(MetadataError::InvalidDelegateArgs.into());
                }
            } else {
                None
            };

            token_record.delegate = Some(*ctx.accounts.delegate_info.key);
            token_record.delegate_role = Some(role);
            token_record.save(
                token_record_info,
                ctx.accounts.payer_info,
                ctx.accounts.system_program_info,
            )?;

            if let Some(master_edition_info) = ctx.accounts.master_edition_info {
                assert_owned_by(master_edition_info, &crate::ID)?;
                // derivation is checked on the thaw function
                thaw(
                    ctx.accounts.mint_info.clone(),
                    token_info.clone(),
                    master_edition_info.clone(),
                    spl_token_program_info.clone(),
                )?;
            } else {
                return Err(MetadataError::MissingEditionAccount.into());
            }
        }
        _ => {
            if !matches!(role, TokenDelegateRole::Standard) {
                return Err(MetadataError::InvalidDelegateRole.into());
            }
        }
    }

    // creates the spl-token delegate
    invoke(
        &spl_token::instruction::approve(
            spl_token_program_info.key,
            token_info.key,
            ctx.accounts.delegate_info.key,
            ctx.accounts.authority_info.key,
            &[],
            amount,
        )?,
        &[
            token_info.clone(),
            ctx.accounts.delegate_info.clone(),
            ctx.accounts.authority_info.clone(),
        ],
    )?;

    if matches!(
        metadata.token_standard,
        Some(TokenStandard::ProgrammableNonFungible)
    ) {
        if let Some(master_edition_info) = ctx.accounts.master_edition_info {
            freeze(
                ctx.accounts.mint_info.clone(),
                token_info.clone(),
                master_edition_info.clone(),
                spl_token_program_info.clone(),
            )?;
        } else {
            // sanity check: this should not happen at this point since the master
            // edition account is validated before the delegation
            return Err(MetadataError::MissingEditionAccount.into());
        }
    }

    Ok(())
}

fn create_pda_account<'a>(
    program_id: &Pubkey,
    delegate_record_info: &'a AccountInfo<'a>,
    delegate_info: &'a AccountInfo<'a>,
    mint_info: &'a AccountInfo<'a>,
    authority_info: &'a AccountInfo<'a>,
    payer_info: &'a AccountInfo<'a>,
    system_program_info: &'a AccountInfo<'a>,
    delegate_role: &str,
) -> ProgramResult {
    // validates the delegate derivation

    let mut signer_seeds = vec![
        PREFIX.as_bytes(),
        program_id.as_ref(),
        mint_info.key.as_ref(),
        delegate_role.as_bytes(),
        authority_info.key.as_ref(),
        delegate_info.key.as_ref(),
    ];
    let bump = &[assert_derivation(
        program_id,
        delegate_record_info,
        &signer_seeds,
    )?];
    signer_seeds.push(bump);

    if !delegate_record_info.data_is_empty() {
        return Err(MetadataError::DelegateAlreadyExists.into());
    }

    // allocate the delegate account

    create_or_allocate_account_raw(
        *program_id,
        delegate_record_info,
        system_program_info,
        payer_info,
        MetadataDelegateRecord::size(),
        &signer_seeds,
    )?;

    let pda = MetadataDelegateRecord {
        bump: bump[0],
        mint: *mint_info.key,
        delegate: *delegate_info.key,
        update_authority: *authority_info.key,
        ..Default::default()
    };
    pda.serialize(&mut *delegate_record_info.try_borrow_mut_data()?)?;

    Ok(())
}