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

bigz/market-initializer-improve #413

Merged
merged 17 commits into from
Apr 6, 2023
Merged
65 changes: 63 additions & 2 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::validation::margin::{validate_margin, validate_margin_weights};
use crate::validation::perp_market::validate_perp_market;
use crate::validation::spot_market::validate_borrow_rate;
use crate::{controller, QUOTE_PRECISION_I64};
use crate::{math, safe_increment};
use crate::{math, safe_decrement, safe_increment};

pub fn handle_initialize(ctx: Context<Initialize>) -> Result<()> {
let (drift_signer, drift_signer_nonce) =
Expand Down Expand Up @@ -443,6 +443,7 @@ pub fn handle_update_serum_vault(ctx: Context<UpdateSerumVault>) -> Result<()> {

pub fn handle_initialize_perp_market(
ctx: Context<InitializePerpMarket>,
market_index: u16,
amm_base_asset_reserve: u128,
amm_quote_asset_reserve: u128,
amm_periodicity: i64,
Expand Down Expand Up @@ -551,7 +552,14 @@ pub fn handle_initialize_perp_market(
)?;

let state = &mut ctx.accounts.state;
let market_index = state.number_of_markets;
validate!(
market_index == state.number_of_markets,
ErrorCode::InvalidMarketAccount,
"market_index={} != state.number_of_markets={}",
market_index,
state.number_of_markets
)?;

**perp_market = PerpMarket {
contract_type: ContractType::Perpetual,
contract_tier: ContractTier::Speculative, // default
Expand Down Expand Up @@ -679,6 +687,36 @@ pub fn handle_initialize_perp_market(
Ok(())
}

pub fn handle_delete_initialized_perp_market(
ctx: Context<DeleteInitializedPerpMarket>,
market_index: u16,
) -> Result<()> {
let perp_market = &mut ctx.accounts.perp_market.load_init()?;
let state = &mut ctx.accounts.state;

// validate
validate!(
state.number_of_markets == market_index,
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
ErrorCode::InvalidInitialPeg
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
)?;
validate!(
perp_market.status == MarketStatus::Initialized,
ErrorCode::InvalidInitialPeg
)?;
validate!(
perp_market.number_of_users == 0,
ErrorCode::InvalidInitialPeg
)?;
validate!(
perp_market.market_index == market_index,
ErrorCode::InvalidInitialPeg
)?;

safe_decrement!(state.number_of_markets, 1);

Ok(())
}

#[access_control(
spot_market_valid(&ctx.accounts.spot_market)
)]
Expand Down Expand Up @@ -2247,6 +2285,29 @@ pub struct InitializePerpMarket<'info> {
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct DeleteInitializedPerpMarket<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
has_one = admin
)]
pub state: Box<Account<'info, State>>,
#[account(
init,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will cause it to try to initialize the perp market account. we want to delete it. see how we do it for delete user

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made another change, would appreciate if you gave more detail (cant tell by looking at delete_user exactly how the rent/closure happens)

seeds = [b"perp_market", state.number_of_markets.to_le_bytes().as_ref()],
space = PerpMarket::SIZE,
bump,
payer = admin
)]
pub perp_market: AccountLoader<'info, PerpMarket>,
/// CHECK: checked in `initialize_perp_market`
pub oracle: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct AdminUpdatePerpMarket<'info> {
pub admin: Signer<'info>,
Expand Down
9 changes: 9 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ pub mod drift {

pub fn initialize_perp_market(
ctx: Context<InitializePerpMarket>,
market_index: u16,
amm_base_asset_reserve: u128,
amm_quote_asset_reserve: u128,
amm_periodicity: i64,
Expand All @@ -487,6 +488,7 @@ pub mod drift {
) -> Result<()> {
handle_initialize_perp_market(
ctx,
market_index,
amm_base_asset_reserve,
amm_quote_asset_reserve,
amm_periodicity,
Expand All @@ -500,6 +502,13 @@ pub mod drift {
)
}

pub fn delete_initialized_perp_market(
ctx: Context<DeleteInitializedPerpMarket>,
market_index: u16,
) -> Result<()> {
handle_delete_initialized_perp_market(ctx, market_index)
}

pub fn move_amm_price(
ctx: Context<AdminUpdatePerpMarket>,
base_asset_reserve: u128,
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class AdminClient extends DriftClient {
}

public async initializePerpMarket(
marketIndex: number,
priceOracle: PublicKey,
baseAssetReserve: BN,
quoteAssetReserve: BN,
Expand All @@ -203,6 +204,7 @@ export class AdminClient extends DriftClient {
const nameBuffer = encodeName(name);
const initializeMarketTx =
await this.program.transaction.initializePerpMarket(
marketIndex,
baseAssetReserve,
quoteAssetReserve,
periodicity,
Expand Down
45 changes: 45 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,10 @@
}
],
"args": [
{
"name": "marketIndex",
"type": "u16"
},
{
"name": "ammBaseAssetReserve",
"type": "u128"
Expand Down Expand Up @@ -2332,6 +2336,47 @@
}
]
},
{
"name": "deleteInitializedPerpMarket",
"accounts": [
{
"name": "admin",
"isMut": true,
"isSigner": true
},
{
"name": "state",
"isMut": true,
"isSigner": false
},
{
"name": "perpMarket",
"isMut": true,
"isSigner": false
},
{
"name": "oracle",
"isMut": false,
"isSigner": false
},
{
"name": "rent",
"isMut": false,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "marketIndex",
"type": "u16"
}
]
},
{
"name": "moveAmmPrice",
"accounts": [
Expand Down
1 change: 1 addition & 0 deletions tests/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('admin', () => {
const periodicity = new BN(60 * 60); // 1 HOUR

await driftClient.initializePerpMarket(
0,
solUsd,
new BN(1000),
new BN(1000),
Expand Down
1 change: 1 addition & 0 deletions tests/assetTier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('asset tiers', () => {

const periodicity = new BN(60 * 60); // 1 HOUR
await driftClient.initializePerpMarket(
0,
solOracle,
AMM_RESERVE_PRECISION,
AMM_RESERVE_PRECISION,
Expand Down
1 change: 1 addition & 0 deletions tests/cancelAllOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('cancel all orders', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
oracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
1 change: 1 addition & 0 deletions tests/cappedSymFunding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async function cappedSymFundingScenario(
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
priceFeedAddress,
kSqrt,
kSqrt,
Expand Down
1 change: 1 addition & 0 deletions tests/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('AMM Curve', () => {
const periodicity = new BN(60 * 60); // 1 HOUR

await driftClient.initializePerpMarket(
0,
solUsdOracle,
ammInitialBaseAssetAmount,
ammInitialQuoteAssetAmount,
Expand Down
1 change: 1 addition & 0 deletions tests/delistMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('delist market', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
solOracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
1 change: 1 addition & 0 deletions tests/delistMarketLiq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ describe('delist market, liquidation of expired position', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
solOracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
1 change: 1 addition & 0 deletions tests/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('drift client', () => {

const marketIndex = 0;
const txSig = await driftClient.initializePerpMarket(
0,
solUsd,
ammInitialBaseAssetAmount,
ammInitialQuoteAssetAmount,
Expand Down
1 change: 1 addition & 0 deletions tests/imbalancePerpPnl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe('imbalanced large perp pnl w/ borrow hitting limits', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
solOracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
1 change: 1 addition & 0 deletions tests/insuranceFundStake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('insurance fund stake', () => {

const periodicity = new BN(60 * 60); // 1 HOUR
await driftClient.initializePerpMarket(
0,
solOracle,
AMM_RESERVE_PRECISION,
AMM_RESERVE_PRECISION,
Expand Down
2 changes: 2 additions & 0 deletions tests/ksolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ describe('AMM Curve', () => {
const periodicity = new BN(60 * 60); // 1 HOUR
const kSqrtNorm = normAssetAmount(kSqrt, initialSOLPriceBN);
await driftClient.initializePerpMarket(
0,

solUsdOracle,
kSqrtNorm,
kSqrtNorm,
Expand Down
1 change: 1 addition & 0 deletions tests/liquidateBorrowForPerpPnl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ describe('liquidate borrow for perp pnl', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
solOracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
2 changes: 2 additions & 0 deletions tests/liquidatePerp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ describe('liquidate perp (no open orders)', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,

oracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
2 changes: 2 additions & 0 deletions tests/liquidatePerpAndLp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ describe('liquidate perp and lp', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,

oracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
1 change: 1 addition & 0 deletions tests/liquidatePerpPnlForDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('liquidate perp pnl for deposit', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
solOracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
2 changes: 2 additions & 0 deletions tests/liquidityProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ describe('liquidity providing', () => {
);
// used for trading / taking on baa
await driftClient.initializePerpMarket(
0,
solusdc,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down Expand Up @@ -247,6 +248,7 @@ describe('liquidity providing', () => {

// second market -- used for funding ..
await driftClient.initializePerpMarket(
1,
solusdc2,
stableAmmInitialBaseAssetReserve,
stableAmmInitialQuoteAssetReserve,
Expand Down
2 changes: 2 additions & 0 deletions tests/marketOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ describe('market order', () => {
const periodicity = new BN(60 * 60); // 1 HOUR

await driftClient.initializePerpMarket(
0,
solUsd,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
periodicity
);

await driftClient.initializePerpMarket(
1,
btcUsd,
ammInitialBaseAssetReserve.div(new BN(3000)),
ammInitialQuoteAssetReserve.div(new BN(3000)),
Expand Down
1 change: 1 addition & 0 deletions tests/marketOrderBaseAssetAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('market orders', () => {
const periodicity = new BN(60 * 60); // 1 HOUR

await driftClient.initializePerpMarket(
0,
solUsd,
ammInitialBaseAssetAmount,
ammInitialQuoteAssetAmount,
Expand Down
1 change: 1 addition & 0 deletions tests/maxLeverageOrderParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ describe('max leverage order params', () => {
const periodicity = new BN(0);

await driftClient.initializePerpMarket(
0,
solOracle,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand Down
2 changes: 2 additions & 0 deletions tests/multipleMakerOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('multiple maker orders', () => {
const periodicity = new BN(60 * 60); // 1 HOUR

await fillerDriftClient.initializePerpMarket(
0,
solUsd,
ammInitialBaseAssetReserve,
ammInitialQuoteAssetReserve,
Expand All @@ -113,6 +114,7 @@ describe('multiple maker orders', () => {
);

await fillerDriftClient.initializePerpMarket(
1,
dogUsd,
ammInitialBaseAssetReserve.div(new BN(100000)),
ammInitialQuoteAssetReserve.div(new BN(100000)),
Expand Down
Loading