forked from syscoin/tipbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withdraws.js
211 lines (185 loc) · 7.24 KB
/
withdraws.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
var exports = module.exports = {};
const com = require('./commandUsage.json')
const c = require('./c.json')
const config = require('./config.json')
var prefix = config.prefix
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')
const backendURL = config.blockURL
var HDSigner, syscoinjs
const sjs = require('syscoinjs-lib')
const BN = sjs.utils.BN
function reverseBuffer (buffer) {
if (buffer.length < 1) return buffer
let j = buffer.length - 1
let tmp = 0
for (let i = 0; i < buffer.length / 2; i++) {
tmp = buffer[i]
buffer[i] = buffer[j]
buffer[j] = tmp
j--
}
return buffer
}
function getTxId(psbt) {
var hashBuf = psbt.extractTransaction().getHash()
var reversed = reverseBuffer(hashBuf)
return reversed.toString('hex')
}
// used to send a tx onchain
async function sendOnchain(sendTo, amount, currency) {
try {
const xpub = HDSigner.getAccountXpub()
const changeAddress = await HDSigner.getNewChangeAddress()
var txOpts
if (currency !== "SYS") {
txOpts = { rbf: false }
} else {
txOpts = { rbf: true }
}
const feeRate = new BN(10)
var txResult
var sentResult
if (currency === "SYS") {
let outputsArr = [
{address: sendTo, value: amount}
]
try {
txResult = await syscoinjs.createTransaction(txOpts, changeAddress, outputsArr, feeRate, xpub)
sentResult = await syscoinjs.signAndSend(txResult.res, null, HDSigner)
return sentResult
} catch (error) {
console.log(error)
console.log("failed")
return
}
} else {
const assetMap = new Map([
[currency, { outputs: [{ address: sendTo, value: amount}], changeAddress: changeAddress }]
])
try {
txResult = await syscoinjs.assetAllocationSend(txOpts, assetMap, changeAddress, feeRate, xpub)
sentResult = await syscoinjs.signAndSend(txResult.res, null, HDSigner)
return sentResult
} catch (error) {
console.log(error)
return
}
}
} catch (error) {
console.log(error)
return
}
}
// withdraws the specified amount of the given cryptocurrency
/**
* command: !withdraw [amount] [symbol/guid]
* args
* 0 - amount, 1 - symbol/guid
*/
exports.withdraw = async function(args, message, client, signer, sysjs) {
try {
HDSigner = signer
syscoinjs = sysjs
var myProfile = await db.getProfile(message.author.id)
if (!myProfile) {
message.channel.send({embed: { color: c.FAIL_COL, description: `You must be a registered user on the tipbot to perform this action. Use the !register command to register.`}})
return
}
if (!utils.hasAllArgs(args, 3)) {
message.channel.send({embed: { color: c.FAIL_COL, description: `Missing information. Usage: ${config.prefix}` + com.withdraw}})
return
}
if (myProfile.restricted) {
message.channel.send({embed: { color: c.FAIL_COL, description: "<@" + message.author.id + "> Sorry, your account has been restricted. Please contact a member of the Syscoin Team."}})
return
}
var user = await client.users.fetch(message.author.id)
var txResult
if (args[0] == undefined || args[1] == undefined || args[2] == undefined) {
message.channel.send({embed: { color: c.FAIL_COL, description: `Usage: ${prefix}withdraw [address] [amount] [symbol/guid]`}})
return
} else {
var myBalance
var currencyID = args[2].toUpperCase()
var value = args[1]
var sendTo = args[0]
var spt = await db.getSPT(currencyID)
if (spt) {
currencyID = spt.guid
}
myBalance = await db.getBalance(message.author.id, currencyID)
if (!myBalance) {
message.channel.send({embed: { color: c.FAIL_COL, description: "You don't have a registered balance for this currency."}})
return
}
const backendAccount = await sjs.utils.fetchBackendAccount(backendURL, sendTo)
if (backendAccount instanceof Error) {
message.channel.send({embed: { color: c.FAIL_COL, description: "The address you've provided is invalid."}})
return
}
var decimals = 8
if (currencyID !== "SYS") {
let token = await sjs.utils.fetchBackendAsset(backendURL, currencyID)
decimals = token.decimals
}
let myBalanceAmount = new BN(myBalance.amount)
var withdrawAmount, withdrawSat, withdrawWhole
if (myBalance) {
if (value == "all") {
withdrawAmount = new BN(myBalance.amount)
withdrawWhole = new BigNumber(withdrawAmount).decimalPlaces(decimals, 1)
} else {
// make sure the amount can't have a higher precision than is supported
withdrawWhole = new BigNumber(value).decimalPlaces(decimals, 1)
if (withdrawWhole.isNaN() || !withdrawWhole.gt(0)) {
message.channel.send({embed: { color: c.FAIL_COL, description: "The value you are trying to withdraw must be a valid number more than 0."}})
return
}
if (withdrawWhole.lt(config.tipMin)) {
message.channel.send({embed: { color: c.FAIL_COL, description: `The value you are trying to withdraw is too small, it must be more than ${config.tipMin}.`}})
return
}
withdrawSat = utils.toSats(withdrawWhole, decimals)
withdrawAmount = utils.bigNumberToBN(withdrawSat)
}
var enoughBalance = utils.hasEnoughBalance(myBalance, withdrawSat)
if (!enoughBalance) {
message.channel.send({embed: { color: c.FAIL_COL, description: "Sorry, you cannot withdraw more than is available in your balance."}})
return
}
// send tx onchain, if tx is successful then edit the user's balances, and log the action
txResult = await sendOnchain(sendTo, withdrawAmount, currencyID)
let txid = getTxId(txResult)
if (txid) {
let updatedBalance = myBalanceAmount.sub(withdrawAmount)
let newBalance = await db.editBalanceAmount(message.author.id, currencyID, updatedBalance)
let link = await utils.getExpLink(txid, c.TX, "Click here to see the transaction.")
user.send({embed: { color: c.SUCCESS_COL, description: `Your withdrawal was successful!\n ${link}`}})
var sendArr = []
sendArr.push(sendTo)
var actionStr = `Withdraw: ${withdrawWhole.toString()} ${currencyID} | txid: ${txid}`
try {
let log = await db.createLog(message.author.id, actionStr, sendArr, withdrawSat.toString())
} catch (error) {
console.log("Error creating withdraw log")
console.log(error)
}
console.log(actionStr)
} else {
user.send({embed: { color: c.FAIL_COL, description: `Your withdrawal failed. Please contact a member of the Syscoin Team.`}})
}
} else {
message.channel.send({embed: { color: c.FAIL_COL, description: `Can't find your balance. Try registering with ${prefix}register first and depositing some crypto.`}})
return
}
}
} catch (error) {
console.log(error)
message.channel.send({embed: { color: c.FAIL_COL, description: `Error withdrawing.`}})
return
}
}