Skip to content

Commit

Permalink
implement parallel processing in sync() method (#843)
Browse files Browse the repository at this point in the history
* sdk: release v2.54.0-beta.5

* sdk: tweak polling tx confirmation (#812)

* sdk: fix tests not building

* sdk: tweak polling tx confirmation

* sdk: release v2.54.0-beta.6

* sdk: add option to get signed settlepnl tix from a market order (#813)

* sdk: add option to get signed settlepnl tix from a market order

* fix lint

* sdk: release v2.54.0-beta.7

* ts-sdk: remove unncessary logs on tx failure

* linter

* prettify

* sdk: release v2.54.0-beta.8

* math/constants.rs: correct QUOTE AMOUNTS constant name

* ts-sdk: filter out erroneous logs in EventSubscriber (#815)

* sdk: release v2.54.0-beta.9

* program: auto derisk lp positions in settle pnl (#766)

* bigz/add-lp-order-risk-mitigations

* friendly perp position iter

* update parmams mutability

* add time_since_last_liquidity_change

* add new variable to margin context mode

* add attempt to burn shares in force cancel/settle pnl

* remove from force cancel orders

* sdk: fix tests not building

* change place orders

* rmeove unused mut

* address some comments

* add to settle_pnl

* use oracle orders

* tweak order params

* oracle order tests

* add typescript test, fix order param market_type

* remove logic from settle_expired_position

* CHANGELOG

---------

Co-authored-by: Chris Heaney <[email protected]>

* sdk: release v2.54.0-beta.10

* v2.54.0

* sdk: release v2.55.0-beta.0

* Revert "sdk: release v2.55.0-beta.0"

This reverts commit 21c4a46.

* Revert "v2.54.0"

This reverts commit 63c7208.

* sdk: release v2.54.0-beta.11

* ts-sdk: fix mixing stopWaiting

* sdk: release v2.54.0-beta.12

* v2.54.0

* sdk: release v2.55.0-beta.0

* Added handling for signature time measurement (#818)

* Added handling for signature time measurement

* Added opt-in metrics flag to drift client

* fixed prettier

---------

Co-authored-by: lowkeynicc <[email protected]>

* sdk: release v2.55.0-beta.1

* sdk: fix ts timeout

* sdk: fix getUnrealizedPNL for market index 0

* sdk: release v2.55.0-beta.2

* sdk: release v2.55.0-beta.3

* sdk: dlp updates (#822)

* dlp updates

* add optional custom margin ratio param to initAndDeposit

* prettify

* sdk: release v2.55.0-beta.4

* program: fix risk reduction dlp burn from not being step size (#826)

* program: fix risk reduction dlp burn from not being step size

* CHANGELOG

* fix lp burn

* v2.55.0

* sdk: release v2.56.0-beta.0

* Luke/extra tx sending (#827)

* added raw tx endpoints to tx senders

* Swapped additional tx endpoints for callbacks

* sdk: account for settle lp in accoutnt breakdown (#828)

* sdk: add settle pnl option to cost basis calc

* sdk: catch error in priorityFeeSubscriber.load (#834)

* Replace sendRawTransaction with sendTransaction (#833)

* program: make it easier for jit fills (#835)

* program: make it easier for jit fills

* CHANGELOG

* sdk: prettify:fix

* sdk: release v2.56.0-beta.1

* v2.56.0

* sdk: release v2.57.0-beta.0

* devnet-market-index-23 (#837)

* devnet-market-index-23

* comment out used oracle

* sdk: release v2.57.0-beta.1

* program: add recenter amm ix (#836)

* bigz/add-amm-recenter-fcn

* add recenter-amm-2 test

* passing recenter_amm_2 test

* add test_move_amm test

* update todo msg

* add to adminClient

* add get_lower_bound_sqrt_k functor

* CHANGELOG

---------

Co-authored-by: Chris Heaney <[email protected]>

* sdk: release v2.57.0-beta.2

* v2.57.0

* sdk: release v2.58.0-beta.0

* add helius method to PriorityFeeSubscriber (#832)

* add helius method to PriorityFeeSubscriber

* eagerly convert PubicKey to string

* use helius from connection obj, or optional rpc url

* lints

* sdk: release v2.58.0-beta.1

* program: AmmPaused doesnt block all fills

* v2.58.0

* sdk: release v2.59.0-beta.0

* sdk: allow hotswap addresses in PriorityFeeSubscriber

* sdk: release v2.59.0-beta.1

* sdk: fix priorityFeeSubscriber rpc endpoint loading

* sdk: release v2.59.0-beta.2

* sdk: add max fee clamp to priority fee

* sdk: release v2.59.0-beta.3

* sdk: fix priority fee clamp, add getter

* sdk: release v2.59.0-beta.4

* program: bump phoenix dependency

* no state ws resub

* sdk: release v2.59.0-beta.5

* program: use decayed last_oracle_conf_pct as lower bound for update (#840)

* bigz/add-oracle-volatility-protections

* fix pct vs price bug, add test + changelog

* cleanup code (naming, mutables)

* program: add paused operations flag (#839)

* program: init pause_operations flag

* tweaks

* write tests for paused operations

* split out spot and perp operations

* fixes

* add sdk and use PerpOperation/SpotOperation naming convention

* fix update funding ix

* stop update to deprecated market status

* add admin fn

* add logging for admin fn

* CHANGELOG

* sdk: release v2.59.0-beta.6

* v2.59.0

* sdk: release v2.60.0-beta.0

* implement parallel processing in sync() method

---------

Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: lil perp <[email protected]>
Co-authored-by: lowkeynicc <[email protected]>
Co-authored-by: wphan <[email protected]>
Co-authored-by: 0xbigz <[email protected]>
Co-authored-by: Luke <[email protected]>
Co-authored-by: lowkeynicc <[email protected]>
Co-authored-by: Chester Sim <[email protected]>
Co-authored-by: Evan Pipta <[email protected]>
Co-authored-by: Nour Alharithi <[email protected]>
  • Loading branch information
11 people authored Feb 27, 2024
1 parent 8698a43 commit ce910cf
Show file tree
Hide file tree
Showing 70 changed files with 3,345 additions and 438 deletions.
64 changes: 63 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,76 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Features

### Fixes

### Breaking


## [2.59.0] - 2023-01-30

### Features

- program: separate out paused operations from market status ([#839](https://github.com/drift-labs/protocol-v2/pull/839))
- program: use decayed last_oracle_conf_pct as lower bound for update ([#840](https://github.com/drift-labs/protocol-v2/pull/840))

### Fixes

### Breaking

## [2.58.0] - 2023-01-27

### Features

### Fixes

- program: AmmPaused doesnt block all fills

### Breaking

## [2.57.0] - 2023-01-25

### Features

- program: add recenter amm ix ([#836](https://github.com/drift-labs/protocol-v2/pull/836))

### Fixes

### Breaking

## [2.56.0] - 2023-01-24

### Features

### Fixes

- program: enable jit maker to fill same slot as taker placed ([#835](https://github.com/drift-labs/protocol-v2/pull/835))

### Breaking

## [2.55.0] - 2023-01-18

### Features

### Fixes

- program: standardize lp shares in attempt_burn_user_lp_shares_for_risk_reduction ([#826](https://github.com/drift-labs/protocol-v2/pull/826))

### Breaking

## [2.54.0] - 2023-01-15

### Features
- sdk: move bracket orders into single instruction
- sdk: add ability to do placeAndTake order with bracket orders attached
- sdk: add option to cancel existing orders in market for place and take order

- sdk: add option to get signed settlePnl tx back from a market order
- program: auto derisk lp positions in settle pnl ([#766](https://github.com/drift-labs/protocol-v2/pull/766))
- program: increase full perp liquidation threshold ([#807](https://github.com/drift-labs/protocol-v2/pull/807))
- program: remove spot fee pool transfer ([#800](https://github.com/drift-labs/protocol-v2/pull/800))
- program: increase insurance tier max ([#784](https://github.com/drift-labs/protocol-v2/pull/784))
- sdk: can specify max custom margin ratio to initialize a new account with

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions programs/drift/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "drift"
version = "2.53.0"
version = "2.59.0"
description = "Created with Anchor"
edition = "2018"

Expand Down Expand Up @@ -31,7 +31,7 @@ arrayref = "0.3.6"
base64 = "0.13.0"
serum_dex = { git = "https://github.com/project-serum/serum-dex", rev = "85b4f14", version = "0.5.6", features = ["no-entrypoint"] }
enumflags2 = "0.6.4"
phoenix-v1 = { git = "https://github.com/drift-labs/phoenix-v1", rev = "4c65c9", version = "0.2.4", features = ["no-entrypoint"] }
phoenix-v1 = { git = "https://github.com/drift-labs/phoenix-v1", rev = "bf6b84", version = "0.2.4", features = ["no-entrypoint"] }
solana-security-txt = "1.1.0"
static_assertions = "1.1.0"
drift-macros = { git = "https://github.com/drift-labs/drift-macros.git", rev = "c57d87" }
Expand Down
53 changes: 53 additions & 0 deletions programs/drift/src/controller/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,63 @@ pub fn move_price(
validate!(
(quote_asset_reserve.cast::<i128>()? - amm.quote_asset_reserve.cast::<i128>()?).abs() < 100,
ErrorCode::InvalidAmmDetected,
"quote_asset_reserve passed doesnt reconcile enough {} vs {}",
quote_asset_reserve.cast::<i128>()?,
amm.quote_asset_reserve.cast::<i128>()?
)?;

amm.sqrt_k = sqrt_k;

let (_, terminal_quote_reserves, terminal_base_reserves) =
amm::calculate_terminal_price_and_reserves(amm)?;
amm.terminal_quote_asset_reserve = terminal_quote_reserves;

let (min_base_asset_reserve, max_base_asset_reserve) =
amm::calculate_bid_ask_bounds(amm.concentration_coef, terminal_base_reserves)?;

amm.max_base_asset_reserve = max_base_asset_reserve;
amm.min_base_asset_reserve = min_base_asset_reserve;

let reserve_price_after = amm.reserve_price()?;
update_spreads(amm, reserve_price_after)?;

Ok(())
}

// recenter peg with balanced terminal reserves
pub fn recenter_perp_market_amm(amm: &mut AMM, peg_multiplier: u128, sqrt_k: u128) -> DriftResult {
// calculate base/quote reserves for balanced terminal reserves
let swap_direction = if amm.base_asset_amount_with_amm > 0 {
SwapDirection::Remove
} else {
SwapDirection::Add
};
let (new_quote_asset_amount, new_base_asset_amount) = amm::calculate_swap_output(
amm.base_asset_amount_with_amm.unsigned_abs(),
sqrt_k,
swap_direction,
sqrt_k,
)?;

amm.base_asset_reserve = new_base_asset_amount;

let k = bn::U256::from(sqrt_k).safe_mul(bn::U256::from(sqrt_k))?;

amm.quote_asset_reserve = k
.safe_div(bn::U256::from(new_base_asset_amount))?
.try_to_u128()?;

validate!(
(new_quote_asset_amount.cast::<i128>()? - amm.quote_asset_reserve.cast::<i128>()?).abs()
< 100,
ErrorCode::InvalidAmmDetected,
"quote_asset_reserve passed doesnt reconcile enough"
)?;

amm.sqrt_k = sqrt_k;
// todo: could calcualte terminal state cost for altering sqrt_k

amm.peg_multiplier = peg_multiplier;

let (_, terminal_quote_reserves, terminal_base_reserves) =
amm::calculate_terminal_price_and_reserves(amm)?;
Expand Down
11 changes: 8 additions & 3 deletions programs/drift/src/controller/lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::{msg, Pubkey};

use crate::bn::U192;
use crate::controller;
use crate::controller::position::PositionDelta;
use crate::controller::position::{get_position_index, PositionDelta};
use crate::controller::position::{update_position_and_market, update_quote_asset_amount};
use crate::emit;
use crate::error::{DriftResult, ErrorCode};
Expand Down Expand Up @@ -357,8 +357,13 @@ pub fn remove_perp_lp_shares(
market_index: u16,
now: i64,
) -> DriftResult<()> {
let position_index = get_position_index(&user.perp_positions, market_index)?;

// standardize n shares to burn
let shares_to_burn: u64 = {
// account for issue where lp shares are smaller than step size
let shares_to_burn = if user.perp_positions[position_index].lp_shares == shares_to_burn {
shares_to_burn
} else {
let market = perp_market_map.get_ref(&market_index)?;
crate::math::orders::standardize_base_asset_amount(
shares_to_burn.cast()?,
Expand All @@ -382,7 +387,7 @@ pub fn remove_perp_lp_shares(

controller::funding::settle_funding_payment(user, &user_key, &mut market, now)?;

let position = user.get_perp_position_mut(market_index)?;
let position = &mut user.perp_positions[position_index];

validate!(
position.lp_shares >= shares_to_burn,
Expand Down
41 changes: 26 additions & 15 deletions programs/drift/src/controller/lp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use crate::state::state::{OracleGuardRails, State, ValidityGuardRails};
use crate::state::user::{SpotPosition, User};
use crate::test_utils::*;
use crate::test_utils::{get_positions, get_pyth_price, get_spot_positions};
use anchor_lang::prelude::Clock;

#[test]
fn test_lp_wont_collect_improper_funding() {
let mut position = PerpPosition {
Expand Down Expand Up @@ -433,8 +435,14 @@ pub fn test_lp_settle_pnl() {
&pyth_program,
oracle_account_info
);
let slot = 0;
let mut oracle_map = OracleMap::load_one(&oracle_account_info, slot, None).unwrap();
let clock = Clock {
slot: 0,
epoch_start_timestamp: 0,
epoch: 0,
leader_schedule_epoch: 0,
unix_timestamp: 0,
};
let mut oracle_map = OracleMap::load_one(&oracle_account_info, clock.slot, None).unwrap();

let mut market = PerpMarket {
amm: AMM {
Expand Down Expand Up @@ -516,8 +524,6 @@ pub fn test_lp_settle_pnl() {
..User::default()
};

let now = 1000000;

let state = State {
oracle_guard_rails: OracleGuardRails {
validity: ValidityGuardRails {
Expand Down Expand Up @@ -555,7 +561,7 @@ pub fn test_lp_settle_pnl() {
&market_map,
&spot_market_map,
&mut oracle_map,
now,
&clock,
&state,
);

Expand Down Expand Up @@ -728,16 +734,21 @@ fn test_lp_margin_calc() {

let strict_quote_price = StrictOraclePrice::test(1000000);
// ensure margin calc doesnt incorrectly count funding rate (funding pnl MUST come before settling lp)
let (margin_requirement, weighted_unrealized_pnl, worse_case_base_asset_value) =
calculate_perp_position_value_and_pnl(
&user.perp_positions[0],
&market,
&oracle_price_data,
&strict_quote_price,
crate::math::margin::MarginRequirementType::Initial,
0,
)
.unwrap();
let (
margin_requirement,
weighted_unrealized_pnl,
worse_case_base_asset_value,
_open_order_fraction,
) = calculate_perp_position_value_and_pnl(
&user.perp_positions[0],
&market,
&oracle_price_data,
&strict_quote_price,
crate::math::margin::MarginRequirementType::Initial,
0,
false,
)
.unwrap();

assert_eq!(margin_requirement, 1012000000); // $1010 + $2 mr for lp_shares
assert_eq!(weighted_unrealized_pnl, -9916900000); // $-9900000000 upnl (+ -16900000 from old funding)
Expand Down
Loading

0 comments on commit ce910cf

Please sign in to comment.