nft_image_and_metadata_generator/
metadata.rs

1/// The high level metadata representation of the NFT collection.
2/// - ```collection_name```: Name of the NFT collection.
3/// - ```symbol```: Symbol used to identify the NFT collection.
4/// - ```description```: Description of the NFT collection.
5/// - ```seller_fee_basis_points```: Define how much % you want from secondary market sales 1000 = 10%
6/// - ```external_url```: External url provided in the metadata.
7/// - ```creators```: Vector containing ```Creator``` structs instances.
8
9#[derive(Debug, Clone)]
10pub struct MetadataHeader {
11    /// Name of the NFT collection
12    pub collection_name: String,
13    pub symbol: String,
14    pub description: String,
15    seller_fee_basis_points: u32,
16    external_url: String,
17    /// Vector containing ```Creator``` structs instances.
18    pub creators: Vec<Creator>,
19}
20
21impl MetadataHeader {
22    /// Returns a ```MetadataHeader``` instance
23    pub fn new(
24        collection_name: String,
25        symbol: String,
26        description: String,
27        seller_fee_basis_points: u32,
28        external_url: String,
29        creators: Vec<Creator>,
30    ) -> MetadataHeader {
31        MetadataHeader {
32            collection_name,
33            symbol,
34            description,
35            seller_fee_basis_points,
36            external_url,
37            creators,
38        }
39    }
40}
41
42/// ```Creator``` object used to define where the distribution of assets will go.
43/// Only important for Solana, for Ethereum you can fill these fields with random values of the correct type.
44/// ```address``` refers to the Solana address of the payee wallet. ```share``` is the percentage of Sol to be
45/// sent to the address. Note that for multiple ```Creator``` objects the sum of the shares must equal 100.
46#[derive(Debug, Clone)]
47pub struct Creator {
48    /// Solana address of the payee wallet
49    pub address: String,
50    /// Share of the Sol to be sent to the address, e.g. a value of 80 corresponds to 80%.
51    pub share: u8,
52}
53
54impl Creator {
55    /// Returns a ```Creator``` instance
56    pub fn new(address: String, share: u8) -> Creator {
57        Creator { address, share }
58    }
59}
60
61/// Attributes related to the NFT. This is automatically generated.
62#[derive(Debug, Clone)]
63pub struct Attributes {
64    trait_type: String,
65    value: String,
66}
67
68impl Attributes {
69    /// Returns a ```Attributes``` instance
70    pub fn new(trait_type: String, value: String) -> Attributes {
71        Attributes { trait_type, value }
72    }
73}
74
75/// Properties related to the NFT. This is automatically generated.
76#[derive(Debug, Clone)]
77pub struct Properties {
78    /// Returns a ```Properties``` instance
79    files: Vec<Files>,
80    category: String,
81    creators: Vec<Creator>,
82}
83
84impl Properties {
85    pub fn new(files: Vec<Files>, category: String, creators: Vec<Creator>) -> Properties {
86        Properties {
87            files,
88            category,
89            creators,
90        }
91    }
92}
93
94/// File information related to the NFT. This is automatically generated.
95#[derive(Debug, Clone)]
96pub struct Files {
97    uri: String,
98    _type: String,
99}
100
101impl Files {
102    /// Returns a ```Files``` instance
103    pub fn new(uri: String, _type: String) -> Files {
104        Files { uri, _type }
105    }
106}
107
108/// Trait used to convert metadata module objects to JSON objects.
109pub trait Json {
110    /// Convert the structs in the metadata module to JSON objects.
111    fn to_json(&self) -> json::JsonValue;
112}
113
114impl Json for MetadataHeader {
115    fn to_json(&self) -> json::JsonValue {
116        let mut metadata = json::JsonValue::new_object();
117        metadata["name"] = self.collection_name.clone().into();
118        metadata["symbol"] = self.symbol.clone().into();
119        metadata["description"] = self.description.clone().into();
120        metadata["seller_fee_basis_points"] = self.seller_fee_basis_points.into();
121        metadata["external_url"] = self.external_url.clone().into();
122        let mut creators: Vec<json::JsonValue> = Vec::new();
123        for creator in &self.creators {
124            creators.push(creator.to_json());
125        }
126        metadata["creators"] = creators.into();
127        metadata
128    }
129}
130
131impl Json for Creator {
132    fn to_json(&self) -> json::JsonValue {
133        let mut creator = json::JsonValue::new_object();
134        creator["address"] = self.address.clone().into();
135        creator["share"] = self.share.into();
136        creator
137    }
138}
139
140impl Json for Attributes {
141    fn to_json(&self) -> json::JsonValue {
142        let mut att = json::JsonValue::new_object();
143        att["trait_type"] = self.trait_type.clone().into();
144        att["value"] = self.value.clone().into();
145        att
146    }
147}
148
149impl Json for Properties {
150    fn to_json(&self) -> json::JsonValue {
151        let mut prop = json::JsonValue::new_object();
152        let mut files: Vec<json::JsonValue> = Vec::new();
153        for file in &self.files {
154            files.push(file.to_json());
155        }
156        prop["files"] = files.into();
157        prop["category"] = self.category.clone().into();
158        let mut creators: Vec<json::JsonValue> = Vec::new();
159        for creator in &self.creators {
160            creators.push(creator.to_json());
161        }
162        prop["creators"] = creators.into();
163        prop
164    }
165}
166
167impl Json for Files {
168    fn to_json(&self) -> json::JsonValue {
169        let mut files = json::JsonValue::new_object();
170        files["uri"] = self.uri.clone().into();
171        files["type"] = self._type.clone().into();
172        files
173    }
174}