準備工作
開始前請確認:
- 已註冊 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 的計算方式與成本最佳化思路。