Skip to content

Commit

Permalink
Ruff: Auto-fix fixable lint errors
Browse files Browse the repository at this point in the history
This is the output of running
ruff src/ --fix
  • Loading branch information
SinaKhalili committed Dec 11, 2024
1 parent 69e8530 commit fb211d1
Show file tree
Hide file tree
Showing 27 changed files with 88 additions and 116 deletions.
42 changes: 22 additions & 20 deletions examples/place_and_take.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
56 changes: 30 additions & 26 deletions examples/start_lp.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -37,32 +40,33 @@ 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:
print("cannot lp with 0 collateral")
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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions src/driftpy/accounts/demo/drift_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
from typing import Optional

from anchorpy import Program
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/driftpy/accounts/get_accounts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
from typing import cast, Optional, Callable
from solders.pubkey import Pubkey
from anchorpy import Program, ProgramAccount
Expand Down
4 changes: 2 additions & 2 deletions src/driftpy/accounts/polling/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/driftpy/accounts/ws/account_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion src/driftpy/accounts/ws/program_account_subscriber.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import traceback
from typing import Dict, Optional, TypeVar, Callable
from anchorpy import Program
from driftpy.accounts.types import (
Expand Down
8 changes: 1 addition & 7 deletions src/driftpy/admin.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions src/driftpy/constants/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -23,8 +23,6 @@

from driftpy.types import (
OracleInfo,
OraclePriceData,
OracleSource,
PerpMarketAccount,
SpotMarketAccount,
)
Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/dlob/dlob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
1 change: 0 additions & 1 deletion src/driftpy/dlob/node_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
)

from driftpy.types import Order, is_variant
import inspect

T = TypeVar("T", bound=DLOBNode)

Expand Down
1 change: 0 additions & 1 deletion src/driftpy/dlob/orderbook_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
)
from driftpy.math.orders import standardize_price
from driftpy.types import (
AMM,
AssetType,
OraclePriceData,
PerpMarketAccount,
Expand Down
3 changes: 1 addition & 2 deletions src/driftpy/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/driftpy/drift_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/driftpy/drift_user_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/events/types.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/events/websocket_log_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion src/driftpy/market_map/market_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import base64
import os
import pickle
import traceback

from typing import Dict, Optional, Union
import jsonrpcclient
Expand Down
Loading

0 comments on commit fb211d1

Please sign in to comment.