nexushub-commands 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Client.d.ts +20 -0
- package/lib/Client.js +28 -0
- package/lib/commands/CheckPaidOrder.command.d.ts +4 -0
- package/lib/commands/CheckPaidOrder.command.js +31 -0
- package/lib/commands/Deposit.command.d.ts +5 -0
- package/lib/commands/Deposit.command.js +208 -0
- package/lib/commands/GetDepositOrder.command.d.ts +5 -0
- package/lib/commands/GetDepositOrder.command.js +92 -0
- package/lib/commands/Withdrawal.command.d.ts +0 -0
- package/lib/commands/Withdrawal.command.js +1 -0
- package/lib/commands/index.d.ts +3 -0
- package/lib/commands/index.js +6 -0
- package/lib/convert.d.ts +17 -0
- package/lib/convert.js +64 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/utils/CoinBase.service.d.ts +33 -0
- package/lib/utils/CoinBase.service.js +113 -0
- package/lib/utils/formatMessage.d.ts +8 -0
- package/lib/utils/formatMessage.js +46 -0
- package/package.json +16 -0
- package/src/Client.ts +49 -0
- package/src/commands/CheckPaidOrder.command.ts +16 -0
- package/src/commands/Deposit.command.ts +243 -0
- package/src/commands/GetDepositOrder.command.ts +88 -0
- package/src/commands/Withdrawal.command.ts +0 -0
- package/src/commands/index.ts +3 -0
- package/src/convert.ts +91 -0
- package/src/index.ts +1 -0
- package/src/utils/CoinBase.service.ts +129 -0
- package/src/utils/formatMessage.ts +43 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
interface CryptoRate {
|
|
4
|
+
data: {
|
|
5
|
+
currency: string;
|
|
6
|
+
rates: { [key: string]: string };
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface CachedRate {
|
|
11
|
+
rates: { [key: string]: string };
|
|
12
|
+
timestamp: number;
|
|
13
|
+
updateInterval: NodeJS.Timeout | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class CoinBaseService {
|
|
17
|
+
private readonly UPDATE_INTERVAL_MS = 300_000; // 5 минут
|
|
18
|
+
private readonly CACHE_LIFETIME_MS = 300_000; // Время жизни кэша - 5 минут
|
|
19
|
+
private ratesCache: Map<string, CachedRate> = new Map();
|
|
20
|
+
|
|
21
|
+
static instance: CoinBaseService;
|
|
22
|
+
static getInstance() {
|
|
23
|
+
if (!CoinBaseService.instance) CoinBaseService.instance = new CoinBaseService();
|
|
24
|
+
return CoinBaseService.instance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Проверяет актуальность кэша
|
|
29
|
+
*/
|
|
30
|
+
private isCacheValid(currency: string): boolean {
|
|
31
|
+
const cachedData = this.ratesCache.get(currency);
|
|
32
|
+
if (!cachedData) return false;
|
|
33
|
+
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
return now - cachedData.timestamp < this.CACHE_LIFETIME_MS;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Получает курсы из кэша
|
|
40
|
+
*/
|
|
41
|
+
public async getRates(currency: string = 'USD') {
|
|
42
|
+
// Если валюта не кэширована или кэш устарел, начинаем обновление
|
|
43
|
+
if (!this.isCacheValid(currency)) await this.startUpdates(currency);
|
|
44
|
+
|
|
45
|
+
const cachedData = this.ratesCache.get(currency)!;
|
|
46
|
+
return cachedData.rates;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Запускает автоматическое обновление курсов для конкретной валюты
|
|
51
|
+
*/
|
|
52
|
+
private async startUpdates(currency: string) {
|
|
53
|
+
// Проверяем, не запущено ли уже обновление для этой валюты
|
|
54
|
+
const existingCache = this.ratesCache.get(currency);
|
|
55
|
+
if (existingCache?.updateInterval) return;
|
|
56
|
+
|
|
57
|
+
// Первое обновление сразу
|
|
58
|
+
await this.updateRates(currency);
|
|
59
|
+
|
|
60
|
+
// Создаем новый интервал обновления
|
|
61
|
+
const updateInterval = setInterval(() => {
|
|
62
|
+
this.updateRates(currency);
|
|
63
|
+
}, this.UPDATE_INTERVAL_MS);
|
|
64
|
+
|
|
65
|
+
// Сохраняем или обновляем информацию в кэше
|
|
66
|
+
this.ratesCache.set(currency, {
|
|
67
|
+
...this.ratesCache.get(currency),
|
|
68
|
+
updateInterval,
|
|
69
|
+
timestamp: Date.now(),
|
|
70
|
+
rates: this.ratesCache.get(currency)?.rates || {},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Останавливает обновление для указанной валюты или всех валют
|
|
76
|
+
*/
|
|
77
|
+
public stopUpdates(currency?: string): void {
|
|
78
|
+
if (currency) {
|
|
79
|
+
const cachedData = this.ratesCache.get(currency);
|
|
80
|
+
if (cachedData?.updateInterval) {
|
|
81
|
+
clearInterval(cachedData.updateInterval);
|
|
82
|
+
cachedData.updateInterval = null;
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
// Останавливаем все обновления
|
|
86
|
+
for (const [, data] of this.ratesCache) {
|
|
87
|
+
if (data.updateInterval) {
|
|
88
|
+
clearInterval(data.updateInterval);
|
|
89
|
+
data.updateInterval = null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Обновляет курсы криптовалют в кэше
|
|
97
|
+
*/
|
|
98
|
+
private async updateRates(currency: string): Promise<void> {
|
|
99
|
+
try {
|
|
100
|
+
const response = await axios.get<CryptoRate>(`https://api.coinbase.com/v2/exchange-rates?currency=${currency}`);
|
|
101
|
+
const rates = response.data.data.rates;
|
|
102
|
+
|
|
103
|
+
// Обновляем кэш
|
|
104
|
+
const existingCache = this.ratesCache.get(currency);
|
|
105
|
+
this.ratesCache.set(currency, {
|
|
106
|
+
...existingCache!,
|
|
107
|
+
rates,
|
|
108
|
+
timestamp: Date.now(),
|
|
109
|
+
});
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error('Ошибка при обновлении курсов криптовалют:', error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Очищает кэш для указанной валюты или весь кэш
|
|
117
|
+
*/
|
|
118
|
+
public clearCache(currency?: string): void {
|
|
119
|
+
if (currency) {
|
|
120
|
+
// Останавливаем обновления перед удалением
|
|
121
|
+
this.stopUpdates(currency);
|
|
122
|
+
this.ratesCache.delete(currency);
|
|
123
|
+
} else {
|
|
124
|
+
// Останавливаем все обновления перед очисткой
|
|
125
|
+
this.stopUpdates();
|
|
126
|
+
this.ratesCache.clear();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//@ts-nocheck
|
|
2
|
+
|
|
3
|
+
export function formatMessage({ header, body, footer }: { header?: string; body?: ({ title: string; data: any } | undefined)[]; footer?: string }) {
|
|
4
|
+
let text = header ? `${header}\n\n` : '';
|
|
5
|
+
|
|
6
|
+
for (const item of body?.filter((x) => x) || []) {
|
|
7
|
+
item.data = item.data.filter((x: any) => x);
|
|
8
|
+
if (!item.data.length) continue;
|
|
9
|
+
|
|
10
|
+
text += `<blockquote><b>${item.title}</b></blockquote>`;
|
|
11
|
+
|
|
12
|
+
item.data.forEach((x: any, i: number) => {
|
|
13
|
+
if (!x[0]) return (text += '\n');
|
|
14
|
+
else if (!x[1] && x[1] !== undefined && x[1] !== null && !isNaN(x[1])) x[1] = 'Отсутствует';
|
|
15
|
+
// else if (x[1] && Number(x[1].toString()?.replace(/\s/g, ''))) x[1] = Number(x[1].toString().replace(/\s/g, '')).toLocaleString('ru');
|
|
16
|
+
|
|
17
|
+
if ((i + 1 === item.data.length || !item.data[i + 1]?.length) && (i === 0 || item.data[i - 1]?.length)) text += '┗';
|
|
18
|
+
else if (i > 0 && !item.data[i - 1]?.length && !item.data[i + 1]?.length) text += '┃';
|
|
19
|
+
else text += '┣';
|
|
20
|
+
|
|
21
|
+
text += ' ';
|
|
22
|
+
|
|
23
|
+
if (x[0].includes('</a>') || x[0].match(/\@\S+/i)) text += x[0];
|
|
24
|
+
else text += `<b>${x[0]}</b>`;
|
|
25
|
+
|
|
26
|
+
if (x[1]) {
|
|
27
|
+
text += ': ';
|
|
28
|
+
|
|
29
|
+
if (x[1].toString().includes('</a>') || x[1].toString().match(/\@\S+/i)) text += x[1];
|
|
30
|
+
else if (x[2] === null) text += x[1];
|
|
31
|
+
else text += `<${x[2] || 'code'}>${x[1]}</${x[2] || 'code'}>`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
text += '\n';
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
text += '\n';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (footer) text += `${footer}`;
|
|
41
|
+
|
|
42
|
+
return text;
|
|
43
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2016",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./lib",
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"emitDecoratorMetadata": true,
|
|
13
|
+
"importHelpers": true
|
|
14
|
+
}
|
|
15
|
+
}
|