-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
129 lines (115 loc) · 4.03 KB
/
index.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
var fs = require("fs")
var hive = require("@hiveio/hive-js")
var SSC = require("sscjs")
var config = require("./config.js").config
var path = require('path').dirname(require.main.filename)
const ssc = new SSC('https://api.hive-engine.com/rpc')
function genList(callback){
fs.readFile(`${path}/ToSend.txt`, (err, data) => {
var csv = data.toString().toLowerCase()
callback(csvFormattor(csv))
})
}
//Looks for accounts that aren't hive accounts.
function processList(callback){
genList(list => {
var keys = Object.keys(list)
var found = []
var diff = []
hive.api.getAccounts(keys, function(err, result) {
for (i in result){
found.push(result[i].name)
}
for (i in keys){
if (!found.includes(keys[i])){
diff.push(keys[i])
}
}
callback(diff)
})
})
}
function send(callback){
//Check for mode being issue or transfer
if (config.mode.toLowerCase() != "issue" && config.mode.toLowerCase() != "transfer"){
callback("Please only use issue or transfer in the mode in config.")
return
}
//Check for error
processList(diff => {
if (diff.length){
callback("Error, Use `airdrop processList` to see what users don't exist.")
return
}
var c = 0
getAccountBalance(balance => {
genList(list => {
if (balance >= getTotalToSend(list) || config.mode.toLowerCase() == "issue"){
var keys = Object.keys(list)
console.log(`Starting...\nEstimated Time To Completion is ${keys.length * 10} seconds.`)
var sending = setInterval(() => {
var sendJSON = {"contractName":"tokens","contractAction":config.mode.toLowerCase() ,"contractPayload":{"symbol": config.tokenSymbol,"to": keys[c],"quantity": list[keys[c]],"memo":"Test"}}
if (keys[c]){
hive.broadcast.customJson(config.accountPrivateActiveKey, [config.accountName], null, "ssc-mainnet1", JSON.stringify(sendJSON), function(err, result) {
if (!err){
console.log(`Sent ${list[keys[c]]} to ${keys[c]}.`)
c++
} else {
console.log(`Error sending ${list[keys[c]]} to ${keys[c]}.`)
c++
}
})
} else {
clearInterval(sending)
callback("Done")
}
} , 10 * 1000)
} else {
callback(`The account only has ${balance} while the total needed to send is ${getTotalToSend(list)}.`)
}
})
})
})
}
//Returns CSV as json
function csvFormattor(csv){
var unFormattedlines = csv.split("\n")
var lines = []
for (i in unFormattedlines){
if (unFormattedlines[i] != ""){
lines.push(unFormattedlines[i])
}
}
var res = {}
for (i in lines){
var current = lines[i]
var split = current.split(",")
res[split[0]] = split[1]
}
return res
}
//Gets balance of account with the token
function getAccountBalance(callback){
ssc.findOne('tokens','balances', { account: config.accountName, symbol : config.tokenSymbol}, (err, result) => {
if (err || !result) {
callback(0)
} else {
var balance = parseFloat(result.balance)
callback(balance)
}
})
}
//Gets total amount to send to people.
function getTotalToSend(list){
var c = 0
for (i in list){
c = c + parseFloat(list[i])
}
return c
}
module.exports = {
genList : genList,
processList : processList,
send : send,
getAccountBalance : getAccountBalance
}