Skip to content

Commit 16580b6

Browse files
authored
Merge pull request #154 from bitholla/develop
Develop
2 parents d6acdb0 + f1879ed commit 16580b6

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

plugins/vault/crons/checkWithdrawals.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const vaultCoins = [];
1919

2020
const checkWithdrawals = () => {
2121
return new Promise((resolve, reject) => {
22+
loggerDeposits.info('/plugins/vault/crons/checkWithdrawals starting');
2223
each(GET_SECRETS().vault.connected_coins, (coin) => {
2324
vaultCoins.push({
2425
currency: coin
@@ -43,7 +44,7 @@ const checkWithdrawals = () => {
4344
})
4445
.then((withdrawals) => {
4546
if (withdrawals.length === 0) {
46-
loggerDeposits.info('No withdrawals need checking');
47+
loggerDeposits.info('/plugins/vault/crons/checkWithdrawals No withdrawals need checking');
4748
resolve();
4849
}
4950
let txids = {};
@@ -72,7 +73,7 @@ const checkWithdrawals = () => {
7273
.then((tx) => {
7374
if (tx.data[0]) {
7475
if (tx.data[0].is_confirmed) {
75-
loggerDeposits.info(`Transaction ${txid} was confirmed`);
76+
loggerDeposits.info('/plugins/vault/crons/checkWithdrawals checkTransaction', `Transaction ${txid} was confirmed`);
7677
return sequelize.transaction((transaction) => {
7778
return all(txids[txid].map((withdrawal) => {
7879
return withdrawal.update(
@@ -96,7 +97,7 @@ const checkWithdrawals = () => {
9697
}));
9798
});
9899
} else if (tx.data[0].is_rejected) {
99-
loggerDeposits.info(`Transaction ${txid} was rejected`);
100+
loggerDeposits.info('/plugins/vault/crons/checkWithdrawals checkTransaction', `Transaction ${txid} was rejected`);
100101
return {
101102
success: true,
102103
status: false,
@@ -110,11 +111,11 @@ const checkWithdrawals = () => {
110111
}
111112
};
112113
} else {
113-
loggerDeposits.info(`Transaction ${txid} is not processed yet`);
114+
loggerDeposits.info('/plugins/vault/crons/checkWithdrawals checkTransaction', `Transaction ${txid} is not processed yet`);
114115
return {};
115116
}
116117
} else {
117-
loggerDeposits.warn(`Transaction ${txid} is not found`);
118+
loggerDeposits.warn('/plugins/vault/crons/checkWithdrawals checkTransaction', `Transaction ${txid} is not found`);
118119
return {
119120
success: false,
120121
info: {
@@ -171,7 +172,7 @@ const checkWithdrawals = () => {
171172
}));
172173
})
173174
.then(() => {
174-
loggerDeposits.info('checkWithdrawals finished');
175+
loggerDeposits.info('/plugins/vault/crons/checkWithdrawals finished');
175176
resolve();
176177
})
177178
.catch((err) => {

plugins/vault/crons/lockWithdrawals.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const vaultCoins = [];
2121

2222
const lockWithdrawals = () => {
2323
return new Promise((resolve, reject) => {
24+
loggerDeposits.info('/plugins/vault/crons/lockWithdrawals starting');
2425
each(GET_SECRETS().vault.connected_coins, (coin) => {
2526
vaultCoins.push({
2627
currency: coin
@@ -45,25 +46,25 @@ const lockWithdrawals = () => {
4546
})
4647
.then((withdrawals) => {
4748
if (withdrawals.length === 0) {
48-
loggerDeposits.info('No withdrawals need locking');
49+
loggerDeposits.info('/plugins/vault/crons/lockWithdrawals', 'No withdrawals need locking');
4950
resolve();
5051
}
51-
loggerDeposits.debug(`Locking ${withdrawals.length} withdrawals`);
52+
loggerDeposits.debug('/plugins/vault/crons/lockWithdrawals', `Locking ${withdrawals.length} withdrawals`);
5253
return all(withdrawals.map((withdrawal) => {
5354
if (checkAddress(withdrawal.address, withdrawal.currency)) {
5455
return withdrawal.update({ processing: true }, { fields: ['processing'], returning: true })
5556
.then((result) => {
56-
loggerDeposits.info(`Withdrawal with ID ${withdrawal.id} locked`);
57+
loggerDeposits.info('/plugins/vault/crons/lockWithdrawals', `Withdrawal with ID ${withdrawal.id} locked`);
5758
return;
5859
})
5960
.catch((err) => {
60-
loggerDeposits.error(`Error occured while locking ${withdrawal.id}: ${err.message}`);
61+
loggerDeposits.error('/plugins/vault/crons/lockWithdrawals', `Error occured while locking ${withdrawal.id}: ${err.message}`);
6162
return;
6263
});
6364
} else {
6465
return withdrawal.update({ rejected: true }, { fields: ['rejected'], returning: true })
6566
.then((result) => {
66-
loggerDeposits.warn(`Withdrawal with ID ${withdrawal.id} rejected because of an invalid address`);
67+
loggerDeposits.warn('/plugins/vault/crons/lockWithdrawals', `Withdrawal with ID ${withdrawal.id} rejected because of an invalid address`);
6768
return sendEmail(
6869
MAILTYPE.INVALID_ADDRESS,
6970
withdrawal.User.email,
@@ -76,14 +77,14 @@ const lockWithdrawals = () => {
7677
);
7778
})
7879
.catch((err) => {
79-
loggerDeposits.error(`Error occured while locking ${withdrawal.id}: ${err.message}`);
80+
loggerDeposits.error('/plugins/vault/crons/lockWithdrawals', `Error occured while locking ${withdrawal.id}: ${err.message}`);
8081
return;
8182
});
8283
}
8384
}));
8485
})
8586
.then(() => {
86-
loggerDeposits.info('lockWithdrawals finished');
87+
loggerDeposits.info('/plugins/vault/crons/lockWithdrawals finished');
8788
resolve();
8889
})
8990
.catch((err) => {

plugins/vault/crons/processWithdrawals.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { each } = require('lodash');
66
const { all, delay } = require('bluebird');
77
const { VAULT_ENDPOINT, GET_CONFIGURATION, GET_SECRETS } = require('../../../constants');
88
const { loggerDeposits } = require('../../../config/logger');
9+
const mathjs = require('mathjs');
910
const VAULT_NAME = () => GET_SECRETS().vault.name;
1011
const VAULT_KEY = () => GET_SECRETS().vault.key;
1112
const VAULT_SECRET = () => GET_SECRETS().vault.secret;
@@ -17,8 +18,13 @@ const { MAILTYPE } = require('../../../mail/strings');
1718

1819
const vaultCoins = [];
1920

21+
const getAmount = (amount, fee) => {
22+
return mathjs.number(mathjs.subtract(mathjs.fraction(amount), mathjs.fraction(fee)));
23+
};
24+
2025
const processWithdrawals = () => {
2126
return new Promise((resolve, reject) => {
27+
loggerDeposits.info('/plugins/vault/crons/processWithdrawals starting');
2228
each(GET_SECRETS().vault.connected_coins, (coin) => {
2329
vaultCoins.push({
2430
currency: coin
@@ -43,7 +49,7 @@ const processWithdrawals = () => {
4349
})
4450
.then((withdrawals) => {
4551
if (withdrawals.length === 0) {
46-
loggerDeposits.info('No withdrawals need processing');
52+
loggerDeposits.info('/plugins/vault/crons/processWithdrawals', 'No withdrawals need processing');
4753
resolve();
4854
}
4955
const btcWithdrawals = [];
@@ -63,7 +69,7 @@ const processWithdrawals = () => {
6369
body: {
6470
data: {
6571
address: withdrawal.address,
66-
amount: withdrawal.amount,
72+
amount: getAmount(withdrawal.amount, withdrawal.fee)
6773
}
6874
},
6975
uri: `${VAULT_ENDPOINT}/${VAULT_WALLET('btc')}/withdraw/simple`,
@@ -88,7 +94,7 @@ const processWithdrawals = () => {
8894
body: {
8995
data: {
9096
address: withdrawal.address,
91-
amount: withdrawal.amount,
97+
amount: getAmount(withdrawal.amount, withdrawal.fee)
9298
}
9399
},
94100
uri: `${VAULT_ENDPOINT}/${VAULT_WALLET('bch')}/withdraw/simple`,
@@ -111,7 +117,7 @@ const processWithdrawals = () => {
111117
body: {
112118
data: {
113119
address: withdrawal.address,
114-
amount: withdrawal.amount,
120+
amount: getAmount(withdrawal.amount, withdrawal.fee)
115121
}
116122
},
117123
uri: `${VAULT_ENDPOINT}/${VAULT_WALLET(withdrawal.currency)}/withdraw/simple`,
@@ -139,7 +145,7 @@ const processWithdrawals = () => {
139145
data: btcWithdrawals.map((withdrawal) => {
140146
return {
141147
address: withdrawal.address,
142-
amount: withdrawal.amount
148+
amount: getAmount(withdrawal.amount, withdrawal.fee)
143149
};
144150
})
145151
},
@@ -161,7 +167,7 @@ const processWithdrawals = () => {
161167
data: bchWithdrawals.map((withdrawal) => {
162168
return {
163169
address: withdrawal.address,
164-
amount: withdrawal.amount
170+
amount: getAmount(withdrawal.amount, withdrawal.fee)
165171
};
166172
})
167173
},
@@ -191,7 +197,7 @@ const processWithdrawals = () => {
191197
.then((dbWithdrawals) => {
192198
return rp(option.data)
193199
.then((data) => {
194-
loggerDeposits.info(`${option.dbWithdrawals[0].currency} withdrawal successful`);
200+
loggerDeposits.info('/plugins/vault/crons/processWithdrawals perfromWithdrawal', `${option.dbWithdrawals[0].currency} withdrawal successful`);
195201
return {
196202
success: true,
197203
data,
@@ -201,7 +207,7 @@ const processWithdrawals = () => {
201207
});
202208
})
203209
.catch((err) => {
204-
loggerDeposits.error(`${option.dbWithdrawals[0].currency} withdrawal failed: ${err.message}`);
210+
loggerDeposits.error('/plugins/vault/crons/processWithdrawals perfromWithdrawal', `${option.dbWithdrawals[0].currency} withdrawal failed: ${err.message}`);
205211
return {
206212
success: false,
207213
info: {
@@ -236,7 +242,7 @@ const processWithdrawals = () => {
236242
}));
237243
})
238244
.catch((err) => {
239-
loggerDeposits.error(`Failed to update successful ${result.dbWithdrawals[0].currency} withdrawal's TXID. ID:${result.dbWithdrawals.map((wd) => wd.id)}, TXID:${result.data.txid}, Error: ${err.message}`);
245+
loggerDeposits.error('/plugins/vault/crons/processWithdrawals txid update', `Failed to update successful ${result.dbWithdrawals[0].currency} withdrawal's TXID. ID:${result.dbWithdrawals.map((wd) => wd.id)}, TXID:${result.data.txid}, Error: ${err.message}`);
240246
return {
241247
success: false,
242248
info: {
@@ -269,7 +275,7 @@ const processWithdrawals = () => {
269275
}));
270276
})
271277
.then(() => {
272-
loggerDeposits.info('processWithdrawals finished');
278+
loggerDeposits.info('/plugins/vault/crons/processWithdrawals finished');
273279
resolve();
274280
})
275281
.catch((err) => {

plugins/vault/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"dependencies": {
55
"multicoin-address-validator": "0.4.1",
6-
"node-cron": "2.0.3"
6+
"node-cron": "2.0.3",
7+
"mathjs": "3.20.2"
78
}
89
}

0 commit comments

Comments
 (0)