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
use crate::{
error::ErrorCode,
state::{Creator, MarketState, PrimaryMetadataCreators},
utils::*,
Withdraw,
};
use anchor_lang::{prelude::*, solana_program::borsh::try_from_slice_unchecked};
use anchor_spl::{
associated_token::{self, get_associated_token_address},
token,
};
use mpl_token_metadata::state::TokenMetadataAccount;
impl<'info> Withdraw<'info> {
pub fn process(
&mut self,
treasury_owner_bump: u8,
_payout_ticket_bump: u8,
remaining_accounts: &[AccountInfo<'info>],
) -> Result<()> {
let market = &self.market;
let token_program = &self.token_program;
let associated_token_program = &self.associated_token_program;
let system_program = &self.system_program;
let treasury_holder = Box::new(&self.treasury_holder);
let treasury_mint = Box::new(&self.treasury_mint);
let treasury_owner = &self.owner;
let destination = &self.destination;
let selling_resource = &self.selling_resource;
let funder = &self.funder;
let payer = &self.payer;
let payout_ticket = &mut self.payout_ticket;
let rent = &self.rent;
let clock = &self.clock;
let metadata = &self.metadata.to_account_info();
let selling_resource_key = selling_resource.key();
let treasury_mint_key = market.treasury_mint;
let funder_key = funder.key();
if let Some(end_date) = market.end_date {
if clock.unix_timestamp as u64 <= end_date {
return Err(ErrorCode::MarketInInvalidState.into());
}
} else if market.state != MarketState::Ended {
return Err(ErrorCode::MarketInInvalidState.into());
}
assert_derivation(
&mpl_token_metadata::id(),
metadata,
&[
mpl_token_metadata::state::PREFIX.as_bytes(),
mpl_token_metadata::id().as_ref(),
selling_resource.resource.as_ref(),
],
)?;
let metadata: mpl_token_metadata::state::Metadata =
mpl_token_metadata::state::Metadata::from_account_info(metadata)?;
let actual_creators = if !metadata.primary_sale_happened {
if remaining_accounts.is_empty() {
return Err(ErrorCode::PrimaryMetadataCreatorsNotProvided.into());
}
let primary_metadata_creators_data = remaining_accounts[0].data.borrow()[8..].to_vec();
let primary_metadata_creators = try_from_slice_unchecked::<PrimaryMetadataCreators>(
&primary_metadata_creators_data,
)?;
Box::new(Some(primary_metadata_creators.creators))
} else if let Some(creators) = metadata.data.creators {
Box::new(Some(
creators
.iter()
.map(|item| Creator::from(item.clone()))
.collect(),
))
} else {
Box::new(None)
};
let funder_creator = if let Some(creators) = *actual_creators {
let funder_creator = creators.iter().find(|c| c.address == funder_key).cloned();
if funder_creator.is_none() && funder_key != market.owner {
return Err(ErrorCode::FunderIsInvalid.into());
}
funder_creator
} else if funder_key != market.owner {
return Err(ErrorCode::FunderIsInvalid.into());
} else {
None
};
if payout_ticket.used {
return Err(ErrorCode::PayoutTicketExists.into());
} else {
payout_ticket.used = true;
}
let is_native = market.treasury_mint == System::id();
let amount = if metadata.primary_sale_happened {
if funder_creator.is_some() && funder_key == market.owner {
let funder_creator = funder_creator.as_ref().unwrap();
let funder_as_creator_share = calculate_secondary_shares_for_creator(
market.funds_collected,
metadata.data.seller_fee_basis_points as u64,
funder_creator.share as u64,
)?;
let funder_as_market_owner_share = calculate_secondary_shares_for_market_owner(
market.funds_collected,
metadata.data.seller_fee_basis_points as u64,
)?;
funder_as_creator_share
.checked_add(funder_as_market_owner_share)
.ok_or(ErrorCode::MathOverflow)?
} else if let Some(funder_creator) = &funder_creator {
calculate_secondary_shares_for_creator(
market.funds_collected,
metadata.data.seller_fee_basis_points as u64,
funder_creator.share as u64,
)?
} else {
calculate_secondary_shares_for_market_owner(
market.funds_collected,
metadata.data.seller_fee_basis_points as u64,
)?
}
} else if let Some(funder_creator) = funder_creator {
calculate_primary_shares_for_creator(
market.funds_collected,
funder_creator.share as u64,
)?
} else {
return Err(ErrorCode::MarketOwnerDoesntHaveShares.into());
};
let signer_seeds: &[&[&[u8]]] = &[&[
HOLDER_PREFIX.as_bytes(),
treasury_mint_key.as_ref(),
selling_resource_key.as_ref(),
&[treasury_owner_bump],
]];
if is_native {
if funder_key != destination.key() {
return Err(ErrorCode::InvalidFunderDestination.into());
}
sys_transfer(
&treasury_holder.to_account_info(),
&destination.to_account_info(),
amount,
signer_seeds[0],
)?;
} else {
if *treasury_mint.owner != spl_token::id() {
return Err(ProgramError::InvalidArgument.into());
}
if *treasury_holder.owner != spl_token::id() {
return Err(ProgramError::InvalidArgument.into());
}
let associated_token_account =
get_associated_token_address(&funder_key, &market.treasury_mint);
if associated_token_account != destination.key() {
return Err(ErrorCode::InvalidFunderDestination.into());
}
if destination.lamports() == 0 && destination.data_is_empty() {
let cpi_program = associated_token_program.to_account_info();
let cpi_accounts = associated_token::Create {
payer: payer.to_account_info(),
associated_token: destination.to_account_info(),
authority: funder.to_account_info(),
mint: treasury_mint.to_account_info(),
rent: rent.to_account_info(),
token_program: token_program.to_account_info(),
system_program: system_program.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
associated_token::create(cpi_ctx)?;
}
let cpi_program = token_program.to_account_info();
let cpi_accounts = token::Transfer {
from: treasury_holder.to_account_info(),
to: destination.to_account_info(),
authority: treasury_owner.to_account_info(),
};
let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);
token::transfer(cpi_ctx, amount)?;
}
Ok(())
}
}