forked from abrkn/tipmebch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
117 lines (89 loc) · 2.8 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const assert = require('assert');
const via = require('viacore-lib');
const numeral = require('numeral');
const qr = require('qr-image');
const BigNumber = require('bignumber.js');
const { sample } = require('lodash');
const stickers = require('./stickers');
const THREE_TICKS = '```';
exports.formatNumber = (value, format) => numeral(value).format(format);
const printError = (...args) => console.error(...args);
exports.printError = printError;
exports.formatVia = _ => {
if (+_ < 1) {
return _.toString()+" VIA";
}
return `${numeral(_.toString()).format('0,0[.00000000]')} VIA`;
};
exports.formatUsd = _ => {
if (+_ < 0.01) {
return '< $0.01';
}
return `$` + numeral(_.toString()).format('0,0.00');
};
exports.swallowError = e => {
printError(e.stack);
return 'Error';
};
exports.randomIntFromInterval = (min, max) =>
Math.floor(Math.random() * (max - min + 1) + min);
exports.createQrCode = text =>
new Promise((resolve, reject) => {
resolve(qr.imageSync(text, { size: 7 }));
});
const n = (..._) => new BigNumber(..._);
const isValidTelegramUserIdFormat = _ => {
console.log(_);
return typeof _ === 'string' && _.match(/^[0-9]+$/);
};
exports.isValidTelegramUserIdFormat = isValidTelegramUserIdFormat;
exports.extractUserDiscordIdFromTag = _ => {
const match = _.match(/^<@!?([0-9]+)>$/);
if (!match) {
return null;
}
return match[1];
};
const VIACOIN_BASE58_ADDRESS_REGEX = /^[V][a-km-zA-HJ-NP-Z1-9]{25,34}$/;
exports.internalViaAddressToStandard = (address, withPrefix = false) => {
assert(address.match(VIACOIN_BASE58_ADDRESS_REGEX));
const viaAddress = new via.Address(address);
return viaAddress.toString(via.Address.CashAddrFormat);
};
exports.viaAddressToInternal = address => {
if (address.match(VIACOIN_BASE58_ADDRESS_REGEX)) {
return address;
}
const viaAddress = via.Address.fromString(
address,
'livenet',
'pubkeyhash',
via.Address.CashAddrFormat
);
return viaAddress.toString(via.Address.LegacyFormat);
};
const getUserAccount = id => `telegram-${id}`;
const hasTooManyDecimalsForSats = (value, decimals) =>
!n(n(value).toFixed(8)).eq(n(value));
exports.getAddressForUser = async (userId, { fetchRpc }) => {
console.log('wat', userId, typeof userId);
assert(isValidTelegramUserIdFormat(userId));
return await fetchRpc('getaccountaddress', [getUserAccount(userId)]);
};
const maybeReplyFromStickerSet = (ctx, setName, stickerName) => {
const set = stickers[setName];
assert(set, `Sticker set ${setName} not found`);
const variants = set[stickerName];
const stickerId = sample(variants);
if (!stickerId) {
return;
}
return ctx.replyWithSticker(stickerId);
};
Object.assign(exports, {
THREE_TICKS,
hasTooManyDecimalsForSats,
n,
maybeReplyFromStickerSet,
});
exports.getUserAccount = getUserAccount;