diff --git a/examples/place_and_take.py b/examples/place_and_take.py index c85969a9..d70ee086 100644 --- a/examples/place_and_take.py +++ b/examples/place_and_take.py @@ -21,38 +21,40 @@ python3 examples/place_and_take.py """ +import asyncio import os import sys -import asyncio -from driftpy.addresses import get_user_stats_account_public_key -from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price +from driftpy.addresses import get_user_stats_account_public_key from driftpy.keypair import load_keypair from driftpy.user_map.user_map import UserMap -from driftpy.user_map.user_map_config import UserMapConfig, WebsocketConfig +from driftpy.user_map.user_map_config import UserMapConfig +from driftpy.user_map.user_map_config import WebsocketConfig +from solders.compute_budget import set_compute_unit_limit +from solders.compute_budget import set_compute_unit_price + sys.path.append("../src/") from anchorpy import Wallet - -from solders.pubkey import Pubkey - -from solana.rpc.async_api import AsyncClient - -from driftpy.types import ( - MakerInfo, - MarketType, - OrderType, - OrderParams, - PositionDirection, - TxParams, - is_variant, -) +from dotenv import load_dotenv from driftpy.account_subscription_config import AccountSubscriptionConfig +from driftpy.constants.numeric_constants import BASE_PRECISION +from driftpy.constants.numeric_constants import PRICE_PRECISION from driftpy.drift_client import DriftClient -from driftpy.constants.numeric_constants import BASE_PRECISION, PRICE_PRECISION - +from driftpy.types import is_variant +from driftpy.types import MakerInfo +from driftpy.types import MarketType +from driftpy.types import OrderParams +from driftpy.types import OrderType +from driftpy.types import PositionDirection +from driftpy.types import TxParams import requests +from solana.rpc.async_api import AsyncClient +from solders.pubkey import Pubkey + + +load_dotenv() def get_l3(market_type: str, market_index: int): diff --git a/examples/start_lp.py b/examples/start_lp.py index 68640b3d..c87cf3e6 100644 --- a/examples/start_lp.py +++ b/examples/start_lp.py @@ -1,21 +1,24 @@ -import sys import json import pprint +import sys + sys.path.append("../src/") from anchorpy import Wallet - -from solana.rpc.async_api import AsyncClient +from dotenv import load_dotenv +from driftpy.account_subscription_config import AccountSubscriptionConfig +from driftpy.accounts import * +from driftpy.constants.config import configs +from driftpy.constants.numeric_constants import AMM_RESERVE_PRECISION +from driftpy.constants.numeric_constants import QUOTE_PRECISION +from driftpy.drift_client import DriftClient +from driftpy.keypair import load_keypair from solana.rpc import commitment +from solana.rpc.async_api import AsyncClient -from solders.keypair import Keypair # type: ignore -from driftpy.constants.config import configs -from driftpy.constants.numeric_constants import AMM_RESERVE_PRECISION, QUOTE_PRECISION -from driftpy.drift_client import DriftClient -from driftpy.accounts import * -from driftpy.account_subscription_config import AccountSubscriptionConfig +load_dotenv() async def view_logs(sig: str, connection: AsyncClient): @@ -37,25 +40,25 @@ async def main( liquidity_amount, operation, ): - with open(keypath, "r") as f: - secret = json.load(f) - kp = Keypair.from_secret_key(bytes(secret)) - print("using public key:", kp.public_key) + kp = load_keypair(keypath) + print("using public key:", kp.pubkey()) print("market:", market_index) - config = configs[env] wallet = Wallet(kp) connection = AsyncClient(url) dc = DriftClient( connection, wallet, - config, + "mainnet", account_subscription=AccountSubscriptionConfig("websocket"), ) + dc.tx_params = TxParams(200_000, 10_000) + + await dc.subscribe() drift_user = dc.get_user() - total_collateral = await drift_user.get_total_collateral() + total_collateral = drift_user.get_total_collateral() print("total collateral:", total_collateral / QUOTE_PRECISION) if total_collateral == 0: @@ -63,6 +66,7 @@ async def main( return market = await get_perp_market_account(dc.program, market_index) + lp_amount = liquidity_amount * AMM_RESERVE_PRECISION lp_amount -= lp_amount % market.amm.order_step_size lp_amount = int(lp_amount) @@ -75,17 +79,17 @@ async def main( sig = None if operation == "add": - resp = input("confirm adding liquidity: Y?") - if resp != "Y": - print("confirmation failed exiting...") + resp = input("confirm adding liquidity? (Y/n)") + if resp == "n": + print("exiting...") return sig = await dc.add_liquidity(lp_amount, market_index) print(sig) elif operation == "remove": - resp = input("confirm removing liquidity: Y?") - if resp != "Y": - print("confirmation failed exiting...") + resp = input("confirm removing liquidity? (Y/n)") + if resp == "n": + print("exiting...") return sig = await dc.remove_liquidity(lp_amount, market_index) print(sig) @@ -94,9 +98,9 @@ async def main( pass elif operation == "settle": - resp = input("confirm settling revenue to if stake: Y?") - if resp != "Y": - print("confirmation failed exiting...") + resp = input("confirm settling revenue to if stake? (Y/n)") + if resp == "n": + print("exiting...") return sig = await dc.settle_lp(dc.authority, market_index) print(sig) @@ -125,7 +129,7 @@ async def main( "--keypath", type=str, required=False, default=os.environ.get("ANCHOR_WALLET") ) parser.add_argument("--env", type=str, default="devnet") - parser.add_argument("--amount", type=float, required=False) + parser.add_argument("--amount", type=float, required=True) parser.add_argument("--market", type=int, required=True) parser.add_argument( "--operation", choices=["remove", "add", "view", "settle"], required=True diff --git a/src/driftpy/accounts/demo/drift_client.py b/src/driftpy/accounts/demo/drift_client.py index 2a228cc0..a12a583b 100644 --- a/src/driftpy/accounts/demo/drift_client.py +++ b/src/driftpy/accounts/demo/drift_client.py @@ -1,4 +1,3 @@ -import asyncio from typing import Optional from anchorpy import Program @@ -8,10 +7,6 @@ from driftpy.accounts.oracle import oracle_ai_to_oracle_price_data from driftpy.accounts.types import DataAndSlot from driftpy.accounts.types import DriftClientAccountSubscriber -from driftpy.constants.perp_markets import devnet_perp_market_configs -from driftpy.constants.perp_markets import mainnet_perp_market_configs -from driftpy.constants.spot_markets import devnet_spot_market_configs -from driftpy.constants.spot_markets import mainnet_spot_market_configs from driftpy.oracles.oracle_id import get_oracle_id from driftpy.types import OraclePriceData from driftpy.types import PerpMarketAccount diff --git a/src/driftpy/accounts/get_accounts.py b/src/driftpy/accounts/get_accounts.py index fb5602a8..35a1e28f 100644 --- a/src/driftpy/accounts/get_accounts.py +++ b/src/driftpy/accounts/get_accounts.py @@ -1,4 +1,3 @@ -import base64 from typing import cast, Optional, Callable from solders.pubkey import Pubkey from anchorpy import Program, ProgramAccount diff --git a/src/driftpy/accounts/polling/drift_client.py b/src/driftpy/accounts/polling/drift_client.py index cab5230d..5098da03 100644 --- a/src/driftpy/accounts/polling/drift_client.py +++ b/src/driftpy/accounts/polling/drift_client.py @@ -222,7 +222,7 @@ async def _set_perp_oracle_map(self): oracle = market_account.amm.oracle oracle_source = market_account.amm.oracle_source oracle_id = get_oracle_id(oracle, oracle_source) - if not oracle_id in self.oracle: + if oracle_id not in self.oracle: await self.add_oracle(oracle, oracle_source) self.perp_oracle_map[market_index] = oracle @@ -234,7 +234,7 @@ async def _set_spot_oracle_map(self): oracle = market_account.oracle oracle_source = market_account.oracle_source oracle_id = get_oracle_id(oracle, oracle_source) - if not oracle_id in self.oracle: + if oracle_id not in self.oracle: await self.add_oracle(oracle, oracle_source) self.spot_oracle_map[market_index] = oracle diff --git a/src/driftpy/accounts/ws/account_subscriber.py b/src/driftpy/accounts/ws/account_subscriber.py index 4238da44..1f04cb12 100644 --- a/src/driftpy/accounts/ws/account_subscriber.py +++ b/src/driftpy/accounts/ws/account_subscriber.py @@ -18,7 +18,7 @@ from typing import cast, Generic, TypeVar, Callable -from driftpy.types import PerpMarketAccount, get_ws_url +from driftpy.types import get_ws_url T = TypeVar("T") @@ -80,7 +80,7 @@ async def subscribe_ws(self): decoded_data = self.decode(account_bytes) self.update_data(DataAndSlot(slot, decoded_data)) except Exception: - print(f"Error processing account data") + print("Error processing account data") break await ws.account_unsubscribe(subscription_id) except websockets.exceptions.ConnectionClosed: diff --git a/src/driftpy/accounts/ws/program_account_subscriber.py b/src/driftpy/accounts/ws/program_account_subscriber.py index 2bbc92e7..769fbf8b 100644 --- a/src/driftpy/accounts/ws/program_account_subscriber.py +++ b/src/driftpy/accounts/ws/program_account_subscriber.py @@ -1,5 +1,4 @@ import asyncio -import traceback from typing import Dict, Optional, TypeVar, Callable from anchorpy import Program from driftpy.accounts.types import ( diff --git a/src/driftpy/admin.py b/src/driftpy/admin.py index 826d15e4..ef0fc9e1 100644 --- a/src/driftpy/admin.py +++ b/src/driftpy/admin.py @@ -1,11 +1,10 @@ from typing import Optional from solders.pubkey import Pubkey from solders.signature import Signature -from solders.keypair import Keypair from solders.system_program import ID from solders.sysvar import RENT from spl.token.constants import TOKEN_PROGRAM_ID -from anchorpy import Program, Provider, Context +from anchorpy import Context from driftpy.drift_client import ( DriftClient, @@ -14,11 +13,6 @@ from driftpy.types import OracleGuardRails, OracleSource, PrelaunchOracleParams from driftpy.addresses import * from driftpy.accounts import get_state_account -from driftpy.constants.config import Config -from anchorpy import Provider, Idl -import driftpy -from pathlib import Path -import json from driftpy.constants.numeric_constants import ( SPOT_RATE_PRECISION, SPOT_WEIGHT_PRECISION, diff --git a/src/driftpy/constants/config.py b/src/driftpy/constants/config.py index 19b89e39..fa0cbde4 100644 --- a/src/driftpy/constants/config.py +++ b/src/driftpy/constants/config.py @@ -3,7 +3,7 @@ from typing import Literal, Optional, Sequence, Tuple, Union import jsonrpcclient -from driftpy.accounts.oracle import decode_oracle, decode_pyth_price_info +from driftpy.accounts.oracle import decode_oracle from driftpy.accounts.types import DataAndSlot, FullOracleWrapper from driftpy.constants.spot_markets import ( @@ -23,8 +23,6 @@ from driftpy.types import ( OracleInfo, - OraclePriceData, - OracleSource, PerpMarketAccount, SpotMarketAccount, ) diff --git a/src/driftpy/dlob/dlob.py b/src/driftpy/dlob/dlob.py index 7bec24c6..67fe96ca 100644 --- a/src/driftpy/dlob/dlob.py +++ b/src/driftpy/dlob/dlob.py @@ -196,7 +196,7 @@ def insert_order( market_type = market_type_to_string(order.market_type) - if not order.market_index in self.order_lists.get(market_type): + if order.market_index not in self.order_lists.get(market_type): self.add_order_list(market_type, order.market_index) if is_variant(order.status, "Open"): diff --git a/src/driftpy/dlob/node_list.py b/src/driftpy/dlob/node_list.py index 59e60e97..83393afa 100644 --- a/src/driftpy/dlob/node_list.py +++ b/src/driftpy/dlob/node_list.py @@ -9,7 +9,6 @@ ) from driftpy.types import Order, is_variant -import inspect T = TypeVar("T", bound=DLOBNode) diff --git a/src/driftpy/dlob/orderbook_levels.py b/src/driftpy/dlob/orderbook_levels.py index e1e9521e..4df15436 100644 --- a/src/driftpy/dlob/orderbook_levels.py +++ b/src/driftpy/dlob/orderbook_levels.py @@ -15,7 +15,6 @@ ) from driftpy.math.orders import standardize_price from driftpy.types import ( - AMM, AssetType, OraclePriceData, PerpMarketAccount, diff --git a/src/driftpy/drift_client.py b/src/driftpy/drift_client.py index 32d61bc2..9d069576 100644 --- a/src/driftpy/drift_client.py +++ b/src/driftpy/drift_client.py @@ -20,7 +20,6 @@ from driftpy.constants import BASE_PRECISION from driftpy.constants import PRICE_PRECISION from driftpy.constants.config import configs -from driftpy.constants.config import decode_account from driftpy.constants.config import DEVNET_SEQUENCER_PROGRAM_ID from driftpy.constants.config import DRIFT_PROGRAM_ID from driftpy.constants.config import DriftEnv @@ -3413,7 +3412,7 @@ async def load_sequence_info(self): sequence_account_raw = await self.sequence_enforcer_program.account[ "SequenceAccount" ].fetch(address) - except anchorpy.error.AccountDoesNotExistError as e: + except anchorpy.error.AccountDoesNotExistError: self.sequence_address_by_subaccount[subaccount] = address self.sequence_number_by_subaccount[subaccount] = 1 self.sequence_bump_by_subaccount[subaccount] = bump diff --git a/src/driftpy/drift_user.py b/src/driftpy/drift_user.py index 72e8498b..7435c38b 100644 --- a/src/driftpy/drift_user.py +++ b/src/driftpy/drift_user.py @@ -6,7 +6,6 @@ from driftpy.account_subscription_config import AccountSubscriptionConfig from driftpy.accounts.oracle import * from driftpy.math.amm import calculate_market_open_bid_ask -from driftpy.math.conversion import convert_to_number from driftpy.math.fuel import calculate_insurance_fuel_bonus from driftpy.math.fuel import calculate_perp_fuel_bonus from driftpy.math.fuel import calculate_spot_fuel_bonus @@ -15,7 +14,6 @@ from driftpy.math.perp_position import * from driftpy.math.spot_balance import get_strict_token_value from driftpy.math.spot_market import * -from driftpy.math.spot_position import calculate_weighted_token_value from driftpy.math.spot_position import get_worst_case_token_amounts from driftpy.math.spot_position import is_spot_position_available from driftpy.oracles.strict_oracle_price import StrictOraclePrice diff --git a/src/driftpy/drift_user_stats.py b/src/driftpy/drift_user_stats.py index 268e63a0..f199f88d 100644 --- a/src/driftpy/drift_user_stats.py +++ b/src/driftpy/drift_user_stats.py @@ -27,7 +27,6 @@ def __init__( user_stats_account_pubkey: Pubkey, config: UserStatsSubscriptionConfig, ): - from driftpy.drift_client import DriftClient self.drift_client = drift_client self.user_stats_account_pubkey = user_stats_account_pubkey diff --git a/src/driftpy/events/types.py b/src/driftpy/events/types.py index a56a345f..7d25b54a 100644 --- a/src/driftpy/events/types.py +++ b/src/driftpy/events/types.py @@ -1,7 +1,7 @@ from dataclasses import dataclass, field from abc import abstractmethod from solana.transaction import Signature -from solana.rpc.commitment import Commitment, Confirmed +from solana.rpc.commitment import Commitment from solana.rpc.async_api import AsyncClient from typing import Callable, Literal, Union diff --git a/src/driftpy/events/websocket_log_provider.py b/src/driftpy/events/websocket_log_provider.py index 290b83cb..28066631 100644 --- a/src/driftpy/events/websocket_log_provider.py +++ b/src/driftpy/events/websocket_log_provider.py @@ -52,7 +52,7 @@ async def subscribe_ws(self, callback: LogProviderCallback): callback(signature, slot, logs) except Exception as e: - print(f"Error processing event data", e) + print("Error processing event data", e) break await ws.account_unsubscribe(subscription_id) except websockets.exceptions.ConnectionClosed: diff --git a/src/driftpy/market_map/market_map.py b/src/driftpy/market_map/market_map.py index c78a7043..a4cc3eeb 100644 --- a/src/driftpy/market_map/market_map.py +++ b/src/driftpy/market_map/market_map.py @@ -2,7 +2,6 @@ import base64 import os import pickle -import traceback from typing import Dict, Optional, Union import jsonrpcclient diff --git a/src/driftpy/math/amm.py b/src/driftpy/math/amm.py index fb56b7e9..3b85e09c 100644 --- a/src/driftpy/math/amm.py +++ b/src/driftpy/math/amm.py @@ -2,36 +2,34 @@ import math import time from typing import Optional, Tuple + from driftpy.constants.numeric_constants import ( - AMM_RESERVE_PRECISION, - AMM_TO_QUOTE_PRECISION_RATIO, - BID_ASK_SPREAD_PRECISION, - DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT, - FUNDING_RATE_BUFFER, - FUNDING_RATE_PRECISION, - PERCENTAGE_PRECISION, - PRICE_DIV_PEG, - PEG_PRECISION, AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO, - PRICE_PRECISION, - QUOTE_PRECISION, -) -from driftpy.math.oracles import calculate_live_oracle_std -from driftpy.math.repeg import ( - calculate_adjust_k_cost, - calculate_budgeted_peg, - calculate_optimal_peg_and_budget, - calculate_repeg_cost, ) -from driftpy.math.utils import clamp_num, sig_num -from driftpy.types import ( - OraclePriceData, - PositionDirection, - SwapDirection, - AMM, - AssetType, - is_variant, +from driftpy.constants.numeric_constants import ( + DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT, ) +from driftpy.constants.numeric_constants import AMM_TO_QUOTE_PRECISION_RATIO +from driftpy.constants.numeric_constants import BID_ASK_SPREAD_PRECISION +from driftpy.constants.numeric_constants import FUNDING_RATE_BUFFER +from driftpy.constants.numeric_constants import PEG_PRECISION +from driftpy.constants.numeric_constants import PERCENTAGE_PRECISION +from driftpy.constants.numeric_constants import PRICE_DIV_PEG +from driftpy.constants.numeric_constants import PRICE_PRECISION +from driftpy.constants.numeric_constants import QUOTE_PRECISION +from driftpy.math.oracles import calculate_live_oracle_std +from driftpy.math.repeg import calculate_adjust_k_cost +from driftpy.math.repeg import calculate_budgeted_peg +from driftpy.math.repeg import calculate_optimal_peg_and_budget +from driftpy.math.repeg import calculate_repeg_cost +from driftpy.math.utils import clamp_num +from driftpy.math.utils import sig_num +from driftpy.types import AMM +from driftpy.types import AssetType +from driftpy.types import is_variant +from driftpy.types import OraclePriceData +from driftpy.types import PositionDirection +from driftpy.types import SwapDirection def calculate_vol_spread_bn( diff --git a/src/driftpy/math/exchange_status.py b/src/driftpy/math/exchange_status.py index b1c1ca2b..c091ac6b 100644 --- a/src/driftpy/math/exchange_status.py +++ b/src/driftpy/math/exchange_status.py @@ -1,6 +1,5 @@ from typing import Union from driftpy.types import ( - ExchangeStatus, PerpMarketAccount, SpotMarketAccount, StateAccount, diff --git a/src/driftpy/math/funding.py b/src/driftpy/math/funding.py index 1e5a444c..021b3d66 100644 --- a/src/driftpy/math/funding.py +++ b/src/driftpy/math/funding.py @@ -7,7 +7,6 @@ from driftpy.types import ( OraclePriceData, PerpMarketAccount, - is_one_of_variant, is_variant, ) diff --git a/src/driftpy/math/oracles.py b/src/driftpy/math/oracles.py index 9573e80f..40dabb18 100644 --- a/src/driftpy/math/oracles.py +++ b/src/driftpy/math/oracles.py @@ -1,7 +1,6 @@ from driftpy.constants.numeric_constants import BID_ASK_SPREAD_PRECISION, FIVE_MINUTE from driftpy.types import ( AMM, - ContractTier, HistoricalOracleData, OracleGuardRails, OraclePriceData, diff --git a/src/driftpy/pickle/vat.py b/src/driftpy/pickle/vat.py index 02e881a8..fce12755 100644 --- a/src/driftpy/pickle/vat.py +++ b/src/driftpy/pickle/vat.py @@ -1,7 +1,6 @@ import asyncio import pickle import os -from dataclasses import dataclass from typing import Optional from driftpy.drift_client import DriftClient from driftpy.market_map.market_map import MarketMap diff --git a/src/driftpy/priority_fees/priority_fee_subscriber.py b/src/driftpy/priority_fees/priority_fee_subscriber.py index 45c4491b..7d703203 100644 --- a/src/driftpy/priority_fees/priority_fee_subscriber.py +++ b/src/driftpy/priority_fees/priority_fee_subscriber.py @@ -4,7 +4,6 @@ from dataclasses import dataclass from solana.rpc.async_api import AsyncClient -from solders.pubkey import Pubkey @dataclass diff --git a/src/driftpy/setup/helpers.py b/src/driftpy/setup/helpers.py index fbae0389..3d667802 100644 --- a/src/driftpy/setup/helpers.py +++ b/src/driftpy/setup/helpers.py @@ -14,9 +14,7 @@ from spl.token.instructions import initialize_mint, InitializeMintParams import math -from spl.token.async_client import AsyncToken from spl.token._layouts import ACCOUNT_LAYOUT -from spl.token.constants import TOKEN_PROGRAM_ID from spl.token.instructions import ( initialize_account, InitializeAccountParams, diff --git a/src/driftpy/user_map/polling_sub.py b/src/driftpy/user_map/polling_sub.py index a969824f..7a968266 100644 --- a/src/driftpy/user_map/polling_sub.py +++ b/src/driftpy/user_map/polling_sub.py @@ -1,7 +1,5 @@ import asyncio -from driftpy.account_subscription_config import AccountSubscriptionConfig -from driftpy.accounts.bulk_account_loader import BulkAccountLoader -from driftpy.user_map.types import ConfigType, Subscription +from driftpy.user_map.types import Subscription class PollingSubscription(Subscription): diff --git a/src/driftpy/user_map/user_map.py b/src/driftpy/user_map/user_map.py index ae96c367..7f9f17de 100644 --- a/src/driftpy/user_map/user_map.py +++ b/src/driftpy/user_map/user_map.py @@ -21,9 +21,8 @@ from driftpy.user_map.websocket_sub import WebsocketSubscription from driftpy.user_map.polling_sub import PollingSubscription -from driftpy.memcmp import get_user_filter, get_non_idle_user_filter -from driftpy.user_map.types import UserMapInterface, ConfigType +from driftpy.user_map.types import UserMapInterface from driftpy.accounts.types import DataAndSlot from driftpy.decode.user import decode_user