Skip to content

Commit

Permalink
add market lookup table
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 26, 2023
1 parent e58aeff commit 4ac43e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/driftpy/address_lookup_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from solana.rpc.async_api import AsyncClient
from solders.pubkey import Pubkey
from solders.address_lookup_table_account import AddressLookupTableAccount

LOOKUP_TABLE_META_SIZE = 56


async def get_address_lookup_table(
connection: AsyncClient, pubkey: Pubkey
) -> AddressLookupTableAccount:
account_info = await connection.get_account_info(pubkey)
return decode_address_lookup_table(pubkey, account_info.value.data)


def decode_address_lookup_table(pubkey: Pubkey, data: bytes):
data_len = len(data)

addresses = []
i = LOOKUP_TABLE_META_SIZE
while i < data_len:
addresses.append(Pubkey.from_bytes(data[i : i + 32]))
i += 32

return AddressLookupTableAccount(pubkey, addresses)
7 changes: 7 additions & 0 deletions src/driftpy/constants/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config:
default_ws: str
markets: list[Market]
banks: list[Bank]
market_lookup_table: Pubkey


configs = {
Expand All @@ -34,6 +35,9 @@ class Config:
default_ws="wss://api.devnet.solana.com",
markets=devnet_markets,
banks=devnet_banks,
market_lookup_table=Pubkey.from_string(
"FaMS3U4uBojvGn5FSDEPimddcXsCfwkKsFgMVVnDdxGb"
),
),
"mainnet": Config(
env="mainnet",
Expand All @@ -47,5 +51,8 @@ class Config:
default_ws="wss://api.mainnet-beta.solana.com",
markets=mainnet_markets,
banks=mainnet_banks,
market_lookup_table=Pubkey.from_string(
"GPZkp76cJtNL2mphCvT6FXkJCVPpouidnacckR6rzKDN"
),
),
}
30 changes: 27 additions & 3 deletions src/driftpy/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from solders.instruction import Instruction
from solders.system_program import ID
from solders.sysvar import RENT
from solders.address_lookup_table_account import AddressLookupTableAccount
from solana.rpc.async_api import AsyncClient
from solana.transaction import AccountMeta
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
Expand All @@ -17,14 +18,15 @@

import driftpy
from driftpy.account_subscription_config import AccountSubscriptionConfig
from driftpy.address_lookup_table import get_address_lookup_table
from driftpy.constants.numeric_constants import (
QUOTE_SPOT_MARKET_INDEX,
)
from driftpy.drift_user import DriftUser
from driftpy.sdk_types import *
from driftpy.accounts import *

from driftpy.constants.config import Config, DriftEnv, DRIFT_PROGRAM_ID
from driftpy.constants.config import Config, DriftEnv, DRIFT_PROGRAM_ID, configs

from typing import Union, Optional, List, Sequence
from driftpy.math.positions import is_available, is_spot_position_available
Expand All @@ -51,13 +53,16 @@ def __init__(
tx_version: Optional[TransactionVersion] = None,
active_sub_account_id: Optional[int] = None,
sub_account_ids: Optional[list[int]] = None,
market_lookup_table: Optional[Pubkey] = None,
):
"""Initializes the drift client object -- likely want to use the .from_config method instead of this one
Args:
program (Program): Drift anchor program (see from_config on how to initialize it)
authority (Keypair, optional): Authority of all txs - if None will default to the Anchor Provider.Wallet Keypair.
"""
self.connection = connection

file = Path(str(driftpy.__path__[0]) + "/idl/drift.json")
with file.open() as f:
raw = file.read_text()
Expand Down Expand Up @@ -97,6 +102,13 @@ def __init__(
)
self.account_subscription_config = account_subscription

self.market_lookup_table = (
market_lookup_table
if market_lookup_table is not None
else configs[env].market_lookup_table
)
self.market_lookup_table_account: Optional[AddressLookupTableAccount] = None

if tx_params is None:
tx_params = TxParams(600_000, 0)

Expand Down Expand Up @@ -170,10 +182,20 @@ async def get_oracle_price_data(self, oracle: Pubkey) -> Optional[OraclePriceDat
)
return getattr(oracle_price_data_and_slot, "data", None)

async def fetch_market_lookup_table(self) -> AddressLookupTableAccount:
if self.market_lookup_table_account is not None:
return self.market_lookup_table_account

self.market_lookup_table_account = await get_address_lookup_table(
self.connection, self.market_lookup_table
)
return self.market_lookup_table_account

async def send_ixs(
self,
ixs: Union[Instruction, list[Instruction]],
signers=None,
lookup_tables: list[AddressLookupTableAccount] = None,
):
if isinstance(ixs, Instruction):
ixs = [ixs]
Expand All @@ -200,8 +222,10 @@ async def send_ixs(
if signers is not None:
[tx.sign_partial(signer) for signer in signers]
elif self.tx_version == 0:
if lookup_tables is None:
lookup_tables = [await self.fetch_market_lookup_table()]
msg = MessageV0.try_compile(
self.wallet.public_key, ixs, [], latest_blockhash
self.wallet.public_key, ixs, lookup_tables, latest_blockhash
)
tx = VersionedTransaction(msg, [self.wallet.payer])
else:
Expand All @@ -225,7 +249,7 @@ async def initialize_user(self, sub_account_id: int = 0, name: str = None):
name = DEFAULT_USER_NAME

if name is None:
name = 'Subaccount ' + str(sub_account_id+1)
name = "Subaccount " + str(sub_account_id + 1)

ix = self.get_initialize_user_instructions(sub_account_id, name)
ixs.append(ix)
Expand Down

0 comments on commit 4ac43e4

Please sign in to comment.