Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant CPI; clean up account closing #880

Merged
merged 5 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 38 additions & 105 deletions auction-house/program/src/execute_sale/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use crate::{constants::*, errors::*, utils::*, AuctionHouse, Auctioneer, AuthorityScope, *};
use anchor_lang::{
prelude::*,
solana_program::{program::invoke, program_pack::Pack},
AnchorDeserialize,
};
use solana_program::program_memory::sol_memset;
use anchor_lang::{prelude::*, solana_program::program_pack::Pack, AnchorDeserialize};
use spl_token::state::Account as SplAccount;

/// Accounts for the [`execute_sale` handler](auction_house/fn.execute_sale.html).
Expand Down Expand Up @@ -1047,16 +1042,17 @@ fn auctioneer_execute_sale_logic<'c, 'info>(
msg!("No delegate detected on token account.");
return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into());
}
let buyer_ts_data = &mut buyer_trade_state.try_borrow_mut_data()?;
let seller_ts_data = &mut seller_trade_state.try_borrow_mut_data()?;

let ts_bump = if buyer_ts_data.len() > 0 {
buyer_ts_data[0]
let ts_bump = if buyer_trade_state.data_len() > 0 {
buyer_trade_state.try_borrow_data()?[0]
} else {
return Err(AuctionHouseError::BuyerTradeStateNotValid.into());
};

if ts_bump == 0 || seller_ts_data.len() == 0 || seller_ts_data[0] == 0 {
if ts_bump == 0
|| seller_trade_state.data_len() == 0
|| seller_trade_state.try_borrow_data()?[0] == 0
{
return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into());
}

Expand Down Expand Up @@ -1342,55 +1338,23 @@ fn auctioneer_execute_sale_logic<'c, 'info>(
)?;

// Close the buyer trade state account if the rest of execute sale was successful.
let curr_buyer_lamp = buyer_trade_state.lamports();
**buyer_trade_state.lamports.borrow_mut() = 0;
sol_memset(&mut *buyer_ts_data, 0, TRADE_STATE_SIZE);
**fee_payer.lamports.borrow_mut() = fee_payer
.lamports()
.checked_add(curr_buyer_lamp)
.ok_or(AuctionHouseError::NumericalOverflow)?;
close_account(
&buyer_trade_state.to_account_info(),
&fee_payer.to_account_info(),
)?;

let token_account_data = SplAccount::unpack(&token_account.data.borrow())?;
if token_account_data.delegated_amount == 0 {
if seller.to_account_info().is_signer {
invoke(
&revoke(
&token_program.key(),
&token_account.key(),
&seller.key(),
&[],
)
.unwrap(),
&[
token_program.to_account_info(),
token_account.to_account_info(),
seller.to_account_info(),
],
)?;
}

let curr_seller_lamp = seller_trade_state.lamports();
**seller_trade_state.lamports.borrow_mut() = 0;
sol_memset(&mut *seller_ts_data, 0, TRADE_STATE_SIZE);

**fee_payer.lamports.borrow_mut() = fee_payer
.lamports()
.checked_add(curr_seller_lamp)
.ok_or(AuctionHouseError::NumericalOverflow)?;
close_account(
&seller_trade_state.to_account_info(),
&fee_payer.to_account_info(),
)?;

if free_trade_state.lamports() > 0 {
let curr_buyer_lamp = free_trade_state.lamports();
**free_trade_state.lamports.borrow_mut() = 0;

**fee_payer.lamports.borrow_mut() = fee_payer
.lamports()
.checked_add(curr_buyer_lamp)
.ok_or(AuctionHouseError::NumericalOverflow)?;
sol_memset(
*free_trade_state.try_borrow_mut_data()?,
0,
TRADE_STATE_SIZE,
);
close_account(
&free_trade_state.to_account_info(),
&fee_payer.to_account_info(),
)?;
}
}
Ok(())
Expand Down Expand Up @@ -1462,16 +1426,16 @@ fn execute_sale_logic<'c, 'info>(
return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into());
};

let buyer_ts_data = &mut buyer_trade_state.try_borrow_mut_data()?;
let seller_ts_data = &mut seller_trade_state.try_borrow_mut_data()?;

let ts_bump = if buyer_ts_data.len() > 0 {
buyer_ts_data[0]
let ts_bump = if buyer_trade_state.data_len() > 0 {
buyer_trade_state.try_borrow_data()?[0]
} else {
return Err(AuctionHouseError::BuyerTradeStateNotValid.into());
};

if ts_bump == 0 || seller_ts_data.len() == 0 || seller_ts_data[0] == 0 {
if ts_bump == 0
|| seller_trade_state.data_len() == 0
|| seller_trade_state.try_borrow_data()?[0] == 0
{
return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into());
}

Expand Down Expand Up @@ -1750,56 +1714,25 @@ fn execute_sale_logic<'c, 'info>(
)?;

// Close the buyer trade state account if the rest of execute sale was successful.
let curr_buyer_lamp = buyer_trade_state.lamports();
**buyer_trade_state.lamports.borrow_mut() = 0;
sol_memset(&mut *buyer_ts_data, 0, TRADE_STATE_SIZE);
**fee_payer.lamports.borrow_mut() = fee_payer
.lamports()
.checked_add(curr_buyer_lamp)
.ok_or(AuctionHouseError::NumericalOverflow)?;
close_account(
&buyer_trade_state.to_account_info(),
&fee_payer.to_account_info(),
)?;

let token_account_data = SplAccount::unpack(&token_account.data.borrow())?;
if token_account_data.delegated_amount == 0 {
if seller.to_account_info().is_signer {
invoke(
&revoke(
&token_program.key(),
&token_account.key(),
&seller.key(),
&[],
)
.unwrap(),
&[
token_program.to_account_info(),
token_account.to_account_info(),
seller.to_account_info(),
],
)?;
}
let curr_seller_lamp = seller_trade_state.lamports();
**seller_trade_state.lamports.borrow_mut() = 0;
sol_memset(&mut *seller_ts_data, 0, TRADE_STATE_SIZE);

**fee_payer.lamports.borrow_mut() = fee_payer
.lamports()
.checked_add(curr_seller_lamp)
.ok_or(AuctionHouseError::NumericalOverflow)?;
close_account(
&seller_trade_state.to_account_info(),
&fee_payer.to_account_info(),
)?;

if free_trade_state.lamports() > 0 {
let curr_buyer_lamp = free_trade_state.lamports();
**free_trade_state.lamports.borrow_mut() = 0;

**fee_payer.lamports.borrow_mut() = fee_payer
.lamports()
.checked_add(curr_buyer_lamp)
.ok_or(AuctionHouseError::NumericalOverflow)?;
sol_memset(
*free_trade_state.try_borrow_mut_data()?,
0,
TRADE_STATE_SIZE,
);
close_account(
&free_trade_state.to_account_info(),
&fee_payer.to_account_info(),
)?;
}
};
}

Ok(())
}
24 changes: 23 additions & 1 deletion auction-house/program/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anchor_lang::{
prelude::*,
solana_program::{
program::invoke_signed,
program_memory::sol_memcmp,
program_memory::{sol_memcmp, sol_memset},
program_option::COption,
program_pack::{IsInitialized, Pack},
pubkey::PUBKEY_BYTES,
Expand Down Expand Up @@ -685,3 +685,25 @@ pub fn assert_scopes_eq(

Ok(())
}

pub fn close_account<'a>(
source_account: &AccountInfo<'a>,
receiver_account: &AccountInfo<'a>,
) -> Result<()> {
let current_lamports = source_account.lamports();
let account_data_size = source_account.data_len();

**source_account.lamports.borrow_mut() = 0;
**receiver_account.lamports.borrow_mut() = receiver_account
.lamports()
.checked_add(current_lamports)
.ok_or(AuctionHouseError::NumericalOverflow)?;

sol_memset(
&mut *source_account.try_borrow_mut_data()?,
0,
account_data_size,
);

Ok(())
}
1 change: 0 additions & 1 deletion release.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
consolidate-commits = false
consolidate-pushes = false
dependent-version = "ignore"
pre-release-commit-message = "(cargo-release) {{crate_name}} v{{version}}"