Skip to content

Instantly share code, notes, and snippets.

@wphan
Last active February 23, 2024 10:21
Show Gist options
  • Save wphan/8b7b4bea142abb3126bb3c2f33a257e6 to your computer and use it in GitHub Desktop.
Save wphan/8b7b4bea142abb3126bb3c2f33a257e6 to your computer and use it in GitHub Desktop.
driftpy EventSubscriber example
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
from anchorpy import Wallet
from driftpy.drift_client import DriftClient
from driftpy.account_subscription_config import AccountSubscriptionConfig
from driftpy.events.event_subscriber import EventSubscriber
from driftpy.events.types import WrappedEvent, EventSubscriptionOptions, WebsocketLogProviderConfig
import os
import asyncio
async def main():
# url = os.getenv("RPC_URL")
url = "your RPC"
wallet = Wallet(Keypair())
connection = AsyncClient(url)
drift_client = DriftClient(
connection,
wallet,
"mainnet",
account_subscription=AccountSubscriptionConfig("websocket"),
tx_params=TxParams(1_400_000, 20_000), # crank priority fees way up
)
await drift_client.subscribe()
options = EventSubscriptionOptions(
event_types = (
'DepositRecord',
'FundingPaymentRecord',
'LiquidationRecord',
'OrderRecord',
'OrderActionRecord',
'FundingRateRecord',
'NewUserRecord',
'SettlePnlRecord',
'LPRecord',
'InsuranceFundRecord',
'SpotInterestRecord',
'InsuranceFundStakeRecord',
'CurveRecord',
),
max_tx= 4096,
max_events_per_type=4096,
order_by="blockchain",
order_dir="asc",
commitment="processed",
log_provider_config=WebsocketLogProviderConfig()
)
event_subscriber = EventSubscriber(connection, drift_client.program, options)
event_subscriber.subscribe()
event_subscriber.event_emitter.new_event += lambda event: print(event)
while True:
await asyncio.sleep(1)
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment