インストール
npm install @chainstream-io/sdk
クイックスタート
import { ChainStreamClient } from '@chainstream-io/sdk';
const client = new ChainStreamClient('YOUR_ACCESS_TOKEN');
REST API の例
トークン情報を取得する:import { ChainStreamClient } from '@chainstream-io/sdk';
const ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN';
async function main() {
// SDK クライアントを初期化
const client = new ChainStreamClient(ACCESS_TOKEN);
const chain = 'sol';
const tokenAddress = 'So11111111111111111111111111111111111111112'; // SOL
console.log(`Querying Token: ${chain}/${tokenAddress}`);
try {
// SDK 経由で API を呼び出す
const result = await client.token.getToken(chain, tokenAddress);
console.log('Result:');
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('API call failed:', error);
}
}
main();
WebSocket の例
リアルタイムのトークンローソク足データを購読する:import { ChainStreamClient, Resolution } from '@chainstream-io/sdk';
const ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN';
async function main() {
const client = new ChainStreamClient(ACCESS_TOKEN, {
autoConnectWebSocket: true,
});
const chain = 'sol';
const tokenAddress = 'So11111111111111111111111111111111111111112'; // SOL
console.log(`Subscribing to Token Candles: ${chain}/${tokenAddress}`);
console.log('Listening... (Press Ctrl+C to stop)\n');
// SDK 経由で WebSocket を購読
client.stream.subscribeTokenCandles({
chain,
tokenAddress,
resolution: Resolution['1s'],
callback: data => {
console.log(new Date().toISOString(), JSON.stringify(data));
},
});
// プロセスを維持
process.on('SIGINT', () => {
console.log('\nClosing connection...');
process.exit(0);
});
// 実行を継続
await new Promise(() => {});
}
main().catch(error => {
console.error('Test failed:', error.message);
process.exit(1);
});
その他の例
トークンを検索
const results = await client.token.searchToken('bonk');
console.log(results);
取引リストを取得
const trades = await client.trade.getTrades('sol', {
tokenAddress: 'TOKEN_ADDRESS',
limit: 20,
});
console.log(trades);
ウォレットの損益(PnL)を取得
const pnl = await client.wallet.getPnl('sol', 'WALLET_ADDRESS');
console.log(`Total PnL: ${pnl.totalPnlUsd}`);
トークンのローソク足(K線)を取得
const candles = await client.token.getCandles('sol', 'TOKEN_ADDRESS', {
resolution: '1h',
limit: 24,
});
for (const c of candles) {
console.log(`${c.time}: O=${c.open} H=${c.high} L=${c.low} C=${c.close}`);
}
新着トークンランキングを取得
const newTokens = await client.ranking.getNewTokens('sol');
for (const token of newTokens) {
console.log(`${token.symbol}: ${token.address}`);
}
エラーハンドリング
import { ChainStreamClient, ChainStreamError } from '@chainstream-io/sdk';
try {
const token = await client.token.getToken('sol', 'INVALID_ADDRESS');
} catch (error) {
if (error instanceof ChainStreamError) {
console.error(`Error code: ${error.code}`);
console.error(`Error message: ${error.message}`);
}
}
指数バックオフによるリトライ
レート制限(HTTP 429)が発生した場合のリトライロジック:async function withRetry<T>(fn: () => Promise<T>, maxRetries = 3): Promise<T> {
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
if (error instanceof ChainStreamError && error.code === 429 && attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
throw new Error('Max retries exceeded');
}
const token = await withRetry(() => client.token.getToken('sol', 'TOKEN_ADDRESS'));
リソース
GitHub
ソースコードを見る
npm
パッケージレジストリ

