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

solana: emit -> emit_cpi #187

Merged
merged 2 commits into from
Oct 7, 2024
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
12 changes: 6 additions & 6 deletions solana/programs/matching-engine/src/composite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub struct CheckedCustodian<'info> {
seeds = [Custodian::SEED_PREFIX],
bump = Custodian::BUMP,
)]
pub custodian: Account<'info, Custodian>,
pub custodian: Box<Account<'info, Custodian>>,
}

impl<'info> Deref for CheckedCustodian<'info> {
Expand Down Expand Up @@ -138,7 +138,7 @@ pub struct OwnerOnlyMut<'info> {
seeds = [Custodian::SEED_PREFIX],
bump = Custodian::BUMP,
)]
pub custodian: Account<'info, Custodian>,
pub custodian: Box<Account<'info, Custodian>>,
}

#[derive(Accounts)]
Expand Down Expand Up @@ -171,7 +171,7 @@ pub struct AdminMut<'info> {
seeds = [Custodian::SEED_PREFIX],
bump = Custodian::BUMP,
)]
pub custodian: Account<'info, Custodian>,
pub custodian: Box<Account<'info, Custodian>>,
}

#[derive(Accounts)]
Expand All @@ -195,7 +195,7 @@ pub struct LocalTokenRouter<'info> {
associated_token::mint = common::USDC_MINT,
associated_token::authority = token_router_emitter,
)]
pub token_router_mint_recipient: Account<'info, token::TokenAccount>,
pub token_router_mint_recipient: Box<Account<'info, token::TokenAccount>>,
}

#[derive(Accounts)]
Expand All @@ -208,7 +208,7 @@ pub struct ExistingMutRouterEndpoint<'info> {
],
bump = endpoint.bump,
)]
pub endpoint: Account<'info, RouterEndpoint>,
pub endpoint: Box<Account<'info, RouterEndpoint>>,
}

impl<'info> Deref for ExistingMutRouterEndpoint<'info> {
Expand Down Expand Up @@ -644,7 +644,7 @@ pub struct ReserveFastFillSequence<'info> {
// This check makes sure that the auction account did not exist before this
// instruction was called.
require!(
auction.vaa_hash == [0; 32],
auction.vaa_hash == <[u8; 32]>::default(),
MatchingEngineError::AuctionExists,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct SettledTokenAccountInfo {
#[derive(Debug)]
pub struct AuctionSettled {
/// The pubkey of the auction that was settled.
pub auction: Pubkey,
pub fast_vaa_hash: [u8; 32],

/// If there was an active auction, this field will have the pubkey of the best offer token that
/// was paid back and its balance after repayment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anchor_lang::prelude::*;
#[derive(Debug)]
pub struct AuctionUpdated {
pub config_id: u32,
pub auction: Pubkey,
pub fast_vaa_hash: [u8; 32],
pub vaa: Option<Pubkey>,
pub source_chain: u16,
pub target_protocol: MessageProtocol,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anchor_lang::prelude::*;

use crate::state::FastFillSeeds;

#[event]
pub struct FastFillRedeemed {
pub prepared_by: Pubkey,
pub fast_fill: Pubkey,
pub fast_fill: FastFillSeeds,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ use anchor_lang::prelude::*;
#[event]
pub struct FastFillSequenceReserved {
pub fast_vaa_hash: [u8; 32],
pub fast_fill_seeds: FastFillSeeds,
pub fast_fill: FastFillSeeds,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anchor_lang::prelude::*;
#[event]
#[derive(Debug)]
pub struct OrderExecuted {
pub auction: Pubkey,
pub fast_vaa_hash: [u8; 32],
pub vaa: Pubkey,
pub source_chain: u16,
pub target_protocol: MessageProtocol,
Expand Down
26 changes: 26 additions & 0 deletions solana/programs/matching-engine/src/processor/admin/init_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anchor_lang::prelude::*;

use crate::composite::*;

#[derive(Accounts)]
pub struct InitEventSubscription<'info> {
#[account(mut)]
payer: Signer<'info>,

admin: OwnerOnly<'info>,

/// This account will be serialized with an event, so its discriminator will change with every
/// update.
///
/// CHECK: Mutable, must have seeds \["event"\].
#[account(
init,
payer = payer,
space = 10_240,
seeds = [b"event"],
bump,
)]
event_subscription: UncheckedAccount<'info>,

system_program: Program<'info, System>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use anchor_lang::prelude::*;

#[derive(Accounts)]
#[event_cpi]
pub struct ProposeAuctionParameters<'info> {
#[account(mut)]
payer: Signer<'info>,
Expand Down Expand Up @@ -56,7 +57,7 @@ pub fn propose_auction_parameters(
)?;

// Emit event reflecting the proposal.
emit!(crate::events::Proposed { action });
emit_cpi!(crate::events::Proposed { action });

// Done.
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use anchor_lang::prelude::*;

#[derive(Accounts)]
#[event_cpi]
pub struct UpdateAuctionParameters<'info> {
#[account(mut)]
payer: Signer<'info>,
Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn update_auction_parameters(ctx: Context<UpdateAuctionParameters>) -> Resul
let action = ctx.accounts.proposal.action;

// Emit event to reflect enacting the proposal.
emit!(crate::events::Enacted { action });
emit_cpi!(crate::events::Enacted { action });

match action {
ProposalAction::UpdateAuctionParameters { id, parameters } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use common::{wormhole_cctp_solana, wormhole_io::TypePrefixedPayload};

/// Accounts required for [execute_fast_order_cctp].
#[derive(Accounts)]
#[event_cpi]
pub struct ExecuteFastOrderCctp<'info> {
#[account(mut)]
payer: Signer<'info>,
Expand Down Expand Up @@ -79,6 +80,7 @@ pub fn handle_execute_fast_order_cctp(
let super::PreparedOrderExecution {
user_amount: amount,
fill,
order_executed_event,
} = super::handle_execute_fast_order(
&mut ctx.accounts.execute_order,
&ctx.accounts.custodian,
Expand Down Expand Up @@ -182,6 +184,10 @@ pub fn handle_execute_fast_order_cctp(
},
)?;

// Emit the order executed event, which liquidators can listen to if this execution ended up
// being penalized so they can collect the base fee at settlement.
emit_cpi!(order_executed_event);

// Finally close the account since it is no longer needed.
token::close_account(CpiContext::new_with_signer(
token_program.to_account_info(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,26 @@ pub fn execute_fast_order_local(ctx: Context<ExecuteFastOrderLocal>) -> Result<(
let super::PreparedOrderExecution {
user_amount: amount,
fill,
order_executed_event,
} = super::handle_execute_fast_order(
&mut ctx.accounts.execute_order,
&ctx.accounts.custodian,
&ctx.accounts.token_program,
)?;

// Emit the order executed event, which liquidators can listen to if this execution ended up
// being penalized so they can collect the base fee at settlement.
emit_cpi!(order_executed_event);

let fast_fill = FastFill::new(
fill,
ctx.accounts.reserved_sequence.fast_fill_seeds.sequence,
ctx.bumps.fast_fill,
ctx.accounts.payer.key(),
amount,
);

// Emit the fast fill.
emit_cpi!(crate::events::LocalFastOrderFilled {
seeds: fast_fill.seeds,
info: fast_fill.info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use local::*;
use crate::{
composite::*,
error::MatchingEngineError,
events::OrderExecuted,
state::{Auction, AuctionStatus, MessageProtocol},
utils::{self, auction::DepositPenalty},
};
Expand All @@ -20,6 +21,7 @@ use common::messages::{
struct PreparedOrderExecution {
pub user_amount: u64,
pub fill: Fill,
pub order_executed_event: OrderExecuted,
}

fn handle_execute_fast_order<'info>(
Expand All @@ -40,7 +42,7 @@ fn handle_execute_fast_order<'info>(
.unwrap()
.to_fast_market_order_unchecked();

let (user_amount, new_status) = {
let (user_amount, new_status, order_executed_event) = {
let auction_info = auction.info.as_ref().unwrap();
let current_slot = Clock::get().unwrap().slot;

Expand Down Expand Up @@ -205,22 +207,19 @@ fn handle_execute_fast_order<'info>(
custodian.key().into(),
)?;

// Emit the order executed event, which liquidators can listen to if this execution ended up
// being penalized so they can collect the base fee at settlement.
emit!(crate::events::OrderExecuted {
auction: auction.key(),
vaa: fast_vaa.key(),
source_chain: auction_info.source_chain,
target_protocol: auction.target_protocol,
penalized,
});

(
user_amount,
AuctionStatus::Completed {
slot: current_slot,
execute_penalty: if penalized { penalty.into() } else { None },
},
OrderExecuted {
fast_vaa_hash: auction.vaa_hash,
vaa: fast_vaa.key(),
source_chain: auction_info.source_chain,
target_protocol: auction.target_protocol,
penalized,
},
)
};

Expand All @@ -238,5 +237,6 @@ fn handle_execute_fast_order<'info>(
.try_into()
.map_err(|_| MatchingEngineError::RedeemerMessageTooLarge)?,
},
order_executed_event,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use common::TRANSFER_AUTHORITY_SEED_PREFIX;

#[derive(Accounts)]
#[instruction(offer_price: u64)]
#[event_cpi]
pub struct ImproveOffer<'info> {
/// The auction participant needs to set approval to this PDA.
///
Expand Down Expand Up @@ -134,9 +135,9 @@ pub fn improve_offer(ctx: Context<ImproveOffer>, offer_price: u64) -> Result<()>
let info = auction.info.as_ref().unwrap();

// Emit event for auction participants to listen to.
emit!(crate::events::AuctionUpdated {
emit_cpi!(crate::utils::log_emit(crate::events::AuctionUpdated {
config_id: info.config_id,
auction: auction.key(),
fast_vaa_hash: auction.vaa_hash,
vaa: Default::default(),
source_chain: info.source_chain,
target_protocol: auction.target_protocol,
Expand All @@ -148,7 +149,7 @@ pub fn improve_offer(ctx: Context<ImproveOffer>, offer_price: u64) -> Result<()>
total_deposit: info.total_deposit(),
max_offer_price_allowed: utils::auction::compute_min_allowed_offer(config, info)
.checked_sub(1),
});
}));
}

// Done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use common::{messages::raw::LiquidityLayerMessage, TRANSFER_AUTHORITY_SEED_PREFI

#[derive(Accounts)]
#[instruction(offer_price: u64)]
#[event_cpi]
pub struct PlaceInitialOfferCctp<'info> {
#[account(mut)]
payer: Signer<'info>,
Expand Down Expand Up @@ -92,7 +93,7 @@ pub struct PlaceInitialOfferCctp<'info> {
)]
auction: Box<Account<'info, Auction>>,

offer_token: Account<'info, token::TokenAccount>,
offer_token: Box<Account<'info, token::TokenAccount>>,

#[account(
init,
Expand All @@ -105,7 +106,7 @@ pub struct PlaceInitialOfferCctp<'info> {
],
bump,
)]
auction_custody_token: Account<'info, token::TokenAccount>,
auction_custody_token: Box<Account<'info, token::TokenAccount>>,

usdc: Usdc<'info>,

Expand Down Expand Up @@ -166,9 +167,9 @@ pub fn place_initial_offer_cctp(
let info = ctx.accounts.auction.info.as_ref().unwrap();

// Emit event for auction participants to listen to.
emit!(crate::events::AuctionUpdated {
emit_cpi!(crate::utils::log_emit(crate::events::AuctionUpdated {
config_id: info.config_id,
auction: ctx.accounts.auction.key(),
fast_vaa_hash: ctx.accounts.auction.vaa_hash,
vaa: ctx.accounts.fast_order_path.fast_vaa.key().into(),
source_chain: info.source_chain,
target_protocol: ctx.accounts.auction.target_protocol,
Expand All @@ -180,7 +181,7 @@ pub fn place_initial_offer_cctp(
total_deposit: info.total_deposit(),
max_offer_price_allowed: utils::auction::compute_min_allowed_offer(config, info)
.checked_sub(1),
});
}));

// Finally transfer tokens from the offer authority's token account to the
// auction's custody account.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anchor_lang::prelude::*;
use anchor_spl::token::{self, TokenAccount};

#[derive(Accounts)]
#[event_cpi]
pub struct SettleAuctionComplete<'info> {
/// CHECK: Must equal prepared_order_response.prepared_by, who paid the rent to post the
/// finalized VAA.
Expand Down Expand Up @@ -247,8 +248,8 @@ fn handle_settle_auction_complete(
None => None,
};

emit!(crate::events::AuctionSettled {
auction: ctx.accounts.auction.key(),
emit_cpi!(crate::events::AuctionSettled {
fast_vaa_hash: ctx.accounts.auction.vaa_hash,
best_offer_token: settled_best_offer_result,
base_fee_token: settled_base_fee_result,
with_execute: Default::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use common::{wormhole_cctp_solana, wormhole_io::TypePrefixedPayload};

/// Accounts required for [settle_auction_none_cctp].
#[derive(Accounts)]
#[event_cpi]
pub struct SettleAuctionNoneCctp<'info> {
#[account(mut)]
payer: Signer<'info>,
Expand Down Expand Up @@ -102,6 +103,7 @@ fn handle_settle_auction_none_cctp(
let super::SettledNone {
user_amount: amount,
fill,
auction_settled_event,
} = super::settle_none_and_prepare_fill(super::SettleNoneAndPrepareFill {
prepared_order_response: &mut ctx.accounts.prepared.order_response,
prepared_custody_token,
Expand Down Expand Up @@ -209,6 +211,9 @@ fn handle_settle_auction_none_cctp(
},
)?;

// Emit an event indicating that the auction has been settled.
emit_cpi!(auction_settled_event);

// Finally close the account since it is no longer needed.
token::close_account(CpiContext::new_with_signer(
token_program.to_account_info(),
Expand Down
Loading
Loading