找不到模块 "solana.publickey

2 投票
1 回答
199 浏览
提问于 2025-04-14 17:00
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()}")

这是我正在运行的代码,还有我收到的错误信息。我搞不明白这个问题。我尝试了一些其他的方法,并且根据这里关于这个问题的另一篇帖子,我对“使用更新的公钥引用获取市场”做了一些修改,但问题还是没有解决。

---------------------------------------------------------------------------
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'

我使用的是以下内容:

PySerum version: 0.5.0a0    
Solana version: 0.32.0
Solders version: 0.20.0

1 个回答

1

好的,我搞明白了——这确实是个依赖问题。如果你查看一下pyserum文档里的需求文件,你会发现唯一兼容的solana版本是0.16.0

所以,你需要把solana降级才能让它正常工作——如果你还没有使用的话,我建议在一个专门的venvconda环境中进行降级。你需要运行:

pip install solana=="0.16.0"

话虽如此,看来你其他的一些语法也有点过时了;这里有一些我成功运行过的更新代码(注意新的连接网址和买卖请求的获取):

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.")

撰写回答