mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-10 08:48:07 +00:00
Add token expiry and Telegram notification features
- Introduced `STANDX_TOKEN_EXPIRY` configuration to manage token expiration, including handling logic for active, expired, and silent states. - Implemented Telegram notifications for key events such as order filled, position opened/closed, stop loss triggered, and token expiration. - Updated Maker Points engine to integrate token expiry checks and notification sending, enhancing user awareness of trading conditions. - Enhanced documentation to include details on configuring token expiry and Telegram notifications for improved user guidance.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { isStandxTokenExpired, getStandxTokenExpiryInfo, standxTokenConfig } from "../config";
|
||||
|
||||
export type TokenExpiryState = "active" | "expired" | "expired_with_position" | "silent";
|
||||
|
||||
export interface TokenExpiryStatus {
|
||||
state: TokenExpiryState;
|
||||
expired: boolean;
|
||||
expiryTimestamp: number | null;
|
||||
remainingMs: number | null;
|
||||
hasPosition: boolean;
|
||||
hasOpenOrders: boolean;
|
||||
}
|
||||
|
||||
export interface TokenExpiryCheckParams {
|
||||
positionAmt: number;
|
||||
openOrderCount: number;
|
||||
}
|
||||
|
||||
export function checkStandxTokenExpiry(params: TokenExpiryCheckParams): TokenExpiryStatus {
|
||||
const info = getStandxTokenExpiryInfo();
|
||||
const hasPosition = Math.abs(params.positionAmt) > 1e-8;
|
||||
const hasOpenOrders = params.openOrderCount > 0;
|
||||
|
||||
if (!info.expired) {
|
||||
return {
|
||||
state: "active",
|
||||
expired: false,
|
||||
expiryTimestamp: info.expiryTimestamp,
|
||||
remainingMs: info.remainingMs,
|
||||
hasPosition,
|
||||
hasOpenOrders,
|
||||
};
|
||||
}
|
||||
|
||||
if (hasPosition) {
|
||||
return {
|
||||
state: "expired_with_position",
|
||||
expired: true,
|
||||
expiryTimestamp: info.expiryTimestamp,
|
||||
remainingMs: 0,
|
||||
hasPosition: true,
|
||||
hasOpenOrders,
|
||||
};
|
||||
}
|
||||
|
||||
if (!hasOpenOrders) {
|
||||
return {
|
||||
state: "silent",
|
||||
expired: true,
|
||||
expiryTimestamp: info.expiryTimestamp,
|
||||
remainingMs: 0,
|
||||
hasPosition: false,
|
||||
hasOpenOrders: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
state: "expired",
|
||||
expired: true,
|
||||
expiryTimestamp: info.expiryTimestamp,
|
||||
remainingMs: 0,
|
||||
hasPosition,
|
||||
hasOpenOrders,
|
||||
};
|
||||
}
|
||||
|
||||
export function formatTokenExpiryMessage(status: TokenExpiryStatus): string | null {
|
||||
if (!status.expired) {
|
||||
if (status.remainingMs != null && status.remainingMs < 3600_000) {
|
||||
const mins = Math.ceil(status.remainingMs / 60_000);
|
||||
return `StandX Token 将在 ${mins} 分钟后过期`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (status.state) {
|
||||
case "expired":
|
||||
return "StandX Token 已过期,正在取消所有挂单";
|
||||
case "expired_with_position":
|
||||
return "StandX Token 已过期,仅保留平仓/止损逻辑";
|
||||
case "silent":
|
||||
return "StandX Token 已过期,进入静默数据接收模式";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function isTokenExpiryConfigured(): boolean {
|
||||
return standxTokenConfig.expiryTimestamp != null;
|
||||
}
|
||||
|
||||
export { isStandxTokenExpired, getStandxTokenExpiryInfo };
|
||||
Reference in New Issue
Block a user