Package Logo
substreams_nftmarketplace
substreams_nftmarketplace@v0.1.4
Total Downloads
446
Published
2 weeks ago
Publisher
User Avatar yaofan
Total Downloads
446
Published
2 weeks ago
Publisher
User Avatar yaofan

Readme

NFT Marketplace Substreams

这是一个使用 Substreams 技术构建的 NFT Marketplace 数据索引项目,用于替代传统的 The Graph subgraph,以提供更快的数据同步速度。

项目概述

本项目索引 BSC 网络上的 NFT Marketplace 合约(地址:0x5842Eec744Dd4a50b32399611E6Fdc421820Db26),追踪以下事件:

  • OrderCreated: 订单创建事件
  • OrderCancelled: 订单取消事件
  • OrderFilled: 订单成交事件
  • BatchOrdersFilled: 批量订单成交事件

项目结构

substreams-nftmarketplace/
├── Cargo.toml              # Rust 依赖配置
├── substreams.yaml         # Substreams 配置文件
├── build.rs                # 构建脚本
├── schema.sql              # 数据库模式
├── deploy.sh               # 部署脚本
├── proto/                  # Protobuf 定义
│   ├── events.proto        # 事件数据结构
│   └── database.proto      # 数据库变更结构
└── src/                    # 源代码
    ├── lib.rs              # 主要逻辑
    ├── events.rs           # 事件处理
    └── pb/                 # 生成的 protobuf 代码
        └── mod.rs

功能特性

数据实体

  1. Orders 表

    • 订单 ID、卖家地址、代币 ID
    • 支付代币、价格、创建时间
    • 订单状态(Active/Filled/Cancelled)
    • 取消/成交时间
  2. Trades 表

    • 关联订单 ID、买家/卖家地址
    • 成交价格、手续费、时间戳
    • 区块信息和交易哈希

性能优势

相比传统 subgraph,Substreams 提供:

  • 并行处理: 多核并行解析区块数据
  • 增量同步: 只处理变更的数据
  • 缓存机制: 智能缓存减少重复计算
  • 更快启动: 无需从创世区块开始同步

部署说明

前置要求

  1. 安装 Rust 和 wasm32 target:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-unknown-unknown
  1. 安装 Substreams CLI:
# Install from https://github.com/streamingfast/substreams/releases

构建和部署

  1. 构建项目:
cd packages/substreams-nftmarketplace
cargo build --target wasm32-unknown-unknown --release
  1. 生成 protobuf:
substreams protogen ./substreams.yaml --exclude-paths="sf/substreams,google"
  1. 打包 substreams:
substreams pack ./substreams.yaml
  1. 本地测试:
substreams run -e bsc.streamingfast.io:443 ./substreams-nftmarketplace-v0.1.0.spkg map_events --start-block=61229970 --stop-block=+100
  1. 部署到网络:
substreams auth
substreams deploy ./substreams.yaml

或者直接运行部署脚本:

./deploy.sh

配置说明

网络配置

项目配置为 BSC 主网:

  • 网络: bsc
  • 起始区块: 61229970
  • RPC 端点: bsc.streamingfast.io:443

合约配置

  • 合约地址: 0x5842Eec744Dd4a50b32399611E6Fdc421820Db26
  • 监听的事件签名已预计算并存储在 src/events.rs

数据查询

部署后,可以通过以下方式查询数据:

  1. PostgreSQL 直接查询:
-- 查询活跃订单
SELECT * FROM orders WHERE status = 'Active';

-- 查询最近的交易
SELECT * FROM trades ORDER BY timestamp DESC LIMIT 10;
  1. GraphQL API (如果启用了 Postgraphile):
query {
  orders(condition: {status: "Active"}) {
    nodes {
      orderId
      seller
      price
      createdAt
    }
  }
}

监控和维护

日志监控

Substreams 提供详细的执行日志,可以监控:

  • 处理的区块数量
  • 事件提取统计
  • 错误和警告信息

性能指标

关键指标包括:

  • 区块处理速度 (blocks/second)
  • 延迟时间 (lag behind chain head)
  • 内存和 CPU 使用率

故障排除

常见问题

  1. 构建失败:

    • 确保 Rust 和 wasm32 target 已安装
    • 检查依赖版本兼容性
  2. 部署失败:

    • 验证网络连接和认证
    • 检查 substreams.yaml 配置
  3. 数据不一致:

    • 验证事件签名是否正确
    • 检查合约地址和起始区块

调试技巧

# 查看详细日志
substreams run --debug

# 验证事件解析
substreams run map_events --start-block=61229970 --stop-block=61229975

# 检查数据库变更
substreams run db_out --start-block=61229970 --stop-block=61229975

与原始 Subgraph 的对比

功能 Subgraph Substreams
同步速度 快 (3-5x)
资源使用
并行处理
实时性 延迟大 延迟小
开发复杂度 中等

贡献指南

  1. Fork 项目
  2. 创建功能分支
  3. 提交变更
  4. 创建 Pull Request

许可证

MIT License

Documentation

Modules

Maps icon
Maps

map
map_events

b2d3f28511136dd74a499db68880fe0caf7de23a
map map_events (
blocksf.ethereum.type.v2.Block
)  -> nftmarketplace.v1.Events
substreams gui substreams-nftmarketplace@v0.1.4 map_events

map
db_out

ab68ee410c8d00c26bb6bc172d33bce57610d864
substreams gui substreams-nftmarketplace@v0.1.4 db_out
Protobuf

Protobuf Docs Explorer

nftmarketplace.v1