Skip to content

Commit

Permalink
Python fixes (#14)
Browse files Browse the repository at this point in the history
* fix: post only

* chore: update python README & bump driftpy to 0.7.8

* fix: merge conflicts
  • Loading branch information
soundsonacid authored Jan 9, 2024
1 parent 3c9c6fa commit 373f9e3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
7 changes: 4 additions & 3 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## QuickStart Guide

1. Add ```.env``` file in ```/sdk/examples``` with your ```RPC_URL``` and ```PRIVATE_KEY``` as a byte array
2. Customize ```JitParams``` in either ```shotgun.py``` or ```sniper.py```
3. Run ```python sniper.py``` or ```python shotgun.py``` while in the ```examples``` directory
1. Run `poetry install` in the `/python` directory
2. Add ```.env``` file in ```/sdk/examples``` with your ```RPC_URL``` and ```PRIVATE_KEY``` as a byte array
3. Customize ```JitParams``` in either ```shotgun.py``` or ```sniper.py```
4. Run ```poetry run python -m examples.sniper``` or ```poetry run python -m examples.shotgun``` while in the ```sdk``` directory

## Shotgun vs Sniper

Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ python = "^3.10"
python-dotenv = "^1.0.0"
solana = "^0.30.1"
anchorpy = "^0.17.1"
driftpy = "0.7.7"
driftpy = "0.7.9"

[build-system]
requires = ["poetry-core"]
Expand Down
12 changes: 10 additions & 2 deletions python/sdk/jit_proxy/jit_proxy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class JitIxParams:
min_position: int
bid: int
ask: int
post_only: Optional[PostOnlyParams]
price_type: Optional[PriceType]
referrer_info: Optional[ReferrerInfo]
sub_account_id: Optional[int]
post_only: PostOnlyParams = PostOnlyParams.MustPostOnly()


@dataclass
Expand Down Expand Up @@ -123,7 +123,7 @@ async def jit(self, params: JitIxParams):
bid=cast(int, params.bid),
ask=cast(int, params.ask),
price_type=self.get_price_type(params.price_type),
post_only=params.post_only,
post_only=self.get_post_only(params.post_only),
)

ix = self.program.instruction["jit"](
Expand Down Expand Up @@ -156,3 +156,11 @@ def get_price_type(self, price_type: PriceType):
return self.program.type["PriceType"].Limit()
else:
raise ValueError(f"Unknown price type: {str(price_type)}")

def get_post_only(self, post_only: PostOnlyParams):
if is_variant(post_only, "MustPostOnly"):
return self.program.type["PostOnlyParam"].MustPostOnly()
elif is_variant(post_only, "TryPostOnly"):
return self.program.type["PostOnlyParam"].TryPostOnly()
else:
return getattr(self.program.type["PostOnlyParam"], 'None')()
39 changes: 22 additions & 17 deletions python/sdk/jit_proxy/jitter/base_jitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,14 @@ async def on_account_update(self, taker: UserAccount, taker_key: Pubkey, slot: i
if order_sig in self.ongoing_auctions:
continue

self.logger.info(f"Market Type: {str(order.market_type)}")
self.logger.info(f"Market Index: {order.market_index}")
self.logger.info(f"Order Price: {convert_to_number(order.price)}")
self.logger.info(f"Order Type: {str(order.order_type)}")
self.logger.info(f"Order Direction: {str(order.direction)}")
self.logger.info(
f"Auction Start Price: {convert_to_number(order.auction_start_price)}"
)
self.logger.info(f"Auction End Price: {convert_to_number(order.auction_end_price)}")
self.logger.info(
f"Order Base Asset Amount: {convert_to_number(order.base_asset_amount)}"
)
self.logger.info(
f"Order Base Asset Amount Filled: {convert_to_number(order.base_asset_amount_filled)}"
)

if is_variant(order.order_type, "Perp"):
if is_variant(order.market_type, "Perp"):
self.logger.info("Perp Auction")
if not order.market_index in self.perp_params:
self.logger.info(f"Jitter not listening to {order.market_index}")
return

self.log_details(order)

perp_market_account = self.drift_client.get_perp_market_account(
order.market_index
)
Expand All @@ -147,6 +133,8 @@ async def on_account_update(self, taker: UserAccount, taker_key: Pubkey, slot: i
self.logger.info(f"Jitter not listening to {order.market_index}")
self.logger.info("----------------------------")
return

self.log_details(order)

spot_market_account = self.drift_client.get_spot_market_account(
order.market_index
Expand All @@ -167,6 +155,23 @@ async def on_account_update(self, taker: UserAccount, taker_key: Pubkey, slot: i
)
self.ongoing_auctions[order_sig] = future

def log_details(self, order: Order):
self.logger.info(f"Market Type: {str(order.market_type)}")
self.logger.info(f"Market Index: {order.market_index}")
self.logger.info(f"Order Price: {convert_to_number(order.price)}")
self.logger.info(f"Order Type: {str(order.order_type)}")
self.logger.info(f"Order Direction: {str(order.direction)}")
self.logger.info(
f"Auction Start Price: {convert_to_number(order.auction_start_price)}"
)
self.logger.info(f"Auction End Price: {convert_to_number(order.auction_end_price)}")
self.logger.info(
f"Order Base Asset Amount: {convert_to_number(order.base_asset_amount)}"
)
self.logger.info(
f"Order Base Asset Amount Filled: {convert_to_number(order.base_asset_amount_filled)}"
)

@abstractmethod
async def create_try_fill(
self,
Expand Down
17 changes: 9 additions & 8 deletions python/sdk/jit_proxy/jitter/jitter_shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from driftpy.drift_client import DriftClient
from driftpy.auction_subscriber.auction_subscriber import AuctionSubscriber
from driftpy.types import is_variant, UserAccount, Order
from driftpy.types import is_variant, UserAccount, Order, PostOnlyParams
from driftpy.accounts.get_accounts import get_user_stats_account

from jit_proxy.jitter.base_jitter import BaseJitter
Expand Down Expand Up @@ -37,7 +37,7 @@ async def create_try_fill(
self.logger.info("JitterShotgun: Creating Try Fill")

async def try_fill():
for _ in range(10):
for i in range(10):
params = (
self.perp_params.get(order.market_index)
if is_variant(order.market_type, "Perp")
Expand All @@ -54,7 +54,7 @@ async def try_fill():

referrer_info = self.get_referrer_info(taker_stats)

self.logger.info(f"Trying to fill {order_sig}")
self.logger.info(f"Trying to fill {order_sig} -> Attempt: {i + 1}")

try:
sig = await self.jit_proxy_client.jit(
Expand All @@ -67,29 +67,30 @@ async def try_fill():
params.min_position,
params.bid,
params.ask,
None,
params.price_type,
referrer_info,
params.sub_account_id,
PostOnlyParams.MustPostOnly(),
)
)

self.logger.info(f"Filled {order_sig}")
self.logger.info(f"Filled Order: {order_sig}")
self.logger.info(f"Signature: {sig}")
await asyncio.sleep(10) # sleep for 10 seconds
del self.ongoing_auctions[order_sig]
return
except Exception as e:
self.logger.error(f"Failed to fill {order_sig}: {e}")
self.logger.error(f"Failed to fill Order: {order_sig}")
if "0x1770" in str(e) or "0x1771" in str(e):
self.logger.error("Order does not cross params yet, retrying")
self.logger.error(f"Order: {order_sig} does not cross params yet, retrying")
elif "0x1793" in str(e):
self.logger.error("Oracle invalid, retrying")
elif "0x1772" in str(e):
self.logger.error("Order already filled")
self.logger.error(f"Order: {order_sig} already filled")
# we don't want to retry if the order is filled
break
else:
self.logger.error(f"Error: {e}")
await asyncio.sleep(10) # sleep for 10 seconds
del self.ongoing_auctions[order_sig]
return
Expand Down
4 changes: 2 additions & 2 deletions python/sdk/jit_proxy/jitter/jitter_sniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from driftpy.auction_subscriber.auction_subscriber import AuctionSubscriber
from driftpy.slot.slot_subscriber import SlotSubscriber
from driftpy.accounts.get_accounts import get_user_stats_account
from driftpy.types import is_variant, OraclePriceData, Order, UserAccount
from driftpy.types import is_variant, OraclePriceData, Order, UserAccount, PostOnlyParams
from driftpy.math.conversion import convert_to_number
from driftpy.math.auction import (
get_auction_price_for_oracle_offset_auction,
Expand Down Expand Up @@ -186,10 +186,10 @@ async def try_fill():
params.min_position,
params.bid,
params.ask,
None,
params.price_type,
referrer_info,
params.sub_account_id,
PostOnlyParams.MustPostOnly(),
}
)

Expand Down

0 comments on commit 373f9e3

Please sign in to comment.