-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
347 lines (290 loc) · 8.45 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
var exports = module.exports = {};
var FAIL_EMOJI = "❌"
var SUCCESS_EMOJI = "✅"
const base64 = require('js-base64');
const Discord = require('discord.js');
const BigNumber = require('bignumber.js')
const lux = require('luxon')
const DateTime = lux.DateTime
const c = require('./c.json')
const config = require('./config.json')
const backendURL = config.blockURL
const nevmExplorerURL = config.nevm.explorerURL;
const sjs = require('syscoinjs-lib')
const BN = sjs.utils.BN
const db = require('./db.js')
// change the precision of a bignumber
exports.toWholeUnit = function(number, precision) {
precision *= -1
return number.shiftedBy(precision)
}
// change the precision of a bignumber
exports.toSats = function(number, precision) {
return number.shiftedBy(precision)
}
// counts the number of decimals a number string has
exports.decimalCount = function(numString) {
return (numString.split('.')[1] || []).length;
}
// converts a bignumber.js number to bn.js (used by syscoinjs-lib)
exports.bigNumberToBN = function(bigNumber) {
return new BN(bigNumber.toString())
}
// checks if the writer of the message has the Admin role
exports.checkAdminRole = function(message) {
try {
if (!message.member.roles.cache.has(config.AdminRoleID)) {
return false
} else {
return true
}
} catch (error) {
console.log(error)
message.channel.send({embed: { color: c.FAIL_COL, description: "Error checking Mission role."}});
return false
}
}
exports.checkMissionRunnerRole = function(message) {
try {
if (!message.member.roles.cache.has(config.MissionRunnerRoleId)) {
return false
} else {
return true
}
} catch (error) {
console.log(error)
message.channel.send({embed: { color: c.FAIL_COL, description: "Error checking MissionRunner role."}});
return false
}
}
// checks if the writer of the message has the Mission role
exports.checkMissionRole = function(message) {
try {
if (!message.member.roles.cache.has(config.MissionRoleID)) {
return false
} else {
return true
}
} catch (error) {
console.log(error)
message.channel.send({embed: { color: c.FAIL_COL, description: "Error checking Mission role."}});
return false
}
}
// checks if a token exists on the backend based on the guid
exports.getSPT = async function getSPT(guid) {
try {
var token = await sjs.utils.fetchBackendAsset(backendURL, guid)
if (token instanceof Error) {
return null
} else {
return token
}
} catch (error) {
console.log(error)
return null
}
}
// returns the number of decimals of the given cryptocurrency
exports.getDecimals = async function(tokenStr) {
try {
if (tokenStr !== "SYS") {
var token = await exports.getSPT(tokenStr)
if (token instanceof Error) {
return false
}
return token.decimals
} else {
return 8
}
} catch (error) {
console.log(error)
return error
}
}
// returns a string identifying the cryptocurrency (either GUID or SYS)
exports.getCurrencyStr = async function(tokenStr) {
try {
if (tokenStr !== "SYS") {
var token = await exports.getSPT(tokenStr)
if (token instanceof Error) {
return false
}
return token.assetGuid
} else {
return "SYS"
}
} catch (error) {
console.log(error)
return error
}
}
// converts from the given time unit to milliseconds
exports.convertToMillisecs = function(time, unit) {
unit = unit.toUpperCase()
switch (unit) {
case "D":
time = time.times(24)
case "H":
time = time.times(60)
case "M":
time = time.times(60)
case "S":
return time.times(1000)
break
}
}
// converts from milliseconds to minutes
exports.fromMilliToMins = function(timeIn) {
// seconds
timeIn = timeIn.dividedBy(1000)
// minutes
timeIn = timeIn.dividedBy(60)
timeOb.time = timeIn
var timeOb = {
time: timeIn,
unit: "m"
}
return timeOb
}
// returns whether the given balance has enough in it to send the given amount
// expects a db Balance, and a number/string amount
exports.hasEnoughBalance = function(balance, amount) {
if (!balance) {
return false
}
var amountToSend = new BigNumber(amount)
var balanceAmount = new BigNumber(balance.amount)
var availableAmount = balanceAmount.minus(balance.lockedAmount)
if (availableAmount.lt(amountToSend)) {
return false
}
if (availableAmount.isNaN()) {
console.log("Available amount is NaN. Probably issue with lockedAmount")
console.log(balance)
return false
}
return true
}
// returns a hyperlink to the specified blockbook address/token/tx
exports.getExpLink = async function(data, type, title) {
try {
var url
switch (type) {
case c.ADDRESS:
url = `[${title}](${backendURL}/address/${data})`
break
case c.TOKEN:
var tok = await sjs.utils.fetchBackendAsset(backendURL, data)
if (tok instanceof Error) {
console.log("Error getting token")
console.log(data)
return false
}
var currencyStr = base64.decode(tok.symbol) + " (" + tok.assetGuid + ")"
currencyStr = currencyStr.toUpperCase()
url = `[${currencyStr}](${backendURL}/asset/${data})`
break
case c.TX:
url = `[${title}](${backendURL}/tx/${data})`
break
}
return url
} catch (error) {
console.log(error)
return error
}
}
/**
*
* @param {string} data wallet address or token contract address or transaction hash
* @param {('address'|'token'|'transaction')} type
* @param {string} title
* @returns
*/
exports.getNevmExplorerLink = function (data, type, title) {
switch (type) {
case "address":
return `[${title}](${nevmExplorerURL}/address/${data})`;
case "token":
return `[${title}](${nevmExplorerURL}/token/${data})`;
case "transaction":
return `[${title}](${nevmExplorerURL}/tx/${data})`;
}
};
// unlocks an amount in a user's balance (locking used in auctions/trades
// to ensure users can't move their locked funds to cheat the system)
exports.unlockAmount = async function(currency, userID, amount) {
var balance = await db.getBalance(userID, currency)
var currentLocked = new BigNumber(balance.lockedAmount)
var amountBig = new BigNumber(amount)
var newLocked = currentLocked.minus(amountBig)
return db.editBalanceLocked(userID, currency, newLocked)
}
// deletes the message after the given delay in milliseconds
exports.deleteMsgAfterDelay = function(msg, tOut) {
msg.delete({timeout: tOut})
}
// reacts to a given message with the specified success/failure emoji
exports.isSuccessMsgReact = function(isSuccess, message) {
if (message && message !== undefined) {
if (isSuccess) {
message.react(SUCCESS_EMOJI)
} else {
message.react(FAIL_EMOJI)
}
}
}
// checks if the correct number of arguments have been provided
exports.hasAllArgs = function(args, numOfArgs) {
return args.length >= numOfArgs
}
// returns the remaining time between now and the end date
exports.getRemainingTime = function(endDate) {
var now = lux.DateTime.now()
var end = lux.DateTime.fromISO(endDate.toISOString())
return end.diff(now, ['days', 'hours', 'minutes', 'seconds'])
}
// returns the elapsed time between the past date and now
exports.getElapsedTime = function(endDate) {
var now = lux.DateTime.now()
var end = lux.DateTime.fromISO(endDate.toISOString())
return now.diff(end, ['days', 'hours', 'minutes', 'seconds'])
}
// returns a string with days/hours/minutes/seconds remaining until the endDate
exports.getTimeDiffStr = function(endDate, past) {
var diff
if (past) {
diff = exports.getElapsedTime(endDate)
} else {
diff = exports.getRemainingTime(endDate)
}
var timeLeft = ""
if (diff.values.days > 0) {
timeLeft += `${diff.values.days} day(s), `
}
if (diff.values.hours > 0) {
timeLeft += `${diff.values.hours} hour(s), `
}
if (diff.values.minutes > 0) {
timeLeft += `${diff.values.minutes} minute(s), `
}
if (diff.values.seconds > 0) {
timeLeft += `${diff.values.seconds.toFixed(0)} second(s)`
}
return timeLeft
}
exports.createNFTEmbed = async function(guid, color, desc, isThumbnail) {
var embed = new Discord.MessageEmbed()
.setColor(color)
.setDescription(desc)
var dbSPT = await db.getSPT(guid)
if (dbSPT && dbSPT.linkToNFT) {
if (isThumbnail) {
embed.setThumbnail(dbSPT.linkToNFT)
} else {
embed.setImage(dbSPT.linkToNFT)
}
}
return embed
}