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
use crate::{id, ErrorCode};
use anchor_lang::{
prelude::*,
solana_program::{program::invoke_signed, system_instruction},
};
pub const NAME_MAX_LEN: usize = 40; pub const NAME_DEFAULT_SIZE: usize = 4 + NAME_MAX_LEN; pub const DESCRIPTION_MAX_LEN: usize = 60;
pub const DESCRIPTION_DEFAULT_SIZE: usize = 4 + DESCRIPTION_MAX_LEN;
pub const HOLDER_PREFIX: &str = "holder";
pub const HISTORY_PREFIX: &str = "history";
pub const VAULT_OWNER_PREFIX: &str = "mt_vault";
pub const PAYOUT_TICKET_PREFIX: &str = "payout_ticket";
pub const PRIMARY_METADATA_CREATORS_PREFIX: &str = "primary_creators";
pub const FLAG_ACCOUNT_SIZE: usize = 1; pub const MAX_PRIMARY_CREATORS_LEN: usize = 5; pub fn assert_derivation(program_id: &Pubkey, account: &AccountInfo, path: &[&[u8]]) -> Result<u8> {
let (key, bump) = Pubkey::find_program_address(path, program_id);
if key != *account.key {
return Err(ErrorCode::DerivedKeyInvalid.into());
}
Ok(bump)
}
pub fn find_treasury_owner_address(
treasury_mint: &Pubkey,
selling_resource: &Pubkey,
) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[
HOLDER_PREFIX.as_bytes(),
treasury_mint.as_ref(),
selling_resource.as_ref(),
],
&id(),
)
}
pub fn find_vault_owner_address(resource_mint: &Pubkey, store: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[
VAULT_OWNER_PREFIX.as_bytes(),
resource_mint.as_ref(),
store.as_ref(),
],
&id(),
)
}
pub fn find_trade_history_address(wallet: &Pubkey, market: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[HISTORY_PREFIX.as_bytes(), wallet.as_ref(), market.as_ref()],
&id(),
)
}
pub fn find_payout_ticket_address(market: &Pubkey, funder: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[
PAYOUT_TICKET_PREFIX.as_bytes(),
market.as_ref(),
funder.as_ref(),
],
&id(),
)
}
pub fn find_primary_metadata_creators(metadata: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[
PRIMARY_METADATA_CREATORS_PREFIX.as_bytes(),
metadata.as_ref(),
],
&id(),
)
}
#[inline(always)]
pub fn sys_create_account<'a>(
from: &AccountInfo<'a>,
to: &AccountInfo<'a>,
lamports: u64,
space: usize,
owner: &Pubkey,
signer_seeds: &[&[u8]],
) -> Result<()> {
invoke_signed(
&system_instruction::create_account(from.key, to.key, lamports, space as u64, owner),
&[from.clone(), to.clone()],
&[signer_seeds],
)?;
Ok(())
}
#[inline(always)]
pub fn sys_transfer<'a>(
from: &AccountInfo<'a>,
to: &AccountInfo<'a>,
lamports: u64,
signer_seeds: &[&[u8]],
) -> Result<()> {
invoke_signed(
&system_instruction::transfer(from.key, to.key, lamports),
&[from.clone(), to.clone()],
&[signer_seeds],
)?;
Ok(())
}
#[inline(always)]
pub fn mpl_mint_new_edition_from_master_edition_via_token<'a>(
new_metadata: &AccountInfo<'a>,
new_edition: &AccountInfo<'a>,
new_mint: &AccountInfo<'a>,
new_mint_authority: &AccountInfo<'a>,
user_wallet: &AccountInfo<'a>,
token_account_owner: &AccountInfo<'a>,
token_account: &AccountInfo<'a>,
master_metadata: &AccountInfo<'a>,
master_edition: &AccountInfo<'a>,
metadata_mint: &Pubkey,
edition_marker: &AccountInfo<'a>,
token_program: &AccountInfo<'a>,
system_program: &AccountInfo<'a>,
rent: &AccountInfo<'a>,
edition: u64,
signers_seeds: &[&[u8]],
) -> Result<()> {
let tx = mpl_token_metadata::instruction::mint_new_edition_from_master_edition_via_token(
mpl_token_metadata::id(),
*new_metadata.key,
*new_edition.key,
*master_edition.key,
*new_mint.key,
*new_mint_authority.key,
*user_wallet.key,
*token_account_owner.key,
*token_account.key,
*user_wallet.key,
*master_metadata.key,
*metadata_mint,
edition,
);
invoke_signed(
&tx,
&[
new_metadata.clone(),
new_edition.clone(),
master_edition.clone(),
new_mint.clone(),
edition_marker.clone(),
new_mint_authority.clone(),
user_wallet.clone(),
token_account_owner.clone(),
token_account.clone(),
user_wallet.clone(),
master_metadata.clone(),
token_program.clone(),
system_program.clone(),
rent.clone(),
],
&[signers_seeds],
)?;
Ok(())
}
#[inline(always)]
pub fn mpl_update_primary_sale_happened_via_token<'a>(
metadata: &AccountInfo<'a>,
owner: &AccountInfo<'a>,
token: &AccountInfo<'a>,
signers_seeds: &[&[u8]],
) -> Result<()> {
let tx = mpl_token_metadata::instruction::update_primary_sale_happened_via_token(
mpl_token_metadata::id(),
metadata.key(),
owner.key(),
token.key(),
);
invoke_signed(
&tx,
&[metadata.clone(), owner.clone(), token.clone()],
&[signers_seeds],
)?;
Ok(())
}
#[inline(always)]
pub fn mpl_update_metadata_accounts_v2<'a>(
metadata: &AccountInfo<'a>,
update_authority: &AccountInfo<'a>,
new_update_authority: Option<Pubkey>,
data: Option<mpl_token_metadata::state::DataV2>,
primary_sale_happened: Option<bool>,
is_mutable: Option<bool>,
signers_seeds: &[&[u8]],
) -> Result<()> {
let tx = mpl_token_metadata::instruction::update_metadata_accounts_v2(
mpl_token_metadata::id(),
metadata.key(),
update_authority.key(),
new_update_authority,
data,
primary_sale_happened,
is_mutable,
);
invoke_signed(
&tx,
&[metadata.clone(), update_authority.clone()],
&[signers_seeds],
)?;
Ok(())
}
pub fn puffed_out_string(s: String, size: usize) -> String {
s.to_string() + std::str::from_utf8(&vec![0u8; size - s.len()]).unwrap()
}
pub fn assert_keys_equal(key1: Pubkey, key2: Pubkey) -> Result<()> {
if key1 != key2 {
Err(ErrorCode::PublicKeyMismatch.into())
} else {
Ok(())
}
}
pub fn calculate_primary_shares_for_creator(total_amount: u64, shares: u64) -> Result<u64> {
Ok(total_amount
.checked_mul(shares)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(100)
.ok_or(ErrorCode::MathOverflow)?)
}
pub fn calculate_secondary_shares_for_creator(
total_amount: u64,
seller_fee_basis_points: u64,
shares: u64,
) -> Result<u64> {
Ok((total_amount
.checked_mul(seller_fee_basis_points)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(10000)
.ok_or(ErrorCode::MathOverflow)?)
.checked_mul(shares)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(100)
.ok_or(ErrorCode::MathOverflow)?)
}
pub fn calculate_secondary_shares_for_market_owner(
total_amount: u64,
seller_fee_basis_points: u64,
) -> Result<u64> {
Ok(total_amount
.checked_sub(
total_amount
.checked_mul(seller_fee_basis_points)
.ok_or(ErrorCode::MathOverflow)?
.checked_div(10000)
.ok_or(ErrorCode::MathOverflow)?,
)
.ok_or(ErrorCode::MathOverflow)?)
}