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
use crate::{error::ErrorCode, state::SellingResourceState, utils::*, InitSellingResource};
use anchor_lang::prelude::*;
use anchor_spl::token;
use mpl_token_metadata::state::TokenMetadataAccount;
impl<'info> InitSellingResource<'info> {
pub fn process(
&mut self,
_master_edition_bump: u8,
_vault_owner_bump: u8,
max_supply: Option<u64>,
) -> Result<()> {
let store = &self.store;
let admin = &self.admin;
let selling_resource = &mut self.selling_resource;
let selling_resource_owner = &self.selling_resource_owner;
let resource_mint = &self.resource_mint;
let master_edition_info = &self.master_edition.to_account_info();
let metadata = &self.metadata;
let vault = &self.vault;
let owner = &self.owner;
let resource_token = &self.resource_token;
let token_program = &self.token_program;
assert_derivation(
&mpl_token_metadata::id(),
master_edition_info,
&[
mpl_token_metadata::state::PREFIX.as_bytes(),
mpl_token_metadata::id().as_ref(),
resource_mint.key().as_ref(),
mpl_token_metadata::state::EDITION.as_bytes(),
],
)?;
assert_derivation(
&mpl_token_metadata::id(),
metadata,
&[
mpl_token_metadata::state::PREFIX.as_bytes(),
mpl_token_metadata::id().as_ref(),
resource_mint.key().as_ref(),
],
)?;
let metadata =
mpl_token_metadata::state::Metadata::from_account_info(&metadata.to_account_info())?;
if !metadata.primary_sale_happened {
if let Some(creators) = metadata.data.creators {
if creators.is_empty() {
return Err(ErrorCode::MetadataCreatorsIsEmpty.into());
}
} else {
return Err(ErrorCode::MetadataCreatorsIsEmpty.into());
}
}
let master_edition =
mpl_token_metadata::state::MasterEditionV2::from_account_info(master_edition_info)?;
let mut actual_max_supply = max_supply;
if let Some(me_max_supply) = master_edition.max_supply {
let x = if let Some(max_supply) = max_supply {
let available_supply = me_max_supply - master_edition.supply;
if max_supply > available_supply {
return Err(ErrorCode::SupplyIsGtThanAvailable.into());
} else {
max_supply
}
} else {
return Err(ErrorCode::SupplyIsNotProvided.into());
};
actual_max_supply = Some(x);
}
let cpi_program = token_program.to_account_info();
let cpi_accounts = token::Transfer {
from: resource_token.to_account_info(),
to: vault.to_account_info(),
authority: admin.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
token::transfer(cpi_ctx, 1)?;
selling_resource.store = store.key();
selling_resource.owner = selling_resource_owner.key();
selling_resource.resource = resource_mint.key();
selling_resource.vault = vault.key();
selling_resource.vault_owner = owner.key();
selling_resource.supply = 0;
selling_resource.max_supply = actual_max_supply;
selling_resource.state = SellingResourceState::Created;
Ok(())
}
}