@debate300/bithumb-pro
Version:
A real-time cryptocurrency price tracker for Bithumb (Pro).
126 lines (125 loc) • 5.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bithumb = void 0;
const ws_1 = __importDefault(require("ws"));
const chalk_1 = __importDefault(require("chalk"));
const axios_1 = __importDefault(require("axios"));
const common_1 = require("../common");
class Bithumb extends common_1.Exchange {
constructor(redrawCallback) {
super(redrawCallback);
this.name = "Bithumb";
this.ws = null;
this.isActive = false;
this.wsUri = "wss://pubwss.bithumb.com/pub/ws";
this.marketInfo = {};
this.iconMap = {};
this.lastClosePrices = {};
}
async connect(appConfig) {
this.isActive = true;
await this.fetchMarketInfo();
appConfig.coins.forEach((coin) => {
this.iconMap[`${coin.symbol}_${coin.unit_currency}`] = coin.icon;
});
const symbols = appConfig.coins.map((coin) => `${coin.symbol}_${coin.unit_currency}`);
this.ws = new ws_1.default(this.wsUri);
this.ws.on("open", () => {
const subscribeMsg = {
type: "ticker",
symbols: symbols,
tickTypes: ["MID"],
};
this.ws?.send(JSON.stringify(subscribeMsg));
});
this.ws.on("message", (data) => {
this.handleMessage(data, appConfig);
});
this.ws.on("error", (error) => {
console.error(chalk_1.default.red("Bithumb WebSocket 오류 발생:"), error);
});
this.ws.on("close", () => {
if (!this.isActive) {
console.log(chalk_1.default.gray(`${this.name} WebSocket 연결이 정상적으로 종료되었습니다.`));
return;
}
console.log(chalk_1.default.yellow(`${this.name} WebSocket 연결이 끊어졌습니다. 5초 후 재연결합니다.`));
setTimeout(() => this.connect(appConfig), 5000);
});
}
disconnect() {
this.isActive = false;
if (this.ws) {
this.ws.close();
this.ws = null;
this.realTimeData.clear();
console.log(chalk_1.default.yellow("Bithumb 연결이 해제되었습니다."));
}
}
handleMessage(data, appConfig) {
const message = JSON.parse(data.toString());
if (message.type === "ticker" && message.content) {
const content = message.content;
const standardizedData = this.standardize(content, appConfig);
this.realTimeData.set(content.symbol, standardizedData);
this.redrawCallback();
}
}
standardize(data, appConfig) {
const symbol = data.symbol;
const currentPrice = parseFloat(data.closePrice);
const prevComparisonPrice = parseFloat(this.lastClosePrices[symbol] || data.prevClosePrice);
this.lastClosePrices[symbol] = data.closePrice;
let priceColor = chalk_1.default.white;
if (currentPrice > prevComparisonPrice)
priceColor = chalk_1.default.redBright;
else if (currentPrice < prevComparisonPrice)
priceColor = chalk_1.default.cyanBright;
const coinConfig = appConfig.coins.find((c) => `${c.symbol}_${c.unit_currency}` === symbol);
let profitLossRate;
if (coinConfig && coinConfig.averagePurchasePrice > 0) {
const avgPrice = coinConfig.averagePurchasePrice;
profitLossRate = ((currentPrice - avgPrice) / avgPrice) * 100;
}
return {
symbol: symbol,
koreanName: this.marketInfo[symbol]?.korean_name || symbol.replace("_KRW", ""),
icon: this.iconMap[symbol] || " ",
currentPrice: currentPrice,
priceChangeRate: parseFloat(data.chgRate),
priceChangeAmount: parseFloat(data.chgAmt),
volumePower: parseFloat(data.volumePower),
highPrice: parseFloat(data.highPrice),
lowPrice: parseFloat(data.lowPrice),
prevClosePrice: parseFloat(data.prevClosePrice),
averagePurchasePrice: coinConfig?.averagePurchasePrice,
profitLossRate: profitLossRate,
priceColor: priceColor,
};
}
async fetchMarketInfo() {
try {
const response = await axios_1.default.get("https://api.bithumb.com/v1/market/all?isDetails=false");
if (response.status === 200) {
const markets = response.data;
markets.forEach((market) => {
if (market.market.startsWith("KRW-")) {
const symbol = `${market.market.replace("KRW-", "")}_KRW`;
this.marketInfo[symbol] = {
market: market.market,
korean_name: market.korean_name,
english_name: market.english_name,
};
}
});
}
}
catch (error) {
console.error(chalk_1.default.red("Bithumb 한글 코인 이름 로딩 오류:"), error);
}
}
}
exports.Bithumb = Bithumb;