Skip to content

Commit

Permalink
feat!: omnichain create offer
Browse files Browse the repository at this point in the history
  • Loading branch information
jusikXL committed Sep 6, 2024
1 parent 62d2005 commit 078a177
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 46 deletions.
78 changes: 67 additions & 11 deletions programs/otc_market/src/instructions/create_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ use anchor_spl::{
associated_token::AssociatedToken,
token_interface::{ Mint, TokenAccount, TokenInterface },
};
use oapp::endpoint::{
instructions::SendParams as EndpointSendParams,
MessagingReceipt,
MessagingFee,
};

#[event_cpi]
#[derive(Accounts)]
#[instruction(params: CreateOfferParams)]
#[instruction(params: CreateOfferParams, fee: MessagingFee)]
pub struct CreateOffer<'info> {
#[account(mut)]
pub seller: Signer<'info>,
Expand All @@ -32,10 +37,14 @@ pub struct CreateOffer<'info> {
#[account(seeds = [OtcConfig::OTC_SEED], bump = otc_config.bump)]
pub otc_config: Account<'info, OtcConfig>,

#[account(mut, seeds = [Escrow::ESCROW_SEED], bump = escrow.bump)]
pub escrow: Account<'info, Escrow>,

#[account(
mint::token_program = token_program,
constraint = src_token_mint.decimals >= OtcConfig::SHARED_DECIMALS @ OtcError::InvalidLocalDecimals
)]
/// NOTICE: required for src spl offer
pub src_token_mint: Option<InterfaceAccount<'info, Mint>>,

#[account(
Expand All @@ -44,6 +53,7 @@ pub struct CreateOffer<'info> {
associated_token::mint = src_token_mint,
associated_token::token_program = token_program,
)]
/// NOTICE: required for src spl offer
pub src_seller_ata: Option<InterfaceAccount<'info, TokenAccount>>,

#[account(
Expand All @@ -53,10 +63,26 @@ pub struct CreateOffer<'info> {
associated_token::mint = src_token_mint,
associated_token::token_program = token_program
)]
/// NOTICE: required for src spl offer
pub src_escrow_ata: Option<InterfaceAccount<'info, TokenAccount>>,

#[account(mut, seeds = [Escrow::ESCROW_SEED], bump = escrow.bump)]
pub escrow: Account<'info, Escrow>,
#[account(
seeds = [Peer::PEER_SEED, otc_config.key().as_ref(), &params.dst_eid.to_be_bytes()],
bump = peer.bump
)]
/// NOTICE: required for crosschain offer
pub peer: Option<Account<'info, Peer>>,

#[account(
seeds = [
EnforcedOptions::ENFORCED_OPTIONS_SEED,
otc_config.key().as_ref(),
&params.dst_eid.to_be_bytes(),
],
bump = enforced_options.bump
)]
/// NOTICE: required for crosschain offer
pub enforced_options: Option<Account<'info, EnforcedOptions>>,

pub token_program: Option<Interface<'info, TokenInterface>>,

Expand All @@ -68,8 +94,9 @@ pub struct CreateOffer<'info> {
impl CreateOffer<'_> {
pub fn apply(
ctx: &mut Context<CreateOffer>,
params: &CreateOfferParams
) -> Result<CreateOfferReceipt> {
params: &CreateOfferParams,
fee: &MessagingFee
) -> Result<(CreateOfferReceipt, MessagingReceipt)> {
let src_token_address = OtcConfig::get_token_address(ctx.accounts.src_token_mint.as_ref());

let (src_amount_sd, src_amount_ld): (u64, u64);
Expand Down Expand Up @@ -115,23 +142,52 @@ impl CreateOffer<'_> {
exchange_rate_sd: offer.exchange_rate_sd,
});

let escrow_sol_account_info: AccountInfo = ctx.accounts.escrow.to_account_info();
let mut receipt = MessagingReceipt::default();

if params.dst_eid != OtcConfig::EID {
// crosschain offer

let peer = ctx.accounts.peer.as_ref().expect(OtcConfig::ERROR_MSG);
let enforced_options = ctx.accounts.enforced_options
.as_ref()
.expect(OtcConfig::ERROR_MSG);

let payload = build_create_offer_payload(&offer_id, &offer);

receipt = oapp::endpoint_cpi::send(
ctx.accounts.otc_config.endpoint_program,
ctx.accounts.otc_config.key(),
ctx.remaining_accounts,
&[OtcConfig::OTC_SEED, &[ctx.accounts.otc_config.bump]],
EndpointSendParams {
dst_eid: params.dst_eid,
receiver: peer.address,
message: payload,
options: enforced_options.get_enforced_options(&None),
native_fee: fee.native_fee,
lz_token_fee: fee.lz_token_fee,
}
)?;
}

OtcConfig::transfer(
ctx.accounts.seller.as_ref(),
src_amount_ld,
Some(&escrow_sol_account_info),
Some(&ctx.accounts.escrow.to_account_info()),
ctx.accounts.token_program.as_ref(),
ctx.accounts.src_seller_ata.as_ref(),
ctx.accounts.src_token_mint.as_ref(),
ctx.accounts.src_escrow_ata.as_ref(),
None
)?;

Ok(CreateOfferReceipt {
offer_id,
src_amount_ld,
})
Ok((
CreateOfferReceipt {
offer_id,
src_amount_ld,
},
receipt,
))
}
}

Expand Down
29 changes: 17 additions & 12 deletions programs/otc_market/src/instructions/quote_create_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ pub struct QuoteCreateOffer<'info> {
mint::token_program = token_program,
constraint = src_token_mint.decimals >= OtcConfig::SHARED_DECIMALS @ OtcError::InvalidLocalDecimals
)]
/// NOTICE: required for src spl offer
pub src_token_mint: Option<InterfaceAccount<'info, Mint>>,

pub token_program: Option<Interface<'info, TokenInterface>>,

/// NOTICE: required for crosschain offer
#[account(
seeds = [Peer::PEER_SEED, otc_config.key().as_ref(), &params.dst_eid.to_be_bytes()],
bump = peer.bump
)]
/// NOTICE: required for crosschain offer
pub peer: Option<Account<'info, Peer>>,

#[account(
Expand All @@ -31,7 +30,10 @@ pub struct QuoteCreateOffer<'info> {
],
bump = enforced_options.bump
)]
/// NOTICE: required for crosschain offer
pub enforced_options: Option<Account<'info, EnforcedOptions>>,

pub token_program: Option<Interface<'info, TokenInterface>>,
}

impl QuoteCreateOffer<'_> {
Expand Down Expand Up @@ -76,16 +78,19 @@ impl QuoteCreateOffer<'_> {

let payload = build_create_offer_payload(
&offer_id,
&src_seller_address,
&params.dst_seller_address,
OtcConfig::EID,
params.dst_eid,
&src_token_address,
&params.dst_token_address,
src_amount_sd,
params.exchange_rate_sd
);
&(Offer {
src_seller_address: *src_seller_address,
dst_seller_address: params.dst_seller_address,
src_eid: OtcConfig::EID,
dst_eid: params.dst_eid,
src_token_address,
dst_token_address: params.dst_token_address,
src_amount_sd,
exchange_rate_sd: params.exchange_rate_sd,

bump: u8::default(), // unused (required for Offer struct creation)
})
);
messaging_fee = oapp::endpoint_cpi::quote(
ctx.accounts.otc_config.endpoint_program,
ctx.remaining_accounts,
Expand Down
9 changes: 5 additions & 4 deletions programs/otc_market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use state::*;

use oapp::endpoint::{ MessagingFee, MessagingReceipt };

declare_id!("7GWXYB7a3Ky5pohnPGiiXshvXWsdb3ZC95qTwkU6NYP8");
declare_id!("4a3mL39r3BNa7dmptJmginDLrt9xdjC4wcwU8gosCUx8");

#[program]
pub mod otc_market {
Expand Down Expand Up @@ -81,9 +81,10 @@ pub mod otc_market {
/// see [create_offer]
pub fn create_offer(
mut ctx: Context<CreateOffer>,
params: CreateOfferParams
) -> Result<CreateOfferReceipt> {
CreateOffer::apply(&mut ctx, &params)
params: CreateOfferParams,
fee: MessagingFee
) -> Result<(CreateOfferReceipt, MessagingReceipt)> {
CreateOffer::apply(&mut ctx, &params, &fee)
}

/// see [quote_accept_offer]
Expand Down
30 changes: 11 additions & 19 deletions programs/otc_market/src/msg_codec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Offer;

#[repr(u8)]
enum Message {
OfferCreated = 0,
Expand All @@ -6,17 +8,7 @@ enum Message {
// OfferCanceled = 3,
}

pub fn build_create_offer_payload(
offer_id: &[u8; 32],
src_seller_address: &[u8; 32],
dst_seller_address: &[u8; 32],
src_eid: u32,
dst_eid: u32,
src_token_address: &[u8; 32],
dst_token_address: &[u8; 32],
src_amount_sd: u64,
exchange_rate_sd: u64
) -> Vec<u8> {
pub fn build_create_offer_payload(offer_id: &[u8; 32], offer: &Offer) -> Vec<u8> {
// let mut msg_payload = Vec::new();
// msg_payload.extend_from_slice(offer_id);
// msg_payload.extend_from_slice(src_seller_address);
Expand All @@ -36,14 +28,14 @@ pub fn build_create_offer_payload(
[
&(Message::OfferCreated as u8).to_be_bytes() as &[u8],
offer_id,
src_seller_address,
dst_seller_address,
&src_eid.to_be_bytes(),
&dst_eid.to_be_bytes(),
src_token_address,
dst_token_address,
&src_amount_sd.to_be_bytes(),
&exchange_rate_sd.to_be_bytes(),
&offer.src_seller_address,
&offer.dst_seller_address,
&offer.src_eid.to_be_bytes(),
&offer.dst_eid.to_be_bytes(),
&offer.src_token_address,
&offer.dst_token_address,
&offer.src_amount_sd.to_be_bytes(),
&offer.exchange_rate_sd.to_be_bytes(),
].concat()
}

Expand Down

0 comments on commit 078a177

Please sign in to comment.