import solana
import pyserum
from pyserum.connection import conn
from pyserum.market import Market
from solana.rpc.api import Client
from solders.pubkey import Pubkey
# Connect to the Solana devnet
http_client = Client("https://api.devnet.solana.com")
connection = conn("https://api.devnet.solana.com")
# Market address we will interact with
market_address = "AMKpY1FVmZTRUDqCGyJvDBEYYqQwoqscu9J65MVXhkcT"
# Fetch the market using the updated Pubkey reference
market = Market.load(connection, Pubkey.from_string(market_address))
# Fetch the order book
order_book = market.load_order_book()
bids = order_book.bids()
asks = order_book.asks()
# Print the best bid and ask
print(f"Best bid: {bids.get_best()}, Best ask: {asks.get_best()}")
This is the code I am running, and here is the error message I am getting. I can't figure this out. I have tried some other things and from another post about this issue on here I made some changes to the "Fetch the market using the updated pubkey reference" and that didn't solve the problem either.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[5], line 3
1 import solana
2 import pyserum
----> 3 from pyserum.connection import conn
4 from pyserum.market import Market
5 from solana.rpc.api import Client
File ~/anaconda3/lib/python3.11/site-packages/pyserum/connection.py:6
3 import requests
5 from solana.rpc.api import Client as conn # pylint: disable=unused-import # noqa:F401
----> 6 from solana.publickey import PublicKey
7 from .market.types import MarketInfo, TokenInfo
9 LIVE_MARKETS_URL = "https://raw.githubusercontent.com/project-serum/serum-ts/master/packages/serum/src/markets.json"
ModuleNotFoundError: No module named 'solana.publickey'
I'm using the following:
PySerum version: 0.5.0a0
Solana version: 0.32.0
Solders version: 0.20.0
Ok, I figured it out—it is indeed a dependency issue. If you go to the requirements file in the pyserum documentation, you'll see that the only compatible solana version is 0.16.0.
So, you're going to have to downgrade solana for this to work—I'd recommend doing this inside a dedicated venv or conda environment if you aren't already using one. You'll need to run:
pip install solana=="0.16.0"
That being said, it appears some of your other syntax is outdated; here's some updated code that I was able to successfully run (note the new connection URL and bid / ask retrieval):
import solana
import pyserum
from pyserum.connection import conn
from pyserum.market import Market
from solana.rpc.api import Client
from solders.pubkey import Pubkey
# Connect to the Solana devnet
http_client = Client("https://api.devnet.solana.com")
connection = conn("https://api.mainnet-beta.solana.com/")
# Market address we will interact with
market_address = "9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT"
# Fetch the market using the updated Pubkey reference
market = Market.load(connection, Pubkey.from_string(market_address))
# Fetch the order book
bids = market.load_bids()
asks = market.load_asks()
# Get the best bid and ask
# For bids, the best bid is the highest price, so we use max.
# For asks, the best ask is the lowest price, so we use min.
best_bid = max(bids, key=lambda order: order.info.price) if bids else None
best_ask = min(asks, key=lambda order: order.info.price) if asks else None
# Print the best bid and ask
if best_bid:
print(f"Best bid:\n Price: {best_bid.info.price}, Size: {best_bid.info.size}")
else:
print("No bids available.")
if best_ask:
print(f"Best ask:\n Price: {best_ask.info.price}, Size: {best_ask.info.size}")
else:
print("No asks available.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With