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
#![allow(clippy::result_large_err)]

use anchor_lang::error::ErrorCode;
use anchor_lang::prelude::*;
use anchor_spl::{
    associated_token::AssociatedToken,
    token_interface::{self, Mint, TokenAccount, TokenInterface, TransferChecked},
};
use mpl_token_metadata::{
    accounts::Metadata,
    instructions::{DelegateTransferV1CpiBuilder, TransferV1CpiBuilder},
    types::{AuthorizationData, TokenStandard},
};
use tensor_vipers::{throw_err, unwrap_opt};

use crate::TensorError;

pub use mpl_token_metadata::ID;

#[inline(never)]
pub fn assert_decode_metadata(mint: &Pubkey, metadata: &AccountInfo) -> Result<Metadata> {
    if *metadata.owner != mpl_token_metadata::ID {
        throw_err!(TensorError::BadMetadata);
    }

    // We must use `safe_deserialize` since there are variations on the metadata struct
    // which are not compatible with borsh's default deserialization. Using `try_from` will
    // fail when there are missing fields.
    let metadata = Metadata::safe_deserialize(&metadata.try_borrow_data()?)
        .map_err(|_error| TensorError::BadMetadata)?;

    if metadata.mint != *mint {
        throw_err!(TensorError::BadMetadata);
    }

    Ok(metadata)
}

/// Transfer Args using AccountInfo types to be more generic.
pub struct TransferArgsAi<'a, 'info> {
    /// Account that will pay for any associated fees.
    pub payer: &'a AccountInfo<'info>,

    /// Account that will transfer the token.
    pub source: &'a AccountInfo<'info>,

    /// Associated token account of the source.
    pub source_ata: &'a AccountInfo<'info>,

    /// Token record of the source.
    pub source_token_record: Option<&'a AccountInfo<'info>>,

    /// Account that will receive the token.
    pub destination: &'a AccountInfo<'info>,

    /// Associated token account of the destination.
    pub destination_ata: &'a AccountInfo<'info>,

    /// Token record of the destination.
    pub destination_token_record: Option<&'a AccountInfo<'info>>,

    /// Mint of the token.
    pub mint: &'a AccountInfo<'info>,

    /// Metadata of the token.
    pub metadata: &'a AccountInfo<'info>,

    /// Edition of the token.
    pub edition: &'a AccountInfo<'info>,

    /// System program account.
    pub system_program: &'a AccountInfo<'info>,

    /// SPL Token program account.
    pub spl_token_program: &'a AccountInfo<'info>,

    /// SPL ATA program account.
    pub spl_ata_program: &'a AccountInfo<'info>,

    /// Sysvar instructions account.
    pub sysvar_instructions: Option<&'a AccountInfo<'info>>,

    /// Token Metadata program account.
    pub token_metadata_program: Option<&'a AccountInfo<'info>>,

    /// Authorization rules program account.
    pub authorization_rules_program: Option<&'a AccountInfo<'info>>,

    /// Authorization rules account.
    pub authorization_rules: Option<&'a AccountInfo<'info>>,

    /// Authorization data.
    pub authorization_data: Option<AuthorizationData>,

    /// Delegate to use in the transfer.
    ///
    /// If passed, we assign a delegate first, and the call invoke_signed() instead of invoke().
    pub delegate: Option<&'a AccountInfo<'info>>,
}

/// Transfer Args using Anchor types to be more ergonomic.
pub struct TransferArgs<'a, 'info> {
    /// Account that will pay for any associated fees.
    pub payer: &'a AccountInfo<'info>,

    /// Account that will transfer the token.
    pub source: &'a AccountInfo<'info>,

    /// Associated token account of the source.
    pub source_ata: &'a InterfaceAccount<'info, TokenAccount>,

    /// Token record of the source.
    pub source_token_record: Option<&'a UncheckedAccount<'info>>,

    /// Account that will receive the token.
    pub destination: &'a AccountInfo<'info>,

    /// Associated token account of the destination.
    pub destination_ata: &'a InterfaceAccount<'info, TokenAccount>,

    /// Token record of the destination.
    pub destination_token_record: Option<&'a UncheckedAccount<'info>>,

    /// Mint of the token.
    pub mint: &'a InterfaceAccount<'info, Mint>,

    /// Metadata of the token.
    pub metadata: &'a UncheckedAccount<'info>,

    /// Edition of the token.
    pub edition: &'a UncheckedAccount<'info>,

    /// System program account.
    pub system_program: &'a Program<'info, System>,

    /// SPL Token program account.
    pub spl_token_program: &'a Interface<'info, TokenInterface>,

    /// SPL ATA program account.
    pub spl_ata_program: &'a Program<'info, AssociatedToken>,

    /// Sysvar instructions account.
    pub sysvar_instructions: Option<&'a UncheckedAccount<'info>>,

    /// Token Metadata program account.
    pub token_metadata_program: Option<&'a UncheckedAccount<'info>>,

    /// Authorization rules program account.
    pub authorization_rules_program: Option<&'a UncheckedAccount<'info>>,

    /// Authorization rules account.
    pub authorization_rules: Option<&'a UncheckedAccount<'info>>,

    /// Authorization data.
    pub authorization_data: Option<AuthorizationData>,

    /// Delegate to use in the transfer.
    ///
    /// If passed, we assign a delegate first, and the call invoke_signed() instead of invoke().
    pub delegate: Option<&'a AccountInfo<'info>>,
}

fn cpi_transfer_ai(args: TransferArgsAi, signer_seeds: Option<&[&[&[u8]]]>) -> Result<()> {
    let token_metadata_program =
        unwrap_opt!(args.token_metadata_program, ErrorCode::AccountNotEnoughKeys);
    let sysvar_instructions =
        unwrap_opt!(args.sysvar_instructions, ErrorCode::AccountNotEnoughKeys);

    // prepares the CPI instruction
    let mut transfer_cpi = TransferV1CpiBuilder::new(token_metadata_program);
    transfer_cpi
        .authority(args.source)
        .token_owner(args.source)
        .token(args.source_ata.as_ref())
        .destination_owner(args.destination)
        .destination_token(args.destination_ata.as_ref())
        .mint(args.mint.as_ref())
        .metadata(args.metadata.as_ref())
        .edition(Some(args.edition))
        .payer(args.payer)
        .spl_ata_program(args.spl_ata_program)
        .spl_token_program(args.spl_token_program)
        .system_program(args.system_program)
        .sysvar_instructions(sysvar_instructions)
        .token_record(args.source_token_record)
        .destination_token_record(args.destination_token_record)
        .authorization_rules_program(args.authorization_rules_program)
        .authorization_rules(args.authorization_rules)
        .amount(1);

    // set the authorization data if passed in
    args.authorization_data
        .clone()
        .map(|data| transfer_cpi.authorization_data(data));

    // invoke delegate if necessary
    if let Some(delegate) = args.delegate {
        // replace authority on the builder with the newly assigned delegate
        transfer_cpi.authority(delegate);

        let mut delegate_cpi = DelegateTransferV1CpiBuilder::new(token_metadata_program);
        delegate_cpi
            .authority(args.source)
            .delegate(delegate)
            .token(args.source_ata.as_ref())
            .mint(args.mint.as_ref())
            .metadata(args.metadata)
            .master_edition(Some(args.edition))
            .payer(args.payer)
            .spl_token_program(Some(args.spl_token_program))
            .token_record(args.source_token_record)
            .authorization_rules(args.authorization_rules)
            .authorization_rules_program(args.authorization_rules_program)
            .amount(1);

        args.authorization_data
            .map(|data| delegate_cpi.authorization_data(data));

        delegate_cpi.invoke()?;
    }

    if let Some(signer_seeds) = signer_seeds {
        transfer_cpi.invoke_signed(signer_seeds)?;
    } else {
        transfer_cpi.invoke()?;
    }

    Ok(())
}

fn cpi_transfer(args: TransferArgs, signer_seeds: Option<&[&[&[u8]]]>) -> Result<()> {
    let token_metadata_program =
        unwrap_opt!(args.token_metadata_program, ErrorCode::AccountNotEnoughKeys);
    let sysvar_instructions =
        unwrap_opt!(args.sysvar_instructions, ErrorCode::AccountNotEnoughKeys);

    // prepares the CPI instruction
    let mut transfer_cpi = TransferV1CpiBuilder::new(token_metadata_program);
    transfer_cpi
        .authority(args.source)
        .token_owner(args.source)
        .token(args.source_ata.as_ref())
        .destination_owner(args.destination)
        .destination_token(args.destination_ata.as_ref())
        .mint(args.mint.as_ref())
        .metadata(args.metadata.as_ref())
        .edition(Some(args.edition))
        .payer(args.payer)
        .spl_ata_program(args.spl_ata_program)
        .spl_token_program(args.spl_token_program)
        .system_program(args.system_program)
        .sysvar_instructions(sysvar_instructions)
        .token_record(args.source_token_record.map(|account| account.as_ref()))
        .destination_token_record(
            args.destination_token_record
                .map(|account| account.as_ref()),
        )
        .authorization_rules_program(
            args.authorization_rules_program
                .map(|account| account.as_ref()),
        )
        .authorization_rules(args.authorization_rules.map(|account| account.as_ref()))
        .amount(1);

    // set the authorization data if passed in
    args.authorization_data
        .clone()
        .map(|data| transfer_cpi.authorization_data(data));

    // invoke delegate if necessary
    if let Some(delegate) = args.delegate {
        // replace authority on the builder with the newly assigned delegate
        transfer_cpi.authority(delegate);

        let mut delegate_cpi = DelegateTransferV1CpiBuilder::new(token_metadata_program);
        delegate_cpi
            .authority(args.source)
            .delegate(delegate)
            .token(args.source_ata.as_ref())
            .mint(args.mint.as_ref())
            .metadata(args.metadata)
            .master_edition(Some(args.edition))
            .payer(args.payer)
            .spl_token_program(Some(args.spl_token_program))
            .token_record(args.source_token_record.map(|account| account.as_ref()))
            .authorization_rules(args.authorization_rules.map(|account| account.as_ref()))
            .authorization_rules_program(
                args.authorization_rules_program
                    .map(|account| account.as_ref()),
            )
            .amount(1);

        args.authorization_data
            .map(|data| delegate_cpi.authorization_data(data));

        delegate_cpi.invoke()?;
    }

    if let Some(signer_seeds) = signer_seeds {
        transfer_cpi.invoke_signed(signer_seeds)?;
    } else {
        transfer_cpi.invoke()?;
    }

    Ok(())
}

/// Transfer a NFT or PNFT using AccountInfos rather than Anchor types.
pub fn transfer_with_ai(
    args: TransferArgsAi,
    //if passed, use signed_invoke() instead of invoke()
    signer_seeds: Option<&[&[&[u8]]]>,
) -> Result<()> {
    let metadata = assert_decode_metadata(&args.mint.key(), args.metadata)?;

    if matches!(
        metadata.token_standard,
        Some(TokenStandard::ProgrammableNonFungible)
            | Some(TokenStandard::ProgrammableNonFungibleEdition)
    ) {
        // pnft transfer
        return cpi_transfer_ai(args, signer_seeds);
    }

    // non-pnft / no token std, normal transfer

    let ctx = CpiContext::new(
        args.spl_token_program.to_account_info(),
        TransferChecked {
            from: args.source_ata.to_account_info(),
            to: args.destination_ata.to_account_info(),
            authority: args.source.to_account_info(),
            mint: args.mint.to_account_info(),
        },
    );

    if let Some(signer_seeds) = signer_seeds {
        token_interface::transfer_checked(ctx.with_signer(signer_seeds), 1, 0)
    } else {
        token_interface::transfer_checked(ctx, 1, 0)
    }
}

/// Transfer a NFT or PNFT.
pub fn transfer(
    args: TransferArgs,
    //if passed, use signed_invoke() instead of invoke()
    signer_seeds: Option<&[&[&[u8]]]>,
) -> Result<()> {
    let metadata = assert_decode_metadata(&args.mint.key(), args.metadata)?;

    if matches!(
        metadata.token_standard,
        Some(TokenStandard::ProgrammableNonFungible)
            | Some(TokenStandard::ProgrammableNonFungibleEdition)
    ) {
        // pnft transfer
        return cpi_transfer(args, signer_seeds);
    }

    // non-pnft / no token std, normal transfer

    let ctx = CpiContext::new(
        args.spl_token_program.to_account_info(),
        TransferChecked {
            from: args.source_ata.to_account_info(),
            to: args.destination_ata.to_account_info(),
            authority: args.source.to_account_info(),
            mint: args.mint.to_account_info(),
        },
    );

    if let Some(signer_seeds) = signer_seeds {
        token_interface::transfer_checked(ctx.with_signer(signer_seeds), 1, 0)
    } else {
        token_interface::transfer_checked(ctx, 1, 0)
    }
}