-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_deposit_backup.js
246 lines (218 loc) · 9.63 KB
/
check_deposit_backup.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
const config = require('./config/database')
const User = require('./models/user')
const Coin = require('./models/coin')
const mongoose = require('mongoose')
const bitcoin_rpc = require('node-bitcoin-rpc')
const pool_params = require('./config/pool_params')
const DepositRequest = require('./models/deposit_request')
const DepositStatus = require('./models/deposit_status')
const axios = require('axios');
var cron2 = require('node-cron');
mongoose.connection.on('connected', () => {
console.log("connected to database : ", config.databse)
})
mongoose.connection.on('error', (err) => {
console.log("database error : ", err)
})
function getrawtransactionOneOne(transactions, senders) {
return new Promise(function (resolve, reject) {
axios.get('https://explorer.hostmasternode.com/insight-api/tx/' + transactions.txid)
.then(async (response) => {
//console.log("response from explorer is ",response)
console.log("address of the sender from explorer is ",response.data.vin[0].addr)
resolve({ sender: response.data.vin[0].addr, receivedaddress: transactions.address, amount: transactions.amount })
})
.catch(error => {
reject(error)
});
})
}
function getrawtransactionOne(res1) {
return new Promise(function (resolve, reject) {
bitcoin_rpc.call('getrawtransaction', [res1.result.vin[0].txid, res1.result.vin[0].vout], (err1, res1) => {
if (err1) {
reject(err1)
}
resolve({ sender: res1.result.vout[res1.result.vout.length - 1].scriptPubKey.addresses, receivedaddress: transactions.address, amount: transactions.amount })
})
})
}
function checkSender(hostname, rpc_port, rpc_user, rpc_password, request) {
return new Promise(function (resolve, reject) {
bitcoin_rpc.init(hostname, rpc_port, rpc_user, rpc_password)
bitcoin_rpc.call('listtransactions', [], async (err, res) => {
var senders = []
if (err !== null) {
console.log("error from listtransactions ", err)
reject(err)
}
else if (res.error) {
console.log("error from listtransacions res.error ",res.error)
reject(res.error)
}
else {
//console.log("there is no error from listttransactions ",res)
for (transactions of res.result) {
console.log("enter in transactions list table and transaction category is: ",transactions.category)
if (transactions.category == "receive") {
try {
console.log("transactions.address receive category : ",transactions.address)
var result = await getrawtransactionOneOne(transactions)
senders.push(result)
} catch (error) {
console.log(error)
}
}
}
resolve(senders)
}
})
})
}
function checkANdUpdatePromise() {
return new Promise(function (resolve, reject) {
var allSernders = []
var pendingRequest = []
DepositRequest.aggregate([
{ $unwind: "$status" },
{
$group: {
_id: { address: "$address", coin: "$coin" },
lastStatus: { $last: "$status.dateOfSubscription" },
count: { $sum: 1 },
allstatus: { $push: { status: "$status.status", dateofstatus: "$status.dateOfSubscription", amount: "$amount" } }
}
}])
.exec(async function (err, data2) {
if (err) {
reject(err)
} else if (data2.length == 0) {
reject("no status find")
} else {
for (request of data2) {
for (let i = 0; i < request.allstatus.length; i++) {
if (request.allstatus[i].status == "pending" && request.allstatus[i].dateofstatus.getTime() == request.lastStatus.getTime()) {
pendingRequest.push(request)
for (credentials of pool_params.COINS_CREDENTIALS) {
if (credentials.name == request._id.coin) {
try {
tab = await checkSender(credentials.hostname, credentials.rpc_port, credentials.rpc_user, credentials.rpc_password, request)
allSernders.push(tab)
} catch (error) {
reject(error)
}
}
}
}
}
}
resolve({ success: true, allSernders: allSernders, pendingRequest: pendingRequest })
}
})
})
}
function updateStatus(pending, sender) {
return new Promise(function (resolve, reject) {
DepositRequest.findOne({ address: pending._id.address, coin: pending._id.coin }, function (err, data) {
if (err) {
console.log("err ", err)
reject({ success: false, message: err });
} else if (!data) {
console.log("no field ", data)
reject({ success: false, message: "no field " });
} else {
const depositstatus = new DepositStatus({
status: "complete",
dateOfSubscription: Date.now()
})
depositstatus.amount = sender.amount
data.status.push(depositstatus)
const latestdate = new Date(Math.max.apply(null, data.status.map(function (e) {
return new Date(e.dateOfSubscription);
})));
//console.log("latest date of neweest status is ", latestdate)
data.save((err,doc)=>{
if(err) resolve({success:false,error:err});
resolve({
success:true,
user:doc
})
})
resolve(data)
}
});
})
}
function addParticipant(pending) {
return new Promise(function (resolve, reject) {
const newAddress = {
owner: pending._id.address,
address: pending._id.address
}
const participant = {
address: newAddress,
amount: pending.allstatus[pending.allstatus.length - 1].amount,
type: pending._id.type
}
Coin.findOne({ name: pending._id.coin }, function (err, data) {
if (err) {
console.log("err ", err)
reject({ success: false, message: err });
} else if (!data) {
console.log("no field ", data)
reject({ success: false, message: "no field " });
} else {
data.participants.push(participant)
data.save((err, user) => {
if (err) {
console.log('failed to add coin ', err)
reject({ success: false, msg: 'failed to add coin ', err })
} else {
console.log('coin added')
resolve({ success: true, msg: 'coin added' })
}
}
)
}
});
})
}
var tasks = cron2.schedule('* * * * *', async() => {
try {
updatesSenders = await checkANdUpdatePromise()
console.log("this is the final senders table: ", updatesSenders)
for (let pending of updatesSenders.pendingRequest) {
const addr = pending._id.address
console.log("allsenders = ", updatesSenders.allSernders)
for (let sender of updatesSenders.allSernders) {
console.log("sender are ",sender)
if (sender != undefined) {
for (senderitem in sender) {
if (pending._id.address == sender[senderitem].sender && pending.allstatus[pending.allstatus.length - 1].amount <= sender[senderitem].amount) {
console.log("pending._id.address == sender[senderitem].sender[0] ",pending._id.address," == ",sender[senderitem].sender)
try {
const resultofupdatesstatus = await updateStatus(pending, sender[senderitem])
try {
const resultOfAddingParticipant = await addParticipant(pending)
} catch (error) {
console.log(error)
}
//console.log("request need to be modified : ",resultofupdatesstatus)
} catch (error) {
console.log(error)
}
}
}
}
}
}
} catch (error) {
console.log(error)
}
console.log('checkANdUpdate running a task every minute');
});
module.exports = async function checkANdUpdate() {
console.log("checkANdUpdate task bech tranni")
//tasks.start();
// tasks.destroy();
}