Skip to content

Commit

Permalink
Merge pull request #213 from drift-labs/sina/add-rpc-logging
Browse files Browse the repository at this point in the history
Add some logs for RPC
  • Loading branch information
SinaKhalili authored Dec 17, 2024
2 parents 79dd2d7 + 9a4fb75 commit 231d9b7
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 69 deletions.
9 changes: 7 additions & 2 deletions src/driftpy/accounts/bulk_account_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ async def load_chunk(self, chunk: List[List[AccountToLoad]]):
]
rpc_request = jsonrpcclient.request(
"getMultipleAccounts",
params=[
params=(
pubkeys_to_send,
{"encoding": "base64", "commitment": self.commitment},
],
),
)
rpc_requests.append(rpc_request)

Expand All @@ -146,6 +146,11 @@ async def load_chunk(self, chunk: List[List[AccountToLoad]]):

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching accounts: {parsed_resp.message}")
if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching accounts - not ok: {parsed_resp}")

for rpc_result, chunk_accounts in zip(parsed_resp, chunk):
if isinstance(rpc_result, jsonrpcclient.Error):
print(f"Failed to get info about accounts: {rpc_result.message}")
Expand Down
15 changes: 14 additions & 1 deletion src/driftpy/accounts/ws/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from driftpy.accounts.ws.account_subscriber import WebsocketAccountSubscriber
from driftpy.addresses import *
from driftpy.constants.config import find_all_market_and_oracles
from driftpy.constants.perp_markets import mainnet_perp_market_configs
from driftpy.market_map.market_map import MarketMap
from driftpy.market_map.market_map_config import MarketMapConfig, WebsocketConfig
from driftpy.oracles.oracle_id import get_oracle_id
Expand Down Expand Up @@ -331,7 +332,19 @@ def get_oracle_price_data_and_slot_for_perp_market(
oracle = self.perp_market_oracle_map.get(market_index)
oracle_id = self.perp_market_oracle_strings_map.get(market_index)

if not perp_market_account or not oracle:
if not perp_market_account:
if any(
market_index == market.market_index
for market in mainnet_perp_market_configs
):
raise ValueError(
f"No perp market account found for market index {market_index} but market should exist. This may be an issue with your RPC?"
)

if not oracle:
print(f"No oracle found for market index {market_index}")

if not perp_market_account or not oracle or not oracle_id:
return None

if perp_market_account.data.amm.oracle != oracle:
Expand Down
41 changes: 25 additions & 16 deletions src/driftpy/constants/config.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import asyncio
from base64 import b64decode
from dataclasses import dataclass
from typing import Literal, Optional, Sequence, Tuple, Union

import jsonrpcclient
from anchorpy import Program
from solders.pubkey import Pubkey

from driftpy.accounts.oracle import decode_oracle
from driftpy.accounts.types import DataAndSlot, FullOracleWrapper

from driftpy.constants.spot_markets import (
devnet_spot_market_configs,
mainnet_spot_market_configs,
SpotMarketConfig,
)
from driftpy.constants.perp_markets import (
PerpMarketConfig,
devnet_perp_market_configs,
mainnet_perp_market_configs,
PerpMarketConfig,
)
from dataclasses import dataclass
from solders.pubkey import Pubkey

from anchorpy import Program

from driftpy.constants.spot_markets import (
SpotMarketConfig,
devnet_spot_market_configs,
mainnet_spot_market_configs,
)
from driftpy.types import (
OracleInfo,
PerpMarketAccount,
Expand Down Expand Up @@ -123,10 +121,10 @@ async def find_all_market_and_oracles(

perp_request = jsonrpcclient.request(
"getProgramAccounts",
[
(
str(program.program_id),
{"filters": perp_filters, "encoding": "base64", "withContext": True},
],
),
)

post = program.provider.connection._provider.session.post(
Expand All @@ -139,6 +137,12 @@ async def find_all_market_and_oracles(

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching perp markets: {parsed_resp.message}")

if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching perp markets - not ok: {parsed_resp}")

perp_slot = int(parsed_resp.result["context"]["slot"])

perp_markets: list[PerpMarketAccount] = [
Expand All @@ -152,10 +156,10 @@ async def find_all_market_and_oracles(

spot_request = jsonrpcclient.request(
"getProgramAccounts",
[
(
str(program.program_id),
{"filters": spot_filters, "encoding": "base64", "withContext": True},
],
),
)

post = program.provider.connection._provider.session.post(
Expand All @@ -168,6 +172,11 @@ async def find_all_market_and_oracles(

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching spot markets: {parsed_resp.message}")
if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching spot markets - not ok: {parsed_resp}")

spot_slot = int(parsed_resp.result["context"]["slot"])

spot_markets: list[SpotMarketAccount] = [
Expand Down
15 changes: 10 additions & 5 deletions src/driftpy/events/fetch_logs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio

import jsonrpcclient
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Commitment
from solana.transaction import Signature
from solders.pubkey import Pubkey
from solders.rpc.responses import (
RpcConfirmedTransactionStatusWithSignature,
)
import jsonrpcclient

from solana.transaction import Signature


async def fetch_logs(
Expand Down Expand Up @@ -63,10 +63,10 @@ async def fetch_transactions(
for signature in signatures:
rpc_request = jsonrpcclient.request(
"getTransaction",
[
(
str(signature.signature),
{"commitment": commitment, "maxSupportedTransactionVersion": 0},
],
),
)
rpc_requests.append(rpc_request)

Expand All @@ -83,6 +83,11 @@ async def fetch_transactions(

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching transactions: {parsed_resp.message}")
if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching transactions - not ok: {parsed_resp}")

response = []
for rpc_result in parsed_resp:
if rpc_result.result:
Expand Down
16 changes: 10 additions & 6 deletions src/driftpy/market_map/market_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
import base64
import os
import pickle

from typing import Dict, Optional, Union
import jsonrpcclient

import jsonrpcclient
from solana.rpc.commitment import Confirmed
from driftpy.accounts.types import DataAndSlot

from driftpy.accounts.types import DataAndSlot
from driftpy.market_map.market_map_config import MarketMapConfig
from driftpy.market_map.websocket_sub import WebsocketSubscription
from driftpy.types import (
PerpMarketAccount,
PickledData,
SpotMarketAccount,
is_variant,
compress,
decompress,
is_variant,
market_type_to_string,
)

Expand Down Expand Up @@ -112,10 +111,10 @@ async def pre_dump(self) -> dict[str, bytes]:

rpc_request = jsonrpcclient.request(
"getProgramAccounts",
[
(
str(self.program.program_id),
{"filters": filters, "encoding": "base64", "withContext": True},
],
),
)

post = self.connection._provider.session.post(
Expand All @@ -128,6 +127,11 @@ async def pre_dump(self) -> dict[str, bytes]:

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching market map: {parsed_resp.message}")
if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching market map - not ok: {parsed_resp}")

slot = int(parsed_resp.result["context"]["slot"])

self.latest_slot = slot
Expand Down
9 changes: 7 additions & 2 deletions src/driftpy/priority_fees/priority_fee_subscriber.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio
import jsonrpcclient

from dataclasses import dataclass

import jsonrpcclient
from solana.rpc.async_api import AsyncClient


Expand Down Expand Up @@ -55,6 +54,12 @@ async def load(self):

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching priority fees: {parsed_resp.message}")

if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching priority fees - not ok: {parsed_resp}")

result = parsed_resp.result

desc_results = sorted(result, key=lambda x: x["slot"], reverse=True)[
Expand Down
35 changes: 17 additions & 18 deletions src/driftpy/user_map/user_map.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import asyncio
import base64
import os
import jsonrpcclient
import pickle
import base64

from typing import Any, Container, Optional, Dict
from typing import Any, Container, Dict, Optional

import jsonrpcclient
from solana.rpc.commitment import Confirmed
from solders.pubkey import Pubkey

from solana.rpc.commitment import Confirmed
from driftpy.account_subscription_config import AccountSubscriptionConfig
from driftpy.accounts.types import DataAndSlot
from driftpy.decode.user import decode_user
from driftpy.dlob.client_types import DLOBSource

from driftpy.drift_client import DriftClient
from driftpy.drift_user import DriftUser
from driftpy.account_subscription_config import AccountSubscriptionConfig

from driftpy.types import OrderRecord, PickledData, UserAccount, compress, decompress

from driftpy.user_map.user_map_config import UserMapConfig, PollingConfig
from driftpy.user_map.websocket_sub import WebsocketSubscription
from driftpy.user_map.polling_sub import PollingSubscription


from driftpy.user_map.types import UserMapInterface
from driftpy.accounts.types import DataAndSlot

from driftpy.decode.user import decode_user
from driftpy.user_map.user_map_config import PollingConfig, UserMapConfig
from driftpy.user_map.websocket_sub import WebsocketSubscription


class UserMap(UserMapInterface, DLOBSource):
Expand Down Expand Up @@ -144,10 +137,10 @@ async def sync(self) -> None:

rpc_request = jsonrpcclient.request(
"getProgramAccounts",
[
(
str(self.drift_client.program_id),
{"filters": filters, "encoding": "base64", "withContext": True},
],
),
)

post = self.drift_client.connection._provider.session.post(
Expand All @@ -160,6 +153,12 @@ async def sync(self) -> None:

parsed_resp = jsonrpcclient.parse(resp.json())

if isinstance(parsed_resp, jsonrpcclient.Error):
raise ValueError(f"Error fetching user map: {parsed_resp.message}")

if not isinstance(parsed_resp, jsonrpcclient.Ok):
raise ValueError(f"Error fetching user map - not ok: {parsed_resp}")

slot = int(parsed_resp.result["context"]["slot"])

self.latest_slot = slot
Expand Down
Loading

0 comments on commit 231d9b7

Please sign in to comment.