-
Notifications
You must be signed in to change notification settings - Fork 18
/
lil_ops.js
327 lines (309 loc) · 11 KB
/
lil_ops.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
const { store } = require('./index')
const { getPathObj, getPathNum } = require('./getPathObj')
const crypto = require('crypto');
const bs58 = require('bs58');
const hashFunction = Buffer.from('12', 'hex');
const stringify = require('json-stable-stringify');
const { postToDiscord } = require('./discord');
const config = require('./config');
const burn = (amount) => {
return new Promise((resolve, reject) => {
getPathNum(['stats', 'tokenSupply'])
.then(sup => {
store.batch([{ type: 'put', path: ['stats', 'tokenSupply'], data: sup - amount }], [resolve, reject, 1])
})
})
}
exports.burn = burn
const forceCancel = (rate, type, block_num) => {
return new Promise((resolve, reject) => {
const price = parseFloat(rate)
let Ps = getPathObj(['dex', type, 'sellOrders'])
let Pb = getPathObj(['dex', type, 'buyOrders'])
Promise.all([Ps, Pb])
.then(s => {
let gone = 0
for (o in s[0]) {
if (parseFloat(o.split(":")[0]) < (price * .6)) {
gone++
release(o.from, o.split(":")[1], block_num)
} else if (parseFloat(o.split(":")[0]) > (price * 1.4)) {
gone++
release(o.from, o.split(":")[1], block_num)
}
}
for (o in s[1]) {
if (parseFloat(o.split(":")[0]) < (price * .6)) {
gone++
release(o.from, o.split(":")[1], block_num)
} else if (parseFloat(o.split(":")[0]) > (price * 1.4)) {
gone++
release(o.from, o.split(":")[1], block_num)
}
}
resolve(gone)
})
.catch(e => { reject(e) })
})
}
exports.forceCancel = forceCancel
const reward_spk = (acc, bn) => {
return new Promise((res, rej) => {
const Pblock = getPathNum(["spkb", acc]);
const Pstats = getPathObj(["stats"]);
const Ppow = getPathNum(["pow", acc]);
const Pgranted = getPathNum(["granted", acc, "t"]);
const Pgranting = getPathNum(["granting", acc, "t"]);
const Pgov = getPathNum(["gov", acc]);
const Pspk = getPathNum(['spk', acc])
const Pspkt = getPathNum(['spk', 't'])
Promise.all([Pblock, Pstats, Ppow, Pgranted, Pgranting, Pgov, Pspk, Pspkt]).then(
(mem) => {
var block = mem[0],
diff = bn - block,
stats = mem[1],
pow = mem[2],
granted = mem[3],
granting = mem[4],
gov = mem[5],
spk = mem[6],
spkt = mem[7],
r = 0, a = 0, b = 0, c = 0, t = 0
if (!block){
store.batch(
[
{
type: "put",
path: ["spkb", acc],
data: bn,
},
],
[res, rej, 0]
);
} else if(diff < 28800){ //min claim period
res(r)
} else {
t = parseInt(diff/28800)
a = simpleInterest(gov, t, stats.spk_rate_lgov)
b = simpleInterest(pow, t, stats.spk_rate_lpow);
c = simpleInterest(
(granted + granting),
t,
stats.spk_rate_ldel
);
const i = a + b + c
if(i){
store.batch(
[
{
type: "put",
path: ["spk", acc],
data: spk + i,
},
{
type: "put",
path: ["spk", "t"],
data: spkt + i,
},
{
type: "put",
path: ["spkb", acc],
data: bn - (diff % 28800),
},
],
[res, rej, i]
);
} else {
res(0)
}
}
}
);
})
}
exports.reward_spk = reward_spk
const simpleInterest = (p, t, r) => {
const amount = p * (1 + r / 365);
const interest = amount - p;
return parseInt(interest * t);
};
const add = (node, amount) => {
return new Promise((resolve, reject) => {
store.get(['balances', node], function (e, a) {
if (!e) {
console.log(amount + ' to ' + node)
const a2 = typeof a != 'number' ? amount : a + amount
console.log('final balance ' + a2)
store.batch([{ type: 'put', path: ['balances', node], data: a2 }], [resolve, reject, 1])
} else {
console.log(e)
}
})
})
}
exports.add = add
const addc = (node, amount) => {
return new Promise((resolve, reject) => {
store.get(['cbalances', node], function (e, a) {
if (!e) {
console.log(amount + ' to ' + node)
const a2 = typeof a != 'number' ? amount : a + amount
console.log('final balance ' + a2)
store.batch([{ type: 'put', path: ['cbalances', node], data: a2 }], [resolve, reject, 1])
} else {
console.log(e)
}
})
})
}
exports.addc = addc
const addMT = (path, amount) => {
return new Promise((resolve, reject) => {
store.get(path, function (e, a) {
if (!e) {
const a2 = typeof a != 'number' ? parseInt(amount) : parseInt(a) + parseInt(amount)
console.log(`MTo:${a},add:${amount},final:${a2}`,)
store.batch([{ type: 'put', path, data: a2 }], [resolve, reject, 1])
} else {
console.log(e)
}
})
})
}
exports.addMT = addMT
const addCol = (node, amount) => {
return new Promise((resolve, reject) => {
store.get(['col', node], function (e, a) {
if (!e) {
const a2 = typeof a != 'number' ? amount : a + amount
console.log({ node, a })
store.batch([{ type: 'put', path: ['col', node], data: a2 }], [resolve, reject, 1])
} else {
console.log(e)
}
})
})
}
exports.addCol = addCol
const addGov = (node, amount) => {
return new Promise((resolve, reject) => {
store.get(['gov', node], function (e, a) {
if (!e) {
const a2 = typeof a != 'number' ? amount : a + amount
console.log({ node, a })
store.batch([{ type: 'put', path: ['gov', node], data: a2 }], [resolve, reject, 1])
} else {
console.log(e)
}
})
})
}
exports.addGov = addGov
const deletePointer = (escrowID, user) => {
return new Promise((resolve, reject) => {
const escrow_id = typeof escrowID == 'string' ? escrowID : escrowID.toString()
store.get(['escrow', escrow_id], function (e, a) {
if (!e) {
var found = false
const users = Object.keys(a)
for (i = 0; i < users.length; i++) {
if (user = users[i]) {
found = true
break
}
}
if (found && users.length == 1) {
store.batch([{ type: 'del', path: ['escrow', escrow_id] }], [resolve, reject, users.length])
} else if (found) {
store.batch([{ type: 'del', path: ['escrow', escrow_id, user] }], [resolve, reject, users.length])
}
}
})
})
}
exports.deletePointer = deletePointer
const credit = (node) => {
return new Promise((resolve, reject) => {
getPathNum(['markets', 'node', node, 'wins'])
.then(a => {
store.batch([{ type: 'put', path: ['markets', 'node', node, 'wins'], data: a++ }], [resolve, reject, 1])
})
.catch(e => {
reject(e)
})
})
}
exports.credit = credit
const nodeUpdate = (node, op, val) => {
return new Promise((resolve, reject) => {
store.get(['markets', 'node', node], function (e, a) {
if (!e) {
if (!a.strikes)
a.strikes = 0
if (!a.burned)
a.burned = 0
if (!a.moved)
a.moved = 0
switch (op) {
case 'strike':
a.strikes++
a.burned += val
break
case 'ops':
a.escrows++
a.moved += val
break
default:
}
store.batch([{ type: 'put', path: ['markets', 'node', node], data: a }], [resolve, reject, 1])
} else {
console.log(e)
resolve()
}
})
})
}
exports.nodeUpdate = nodeUpdate
const penalty = (node, amount) => {
console.log('penalty: ', { node, amount })
return new Promise((resolve, reject) => {
pts = getPathNum(['gov', node])
Promise.all([pts]).then(r => {
var a2 = r[1]
newBal = a2 - amount
if (newBal < 0) { newBal = 0 }
const forfiet = a2 - newBal
var ops = [{ type: 'put', path: ['gov', node], data: newBal }]
nodeUpdate(node, 'strike', amount)
.then(empty => {
store.batch(ops, [resolve, reject, forfiet])
})
.catch(e => { reject(e) })
}).catch(e => {
reject(e)
})
})
}
exports.penalty = penalty
const chronAssign = (block, op) => {
return new Promise((resolve, reject) => {
const t = block + ':' + hashThis(stringify(op))
store.batch([{ type: 'put', path: ['chrono', t], data: op }], [resolve, reject, t])
})
}
exports.chronAssign = chronAssign
function hashThis(data) {
const digest = crypto.createHash('sha256').update(data).digest()
const digestSize = Buffer.from(digest.byteLength.toString(16), 'hex')
const combined = Buffer.concat([hashFunction, digestSize, digest])
const multihash = bs58.encode(combined)
return multihash.toString()
}
exports.hashThis = hashThis
function isEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) return false;
}
return true
}
exports.isEmpty = isEmpty;