You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

suix_subscribeEvent调用失败求助:Python脚本报错排查与替代方案

Sui事件订阅报错排查与解决方案

问题概述

调用suix_subscribeEvent时触发反序列化错误,返回报错信息:

New event received: {'jsonrpc': '2.0', 'error': {'code': -32602, 'message': 'byte deserialization failed, cause by: AccountAddressParseError'}, 'id': 1}
需要监听Sui链上Buy和Sell操作的触发事件,尝试多种代码均无效,且怀疑suix_subscribeEvent已废弃但找不到替代方案。

错误原因

  1. 地址格式错误:报错AccountAddressParseError是因为传入的包地址带了0x前缀,Sui RPC要求十六进制地址必须去掉该前缀才能正常解析。
  2. 方法已废弃:suix_subscribeEvent确实已被Sui官方废弃,当前标准事件订阅接口为suix_subscribeEventStream。

修复后的代码

import asyncio
import websockets
import json

async def subscribe_events():
    uri = "wss://rpc-mainnet.suiscan.xyz/websocket"
    async with websockets.connect(uri) as websocket:
        # 使用官方推荐的suix_subscribeEventStream方法
        # 包地址去掉0x前缀
        base_subscribe_msg = {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "suix_subscribeEventStream",
            "params": [
                {
                    "filter": {
                        "MoveEvent": {
                            "package": "fa9302fe83a1b717293ca0d4f80a9195e66ac3d401977df5b16f1aae00232ab9",
                            "module": "move_pump",
                            "event": "Buy"  # 需与合约中定义的事件名称完全匹配(注意大小写)
                        }
                    },
                    "options": {
                        "showType": True,
                        "showSender": True,
                        "showPayload": True
                    }
                }
            ]
        }
        # 订阅Buy事件
        await websocket.send(json.dumps(base_subscribe_msg))
        
        # 复制消息并修改事件名称,订阅Sell事件
        sell_subscribe_msg = base_subscribe_msg.copy()
        sell_subscribe_msg["id"] = 2
        sell_subscribe_msg["params"][0]["filter"]["MoveEvent"]["event"] = "Sell"
        await websocket.send(json.dumps(sell_subscribe_msg))
        
        while True:
            response = await websocket.recv()
            event = json.loads(response)
            print("New event received:", event)

asyncio.get_event_loop().run_until_complete(subscribe_events())

关键修改说明

  • 地址修正:移除包地址的0x前缀,符合Sui RPC的地址解析规则
  • 接口替换:用suix_subscribeEventStream替代废弃的suix_subscribeEvent,该接口支持更灵活的事件过滤和参数配置
  • 事件名称匹配:确保event字段与Move合约中定义的事件名称大小写完全一致(比如合约中定义event Buy { ... },就必须写"Buy")
  • 多事件订阅:通过复制订阅消息并修改事件ID和名称,实现同时监听Buy和Sell两类事件

内容的提问来源于stack exchange,提问作者Xpycteppp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 00:26:17