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

zerofi integration #1407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
"@solana/web3.js": "1.73.2",
"@types/bn.js": "5.1.6",
"@types/chai": "5.0.0",
"@types/glob": "^8.1.0",
"@types/mocha": "8.2.3",
"@typescript-eslint/eslint-plugin": "4.33.0",
"@typescript-eslint/parser": "4.33.0",
"bs58": "^6.0.0",
"chai": "4.4.1",
"eslint": "7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-prettier": "3.4.0",
"glob": "^11.0.0",
"husky": "7.0.4",
"mocha": "^11.0.1",
"prettier": "3.0.1",
"ts-mocha": "^10.0.0",
"typedoc": "0.23.23",
"typescript": "4.9.5",
"@pythnetwork/price-service-client": "1.9.0"
Expand Down
6 changes: 6 additions & 0 deletions programs/drift/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ pub enum ErrorCode {
InvalidPythLazerMessage,
#[msg("Pyth lazer message does not correspond to correct fed id")]
PythLazerMessagePriceFeedMismatch,
#[msg("FailedZerofiCPI")]
FailedZerofiCPI,
#[msg("InvalidZerofiProgram")]
InvalidZerofiProgram,
#[msg("InvalidZerofiMarket")]
InvalidZerofiMarket,
}

#[macro_export]
Expand Down
108 changes: 108 additions & 0 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::state::fulfillment_params::phoenix::PhoenixMarketContext;
use crate::state::fulfillment_params::phoenix::PhoenixV1FulfillmentConfig;
use crate::state::fulfillment_params::serum::SerumContext;
use crate::state::fulfillment_params::serum::SerumV3FulfillmentConfig;
use crate::state::fulfillment_params::zerofi::{ZerofiContext, ZerofiFulfillmentConfig};
use crate::state::high_leverage_mode_config::HighLeverageModeConfig;
use crate::state::insurance_fund_stake::ProtocolIfSharesTransferConfig;
use crate::state::oracle::get_sb_on_demand_price;
Expand Down Expand Up @@ -631,6 +632,60 @@ pub fn handle_update_phoenix_fulfillment_config_status(
Ok(())
}

pub fn handle_initialize_zerofi_fulfillment_config(
ctx: Context<InitializeZerofiFulfillmentConfig>,
market_index: u16,
) -> Result<()> {
validate!(
market_index != QUOTE_SPOT_MARKET_INDEX,
ErrorCode::InvalidSpotMarketAccount,
"Cannot add zerofi market to quote asset"
)?;

let base_spot_market = load!(&ctx.accounts.base_spot_market)?;
let quote_spot_market = load!(&ctx.accounts.quote_spot_market)?;

let zerofi_program_id = crate::state::fulfillment_params::zerofi::zerofi_program_id::id();

validate!(
ctx.accounts.zerofi_program.key() == zerofi_program_id,
ErrorCode::InvalidZerofiProgram
)?;

let zerofi_market_context = ZerofiContext {
zerofi_program: &ctx.accounts.zerofi_program,
zerofi_market: &ctx.accounts.zerofi_market,
};
let market = zerofi_market_context.load_zerofi_market()?;
validate!(
market.mint_base == base_spot_market.mint,
ErrorCode::InvalidZerofiMarket,
"Invalid base mint"
)?;

validate!(
market.mint_quote == quote_spot_market.mint,
ErrorCode::InvalidZerofiMarket,
"Invalid quote mint"
)?;

let zerofi_fulfillment_config_key = ctx.accounts.zerofi_fulfillment_config.key();
let mut zerofi_fulfillment_config = ctx.accounts.zerofi_fulfillment_config.load_init()?;
*zerofi_fulfillment_config = zerofi_market_context
.to_zerofi_fulfillment_config(&zerofi_fulfillment_config_key, market_index)?;
Ok(())
}

pub fn handle_update_zerofi_fulfillment_config_status(
ctx: Context<UpdateZerofiFulfillmentConfig>,
status: SpotFulfillmentConfigStatus,
) -> Result<()> {
let mut config = load_mut!(ctx.accounts.zerofi_fulfillment_config)?;
msg!("config.status {:?} -> {:?}", config.status, status);
config.status = status;
Ok(())
}

pub fn handle_initialize_perp_market(
ctx: Context<InitializePerpMarket>,
market_index: u16,
Expand Down Expand Up @@ -4457,6 +4512,59 @@ pub struct UpdatePhoenixFulfillmentConfig<'info> {
pub admin: Signer<'info>,
}

#[derive(Accounts)]
#[instruction(market_index: u16)]
pub struct InitializeZerofiFulfillmentConfig<'info> {
#[account(
seeds = [b"spot_market", market_index.to_le_bytes().as_ref()],
bump,
)]
pub base_spot_market: AccountLoader<'info, SpotMarket>,
#[account(
seeds = [b"spot_market", 0_u16.to_le_bytes().as_ref()],
bump,
)]
pub quote_spot_market: AccountLoader<'info, SpotMarket>,
#[account(
mut,
has_one = admin
)]
pub state: Box<Account<'info, State>>,
/// CHECK: checked in ix
pub zerofi_program: AccountInfo<'info>,
/// CHECK: checked in ix
pub zerofi_market: AccountInfo<'info>,
#[account(
constraint = state.signer.eq(&drift_signer.key())
)]
/// CHECK: program signer
pub drift_signer: AccountInfo<'info>,
#[account(
init,
seeds = [b"zerofi_fulfillment_config".as_ref(), zerofi_market.key.as_ref()],
space = ZerofiFulfillmentConfig::SIZE,
bump,
payer = admin,
)]
pub zerofi_fulfillment_config: AccountLoader<'info, ZerofiFulfillmentConfig>,
#[account(mut)]
pub admin: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct UpdateZerofiFulfillmentConfig<'info> {
#[account(
has_one = admin
)]
pub state: Box<Account<'info, State>>,
#[account(mut)]
pub zerofi_fulfillment_config: AccountLoader<'info, ZerofiFulfillmentConfig>,
#[account(mut)]
pub admin: Signer<'info>,
}

#[derive(Accounts)]
pub struct UpdateSerumVault<'info> {
#[account(
Expand Down
13 changes: 13 additions & 0 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::state::fulfillment_params::drift::MatchFulfillmentParams;
use crate::state::fulfillment_params::openbook_v2::OpenbookV2FulfillmentParams;
use crate::state::fulfillment_params::phoenix::PhoenixFulfillmentParams;
use crate::state::fulfillment_params::serum::SerumFulfillmentParams;
use crate::state::fulfillment_params::zerofi::ZerofiFulfillmentParams;
use crate::state::high_leverage_mode_config::HighLeverageModeConfig;
use crate::state::insurance_fund_stake::InsuranceFundStake;
use crate::state::oracle_map::OracleMap;
Expand Down Expand Up @@ -186,6 +187,7 @@ pub enum SpotFulfillmentType {
Match,
PhoenixV1,
OpenbookV2,
Zerofi,
}

#[access_control(
Expand Down Expand Up @@ -283,6 +285,17 @@ fn fill_spot_order<'c: 'info, 'info>(
clock.unix_timestamp,
)?)
}
SpotFulfillmentType::Zerofi => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Box::new(ZerofiFulfillmentParams::new(
remaining_accounts_iter,
&ctx.accounts.state,
&base_market,
&quote_market,
clock.unix_timestamp,
)?)
}
SpotFulfillmentType::Match => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Expand Down
23 changes: 23 additions & 0 deletions programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::state::fulfillment_params::drift::MatchFulfillmentParams;
use crate::state::fulfillment_params::openbook_v2::OpenbookV2FulfillmentParams;
use crate::state::fulfillment_params::phoenix::PhoenixFulfillmentParams;
use crate::state::fulfillment_params::serum::SerumFulfillmentParams;
use crate::state::fulfillment_params::zerofi::ZerofiFulfillmentParams;
use crate::state::high_leverage_mode_config::HighLeverageModeConfig;
use crate::state::oracle::StrictOraclePrice;
use crate::state::order_params::RFQMatch;
Expand Down Expand Up @@ -1747,6 +1748,17 @@ pub fn handle_place_and_take_spot_order<'c: 'info, 'info>(
clock.unix_timestamp,
)?)
}
SpotFulfillmentType::Zerofi => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Box::new(ZerofiFulfillmentParams::new(
remaining_accounts_iter,
&ctx.accounts.state,
&base_market,
&quote_market,
clock.unix_timestamp,
)?)
}
SpotFulfillmentType::Match => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Expand Down Expand Up @@ -1895,6 +1907,17 @@ pub fn handle_place_and_make_spot_order<'c: 'info, 'info>(
clock.unix_timestamp,
)?)
}
SpotFulfillmentType::Zerofi => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Box::new(ZerofiFulfillmentParams::new(
remaining_accounts_iter,
&ctx.accounts.state,
&base_market,
&quote_market,
clock.unix_timestamp,
)?)
}
SpotFulfillmentType::Match => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Expand Down
15 changes: 15 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ pub mod drift {
) -> Result<()> {
handle_update_openbook_v2_fulfillment_config_status(ctx, status)
}

pub fn initialize_phoenix_fulfillment_config(
ctx: Context<InitializePhoenixFulfillmentConfig>,
market_index: u16,
Expand All @@ -827,6 +828,20 @@ pub mod drift {
handle_update_phoenix_fulfillment_config_status(ctx, status)
}

pub fn initialize_zerofi_fulfillment_config(
ctx: Context<InitializeZerofiFulfillmentConfig>,
market_index: u16,
) -> Result<()> {
handle_initialize_zerofi_fulfillment_config(ctx, market_index)
}

pub fn zerofi_fulfillment_config_status(
ctx: Context<UpdateZerofiFulfillmentConfig>,
status: SpotFulfillmentConfigStatus,
) -> Result<()> {
handle_update_zerofi_fulfillment_config_status(ctx, status)
}

pub fn update_serum_vault(ctx: Context<UpdateSerumVault>) -> Result<()> {
handle_update_serum_vault(ctx)
}
Expand Down
1 change: 1 addition & 0 deletions programs/drift/src/state/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ pub enum OrderActionExplanation {
OrderFilledWithLPJit,
DeriskLp,
OrderFilledWithOpenbookV2,
OrderFilledWithZerofi,
}

#[event]
Expand Down
1 change: 1 addition & 0 deletions programs/drift/src/state/fulfillment_params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod drift;
pub mod openbook_v2;
pub mod phoenix;
pub mod serum;
pub mod zerofi;
Loading