-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtips.js
225 lines (193 loc) · 8.36 KB
/
tips.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
var exports = module.exports = {};
var SYS_EMOJI = ":boom:"
var GEN_EMOJI = ":zap:"
const com = require('./commandUsage.json')
const c = require('./c.json')
const config = require('./config.json')
var prefix = config.prefix
const base64 = require('js-base64');
const sjs = require('syscoinjs-lib')
const backendURL = config.blockURL
const BigNumber = require('bignumber.js')
BigNumber.config({ DECIMAL_PLACES: 8 })
BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
const db = require('./db.js')
const utils = require('./utils.js')
// sends a tip of the given amount of cryptocurrency from one user to another
// used by trades/missions/auctions/giveaways to make transfers between users
/**
* command: !tip @user [amount] [symbol/guid]
* args
* 0 - [amount], 1 - [symbol/guid]
*/
exports.tipUser = async function(args, fromProfile, toProfile, type, client, message) {
try {
if (args && !args[1].isNaN()) {
var token
var isToken = false
let tipCurrency = args[2]
let currencyStr = ""
let decimals = 8
let emoji = SYS_EMOJI
var msgChanOrUser
var discUser = await client.users.fetch(fromProfile.userID)
if (!discUser) {
console.log(`User ${fromProfile.userID} not found.`)
return false
}
// if there's no message, i.e. called by another function instead of !tip command
// then message the sender directly
if (!message) {
msgChanOrUser = discUser
} else {
msgChanOrUser = message.channel
}
if (fromProfile.userID === toProfile.userID) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: `You can't send to yourself.`}})
utils.isSuccessMsgReact(false, message)
return false
}
if (utils.decimalCount(args[1].toString()) > config.tipMaxDecimals) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: `You can only send tips with a maximum of ${config.tipMaxDecimals} decimal places.`}})
utils.isSuccessMsgReact(false, message)
return false
}
// set up the currency strings and get the decimals for converting between
// wholeUnit and sats later on
if (args[2] == undefined) {
tipCurrency = "SYS"
currencyStr = "SYS"
} else {
tipCurrency = args[2].toUpperCase()
if (tipCurrency !== "SYS") {
var token;
let verifiedSPTLink = await db.getSPT(tipCurrency)
if (verifiedSPTLink) {
token = await utils.getSPT(verifiedSPTLink.guid)
} else {
token = await utils.getSPT(tipCurrency)
}
if (!token) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: `Couldn't find the token: ${tipCurrency}. Please ensure you entered the symbol/GUID correctly.`}})
return false
}
isToken = true
decimals = token.decimals
tipCurrency = token.assetGuid
var symbol = base64.decode(token.symbol).toUpperCase()
var tokenStr = `${symbol} (${token.assetGuid})`
currencyStr = await utils.getExpLink(token.assetGuid, c.TOKEN, tokenStr)
emoji = GEN_EMOJI
} else {
tipCurrency = "SYS"
currencyStr = "SYS"
}
}
// make sure the tip can't have more decimals than is possible or supported by
// the tipbot
if (utils.decimalCount(args[1].toString()) > decimals) {
if (decimals > 0) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: `You are trying to use too many decimals for the ${currencyStr} amount. It can't have any more than ${decimals} decimals.`}})
} else {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: `${currencyStr} is a non-divisible token. It can't have any decimals.`}})
}
utils.isSuccessMsgReact(false, message)
return false
}
let fromProfileBalance = await db.getBalance(fromProfile.userID, tipCurrency)
let toProfileBalance = await db.getBalance(toProfile.userID, tipCurrency)
if (!fromProfile) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: "You must first register with the tipbot and deposit crypto to send funds."}})
utils.isSuccessMsgReact(false, message)
return false
}
if (!toProfile) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: "The user you are trying to send funds to is not registered with the tipbot."}})
utils.isSuccessMsgReact(false, message)
return false
}
if (!fromProfileBalance) {
msgChanOrUser.send({embed: { color: 16776960, description: "You do not have a registered balance for this currency."}})
utils.isSuccessMsgReact(false, message)
return false
}
if (!toProfileBalance) {
toProfileBalance = await db.createBalance(toProfile.userID, tipCurrency, "0")
}
let fromBalanceAmount = new BigNumber(fromProfileBalance.amount)
let toBalanceAmount = new BigNumber(toProfileBalance.amount)
// make sure the tip amount can't have a higher precision than is supported
let tipWhole = args[1].decimalPlaces(decimals, 1)
if (tipWhole.isNaN()) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: "You haven't entered a valid number for the amount to send."}})
utils.isSuccessMsgReact(false, message)
return false
}
if (tipWhole.lte(0)) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: "Amount to send must be more than 0."}})
utils.isSuccessMsgReact(false, message)
return false
}
var tipInSats = utils.toSats(tipWhole, decimals)
var enoughBalance
if (type !== c.TRADE && type !== c.AUCTION) {
enoughBalance = utils.hasEnoughBalance(fromProfileBalance, tipInSats)
} else {
enoughBalance = fromBalanceAmount.gte(tipInSats)
}
if (!enoughBalance) {
msgChanOrUser.send({embed: { color: c.FAIL_COL, description: `<@${fromProfile.userID}> you don't have enough ${currencyStr}. Please deposit more to continue.`}})
utils.isSuccessMsgReact(false, message)
return false
}
// calculate the new balances and update them in the db
let fromBalanceUpdated = fromBalanceAmount.minus(tipInSats)
let toBalanceUpdated = toBalanceAmount.plus(tipInSats)
let fromBalance = await db.editBalanceAmount(fromProfile.userID, tipCurrency, fromBalanceUpdated)
let toBalance = await db.editBalanceAmount(toProfile.userID, tipCurrency, toBalanceUpdated)
var newAmountTo = utils.toWholeUnit(toBalanceUpdated, decimals)
var newAmountFrom = utils.toWholeUnit(fromBalanceUpdated, decimals)
client.users.fetch(toProfile.userID).then((usermsg) => {
usermsg.send({embed: { color: c.SUCCESS_COL, description: `${emoji} <@${fromProfile.userID}> sent you **${tipWhole}** ${currencyStr}! ${emoji}\n Your new balance: ${newAmountTo.toString()} ${currencyStr}` }})
});
client.users.fetch(fromProfile.userID).then((usermsg) => {
usermsg.send({embed: { color: c.SUCCESS_COL, description: `${emoji} You sent <@${toProfile.userID}> **${tipWhole}** ${currencyStr}! ${emoji}\n Your new balance: ${newAmountFrom.toString()} ${currencyStr}` }})
});
if (type === c.GENERAL) {
message.channel.send({embed: { color: c.SUCCESS_COL, description: `${emoji} <@${fromProfile.userID}> sent **${tipWhole.toString()}** ${currencyStr} to <@${toProfile.userID}>! ${emoji}`}});
}
var typeStr = ""
switch (type) {
case c.GENERAL:
typeStr = "GENERAL"
break
case c.AUCTION:
typeStr = "AUCTION"
break
case c.TRADE:
typeStr = "TRADE"
break
case c.MISSION:
typeStr = "MISSION"
break
case c.GIVEAWAY:
typeStr = "GIVEAWAY"
break
case c.EXCHANGE:
typeStr = "EXCHANGE"
break
default:
typeStr = "GENERAL"
break
}
var actionStr = `${typeStr} Tip: ${fromProfile.userID} | ${tipWhole.toString()} ${tipCurrency} to ${toProfile.userID}`
console.log(actionStr)
let log = await db.createLog(fromProfile.userID, actionStr, [toProfile.userID], tipWhole.toString())
utils.isSuccessMsgReact(true, message)
return true
}
} catch (error) {
console.log(error)
return false
}
}