@snjyor/binance-mcp
Version:
Binance Cryptocurrency Market Data MCP Service
183 lines • 5.96 kB
JavaScript
import axios from 'axios';
/**
* 币安API调用测试脚本
* 用于测试和演示币安API调用
*/
const BASE_URL = "https://api.binance.com";
// 工具函数 - 格式化输出JSON
function prettyPrint(data) {
console.log(JSON.stringify(data, null, 2));
}
// 1. 获取订单簿
async function getOrderBook(symbol, limit) {
try {
console.log(`\n===== 获取订单簿 (${symbol}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/depth`, {
params: {
symbol,
limit
}
});
// 只显示部分数据以保持输出简洁
const bids = response.data.bids.slice(0, 3);
const asks = response.data.asks.slice(0, 3);
console.log(`订单簿ID: ${response.data.lastUpdateId}`);
console.log(`前3个买单: `, bids);
console.log(`前3个卖单: `, asks);
}
catch (error) {
console.error(`获取订单簿失败: ${error.message}`);
}
}
// 2. 获取最近交易
async function getRecentTrades(symbol, limit) {
try {
console.log(`\n===== 获取最近交易 (${symbol}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/trades`, {
params: {
symbol,
limit: limit || 5
}
});
prettyPrint(response.data);
}
catch (error) {
console.error(`获取最近交易失败: ${error.message}`);
}
}
// 3. 获取K线数据
async function getKlines(symbol, interval, limit) {
try {
console.log(`\n===== 获取K线数据 (${symbol}, ${interval}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/klines`, {
params: {
symbol,
interval,
limit: limit || 5
}
});
// 格式化K线数据以提高可读性
const formattedData = response.data.map((kline) => {
return {
openTime: new Date(kline[0]).toISOString(),
open: kline[1],
high: kline[2],
low: kline[3],
close: kline[4],
volume: kline[5],
closeTime: new Date(kline[6]).toISOString(),
trades: kline[8]
};
});
prettyPrint(formattedData);
}
catch (error) {
console.error(`获取K线数据失败: ${error.message}`);
}
}
// 4. 获取当前价格
async function getCurrentPrice(symbol) {
try {
console.log(`\n===== 获取当前价格 (${symbol}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/ticker/price`, {
params: {
symbol
}
});
prettyPrint(response.data);
}
catch (error) {
console.error(`获取当前价格失败: ${error.message}`);
}
}
// 5. 获取24小时价格统计
async function get24hrTicker(symbol) {
try {
console.log(`\n===== 获取24小时价格统计 (${symbol}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/ticker/24hr`, {
params: {
symbol
}
});
const { symbol: sym, priceChange, priceChangePercent, lastPrice, volume, highPrice, lowPrice } = response.data;
console.log(`Symbol: ${sym}`);
console.log(`价格变动: ${priceChange} (${priceChangePercent}%)`);
console.log(`最新价格: ${lastPrice}`);
console.log(`24h最高: ${highPrice}`);
console.log(`24h最低: ${lowPrice}`);
console.log(`24h成交量: ${volume}`);
}
catch (error) {
console.error(`获取24小时价格统计失败: ${error.message}`);
}
}
// 6. 获取平均价格
async function getAvgPrice(symbol) {
try {
console.log(`\n===== 获取平均价格 (${symbol}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/avgPrice`, {
params: {
symbol
}
});
prettyPrint(response.data);
}
catch (error) {
console.error(`获取平均价格失败: ${error.message}`);
}
}
// 7. 获取订单簿行情
async function getBookTicker(symbol) {
try {
console.log(`\n===== 获取订单簿行情 (${symbol}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/ticker/bookTicker`, {
params: {
symbol
}
});
prettyPrint(response.data);
}
catch (error) {
console.error(`获取订单簿行情失败: ${error.message}`);
}
}
// 8. 获取滚动窗口价格统计
async function getRollingWindowTicker(symbol, windowSize) {
try {
console.log(`\n===== 获取滚动窗口价格统计 (${symbol}, ${windowSize}) =====`);
const response = await axios.get(`${BASE_URL}/api/v3/ticker`, {
params: {
symbol,
windowSize
}
});
prettyPrint(response.data);
}
catch (error) {
console.error(`获取滚动窗口价格统计失败: ${error.message}`);
}
}
// 主函数
async function main() {
const symbol = 'BTCUSDT'; // 默认使用BTC/USDT交易对
console.log('===== 币安API调用演示 =====');
console.log('正在发起请求,请稍候...\n');
try {
// 依次执行各种API调用
await getOrderBook(symbol, 10);
await getRecentTrades(symbol, 5);
await getKlines(symbol, '1d', 5);
await getCurrentPrice(symbol);
await get24hrTicker(symbol);
await getAvgPrice(symbol);
await getBookTicker(symbol);
await getRollingWindowTicker(symbol, '1d');
console.log('\n===== 测试完成 =====');
}
catch (error) {
console.error('测试过程中发生错误:', error);
}
}
// 运行主函数
main().catch((error) => console.error('顶层错误:', error));
//# sourceMappingURL=demo.js.map