Skip to content
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
49 changes: 24 additions & 25 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,54 @@
# Drift Protocol Dev Container
#

FROM --platform=linux/amd64 rust:1.70.0
FROM --platform=linux/amd64 ubuntu:24.04

ARG DEBIAN_FRONTEND=noninteractive
ARG SOLANA_CLI="1.16.27"
ARG ANCHOR_CLI="0.29.0"
ARG NODE_VERSION="20.18.1"
ARG NODE_VERSION="24.0.0"

ENV HOME="/root"
ENV PATH="/usr/local/cargo/bin:${PATH}"
ENV PATH="/root/.local/share/solana/install/active_release/bin:${PATH}"

RUN mkdir -p /workdir /tmp \
&& apt-get update -qq \
&& apt-get upgrade -qq \
&& apt-get install -y --no-install-recommends \
build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \
libssl-dev libudev-dev bash software-properties-common \
&& add-apt-repository 'deb http://deb.debian.org/debian bookworm main' \
&& apt-get update -qq \
&& apt-get install -y libc6 libc6-dev \
&& rm -rf /var/lib/apt/lists/*
&& apt-get update -qq \
&& apt-get upgrade -qq \
&& apt-get install -y --no-install-recommends \
build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \
libssl-dev libudev-dev bash software-properties-common \
&& rm -rf /var/lib/apt/lists/*

RUN rustup component add rustfmt
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.70.0 \
&& . "$HOME/.cargo/env" \
&& rustup install 1.78.0 \
&& rustup component add rustfmt clippy --toolchain 1.78.0

RUN rustup install 1.78.0 \
&& rustup component add rustfmt clippy --toolchain 1.78.0
ENV PATH="/root/.cargo/bin:${PATH}"

RUN curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" -o /tmp/node.tar.xz \
&& tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \
&& rm /tmp/node.tar.xz \
&& corepack enable \
&& npm install -g ts-mocha typescript mocha \
&& node -v && npm -v && yarn -v
&& tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \
&& rm /tmp/node.tar.xz \
&& corepack enable \
&& npm install -g ts-mocha typescript mocha \
&& node -v && npm -v && yarn -v

# Solana CLI (x86_64 build)
RUN curl -sSfL "https://github.com/solana-labs/solana/releases/download/v${SOLANA_CLI}/solana-release-x86_64-unknown-linux-gnu.tar.bz2" \
| tar -xjC /tmp \
&& mv /tmp/solana-release/bin/* /usr/local/bin/ \
&& rm -rf /tmp/solana-release
| tar -xjC /tmp \
&& mv /tmp/solana-release/bin/* /usr/local/bin/ \
&& rm -rf /tmp/solana-release

# Anchor CLI
RUN cargo install --git https://github.com/coral-xyz/anchor --tag "v${ANCHOR_CLI}" anchor-cli --locked

# Set up Solana key + config for root
RUN solana-keygen new --no-bip39-passphrase --force \
&& solana config set --url localhost
&& solana config set --url localhost

RUN apt-get update && apt-get install -y zsh curl git \
&& sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended \
&& chsh -s /usr/bin/zsh root
&& sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended \
&& chsh -s /usr/bin/zsh root

WORKDIR /workdir
2 changes: 2 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node-option:
- no-experimental-strip-types
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@
"chalk-template": "<1.1.1",
"supports-hyperlinks": "<4.1.1",
"has-ansi": "<6.0.1"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
92 changes: 4 additions & 88 deletions programs/drift/src/controller/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,77 +604,13 @@ pub fn update_pool_balances(
user_unsettled_pnl: i128,
now: i64,
) -> DriftResult<i128> {
// current spot_market balance of amm fee pool
let amm_fee_pool_token_amount = get_token_amount(
market.amm.fee_pool.balance(),
spot_market,
market.amm.fee_pool.balance_type(),
)?
.cast::<i128>()?;

let mut fraction_for_amm = 100;

let amm_target_max_fee_pool_token_amount = market
.amm
.total_fee_minus_distributions
.safe_add(market.amm.total_liquidation_fee.cast()?)?
.safe_sub(market.amm.total_fee_withdrawn.cast()?)?;

if amm_target_max_fee_pool_token_amount <= amm_fee_pool_token_amount {
// owe the market pnl pool before settling user
let pnl_pool_addition =
max(0, amm_target_max_fee_pool_token_amount).safe_sub(amm_fee_pool_token_amount)?;

if pnl_pool_addition < 0 {
transfer_spot_balances(
pnl_pool_addition.abs(),
spot_market,
&mut market.amm.fee_pool,
&mut market.pnl_pool,
)?;
}

fraction_for_amm = 0;
}

{
let amm_target_min_fee_pool_token_amount = get_total_fee_lower_bound(market)?
.safe_add(market.amm.total_liquidation_fee)?
.safe_sub(market.amm.total_fee_withdrawn)?;

let amm_fee_pool_token_amount = get_token_amount(
market.amm.fee_pool.balance(),
spot_market,
market.amm.fee_pool.balance_type(),
)?;

if amm_fee_pool_token_amount < amm_target_min_fee_pool_token_amount {
let pnl_pool_token_amount = get_token_amount(
market.pnl_pool.balance(),
spot_market,
market.pnl_pool.balance_type(),
)?;

let pnl_pool_removal = amm_target_min_fee_pool_token_amount
.safe_sub(amm_fee_pool_token_amount)?
.min(pnl_pool_token_amount);

if pnl_pool_removal > 0 {
transfer_spot_balances(
pnl_pool_removal.cast::<i128>()?,
spot_market,
&mut market.pnl_pool,
&mut market.amm.fee_pool,
)?;
}
}

let amm_fee_pool_token_amount_after = get_token_amount(
market.amm.fee_pool.balance(),
spot_market,
market.amm.fee_pool.balance_type(),
)?;

let terminal_state_surplus = market
.amm
.total_fee_minus_distributions
Expand All @@ -697,7 +633,7 @@ pub fn update_pool_balances(
let revenue_pool_transfer = calculate_revenue_pool_transfer(
market,
spot_market,
amm_fee_pool_token_amount_after,
amm_fee_pool_token_amount,
terminal_state_surplus,
)?;

Expand All @@ -714,17 +650,11 @@ pub fn update_pool_balances(
.total_fee_withdrawn
.safe_add(revenue_pool_transfer.unsigned_abs())?;
}
Ordering::Less => {
transfer_revenue_pool_to_spot_balance(
revenue_pool_transfer.unsigned_abs(),
spot_market,
&mut market.amm.fee_pool,
)?;
}
Ordering::Less => (),
Ordering::Equal => (),
}

if revenue_pool_transfer != 0 {
if revenue_pool_transfer > 0 {
market.amm.total_fee_minus_distributions = market
.amm
.total_fee_minus_distributions
Expand Down Expand Up @@ -759,21 +689,7 @@ pub fn update_pool_balances(
max_withdraw_amount.max(user_unsettled_pnl)
};

let pnl_fraction_for_amm = if fraction_for_amm > 0 && pnl_to_settle_with_user < 0 {
let pnl_fraction_for_amm = pnl_to_settle_with_user.safe_div(fraction_for_amm)?;
update_spot_balances(
pnl_fraction_for_amm.unsigned_abs(),
&SpotBalanceType::Deposit,
spot_market,
&mut market.amm.fee_pool,
false,
)?;
pnl_fraction_for_amm
} else {
0
};

let pnl_to_settle_with_market = -(pnl_to_settle_with_user.safe_sub(pnl_fraction_for_amm)?);
let pnl_to_settle_with_market = -(pnl_to_settle_with_user);

update_spot_balances(
pnl_to_settle_with_market.unsigned_abs(),
Expand Down
Loading
Loading