准备工作
开始前请确认:
- 已注册 ChainStream 账号(点此注册)
- 已拥有 API Key(以
cs_live_... 开头)
GraphQL API 与 REST Data API 共用同一 API Key。若你已有 REST API 的 Key,可直接复用。
步骤 1:获取 API Key
- 登录 ChainStream Dashboard
- 进入 Applications
- 点击 Create New App(或选择已有应用)
- 复制 API Key
步骤 2:打开 GraphQL IDE
访问 ChainStream GraphQL IDE:
IDE 提供自动补全、语法高亮、行内文档与查询历史 —— 是探索 schema 与编写查询最快的方式。
步骤 3:配置认证
在 IDE 左下角的 Headers 面板中添加 API Key:
{
"X-API-KEY": "your_api_key"
}
步骤 4:运行示例查询
将以下查询粘贴到编辑器。它会拉取 Solana 上最近 10 笔 DEX 成交,包含区块时间、交易哈希、买卖代币信息、数量、USD 价格与 DEX 协议名称。
query {
DEXTrades(
network: sol
limit: {count: 10}
orderBy: Block_Time_DESC
) {
Block {
Time
Slot
}
Transaction {
Hash
}
Trade {
Buy {
Currency { MintAddress }
Amount
PriceInUSD
}
Sell {
Currency { MintAddress }
Amount
}
Dex { ProtocolName }
}
}
}
点击 Play 按钮(或按 Ctrl+Enter / Cmd+Enter)执行。
cURL 等价写法
在终端中可执行相同查询:
curl -X POST "https://graphql.chainstream.io/graphql" \
-H "Content-Type: application/json" \
-H "X-API-KEY: your_api_key" \
-d '{
"query": "{ DEXTrades(network: sol, limit: {count: 10}, orderBy: Block_Time_DESC) { Block { Time Slot } Transaction { Hash } Trade { Buy { Currency { MintAddress } Amount PriceInUSD } Sell { Currency { MintAddress } Amount } Dex { ProtocolName } } } }"
}'
响应示例
成功响应大致如下:
{
"data": {
"DEXTrades": [
{
"Block": {
"Time": "2025-03-27T10:32:18Z",
"Slot": 325847291
},
"Transaction": {
"Hash": "4vKzR8g...x9bQ"
},
"Trade": {
"Buy": {
"Currency": { "MintAddress": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" },
"Amount": 1523.45,
"PriceInUSD": 1.0001
},
"Sell": {
"Currency": { "MintAddress": "So11111111111111111111111111111111111111112" },
"Amount": 10.237
},
"Dex": { "ProtocolName": "Raydium" }
}
},
{
"Block": {
"Time": "2025-03-27T10:32:17Z",
"Slot": 325847289
},
"Transaction": {
"Hash": "3mPqW7n...kR2J"
},
"Trade": {
"Buy": {
"Currency": { "MintAddress": "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263" },
"Amount": 48291037.12,
"PriceInUSD": 0.00003152
},
"Sell": {
"Currency": { "MintAddress": "So11111111111111111111111111111111111111112" },
"Amount": 10.5
},
"Dex": { "ProtocolName": "Orca" }
}
}
]
},
"extensions": {
"credits": {
"total": 50,
"cubes": [
{ "cube": "DEXTrades", "credits": 50, "row_count": 10 }
]
}
}
}
理解响应
响应结构与查询一一对应:
| Path | 说明 |
|---|
Block.Time | 区块时间戳(ISO 8601) |
Block.Slot | Solana slot 编号(Solana 特有) |
Transaction.Hash | 链上交易哈希 |
Trade.Buy.Currency.MintAddress | 买入资产的代币地址 |
Trade.Buy.Amount | 买入代币数量 |
Trade.Buy.PriceInUSD | 成交时买入代币的 USD 价格 |
Trade.Sell.Currency.MintAddress | 卖出资产的代币地址 |
Trade.Sell.Amount | 卖出代币数量 |
Trade.Dex.ProtocolName | 执行成交的 DEX 协议(如 Raydium、Orca、PancakeSwap) |
extensions.credits 表示本次查询消耗的计费 credits 与剩余额度。详见 计费与额度。
尝试修改查询
在能跑通基础查询后,可以试试这些改动:
将 network: sol 改为 network: eth 即可查询 Ethereum DEX 成交(Uniswap、SushiSwap 等)。
下一步
Schema 与数据模型
浏览全部 25 个 Cube、字段类型、过滤运算符与聚合函数。
查询示例
查看 DEX 成交、转账、OHLC、持有者等实际查询示例。
GraphQL IDE 指南
掌握 IDE —— 查询模板、已保存查询、变量面板与代码导出。
计费与额度
了解 query credits 的计算方式与成本优化思路。