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

如何在Python中序列化Solana的VersionedTransaction(适配Jito Bundle)

在Python中序列化Solana VersionedTransaction的正确方式

你遇到的问题是因为Python的solders库(Solana官方推荐的底层交易处理库)里的VersionedTransaction类并没有serialize()方法——这和Node.js的@solana/web3.js API设计不同,Python里对应的方法是to_bytes(),具体操作步骤如下:

1. 基础序列化(获取原始字节)

直接调用VersionedTransaction实例的to_bytes()方法,就能得到符合Solana标准的交易二进制数据:

from solders.transaction import VersionedTransaction

# 假设你已经通过构建指令、签名等流程得到了versioned_tx实例
raw_serialized = versioned_tx.to_bytes()

2. 转换为Jito Bundle所需的Base64格式

Jito的Bundle API通常要求交易以Base64编码的字符串形式提交,所以需要再对原始字节做Base64编码:

import base64

# 编码为Base64字符串
base64_serialized = base64.b64encode(raw_serialized).decode("utf-8")

批量处理多笔交易(适配Jito Bundle)

如果要构建包含多笔交易的Jito Bundle,只需要循环处理每笔交易即可:

# 假设你有多个已签名的VersionedTransaction实例:tx1, tx2, tx3
bundle_tx_list = [
    base64.b64encode(tx.to_bytes()).decode("utf-8")
    for tx in [tx1, tx2, tx3]
]

# 之后就可以把bundle_tx_list作为交易列表,传入Jito的Bundle提交接口

需要注意的是,to_bytes()方法输出的是完全符合Solana交易序列化规范的二进制数据,和Node.js里serialize()的输出格式一致,只是Python库用了不同的方法名而已。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:12:05