설치
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
패키지 레지스트리

