Skip to content

Commit

Permalink
add priority fees (#38)
Browse files Browse the repository at this point in the history
* update dependencies and add compute units/priority fee

* update submodule

* add versioned tx

* add black to pre-commit

* merge master + anchorpy upgrade pubkey fixes

* fix tests/acceptance_test.py and idl load

---------

Co-authored-by: 0xbigz <[email protected]>
  • Loading branch information
crispheaney and 0xbigz authored Nov 17, 2023
1 parent 15eb39c commit 8e7da0e
Show file tree
Hide file tree
Showing 28 changed files with 845 additions and 629 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
language_version: python3.10
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ test:

lint:
poetry run black --check --diff src tests
poetry run flake8 src tests
poetry run mypy src tests

lintfix:
poetry run black src tests

281 changes: 146 additions & 135 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion protocol-v2
Submodule protocol-v2 updated 42 files
+74 −30 .github/workflows/main.yml
+1 −0 Anchor.toml
+8 −5 CHANGELOG.md
+1 −1 Cargo.lock
+9 −0 codecov.yml
+1 −1 programs/drift/Cargo.toml
+2 −2 programs/drift/src/controller/insurance/tests.rs
+205 −68 programs/drift/src/controller/liquidation/tests.rs
+10 −2 programs/drift/src/controller/orders.rs
+8 −0 programs/drift/src/instructions/admin.rs
+1 −1 programs/drift/src/instructions/user.rs
+7 −0 programs/drift/src/lib.rs
+0 −1 programs/drift/src/math/amm.rs
+1 −4 programs/drift/src/math/cp_curve.rs
+5 −0 programs/drift/src/math/liquidation.rs
+23 −0 programs/drift/src/math/liquidation/tests.rs
+58 −48 programs/drift/src/math/orders.rs
+467 −2 programs/drift/src/math/orders/tests.rs
+4 −15 programs/drift/src/math/position.rs
+0 −2 programs/drift/src/math/repeg.rs
+1 −0 sdk/README.md
+1 −1 sdk/VERSION
+ sdk/bun.lockb
+47 −0 sdk/get_events.ts
+2 −1 sdk/package.json
+20 −6 sdk/src/accounts/webSocketAccountSubscriber.ts
+19 −6 sdk/src/accounts/webSocketProgramAccountSubscriber.ts
+14 −0 sdk/src/adminClient.ts
+20 −0 sdk/src/constants/perpMarkets.ts
+1 −1 sdk/src/constants/spotMarkets.ts
+117 −21 sdk/src/dlob/DLOB.ts
+13 −1 sdk/src/dlob/orderBookLevels.ts
+129 −82 sdk/src/driftClient.ts
+22 −1 sdk/src/idl/drift.json
+12 −2 sdk/src/jupiter/jupiterClient.ts
+2 −2 sdk/src/math/amm.ts
+26 −26 sdk/src/math/trade.ts
+7 −3 sdk/src/tx/fastSingleTxSender.ts
+17 −9 sdk/src/user.ts
+78 −0 sdk/tests/amm/test.ts
+10 −13 sdk/tests/user/test.ts
+19 −0 sdk/yarn.lock
22 changes: 12 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ documentation = "https://drift-labs.github.io/driftpy/"

[tool.poetry.dependencies]
python = "^3.10"
anchorpy = "0.10.0"
solana = "0.25.1"
anchorpy = "0.17.1"
solana = "0.30.1"
requests = "2.28.1"
types-requests = "^2.28.9"
mkdocs = "^1.3.0"
Expand Down Expand Up @@ -62,12 +62,12 @@ pycparser = "2.21"
pyflakes = "3.0.1"
pyheck = "0.1.5"
pyrsistent = "0.19.2"
pytest = "6.2.5"
pytest-asyncio = "0.17.2"
pytest-xprocess = "0.18.1"
pytest = "^7.2.0"
pytest-xprocess = "^0.18.1"
pytest-asyncio = "^0.21.0"
rfc3986 = "1.5.0"
sniffio = "1.3.0"
solders = "0.2.0"
solders = "0.17.0"
sumtypes = "0.1a6"
toml = "0.10.2"
tomli = "2.0.1"
Expand All @@ -77,19 +77,21 @@ typing-extensions = "4.4.0"
urllib3 = "1.26.13"
websockets = "10.4"
yarl = "1.8.2"
zstandard = "0.17.0"
zstandard = "0.18.0"
jinja2 = "<3.1"
mypy = "^1.7.0"

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
pytest = "^7.2.0"
flake8 = "6.0.0"
mypy = "^0.931"
black = "^23.3.0"
pytest-asyncio = "^0.17.2"
pytest-asyncio = "^0.21.0"
mkdocs = "^1.3.0"
mkdocstrings = "^0.17.0"
mkdocs-material = "^8.1.8"
bump2version = "^1.0.1"
autopep8 = "^2.0.4"
mypy = "^1.7.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
21 changes: 10 additions & 11 deletions src/driftpy/_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from driftpy.constants.numeric_constants import SPOT_RATE_PRECISION
from driftpy.types import OracleSource
from typing import Optional, Any
from dataclasses import dataclass
from sumtypes import constructor # type: ignore
Expand Down Expand Up @@ -287,9 +289,10 @@ class MarketPosition:
padding3: int
padding4: int

## dw why this doesnt register :(
# dw why this doesnt register :(
# def is_available(self):
# return self.base_asset_amount == 0 and self.open_orders == 0 and self.lp_shares == 0
# return self.base_asset_amount == 0 and self.open_orders == 0 and
# self.lp_shares == 0


@dataclass
Expand Down Expand Up @@ -377,14 +380,14 @@ class AMM:
quote_asset_amount_long: int = 0
quote_asset_amount_short: int = 0

## lp stuff
# lp stuff
cumulative_funding_payment_per_lp: int = 0
cumulative_fee_per_lp: int = 0
cumulative_base_asset_amount_with_amm_per_lp: int = 0
lp_cooldown_time: int = 0
user_lp_shares: int = 0

## funding
# funding
last_funding_rate: int = 0
last_funding_rate_ts: int = 0
funding_period: int = 0
Expand All @@ -397,11 +400,11 @@ class AMM:
last_mark_price_twap: int = 0
last_mark_price_twap_ts: int = 0

## trade constraints
# trade constraints
minimum_quote_asset_trade_size: int = 0
base_asset_amount_step_size: int = 0

## market making
# market making
base_spread: int = 0
long_spread: int = 0
short_spread: int = 0
Expand All @@ -420,7 +423,7 @@ class AMM:
short_intensity_volume: int = 0
curve_update_intensity: int = 0

## fee tracking
# fee tracking
total_fee: int = 0
total_mm_fee: int = 0
total_exchange_fee: int = 0
Expand Down Expand Up @@ -462,10 +465,6 @@ class Market:
padding4: int = 0


from driftpy.types import OracleSource
from driftpy.constants.numeric_constants import SPOT_RATE_PRECISION


@dataclass
class SpotMarket:
mint: PublicKey # this
Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/accounts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .get_accounts import *
from .types import *
from .types import *
2 changes: 1 addition & 1 deletion src/driftpy/accounts/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .drift_client import *
from .user import *
from .user import *
42 changes: 29 additions & 13 deletions src/driftpy/accounts/cache/drift_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from anchorpy import Program
from solana.publickey import PublicKey
from solders.pubkey import Pubkey
from solana.rpc.commitment import Commitment

from driftpy.accounts import get_state_account_and_slot, get_spot_market_account_and_slot, \
get_perp_market_account_and_slot
from driftpy.accounts import (
get_state_account_and_slot,
get_spot_market_account_and_slot,
get_perp_market_account_and_slot,
)
from driftpy.accounts.oracle import get_oracle_price_data_and_slot
from driftpy.accounts.types import DriftClientAccountSubscriber, DataAndSlot
from typing import Optional
Expand All @@ -28,30 +31,37 @@ async def update_cache(self):

spot_markets = []
for i in range(state_and_slot.data.number_of_spot_markets):
spot_market_and_slot = await get_spot_market_account_and_slot(self.program, i)
spot_market_and_slot = await get_spot_market_account_and_slot(
self.program, i
)
spot_markets.append(spot_market_and_slot)

oracle_price_data_and_slot = await get_oracle_price_data_and_slot(
self.program.provider.connection,
spot_market_and_slot.data.oracle,
spot_market_and_slot.data.oracle_source

spot_market_and_slot.data.oracle_source,
)
oracle_data[str(spot_market_and_slot.data.oracle)] = oracle_price_data_and_slot
oracle_data[
str(spot_market_and_slot.data.oracle)
] = oracle_price_data_and_slot

self.cache["spot_markets"] = spot_markets

perp_markets = []
for i in range(state_and_slot.data.number_of_markets):
perp_market_and_slot = await get_perp_market_account_and_slot(self.program, i)
perp_market_and_slot = await get_perp_market_account_and_slot(
self.program, i
)
perp_markets.append(perp_market_and_slot)

oracle_price_data_and_slot = await get_oracle_price_data_and_slot(
self.program.provider.connection,
perp_market_and_slot.data.amm.oracle,
perp_market_and_slot.data.amm.oracle_source
perp_market_and_slot.data.amm.oracle_source,
)
oracle_data[str(perp_market_and_slot.data.amm.oracle)] = oracle_price_data_and_slot
oracle_data[
str(perp_market_and_slot.data.amm.oracle)
] = oracle_price_data_and_slot

self.cache["perp_markets"] = perp_markets

Expand All @@ -61,15 +71,21 @@ async def get_state_account_and_slot(self) -> Optional[DataAndSlot[State]]:
await self.cache_if_needed()
return self.cache["state"]

async def get_perp_market_and_slot(self, market_index: int) -> Optional[DataAndSlot[PerpMarket]]:
async def get_perp_market_and_slot(
self, market_index: int
) -> Optional[DataAndSlot[PerpMarket]]:
await self.cache_if_needed()
return self.cache["perp_markets"][market_index]

async def get_spot_market_and_slot(self, market_index: int) -> Optional[DataAndSlot[SpotMarket]]:
async def get_spot_market_and_slot(
self, market_index: int
) -> Optional[DataAndSlot[SpotMarket]]:
await self.cache_if_needed()
return self.cache["spot_markets"][market_index]

async def get_oracle_data_and_slot(self, oracle: PublicKey) -> Optional[DataAndSlot[OraclePriceData]]:
async def get_oracle_data_and_slot(
self, oracle: Pubkey
) -> Optional[DataAndSlot[OraclePriceData]]:
await self.cache_if_needed()
return self.cache["oracle_price_data"][str(oracle)]

Expand Down
9 changes: 7 additions & 2 deletions src/driftpy/accounts/cache/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from anchorpy import Program
from solana.publickey import PublicKey
from solders.pubkey import Pubkey
from solana.rpc.commitment import Commitment

from driftpy.accounts import get_user_account_and_slot
Expand All @@ -10,7 +10,12 @@


class CachedUserAccountSubscriber(UserAccountSubscriber):
def __init__(self, user_pubkey: PublicKey, program: Program, commitment: Commitment = "confirmed"):
def __init__(
self,
user_pubkey: Pubkey,
program: Program,
commitment: Commitment = "confirmed",
):
self.program = program
self.commitment = commitment
self.user_pubkey = user_pubkey
Expand Down
41 changes: 24 additions & 17 deletions src/driftpy/accounts/get_accounts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
from typing import cast
from solana.publickey import PublicKey
from solders.pubkey import Pubkey
from anchorpy import Program, ProgramAccount
from solana.rpc.commitment import Commitment

Expand All @@ -9,19 +9,20 @@
from .types import DataAndSlot, T


async def get_account_data_and_slot(address: PublicKey, program: Program, commitment: Commitment = "processed") -> Optional[
DataAndSlot[T]]:
async def get_account_data_and_slot(
address: Pubkey, program: Program, commitment: Commitment = "processed"
) -> Optional[DataAndSlot[T]]:
account_info = await program.provider.connection.get_account_info(
address,
encoding="base64",
commitment=commitment,
)

if not account_info["result"]["value"]:
if not account_info.value:
return None

slot = account_info["result"]["context"]["slot"]
data = base64.b64decode(account_info["result"]["value"]["data"][0])
slot = account_info.context.slot
data = account_info.value.data

decoded_data = program.coder.accounts.decode(data)

Expand All @@ -38,7 +39,7 @@ async def get_state_account(program: Program) -> State:


async def get_if_stake_account(
program: Program, authority: PublicKey, spot_market_index: int
program: Program, authority: Pubkey, spot_market_index: int
) -> InsuranceFundStake:
if_stake_pk = get_insurance_fund_stake_public_key(
program.program_id, authority, spot_market_index
Expand All @@ -48,8 +49,8 @@ async def get_if_stake_account(


async def get_user_stats_account(
program: Program,
authority: PublicKey,
program: Program,
authority: Pubkey,
) -> UserStats:
user_stats_public_key = get_user_stats_account_public_key(
program.program_id,
Expand All @@ -58,21 +59,27 @@ async def get_user_stats_account(
response = await program.account["UserStats"].fetch(user_stats_public_key)
return cast(UserStats, response)


async def get_user_account_and_slot(
program: Program,
user_public_key: PublicKey,
program: Program,
user_public_key: Pubkey,
) -> DataAndSlot[User]:
return await get_account_data_and_slot(user_public_key, program)


async def get_user_account(
program: Program,
user_public_key: PublicKey,
program: Program,
user_public_key: Pubkey,
) -> User:
return (await get_user_account_and_slot(program, user_public_key)).data


async def get_perp_market_account_and_slot(program: Program, market_index: int) -> Optional[DataAndSlot[PerpMarket]]:
perp_market_public_key = get_perp_market_public_key(program.program_id, market_index)
async def get_perp_market_account_and_slot(
program: Program, market_index: int
) -> Optional[DataAndSlot[PerpMarket]]:
perp_market_public_key = get_perp_market_public_key(
program.program_id, market_index
)
return await get_account_data_and_slot(perp_market_public_key, program)


Expand All @@ -85,7 +92,7 @@ async def get_all_perp_market_accounts(program: Program) -> list[ProgramAccount]


async def get_spot_market_account_and_slot(
program: Program, spot_market_index: int
program: Program, spot_market_index: int
) -> DataAndSlot[SpotMarket]:
spot_market_public_key = get_spot_market_public_key(
program.program_id, spot_market_index
Expand All @@ -94,7 +101,7 @@ async def get_spot_market_account_and_slot(


async def get_spot_market_account(
program: Program, spot_market_index: int
program: Program, spot_market_index: int
) -> SpotMarket:
return (await get_spot_market_account_and_slot(program, spot_market_index)).data

Expand Down
Loading

0 comments on commit 8e7da0e

Please sign in to comment.