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

openbook v2 #1112

Merged
merged 54 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
49f6cab
switch workflows to ubicloud
soundsonacid Jun 25, 2024
68a8459
trigger workflow
soundsonacid Jun 25, 2024
5e52092
sync
Norbaeocystin Jun 26, 2024
cabe88f
run anchor tests sh
Norbaeocystin Jun 26, 2024
dd7343c
remove num, static
Norbaeocystin Jun 30, 2024
bc3ffd9
remove client
Norbaeocystin Jun 30, 2024
6e4741d
remove forgotten TODOs
Norbaeocystin Jun 30, 2024
2d2d8b0
Merge branch 'master' into master
Norbaeocystin Jun 30, 2024
b542e2a
removed double
Norbaeocystin Jun 30, 2024
0d952b7
ref market
Norbaeocystin Jun 30, 2024
ed87c35
ref as requested
Norbaeocystin Jun 30, 2024
c1f2051
market maker - note
Norbaeocystin Jun 30, 2024
a2141d7
cargo fmt
Norbaeocystin Jul 1, 2024
dd1f66a
load_ref, cleaning
Norbaeocystin Jul 2, 2024
bbecbbf
from dep to fixtures
Norbaeocystin Jul 2, 2024
4c980f7
cleaning done
Norbaeocystin Jul 2, 2024
5443716
update
Norbaeocystin Jul 2, 2024
8c412c8
fulfillment - resolved
Norbaeocystin Jul 2, 2024
8a048c7
removed stuff
Norbaeocystin Jul 2, 2024
f23d438
no entrypoint for associated
Norbaeocystin Jul 2, 2024
9af3f49
no entrypoints
Norbaeocystin Jul 2, 2024
8681c32
add assertions
Norbaeocystin Jul 2, 2024
b060320
clippy + fmt
Norbaeocystin Jul 2, 2024
d681f49
assertions
Norbaeocystin Jul 2, 2024
286b754
hygiene
Norbaeocystin Jul 2, 2024
fc5c849
token amounts assertions
Norbaeocystin Jul 3, 2024
5d92631
Merge branch 'master' into master
Norbaeocystin Jul 3, 2024
9da7744
remove anyhow
Norbaeocystin Jul 5, 2024
f6e5b3d
lock file from master
Norbaeocystin Jul 5, 2024
b256a14
Merge branch 'master' into openbook-v2
soundsonacid Jul 8, 2024
7642a83
wip bankrun refactor
soundsonacid Jul 8, 2024
6bd2115
merge
soundsonacid Jul 8, 2024
37585da
wip refactor ob tests to bankrun
soundsonacid Jul 8, 2024
7fd1942
Update openbookTest.ts
Norbaeocystin Jul 10, 2024
f0d805a
start fixing test
crispheaney Jul 11, 2024
efa70ca
Update openbook_v2.rs
Norbaeocystin Jul 12, 2024
584a918
fix test
crispheaney Jul 12, 2024
4d284a3
Update openbook_v2.rs
Norbaeocystin Jul 14, 2024
187bff5
Update constants.rs
Norbaeocystin Jul 14, 2024
09393f9
Update openbook_v2.rs
Norbaeocystin Jul 14, 2024
b16cfbf
Update openbook_v2.rs
Norbaeocystin Jul 14, 2024
063c027
Update openbook_v2.rs
Norbaeocystin Jul 14, 2024
9a0171c
assert fee 0
crispheaney Jul 16, 2024
72f6a47
Merge branch 'master' into AphelionTrading/master
crispheaney Jul 16, 2024
bf05e0f
add ooa remaining account
crispheaney Jul 16, 2024
515010d
prettify
crispheaney Jul 16, 2024
2f9af28
add asserts for ooas in PlaceTakerOrder
crispheaney Jul 16, 2024
3733523
rm dev dependencies
crispheaney Jul 17, 2024
6285611
add openbook
crispheaney Jul 17, 2024
84a7777
Merge branch 'master' into AphelionTrading/master
crispheaney Jul 17, 2024
7f84c10
try fixing warning
crispheaney Jul 17, 2024
b898516
Merge branch 'master' into AphelionTrading/master
crispheaney Jul 17, 2024
b5ebb15
yarn prettify:fix
crispheaney Jul 17, 2024
2f0a50d
CHANGELOG
crispheaney Jul 17, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,4 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: build
path: target/deploy/drift.so
path: target/deploy/drift.so
3 changes: 3 additions & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
cluster = "localnet"
Norbaeocystin marked this conversation as resolved.
Show resolved Hide resolved
wallet = "~/.config/solana/id.json"

[workspace]
exclude = ["programs/openbook_v2"]

[scripts]
test = "echo" # need to call anchor test to update metadata field in idl before running tests, so just do a noop
lint = "yarn prettify:fix && cargo fmt"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- program: track fuel for if staking ([#1127](https://github.com/drift-labs/protocol-v2/pull/1127))
- program: validate fee structure ([#1075](https://github.com/drift-labs/protocol-v2/pull/1075))
- program: check 5 min oracle twap divergence in trigger order ([#1116](https://github.com/drift-labs/protocol-v2/pull/1116))
- program: openbook v2 integration ([#1112](https://github.com/drift-labs/protocol-v2/pull/1112))

### Fixes

Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/drift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ solana-security-txt = "1.1.0"
static_assertions = "1.1.0"
drift-macros = { git = "https://github.com/drift-labs/drift-macros.git", rev = "c57d87" }
switchboard = { path = "../switchboard", features = ["no-entrypoint"] }
openbook-v2-light = { path = "../openbook_v2", features = ["no-entrypoint"] }
ahash = "=0.8.6"
byteorder = "1.4.3"

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 @@ -563,6 +563,12 @@ pub enum ErrorCode {
OracleMismatchedVaaAndPriceUpdates,
#[msg("Remaining account passed is not a valid pda")]
OracleBadRemainingAccountPublicKey,
#[msg("FailedOpenbookV2CPI")]
FailedOpenbookV2CPI,
#[msg("InvalidOpenbookV2Program")]
InvalidOpenbookV2Program,
#[msg("InvalidOpenbookV2Market")]
InvalidOpenbookV2Market,
}

#[macro_export]
Expand Down
132 changes: 132 additions & 0 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ use crate::math::spot_balance::get_token_amount;
use crate::math::{amm, bn};
use crate::math_error;
use crate::state::events::CurveRecord;
use crate::state::fulfillment_params::openbook_v2::{
OpenbookV2Context, OpenbookV2FulfillmentConfig,
};
use crate::state::fulfillment_params::phoenix::PhoenixMarketContext;
use crate::state::fulfillment_params::phoenix::PhoenixV1FulfillmentConfig;
use crate::state::fulfillment_params::serum::SerumContext;
Expand Down Expand Up @@ -444,6 +447,82 @@ pub fn handle_update_serum_vault(ctx: Context<UpdateSerumVault>) -> Result<()> {
Ok(())
}

pub fn handle_initialize_openbook_v2_fulfillment_config(
ctx: Context<InitializeOpenbookV2FulfillmentConfig>,
market_index: u16,
) -> Result<()> {
validate!(
market_index != QUOTE_SPOT_MARKET_INDEX,
ErrorCode::InvalidSpotMarketAccount,
"Cannot add openbook v2 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 openbook_v2_program_id = openbook_v2_light::id();

validate!(
ctx.accounts.openbook_v2_program.key() == openbook_v2_program_id,
ErrorCode::InvalidOpenbookV2Program
)?;

let openbook_v2_market_context = OpenbookV2Context {
openbook_v2_program: &ctx.accounts.openbook_v2_program,
openbook_v2_market: &ctx.accounts.openbook_v2_market,
};
let market = openbook_v2_market_context.load_openbook_v2_market()?;
validate!(
market.base_mint == base_spot_market.mint,
ErrorCode::InvalidOpenbookV2Market,
"Invalid base mint"
)?;

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

validate!(
market.taker_fee == 0,
ErrorCode::InvalidOpenbookV2Market,
"Fee must be 0"
)?;

let market_step_size = market.base_lot_size as u64;
let valid_step_size = base_spot_market.order_step_size >= market_step_size
&& base_spot_market
.order_step_size
.rem_euclid(market_step_size)
== 0;

validate!(
valid_step_size,
ErrorCode::InvalidOpenbookV2Market,
"base market step size ({}) not a multiple of Openbook V2 base lot size ({})",
base_spot_market.order_step_size,
market_step_size
)?;

let openbook_v2_fulfillment_config_key = ctx.accounts.openbook_v2_fulfillment_config.key();
let mut openbook_v2_fulfillment_config =
ctx.accounts.openbook_v2_fulfillment_config.load_init()?;
*openbook_v2_fulfillment_config = openbook_v2_market_context
.to_openbook_v2_fulfillment_config(&openbook_v2_fulfillment_config_key, market_index)?;
Ok(())
}

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

pub fn handle_initialize_phoenix_fulfillment_config(
ctx: Context<InitializePhoenixFulfillmentConfig>,
market_index: u16,
Expand Down Expand Up @@ -4355,6 +4434,59 @@ pub struct DeletePrelaunchOracle<'info> {
pub state: Box<Account<'info, State>>,
}

#[derive(Accounts)]
#[instruction(market_index: u16)]
pub struct InitializeOpenbookV2FulfillmentConfig<'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 openbook_v2_program: AccountInfo<'info>,
/// CHECK: checked in ix
pub openbook_v2_market: AccountInfo<'info>,
#[account(
constraint = state.signer.eq(&drift_signer.key())
)]
/// CHECK: program signer
pub drift_signer: AccountInfo<'info>,
#[account(
init,
seeds = [b"openbook_v2_fulfillment_config".as_ref(), openbook_v2_market.key.as_ref()],
space = OpenbookV2FulfillmentConfig::SIZE,
bump,
payer = admin,
)]
pub openbook_v2_fulfillment_config: AccountLoader<'info, OpenbookV2FulfillmentConfig>,
#[account(mut)]
pub admin: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}

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

#[derive(Accounts)]
#[instruction(feed_id : [u8; 32])]
pub struct InitPythPullPriceFeed<'info> {
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 @@ -12,6 +12,7 @@ use crate::math::spot_withdraw::validate_spot_market_vault_amount;
use crate::optional_accounts::update_prelaunch_oracle;
use crate::state::fill_mode::FillMode;
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::insurance_fund_stake::InsuranceFundStake;
Expand Down Expand Up @@ -147,6 +148,7 @@ pub enum SpotFulfillmentType {
SerumV3,
Match,
PhoenixV1,
OpenbookV2,
}

#[access_control(
Expand Down Expand Up @@ -233,6 +235,17 @@ fn fill_spot_order<'c: 'info, 'info>(
&quote_market,
)?)
}
SpotFulfillmentType::OpenbookV2 => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Box::new(OpenbookV2FulfillmentParams::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 @@ -41,6 +41,7 @@ use crate::state::events::{
};
use crate::state::fill_mode::FillMode;
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::oracle::StrictOraclePrice;
Expand Down Expand Up @@ -1443,6 +1444,17 @@ pub fn handle_place_and_take_spot_order<'c: 'info, 'info>(
&quote_market,
)?)
}
SpotFulfillmentType::OpenbookV2 => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Box::new(OpenbookV2FulfillmentParams::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 @@ -1573,6 +1585,17 @@ pub fn handle_place_and_make_spot_order<'c: 'info, 'info>(
&quote_market,
)?)
}
SpotFulfillmentType::OpenbookV2 => {
let base_market = spot_market_map.get_ref(&market_index)?;
let quote_market = spot_market_map.get_quote_spot_market()?;
Box::new(OpenbookV2FulfillmentParams::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
13 changes: 13 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,19 @@ pub mod drift {
handle_update_serum_fulfillment_config_status(ctx, status)
}

pub fn initialize_openbook_v2_fulfillment_config(
ctx: Context<InitializeOpenbookV2FulfillmentConfig>,
market_index: u16,
) -> Result<()> {
handle_initialize_openbook_v2_fulfillment_config(ctx, market_index)
}

pub fn openbook_v2_fulfillment_config_status(
ctx: Context<UpdateOpenbookV2FulfillmentConfig>,
status: SpotFulfillmentConfigStatus,
) -> Result<()> {
handle_update_openbook_v2_fulfillment_config_status(ctx, status)
}
pub fn initialize_phoenix_fulfillment_config(
ctx: Context<InitializePhoenixFulfillmentConfig>,
market_index: u16,
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 @@ -332,6 +332,7 @@ pub enum OrderActionExplanation {
OrderFilledWithAMMJitLPSplit,
OrderFilledWithLPJit,
DeriskLp,
OrderFilledWithOpenbookV2,
}

#[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
@@ -1,3 +1,4 @@
pub mod drift;
pub mod openbook_v2;
pub mod phoenix;
pub mod serum;
Loading
Loading