-
Notifications
You must be signed in to change notification settings - Fork 8
/
mod.rs
468 lines (429 loc) · 17.7 KB
/
mod.rs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use crate::{
constants::{LISTING, REWARD_CENTER},
errors::RewardCenterError,
metaplex_cpi::auction_house::{make_auctioneer_instruction, AuctioneerInstructionArgs},
state::{Listing, RewardCenter},
};
use anchor_lang::{
prelude::{Result, *},
InstructionData,
};
use anchor_spl::{
associated_token::AssociatedToken,
token::{transfer, Mint, Token, TokenAccount, Transfer},
};
use mpl_auction_house::{
constants::{AUCTIONEER, FEE_PAYER, PREFIX, SIGNER, TREASURY},
cpi::accounts::{AuctioneerDeposit, AuctioneerExecuteSale, AuctioneerPublicBuy},
instruction::AuctioneerExecuteSale as AuctioneerExecuteSaleParams,
program::AuctionHouse as AuctionHouseProgram,
utils::assert_metadata_valid,
AuctionHouse, Auctioneer,
};
use solana_program::program::invoke_signed;
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct BuyListingParams {
pub buyer_trade_state_bump: u8,
pub escrow_payment_bump: u8,
pub free_trade_state_bump: u8,
pub seller_trade_state_bump: u8,
pub program_as_signer_bump: u8,
}
#[derive(Accounts, Clone)]
#[instruction(buy_listing_params: BuyListingParams)]
pub struct BuyListing<'info> {
// Accounts passed into Auction House CPI call
/// CHECK: Verified through CPI
/// Buyer user wallet account.
#[account(mut)]
pub buyer: UncheckedAccount<'info>,
/// CHECK: Validated in public_bid_logic.
#[account(mut)]
pub payment_account: UncheckedAccount<'info>,
/// CHECK: Validated in public_bid_logic.
pub transfer_authority: UncheckedAccount<'info>,
/// The token account to receive the buyer rewards.
#[account(
mut,
constraint = reward_center.token_mint == buyer_reward_token_account.mint @ RewardCenterError::MintMismatch,
constraint = buyer_reward_token_account.owner == buyer.key() @ RewardCenterError::BuyerTokenAccountMismatch,
)]
pub buyer_reward_token_account: Box<Account<'info, TokenAccount>>,
/// CHECK: Verified through CPI
/// Seller user wallet account.
#[account(mut)]
pub seller: UncheckedAccount<'info>,
/// The token account to receive the seller rewards.
#[account(
mut,
constraint = buyer_reward_token_account.mint == seller_reward_token_account.mint @ RewardCenterError::MintMismatch,
constraint = seller_reward_token_account.owner == seller.key() @ RewardCenterError::SellerTokenAccountMismatch,
)]
pub seller_reward_token_account: Box<Account<'info, TokenAccount>>,
// Accounts used for Auctioneer
/// The Listing Config used for listing settings
#[account(
mut,
seeds = [
LISTING.as_bytes(),
seller.key().as_ref(),
metadata.key().as_ref(),
reward_center.key().as_ref(),
],
bump = listing.bump,
close = seller,
)]
pub listing: Box<Account<'info, Listing>>,
///Token account where the SPL token is stored.
#[account(
mut,
constraint = token_account.owner == seller.key(),
constraint = token_account.mint == token_mint.key() @ RewardCenterError::MintMismatch,
)]
pub token_account: Box<Account<'info, TokenAccount>>,
/// Token mint account for the SPL token.
pub token_mint: Box<Account<'info, Mint>>,
/// CHECK: assertion with mpl_auction_house assert_metadata_valid
/// Metaplex metadata account decorating SPL mint account.
#[account(mut)]
pub metadata: UncheckedAccount<'info>,
/// Auction House treasury mint account.
#[account(
address = auction_house.treasury_mint
)]
pub treasury_mint: Box<Account<'info, Mint>>,
/// CHECK: Verified through CPI
/// Seller SOL or SPL account to receive payment at.
#[account(mut)]
pub seller_payment_receipt_account: UncheckedAccount<'info>,
/// CHECK: Verified through CPI
/// Buyer SPL token account to receive purchased item at.
#[account(mut)]
pub buyer_receipt_token_account: UncheckedAccount<'info>,
/// CHECK: Verified through CPI
/// Auction House instance authority.
pub authority: UncheckedAccount<'info>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
/// Buyer escrow payment account.
#[account(
mut,
seeds = [
PREFIX.as_bytes(),
auction_house.key().as_ref(),
buyer.key().as_ref()
],
seeds::program = auction_house_program,
bump = buy_listing_params.escrow_payment_bump
)]
pub escrow_payment_account: UncheckedAccount<'info>,
/// Auction House instance PDA account.
#[account(
seeds = [
PREFIX.as_bytes(),
auction_house.creator.as_ref(),
auction_house.treasury_mint.as_ref()
],
seeds::program = auction_house_program,
bump = auction_house.bump,
has_one = treasury_mint,
has_one = auction_house_treasury,
has_one = auction_house_fee_account
)]
pub auction_house: Box<Account<'info, AuctionHouse>>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
/// Auction House instance fee account.
#[account(
mut,
seeds = [
PREFIX.as_bytes(),
auction_house.key().as_ref(),
FEE_PAYER.as_bytes()
],
seeds::program = auction_house_program,
bump = auction_house.fee_payer_bump
)]
pub auction_house_fee_account: UncheckedAccount<'info>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
/// Auction House instance treasury account.
#[account(
mut,
seeds = [
PREFIX.as_bytes(),
auction_house.key().as_ref(),
TREASURY.as_bytes()
],
seeds::program = auction_house_program,
bump = auction_house.treasury_bump
)]
pub auction_house_treasury: UncheckedAccount<'info>,
/// CHECK: Verified through CPI
/// Buyer trade state PDA account encoding the buy order.
#[account(
mut,
seeds = [
PREFIX.as_bytes(),
buyer.key().as_ref(),
auction_house.key().as_ref(),
treasury_mint.key().as_ref(),
token_mint.key().as_ref(),
&listing.price.to_le_bytes(),
&listing.token_size.to_le_bytes()
],
seeds::program = auction_house_program,
bump = buy_listing_params.buyer_trade_state_bump
)]
pub buyer_trade_state: UncheckedAccount<'info>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
/// Seller trade state PDA account encoding the sell order.
#[account(
mut,
seeds = [
PREFIX.as_bytes(),
seller.key().as_ref(),
auction_house.key().as_ref(),
token_account.key().as_ref(),
treasury_mint.key().as_ref(),
token_mint.key().as_ref(),
&u64::MAX.to_le_bytes(),
&listing.token_size.to_le_bytes()
],
seeds::program = auction_house_program,
bump = buy_listing_params.seller_trade_state_bump,
)]
pub seller_trade_state: UncheckedAccount<'info>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
/// Free seller trade state PDA account encoding a free sell order.
#[account(
mut,
seeds = [
PREFIX.as_bytes(),
seller.key().as_ref(),
auction_house.key().as_ref(),
token_account.key().as_ref(),
treasury_mint.key().as_ref(),
token_mint.key().as_ref(),
&0u64.to_le_bytes(),
&listing.token_size.to_le_bytes()
],
seeds::program = auction_house_program,
bump = buy_listing_params.free_trade_state_bump
)]
pub free_seller_trade_state: UncheckedAccount<'info>,
/// CHECK: Verified through CPI
/// The auctioneer authority PDA running this auction.
#[account(
has_one = auction_house,
seeds = [
REWARD_CENTER.as_bytes(),
auction_house.key().as_ref()
],
bump = reward_center.bump
)]
pub reward_center: Box<Account<'info, RewardCenter>>,
#[
account(
mut,
constraint = reward_center.token_mint == reward_center_reward_token_account.mint @ RewardCenterError::MintMismatch
)
]
/// The token account holding the reward token for the reward center.
pub reward_center_reward_token_account: Box<Account<'info, TokenAccount>>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
/// The auctioneer PDA owned by Auction House storing scopes.
#[account(
seeds = [
AUCTIONEER.as_bytes(),
auction_house.key().as_ref(),
reward_center.key().as_ref()
],
seeds::program = auction_house_program,
bump = ah_auctioneer_pda.bump
)]
pub ah_auctioneer_pda: Box<Account<'info, Auctioneer>>,
/// CHECK: Not dangerous. Account seeds checked in constraint.
#[account(
seeds = [
PREFIX.as_bytes(),
SIGNER.as_bytes()
],
seeds::program = auction_house_program,
bump = buy_listing_params.program_as_signer_bump
)]
pub program_as_signer: UncheckedAccount<'info>,
/// Auction House Program
pub auction_house_program: Program<'info, AuctionHouseProgram>,
/// Token Program
pub token_program: Program<'info, Token>,
/// System Program
pub system_program: Program<'info, System>,
/// Associated Token Program
pub ata_program: Program<'info, AssociatedToken>,
/// Rent
pub rent: Sysvar<'info, Rent>,
}
pub fn handler<'info>(
ctx: Context<'_, '_, '_, 'info, BuyListing<'info>>,
BuyListingParams {
buyer_trade_state_bump,
escrow_payment_bump,
program_as_signer_bump,
free_trade_state_bump,
..
}: BuyListingParams,
) -> Result<()> {
let metadata = &ctx.accounts.metadata;
let reward_center = &ctx.accounts.reward_center;
let auction_house = &ctx.accounts.auction_house;
let token_account = &ctx.accounts.token_account;
let listing = &ctx.accounts.listing;
let listing_price = listing.price;
let token_size = listing.token_size;
let auction_house_key = auction_house.key();
let reward_center_signer_seeds: &[&[&[u8]]] = &[&[
REWARD_CENTER.as_bytes(),
auction_house_key.as_ref(),
&[reward_center.bump],
]];
assert_metadata_valid(metadata, token_account)?;
mpl_auction_house::cpi::auctioneer_deposit(
CpiContext::new_with_signer(
ctx.accounts.auction_house_program.to_account_info(),
AuctioneerDeposit {
wallet: ctx.accounts.buyer.to_account_info(),
transfer_authority: ctx.accounts.transfer_authority.to_account_info(),
treasury_mint: ctx.accounts.treasury_mint.to_account_info(),
ah_auctioneer_pda: ctx.accounts.ah_auctioneer_pda.to_account_info(),
auctioneer_authority: ctx.accounts.reward_center.to_account_info(),
auction_house: ctx.accounts.auction_house.to_account_info(),
auction_house_fee_account: ctx.accounts.auction_house_fee_account.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
escrow_payment_account: ctx.accounts.escrow_payment_account.to_account_info(),
payment_account: ctx.accounts.payment_account.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
},
reward_center_signer_seeds,
),
escrow_payment_bump,
listing_price,
)?;
mpl_auction_house::cpi::auctioneer_public_buy(
CpiContext::new_with_signer(
ctx.accounts.auction_house_program.to_account_info(),
AuctioneerPublicBuy {
wallet: ctx.accounts.buyer.to_account_info(),
payment_account: ctx.accounts.payment_account.to_account_info(),
transfer_authority: ctx.accounts.transfer_authority.to_account_info(),
treasury_mint: ctx.accounts.treasury_mint.to_account_info(),
token_account: ctx.accounts.token_account.to_account_info(),
metadata: ctx.accounts.metadata.to_account_info(),
escrow_payment_account: ctx.accounts.escrow_payment_account.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
auctioneer_authority: ctx.accounts.reward_center.to_account_info(),
auction_house: ctx.accounts.auction_house.to_account_info(),
auction_house_fee_account: ctx.accounts.auction_house_fee_account.to_account_info(),
buyer_trade_state: ctx.accounts.buyer_trade_state.to_account_info(),
ah_auctioneer_pda: ctx.accounts.ah_auctioneer_pda.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
},
reward_center_signer_seeds,
),
buyer_trade_state_bump,
escrow_payment_bump,
listing_price,
token_size,
)?;
let (execute_sale_ix, execute_sale_account_infos) =
make_auctioneer_instruction(AuctioneerInstructionArgs {
accounts: AuctioneerExecuteSale {
buyer: ctx.accounts.buyer.to_account_info(),
seller: ctx.accounts.seller.to_account_info(),
token_account: ctx.accounts.token_account.to_account_info(),
ah_auctioneer_pda: ctx.accounts.ah_auctioneer_pda.to_account_info(),
auction_house: ctx.accounts.auction_house.to_account_info(),
auction_house_fee_account: ctx.accounts.auction_house_fee_account.to_account_info(),
auction_house_treasury: ctx.accounts.auction_house_treasury.to_account_info(),
buyer_receipt_token_account: ctx
.accounts
.buyer_receipt_token_account
.to_account_info(),
seller_payment_receipt_account: ctx
.accounts
.seller_payment_receipt_account
.to_account_info(),
buyer_trade_state: ctx.accounts.buyer_trade_state.to_account_info(),
free_trade_state: ctx.accounts.free_seller_trade_state.to_account_info(),
seller_trade_state: ctx.accounts.seller_trade_state.to_account_info(),
escrow_payment_account: ctx.accounts.escrow_payment_account.to_account_info(),
program_as_signer: ctx.accounts.program_as_signer.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
metadata: ctx.accounts.metadata.to_account_info(),
token_mint: ctx.accounts.token_mint.to_account_info(),
treasury_mint: ctx.accounts.treasury_mint.to_account_info(),
auctioneer_authority: ctx.accounts.reward_center.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
ata_program: ctx.accounts.ata_program.to_account_info(),
rent: ctx.accounts.rent.to_account_info(),
},
instruction_data: AuctioneerExecuteSaleParams {
escrow_payment_bump,
program_as_signer_bump,
token_size,
buyer_price: listing_price,
_free_trade_state_bump: free_trade_state_bump,
}
.data(),
auctioneer_authority: ctx.accounts.reward_center.key(),
remaining_accounts: Some(ctx.remaining_accounts),
});
invoke_signed(
&execute_sale_ix,
&execute_sale_account_infos,
reward_center_signer_seeds,
)?;
let (seller_payout, buyer_payout) = reward_center.payouts(listing_price)?;
// Buyer transfer
let reward_center_reward_token_balance = ctx.accounts.reward_center_reward_token_account.amount;
if buyer_payout > 0 && reward_center_reward_token_balance >= buyer_payout {
transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
Transfer {
authority: ctx.accounts.reward_center.to_account_info(),
from: ctx
.accounts
.reward_center_reward_token_account
.to_account_info(),
to: ctx.accounts.buyer_reward_token_account.to_account_info(),
},
reward_center_signer_seeds,
),
buyer_payout,
)?;
}
// Seller transfer
ctx.accounts.reward_center_reward_token_account.reload()?;
let reward_center_reward_token_balance = ctx.accounts.reward_center_reward_token_account.amount;
if seller_payout > 0 && reward_center_reward_token_balance >= seller_payout {
transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
Transfer {
authority: ctx.accounts.reward_center.to_account_info(),
from: ctx
.accounts
.reward_center_reward_token_account
.to_account_info(),
to: ctx.accounts.seller_reward_token_account.to_account_info(),
},
reward_center_signer_seeds,
),
seller_payout,
)?
};
Ok(())
}