Skip to content

Commit 283016a

Browse files
committed
Merge branch 'master' into crispheaney/isolated-position
2 parents 3bd7525 + 6a1d7a2 commit 283016a

File tree

119 files changed

+12294
-3744
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+12294
-3744
lines changed

.devcontainer/Dockerfile

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,49 @@
11
#
2-
# Docker image to generate deterministic, verifiable builds of Anchor programs.
3-
# This must be run *after* a given ANCHOR_CLI version is published and a git tag
4-
# is released on GitHub.
2+
# Drift Protocol Dev Container
53
#
64

7-
FROM rust:1.75
5+
FROM --platform=linux/amd64 rust:1.70.0
86

97
ARG DEBIAN_FRONTEND=noninteractive
10-
11-
ARG SOLANA_CLI="1.14.7"
12-
ARG ANCHOR_CLI="0.26.0"
13-
ARG NODE_VERSION="v18.16.0"
8+
ARG SOLANA_CLI="1.16.27"
9+
ARG ANCHOR_CLI="0.29.0"
10+
ARG NODE_VERSION="20.18.1"
1411

1512
ENV HOME="/root"
16-
ENV PATH="${HOME}/.cargo/bin:${PATH}"
17-
ENV PATH="${HOME}/.local/share/solana/install/active_release/bin:${PATH}"
18-
ENV PATH="${HOME}/.nvm/versions/node/${NODE_VERSION}/bin:${PATH}"
19-
20-
# Install base utilities.
21-
RUN mkdir -p /workdir && mkdir -p /tmp && \
22-
apt-get update -qq && apt-get upgrade -qq && apt-get install -qq \
23-
build-essential git curl wget jq pkg-config python3-pip \
24-
libssl-dev libudev-dev
25-
26-
RUN wget http://nz2.archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
27-
RUN dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
28-
29-
# Install rust.
30-
RUN curl "https://sh.rustup.rs" -sfo rustup.sh && \
31-
sh rustup.sh -y && \
32-
rustup component add rustfmt clippy
33-
34-
# Install node / npm / yarn.
35-
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
36-
ENV NVM_DIR="${HOME}/.nvm"
37-
RUN . $NVM_DIR/nvm.sh && \
38-
nvm install ${NODE_VERSION} && \
39-
nvm use ${NODE_VERSION} && \
40-
nvm alias default node && \
41-
npm install -g yarn && \
42-
yarn add ts-mocha
43-
44-
# Install Solana tools.
45-
RUN sh -c "$(curl -sSfL https://release.solana.com/v${SOLANA_CLI}/install)"
46-
47-
# Install anchor.
48-
RUN cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
49-
RUN avm install ${ANCHOR_CLI} && avm use ${ANCHOR_CLI}
50-
51-
RUN solana-keygen new --no-bip39-passphrase
13+
ENV PATH="/usr/local/cargo/bin:${PATH}"
14+
ENV PATH="/root/.local/share/solana/install/active_release/bin:${PATH}"
15+
16+
RUN mkdir -p /workdir /tmp && \
17+
apt-get update -qq && apt-get upgrade -qq && apt-get install -y --no-install-recommends \
18+
build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \
19+
libssl-dev libudev-dev bash && \
20+
rm -rf /var/lib/apt/lists/*
21+
22+
RUN rustup install 1.78.0 \
23+
&& rustup component add rustfmt clippy --toolchain 1.78.0
24+
25+
RUN curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" -o /tmp/node.tar.xz \
26+
&& tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \
27+
&& rm /tmp/node.tar.xz \
28+
&& corepack enable \
29+
&& npm install -g ts-mocha typescript mocha \
30+
&& node -v && npm -v && yarn -v
31+
32+
# Solana CLI (x86_64 build)
33+
RUN curl -sSfL "https://github.com/solana-labs/solana/releases/download/v${SOLANA_CLI}/solana-release-x86_64-unknown-linux-gnu.tar.bz2" \
34+
| tar -xjC /tmp \
35+
&& mv /tmp/solana-release/bin/* /usr/local/bin/ \
36+
&& rm -rf /tmp/solana-release
37+
38+
# Anchor CLI
39+
RUN cargo install --git https://github.com/coral-xyz/anchor --tag "v${ANCHOR_CLI}" anchor-cli --locked
40+
41+
# Set up Solana key + config for root
42+
RUN solana-keygen new --no-bip39-passphrase --force \
43+
&& solana config set --url localhost
44+
45+
RUN apt-get update && apt-get install -y zsh curl git \
46+
&& sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended \
47+
&& chsh -s /usr/bin/zsh root
5248

5349
WORKDIR /workdir
54-
#be sure to add `/root/.avm/bin` to your PATH to be able to run the installed binaries

.devcontainer/devcontainer.json

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
11
{
2-
"build": { "dockerfile": "Dockerfile" },
3-
}
2+
"name": "Drift Protocol Development",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"platform": "linux/amd64"
6+
},
7+
"workspaceFolder": "/workdir",
8+
"remoteUser": "root",
9+
"mounts": [
10+
"source=${localWorkspaceFolder},target=/workdir,type=bind,consistency=cached",
11+
"source=drift-target,target=/workdir/target,type=volume,consistency=delegated"
12+
],
13+
"postCreateCommand": "yarn config set ignore-package-manager true && echo 'Dev container ready! You can now run: anchor build, anchor test, cargo build, etc.'",
14+
"customizations": {
15+
"vscode": {
16+
"extensions": [
17+
"rust-lang.rust-analyzer",
18+
"ms-vscode.vscode-json",
19+
"tamasfe.even-better-toml"
20+
],
21+
"settings": {
22+
"rust-analyzer.cachePriming.numThreads": 1,
23+
"rust-analyzer.cargo.buildScripts.enable": true,
24+
"rust-analyzer.procMacro.enable": true,
25+
"rust-analyzer.checkOnSave": true,
26+
"rust-analyzer.check.command": "clippy",
27+
"rust-analyzer.server.extraEnv": {
28+
"NODE_OPTIONS": "--max-old-space-size=4096",
29+
"RUSTUP_TOOLCHAIN": "1.78.0-x86_64-unknown-linux-gnu"
30+
},
31+
"editor.formatOnSave": true,
32+
"git.ignoreLimitWarning": true
33+
}
34+
}
35+
},
36+
"forwardPorts": [
37+
8899,
38+
8900
39+
],
40+
"portsAttributes": {
41+
"8899": {
42+
"label": "Solana Test Validator",
43+
"onAutoForward": "notify"
44+
},
45+
"8900": {
46+
"label": "Solana Test Validator RPC",
47+
"onAutoForward": "notify"
48+
}
49+
},
50+
"containerEnv": {
51+
"ANCHOR_WALLET": "/root/.config/solana/id.json",
52+
"RUST_LOG": "solana_runtime::message_processor::stable_log=debug"
53+
}
54+
}

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [master]
66
pull_request:
7-
branches: [master]
7+
branches: [master, devnet]
88

99
defaults:
1010
run:

CHANGELOG.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,74 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Features
1111

12+
### Fixes
13+
14+
### Breaking
15+
16+
## [2.143.0] - 2025-10-22
17+
18+
- program: relax filling conditions for low risk orders vs amm ([#1968](https://github.com/drift-labs/protocol-v2/pull/1968))
19+
- sdk: make oracle validity match program and propogate to dlob and math functions ([#1968](https://github.com/drift-labs/protocol-v2/pull/1968))
20+
21+
### Features
22+
23+
- program: make imf smoother between hlm and non hlm users ([#1969](https://github.com/drift-labs/protocol-v2/pull/1969))
24+
25+
### Fixes
26+
27+
### Breaking
28+
29+
## [2.142.0] - 2025-10-14
30+
31+
### Features
32+
33+
- program: add titan to whitelisted swap programs ([#1952](https://github.com/drift-labs/protocol-v2/pull/1952))
34+
- program: allow hot wallet to increase max spread and pause funding ([#1957](https://github.com/drift-labs/protocol-v2/pull/1957))
35+
36+
### Fixes
37+
38+
### Breaking
39+
40+
## [2.141.0] - 2025-10-03
41+
42+
### Features
43+
44+
- program: disallow builder to be escrow authority ([#1930](https://github.com/drift-labs/protocol-v2/pull/1930))
45+
- dont panic on settle-pnl when no position ([#1928](https://github.com/drift-labs/protocol-v2/pull/1928))
46+
47+
### Fixes
48+
49+
### Breaking
50+
51+
## [2.140.0] - 2025-09-29
52+
53+
### Features
54+
55+
- program: builder codes ([#1805](https://github.com/drift-labs/protocol-v2/pull/1805))
56+
57+
### Fixes
58+
59+
### Breaking
60+
61+
## [2.139.0] - 2025-09-25
62+
63+
### Features
64+
65+
- program: all token 22 use immutable owner ([#1904](https://github.com/drift-labs/protocol-v2/pull/1904))
66+
- program: allow resolve perp pnl deficit if pnl pool isnt 0 but at deficit ([#1909](https://github.com/drift-labs/protocol-v2/pull/1909))
67+
- program: auction order params account for twap divergence ([#1882](https://github.com/drift-labs/protocol-v2/pull/1882))
68+
- program: add delegate stake if ([#1859](https://github.com/drift-labs/protocol-v2/pull/1859))
69+
70+
### Fixes
71+
72+
### Breaking
73+
74+
## [2.138.0] - 2025-09-22
75+
76+
### Features
77+
1278
- program: support scaled ui extension ([#1894](https://github.com/drift-labs/protocol-v2/pull/1894))
79+
- Revert "Crispeaney/revert swift max margin ratio ([#1877](https://github.com/drift-labs/protocol-v2/pull/1877))
1380

1481
### Fixes
1582

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,48 @@ cargo test
5555
bash test-scripts/run-anchor-tests.sh
5656
```
5757

58+
# Development (with devcontainer)
59+
60+
We've provided a devcontainer `Dockerfile` to help you spin up a dev environment with the correct versions of Rust, Solana, and Anchor for program development.
61+
62+
Build the container and tag it `drift-dev`:
63+
```
64+
cd .devcontainer && docker build -t drift-dev .
65+
```
66+
67+
Open a shell to the container:
68+
```
69+
# Find the container ID first
70+
docker ps
71+
72+
# Then exec into it
73+
docker exec -it <CONTAINER_ID> /bin/bash
74+
```
75+
76+
Alternatively use an extension provided by your IDE to make use of the dev container. For example on vscode/cursor:
77+
78+
```
79+
1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
80+
2. Type "Dev Containers: Reopen in Container"
81+
3. Select it and wait for the container to build
82+
4. The IDE terminal should be targeting the dev container now
83+
```
84+
85+
Use the dev container as you would a local build environment:
86+
```
87+
# build program
88+
anchor build
89+
90+
# update idl
91+
anchor build -- --features anchor-test && cp target/idl/drift.json sdk/src/idl/drift.json
92+
93+
# run cargo tests
94+
cargo test
95+
96+
# run typescript tests
97+
bash test-scripts/run-anchor-tests.sh
98+
```
99+
58100
# Bug Bounty
59101

60102
Information about the Bug Bounty can be found [here](./bug-bounty/README.md)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"prettify:fix": "prettier --write './sdk/src/**/*.ts' './tests/**.ts' './cli/**.ts'",
5151
"lint": "eslint . --ext ts --quiet --format unix",
5252
"lint:fix": "eslint . --ext ts --fix",
53-
"update-idl": "cp target/idl/drift.json sdk/src/idl/drift.json"
53+
"update-idl": "anchor build -- --features anchor-test && cp target/idl/drift.json sdk/src/idl/drift.json"
5454
},
5555
"engines": {
5656
"node": ">=12"

programs/drift/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "drift"
3-
version = "2.137.0"
3+
version = "2.143.0"
44
description = "Created with Anchor"
55
edition = "2018"
66

programs/drift/src/controller/amm.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::controller::spot_balance::{
1111
};
1212
use crate::error::{DriftResult, ErrorCode};
1313
use crate::get_then_update_id;
14-
use crate::math::amm::calculate_quote_asset_amount_swapped;
14+
use crate::math::amm::{calculate_net_user_pnl, calculate_quote_asset_amount_swapped};
1515
use crate::math::amm_spread::{calculate_spread_reserves, get_spread_reserves};
1616
use crate::math::casting::Cast;
1717
use crate::math::constants::{
@@ -185,20 +185,33 @@ pub fn update_spreads(
185185
let max_ref_offset = market.amm.get_max_reference_price_offset()?;
186186

187187
let reference_price_offset = if max_ref_offset > 0 {
188-
let liquidity_ratio = amm_spread::calculate_inventory_liquidity_ratio(
189-
market.amm.base_asset_amount_with_amm,
190-
market.amm.base_asset_reserve,
191-
market.amm.min_base_asset_reserve,
192-
market.amm.max_base_asset_reserve,
193-
)?;
188+
let liquidity_ratio =
189+
amm_spread::calculate_inventory_liquidity_ratio_for_reference_price_offset(
190+
market.amm.base_asset_amount_with_amm,
191+
market.amm.base_asset_reserve,
192+
market.amm.min_base_asset_reserve,
193+
market.amm.max_base_asset_reserve,
194+
)?;
194195

195196
let signed_liquidity_ratio =
196197
liquidity_ratio.safe_mul(market.amm.get_protocol_owned_position()?.signum().cast()?)?;
197198

199+
let deadband_pct = market.amm.get_reference_price_offset_deadband_pct()?;
200+
let liquidity_fraction_after_deadband =
201+
if signed_liquidity_ratio.unsigned_abs() <= deadband_pct {
202+
0
203+
} else {
204+
signed_liquidity_ratio.safe_sub(
205+
deadband_pct
206+
.cast::<i128>()?
207+
.safe_mul(signed_liquidity_ratio.signum())?,
208+
)?
209+
};
210+
198211
amm_spread::calculate_reference_price_offset(
199212
reserve_price,
200213
market.amm.last_24h_avg_funding_rate,
201-
signed_liquidity_ratio,
214+
liquidity_fraction_after_deadband,
202215
market.amm.min_order_size,
203216
market
204217
.amm
@@ -966,7 +979,7 @@ pub fn calculate_perp_market_amm_summary_stats(
966979
.safe_add(fee_pool_token_amount)?
967980
.cast()?;
968981

969-
let net_user_pnl = amm::calculate_net_user_pnl(&perp_market.amm, perp_market_oracle_price)?;
982+
let net_user_pnl = calculate_net_user_pnl(&perp_market.amm, perp_market_oracle_price)?;
970983

971984
// amm's mm_fee can be incorrect with drifting integer math error
972985
let mut new_total_fee_minus_distributions = pnl_tokens_available.safe_sub(net_user_pnl)?;

0 commit comments

Comments
 (0)