Skip to content

Latest commit

 

History

History
197 lines (145 loc) · 9.15 KB

File metadata and controls

197 lines (145 loc) · 9.15 KB

⚡ Sol Parser SDK - Node.js

高性能 Solana DEX 事件解析(Node.js / TypeScript)

npm License

中文 | English | 官网 | Telegram | Discord

支持本项目

本 SDK 完全免费且开源。但维护和持续更新需要消耗大量 AI 算力与 Token。如果这个 SDK 对您的开发有帮助,欢迎每月捐赠任意数量的 SOL,您的支持将帮助这个项目持续运行!

捐赠钱包: 6oW7AXz1yRb57pYSxysuXnMs2aR1ha5rzGzReZ1MjPV8


其他语言 SDK

语言 仓库
Rust sol-parser-sdk
Node.js sol-parser-sdk-nodejs
Python sol-parser-sdk-python
Go sol-parser-sdk-golang

怎么用

1. 安装

npm

npm install sol-parser-sdk-nodejs@0.4.4

源码(monorepo 里目录可能是 sol-parser-sdk-ts

git clone https://github.com/0xfnzero/sol-parser-sdk-nodejs
cd sol-parser-sdk-nodejs
npm install
# npm run build   # 若从 dist 引用而非 tsx 跑 examples,再执行

2. 环境变量(Yellowstone gRPC 示例)

包根目录(与 package.json 同级):

cp .env.example .env
# 填写 GRPC_URL、GRPC_TOKEN

在该目录下执行下面的 npx tsx,以便加载 .env

3. 冒烟

npx tsx scripts/test-grpc-ts.ts

需要 GRPC_URLGRPC_TOKEN。更多变量见 .env.example(如 MAX_EVENTSTIMEOUT_MS)。

4. 最小 gRPC 订阅示例

import {
  YellowstoneGrpc,
  parseDexEventsFromGrpcTransactionInfo,
  dexEventToJsonString,
} from "sol-parser-sdk-nodejs";

const ENDPOINT = process.env.GRPC_URL?.trim() ?? "";
const X_TOKEN = process.env.GRPC_TOKEN?.trim() ?? "";
if (!ENDPOINT || !X_TOKEN) throw new Error("GRPC_URL and GRPC_TOKEN are required");

const client = new YellowstoneGrpc(ENDPOINT, X_TOKEN);

const filter = {
  account_include: [
    "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P", // PumpFun
    "pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA", // PumpSwap
  ],
  account_exclude: [],
  account_required: [],
  vote: false,
  failed: false,
};

const sub = await client.subscribeTransactions(filter, {
  onUpdate: (update) => {
    const txInfo = update.transaction?.transaction;
    if (!txInfo?.transactionRaw || !txInfo.metaRaw) return;
    const slot = update.transaction!.slot;
    const events = parseDexEventsFromGrpcTransactionInfo(txInfo, slot, undefined);
    for (const ev of events) console.log(dexEventToJsonString(ev, 2));
  },
  onError: (err) => console.error(err.message),
  onEnd: () => {},
});

console.log("subscribed", sub.id);

更轻量:仅用日志用 parseLogsOnly;要补账户可再配合 applyAccountFillsToLogEvents

5. ShredStream(HTTP,不是 Yellowstone gRPC)

使用 SHREDSTREAM_URL / SHRED_URL(默认 http://127.0.0.1:10800)或命令行 --url不用 GRPC_URL

客户端在 TypeScript 中解码 gRPC entries 负载(布局与 Go shredstream/entries_decode 一致),并用 @solana/web3.js 反序列化线格式交易,无需 WebAssembly / wasm-pack

npx tsx examples/shredstream_example.ts -- --url=http://127.0.0.1:10800

shredstream_pumpfun_json.ts 另需 Solana RPC_URL(或 --rpc)解析 ALT。


示例列表

包根目录执行,npm install 后即可。示例用 npx tsx 直接加载 src/不必先 npm run build源码列每个文件单独一行,链接指向 GitHub 上的对应源码(GitHub / npm 均可点击)。

描述 运行命令 源码
本包脚本
gRPC 集成测试(PumpFun + PumpSwap,账户填充后的 DexEvent npx tsx scripts/test-grpc-ts.ts test-grpc-ts.ts
调试:打印 metaRaw / 日志结构 npx tsx scripts/debug-grpc-ts.ts debug-grpc-ts.ts
PumpFun
gRPC 输出完整 JSON DexEvent npx tsx examples/pumpfun_grpc_json.ts pumpfun_grpc_json.ts
PumpFun 事件 + 性能指标 npx tsx examples/pumpfun_with_metrics.ts pumpfun_with_metrics.ts
PumpFun 交易类型过滤 npx tsx examples/pumpfun_trade_filter.ts pumpfun_trade_filter.ts
PumpFun 快速连接测试 npx tsx examples/pumpfun_quick_test.ts pumpfun_quick_test.ts
PumpSwap
gRPC 输出完整 JSON DexEvent npx tsx examples/pumpswap_grpc_json.ts pumpswap_grpc_json.ts
PumpSwap 事件 + 性能指标 npx tsx examples/pumpswap_with_metrics.ts pumpswap_with_metrics.ts
PumpSwap 超低延迟 npx tsx examples/pumpswap_low_latency.ts pumpswap_low_latency.ts
Meteora DAMM
Meteora DAMM V2 事件 npx tsx examples/meteora_damm_grpc.ts meteora_damm_grpc.ts
ShredStream(HTTP, Yellowstone gRPC;端点见上文步骤 5)
超低延迟订阅、队列与延迟统计。端点:--url / SHREDSTREAM_URL / .env(默认 http://127.0.0.1:10800)。 npx tsx examples/shredstream_example.ts shredstream_example.ts
ShredStream → PumpFun DexEvent JSON;需 Solana RPC 解析 ALT(RPC_URL--rpc)。 npx tsx examples/shredstream_pumpfun_json.ts shredstream_pumpfun_json.ts
多协议
同时订阅所有 DEX 协议 npx tsx examples/multi_protocol_grpc.ts multi_protocol_grpc.ts
工具
验证 onUpdate 同步错误不会打断 gRPC 流 npx tsx examples/grpc_onupdate_error_test.ts grpc_onupdate_error_test.ts
按签名解析交易(parseTransactionFromRpc;非 gRPC)。在 .env 或环境中设置 TX_SIGNATURE npx tsx examples/parse_tx_by_signature.ts parse_tx_by_signature.ts

npm run 别名(与上表 ShredStream 行同一源码文件,每行一条):

环境变量: gRPC 示例需要 GRPC_URLGRPC_TOKEN。ShredStream 使用 SHREDSTREAM_URL / SHRED_URL--urlshredstream_pumpfun_json 另需 RPC_URL / --rpc。详见 .env.example


协议

PumpFun、PumpSwap、Raydium AMM V4 / CLMM / CPMM、Orca Whirlpool、Meteora DAMM V2 / DLMM、Bonk Launchpad(见 src/instr/)。


常用 API

  • parseDexEventsFromGrpcTransactionInfo — 需要 transactionRaw + metaRaw(与 Rust gRPC 对齐)。
  • parseRpcTransaction / parseTransactionFromRpc — HTTP RPC 全量解析。
  • dexEventToJsonString — 含 BigInt 的安全 JSON 输出。

开发

npm run build
npm run check:migration   # 对齐与校验,需先 build

许可证

MIT — https://github.com/0xfnzero/sol-parser-sdk-nodejs