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
use borsh::{BorshDeserialize, BorshSerialize};
#[cfg(feature = "serde-feature")]
use serde::{Deserialize, Serialize};
use solana_program::pubkey::Pubkey;

use super::{Collection, CollectionDetails, Creator, Data, DataV2, TokenStandard, Uses};

/// Data representation of an asset.
#[repr(C)]
#[cfg_attr(feature = "serde-feature", derive(Serialize, Deserialize))]
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)]
pub struct AssetData {
    /// Update Authority for the asset.
    pub update_authority: Pubkey,
    /// The name of the asset.
    pub name: String,
    /// The symbol for the asset.
    pub symbol: String,
    /// URI pointing to JSON representing the asset.
    pub uri: String,
    /// Royalty basis points that goes to creators in secondary sales (0-10000).
    pub seller_fee_basis_points: u16,
    /// Array of creators.
    pub creators: Option<Vec<Creator>>,
    // Immutable, once flipped, all sales of this metadata are considered secondary.
    pub primary_sale_happened: bool,
    // Whether or not the data struct is mutable (default is not).
    pub is_mutable: bool,
    /// Type of the token.
    pub token_standard: TokenStandard,
    /// Collection information.
    pub collection: Option<Collection>,
    /// Uses information.
    pub uses: Option<Uses>,
    /// Collection item details.
    pub collection_details: Option<CollectionDetails>,
    /// Programmable rule set for the asset.
    pub rule_set: Option<Pubkey>,
}

impl AssetData {
    pub fn new(
        token_standard: TokenStandard,
        name: String,
        symbol: String,
        uri: String,
        update_authority: Pubkey,
    ) -> Self {
        Self {
            name,
            symbol,
            uri,
            seller_fee_basis_points: 0,
            update_authority,
            creators: None,
            primary_sale_happened: false,
            is_mutable: true,
            token_standard,
            collection: None,
            uses: None,
            collection_details: None,
            rule_set: None,
        }
    }

    pub fn as_data_v2(&self) -> DataV2 {
        DataV2 {
            collection: self.collection.clone(),
            creators: self.creators.clone(),
            name: self.name.clone(),
            seller_fee_basis_points: self.seller_fee_basis_points,
            symbol: self.symbol.clone(),
            uri: self.uri.clone(),
            uses: self.uses.clone(),
        }
    }

    pub fn as_data(&self) -> Data {
        Data {
            name: self.name.clone(),
            symbol: self.symbol.clone(),
            uri: self.uri.clone(),
            seller_fee_basis_points: self.seller_fee_basis_points,
            creators: self.creators.clone(),
        }
    }
}