Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ad8c91b
sdk: release v0.1.18-master.7
github-actions[bot] Jan 28, 2022
b4fcf3e
latest
lmvdz Jan 28, 2022
97dbdcc
Merge branch 'master' of https://github.com/lmvdz/protocol-v1
lmvdz Jan 28, 2022
bbef33b
Merge branch 'drift-labs:master' into master
lmvdz Jan 31, 2022
d30f603
sdk: release v0.1.18-master.8
github-actions[bot] Jan 31, 2022
1d86b5e
Merge branch 'drift-labs:master' into master
lmvdz Feb 1, 2022
9c22500
sdk: release v0.1.18-master.9
github-actions[bot] Feb 1, 2022
f69bad5
fix merge
lmvdz Feb 9, 2022
cf3017a
sdk: release v0.1.21-master.0
github-actions[bot] Feb 9, 2022
63c5a1a
add fees math
lmvdz Feb 15, 2022
c5a7833
fix up imports
lmvdz Feb 15, 2022
739ce25
return discount
lmvdz Feb 15, 2022
be05f54
fix typescript config errors
lmvdz Feb 15, 2022
0aa4578
merge from upstream/crispheaney/off-chain-orders
lmvdz Feb 15, 2022
28cea4d
add fees to exports
lmvdz Feb 15, 2022
6e2cc0b
add return type to calculateOrderFeeTier
lmvdz Feb 15, 2022
221fe0a
convert bn array return types into dev friendly interfaces
lmvdz Feb 15, 2022
f91d3ca
export calculateAmountToTrade
lmvdz Feb 16, 2022
a4a12b6
Merge remote-tracking branch 'upstream/crispheaney/off-chain-orders' …
lmvdz Feb 17, 2022
78455a3
merge from upstream
lmvdz Feb 17, 2022
5712839
clearing_house: init orders
crispheaney Feb 18, 2022
201611a
sdk: release v0.1.21-master.3
github-actions[bot] Feb 18, 2022
62de3d7
sdk: amm.ts add k/repeg budget
0xbigz Feb 18, 2022
9736da1
sdk: fix merge issues from orders
crispheaney Feb 18, 2022
f2d6f30
sdk: fix prettier
crispheaney Feb 18, 2022
0fc9be2
sdk: release v0.1.21-master.4
github-actions[bot] Feb 18, 2022
9efb1d4
clearing_house: support 32 orders
crispheaney Feb 18, 2022
8bfdfaf
sdk: release v0.1.21-master.5
github-actions[bot] Feb 18, 2022
f8c80cf
clearing_house: remove default trait implementations for history structs
crispheaney Feb 19, 2022
4eab8dd
Merge remote-tracking branch 'upstream/crispheaney/off-chain-orders' …
lmvdz Feb 21, 2022
c748c64
clearing_house: fix reduce only
crispheaney Feb 21, 2022
c75ffa2
sdk: release v0.1.22
crispheaney Feb 21, 2022
ae08635
latest
lmvdz Feb 21, 2022
b1c00de
merge master
lmvdz Feb 21, 2022
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
16 changes: 16 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/typescript-node/.devcontainer/base.Dockerfile

# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster
ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"

# [Optional] Uncomment if you want to install more global node packages
# RUN su node -c "npm install -g <your-package-list -here>"
39 changes: 39 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/typescript-node
{
"name": "Node.js & TypeScript",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 16, 14, 12.
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"args": {
"VARIANT": "16-bullseye"
}
},

// Set *default* container specific settings.json values on container create.
"settings": {},


// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint"
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node",
"features": {
"docker-in-docker": "latest",
"git": "latest",
"github-cli": "latest",
"python": "latest",
"rust": "latest"
}
}
8 changes: 4 additions & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cargo fmt -- --check
cargo +nightly clippy
yarn prettify
yarn lint
#cargo fmt -- --check
#cargo +nightly clippy
#yarn prettify
#yarn lint
8 changes: 4 additions & 4 deletions programs/clearing_house/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ pub fn execute_market_order(
let market_position = &mut user_positions.positions[position_index];
let market = markets.get_market_mut(market_index);

let (potentially_risk_increasing, base_asset_amount, quote_asset_amount) =
let (potentially_risk_increasing, reduce_only, base_asset_amount, quote_asset_amount) =
if order.base_asset_amount > 0 {
controller::position::update_position_with_base_asset_amount(
order.base_asset_amount,
Expand All @@ -675,7 +675,7 @@ pub fn execute_market_order(
)?
};

if potentially_risk_increasing && order.reduce_only {
if !reduce_only && order.reduce_only {
return Err(ErrorCode::ReduceOnlyOrderIncreasedRisk);
}

Expand Down Expand Up @@ -768,7 +768,7 @@ pub fn execute_non_market_order(
let position_index = get_position_index(user_positions, market_index)?;
let market_position = &mut user_positions.positions[position_index];

let (potentially_risk_increasing, _, quote_asset_amount) =
let (potentially_risk_increasing, reduce_only, _, quote_asset_amount) =
controller::position::update_position_with_base_asset_amount(
base_asset_amount,
order.direction,
Expand All @@ -778,7 +778,7 @@ pub fn execute_non_market_order(
now,
)?;

if potentially_risk_increasing && order.reduce_only {
if !reduce_only && order.reduce_only {
return Err(ErrorCode::ReduceOnlyOrderIncreasedRisk);
}

Expand Down
10 changes: 8 additions & 2 deletions programs/clearing_house/src/controller/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,14 @@ pub fn update_position_with_base_asset_amount(
user: &mut User,
market_position: &mut MarketPosition,
now: i64,
) -> ClearingHouseResult<(bool, u128, u128)> {
) -> ClearingHouseResult<(bool, bool, u128, u128)> {
// A trade is risk increasing if it increases the users leverage
// If a trade is risk increasing and brings the user's margin ratio below initial requirement
// the trade fails
// If a trade is risk increasing and it pushes the mark price too far away from the oracle price
// the trade fails
let mut potentially_risk_increasing = true;
let mut reduce_only = false;

// The trade increases the the user position if
// 1) the user does not have a position
Expand All @@ -471,6 +472,7 @@ pub fn update_position_with_base_asset_amount(
now,
)?;

reduce_only = true;
potentially_risk_increasing = false;
} else {
// after closing existing position, how large should trade be in opposite direction
Expand Down Expand Up @@ -500,6 +502,7 @@ pub fn update_position_with_base_asset_amount(

Ok((
potentially_risk_increasing,
reduce_only,
base_asset_amount,
quote_asset_amount,
))
Expand All @@ -513,13 +516,14 @@ pub fn update_position_with_quote_asset_amount(
market_position: &mut MarketPosition,
mark_price_before: u128,
now: i64,
) -> ClearingHouseResult<(bool, u128, u128)> {
) -> ClearingHouseResult<(bool, bool, u128, u128)> {
// A trade is risk increasing if it increases the users leverage
// If a trade is risk increasing and brings the user's margin ratio below initial requirement
// the trade fails
// If a trade is risk increasing and it pushes the mark price too far away from the oracle price
// the trade fails
let mut potentially_risk_increasing = true;
let mut reduce_only = false;

let mut quote_asset_amount = quote_asset_amount;
let base_asset_amount;
Expand Down Expand Up @@ -563,6 +567,7 @@ pub fn update_position_with_quote_asset_amount(
.unsigned_abs();

potentially_risk_increasing = false;
reduce_only = true;
} else {
// after closing existing position, how large should trade be in opposite direction
let quote_asset_amount_after_close = quote_asset_amount
Expand Down Expand Up @@ -595,6 +600,7 @@ pub fn update_position_with_quote_asset_amount(

Ok((
potentially_risk_increasing,
reduce_only,
base_asset_amount,
quote_asset_amount,
))
Expand Down
2 changes: 1 addition & 1 deletion programs/clearing_house/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ pub mod clearing_house {
{
let markets = &mut ctx.accounts.markets.load_mut()?;
let market = markets.get_market_mut(market_index);
let (_potentially_risk_increasing, _base_asset_amount, _quote_asset_amount) =
let (_potentially_risk_increasing, _, _base_asset_amount, _quote_asset_amount) =
controller::position::update_position_with_quote_asset_amount(
quote_asset_amount,
direction,
Expand Down
9 changes: 0 additions & 9 deletions programs/clearing_house/src/state/history/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ pub struct ExtendedCurveHistory {
curve_records: [ExtendedCurveRecord; 1024],
}

impl Default for ExtendedCurveHistory {
fn default() -> Self {
ExtendedCurveHistory {
head: 0,
curve_records: [ExtendedCurveRecord::default(); 1024],
}
}
}

impl ExtendedCurveHistory {
pub fn append(&mut self, pos: ExtendedCurveRecord) {
self.curve_records[ExtendedCurveHistory::index_of(self.head)] = pos;
Expand Down
9 changes: 0 additions & 9 deletions programs/clearing_house/src/state/history/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ pub struct DepositHistory {
deposit_records: [DepositRecord; 1024],
}

impl Default for DepositHistory {
fn default() -> Self {
DepositHistory {
head: 0,
deposit_records: [DepositRecord::default(); 1024],
}
}
}

impl DepositHistory {
pub fn append(&mut self, pos: DepositRecord) {
self.deposit_records[DepositHistory::index_of(self.head)] = pos;
Expand Down
9 changes: 0 additions & 9 deletions programs/clearing_house/src/state/history/funding_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ pub struct FundingPaymentHistory {
funding_payment_records: [FundingPaymentRecord; 1024],
}

impl Default for FundingPaymentHistory {
fn default() -> Self {
FundingPaymentHistory {
head: 0,
funding_payment_records: [FundingPaymentRecord::default(); 1024],
}
}
}

impl FundingPaymentHistory {
pub fn append(&mut self, pos: FundingPaymentRecord) {
self.funding_payment_records[FundingPaymentHistory::index_of(self.head)] = pos;
Expand Down
9 changes: 0 additions & 9 deletions programs/clearing_house/src/state/history/funding_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ pub struct FundingRateHistory {
funding_rate_records: [FundingRateRecord; 1024],
}

impl Default for FundingRateHistory {
fn default() -> Self {
FundingRateHistory {
head: 0,
funding_rate_records: [FundingRateRecord::default(); 1024],
}
}
}

impl FundingRateHistory {
pub fn append(&mut self, pos: FundingRateRecord) {
self.funding_rate_records[FundingRateHistory::index_of(self.head)] = pos;
Expand Down
9 changes: 0 additions & 9 deletions programs/clearing_house/src/state/history/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ pub struct LiquidationHistory {
liquidation_records: [LiquidationRecord; 1024],
}

impl Default for LiquidationHistory {
fn default() -> Self {
LiquidationHistory {
head: 0,
liquidation_records: [LiquidationRecord::default(); 1024],
}
}
}

impl LiquidationHistory {
pub fn append(&mut self, pos: LiquidationRecord) {
self.liquidation_records[LiquidationHistory::index_of(self.head)] = pos;
Expand Down
10 changes: 0 additions & 10 deletions programs/clearing_house/src/state/history/order_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ pub struct OrderHistory {
order_records: [OrderRecord; 1024],
}

impl Default for OrderHistory {
fn default() -> Self {
OrderHistory {
head: 0,
last_order_id: 0,
order_records: [OrderRecord::default(); 1024],
}
}
}

impl OrderHistory {
pub fn append(&mut self, record: OrderRecord) {
self.order_records[OrderHistory::index_of(self.head)] = record;
Expand Down
9 changes: 0 additions & 9 deletions programs/clearing_house/src/state/history/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ pub struct TradeHistory {
trade_records: [TradeRecord; 1024],
}

impl Default for TradeHistory {
fn default() -> Self {
TradeHistory {
head: 0,
trade_records: [TradeRecord::default(); 1024],
}
}
}

impl TradeHistory {
pub fn append(&mut self, pos: TradeRecord) {
self.trade_records[TradeHistory::index_of(self.head)] = pos;
Expand Down
2 changes: 1 addition & 1 deletion programs/clearing_house/src/state/user_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
#[derive(Default)]
pub struct UserOrders {
pub user: Pubkey,
pub orders: [Order; 20],
pub orders: [Order; 32],
}

impl UserOrders {
Expand Down
Loading