Skip to content

Commit d6acdb0

Browse files
authored
Merge pull request #152 from bitholla/develop
Develop
2 parents 7328121 + 93b0130 commit d6acdb0

File tree

6 files changed

+375
-375
lines changed

6 files changed

+375
-375
lines changed

plugins/vault/crons/checkWithdrawals.js

Lines changed: 112 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -18,119 +18,119 @@ const { MAILTYPE } = require('../../../mail/strings');
1818
const vaultCoins = [];
1919

2020
const checkWithdrawals = () => {
21-
each(GET_SECRETS().vault.connected_coins, (coin) => {
22-
vaultCoins.push({
23-
currency: coin
24-
});
25-
});
26-
Deposit.findAll({
27-
where: {
28-
type: 'withdrawal',
29-
status: false,
30-
dismissed: false,
31-
rejected: false,
32-
processing: false,
33-
waiting: true,
34-
$or: vaultCoins
35-
},
36-
include: [
37-
{
38-
model: User,
39-
attributes: ['email', 'settings']
40-
}
41-
]
42-
})
43-
.then((withdrawals) => {
44-
if (withdrawals.length === 0) {
45-
loggerDeposits.info('No withdrawals need checking');
46-
return;
47-
}
48-
let txids = {};
49-
each(withdrawals, (withdrawal) => {
50-
txids[withdrawal.transaction_id]
51-
? txids[withdrawal.transaction_id].push(withdrawal)
52-
: txids[withdrawal.transaction_id] = [withdrawal];
21+
return new Promise((resolve, reject) => {
22+
each(GET_SECRETS().vault.connected_coins, (coin) => {
23+
vaultCoins.push({
24+
currency: coin
5325
});
54-
return all(Object.keys(txids).map((txid, i) => {
55-
const option = {
56-
method: 'GET',
57-
headers: {
58-
key: VAULT_KEY(),
59-
secret: VAULT_SECRET()
60-
},
61-
qs: {
62-
txid
63-
},
64-
uri: `${VAULT_ENDPOINT}/${VAULT_WALLET(txids[txid][0].currency)}/transactions`,
65-
json: true
66-
};
67-
return delay(500 * i)
68-
.then(() => {
69-
return rp(option);
70-
})
71-
.then((tx) => {
72-
if (tx.data[0]) {
73-
if (tx.data[0].is_confirmed) {
74-
loggerDeposits.info(`Transaction ${txid} was confirmed`);
75-
return sequelize.transaction((transaction) => {
76-
return all(txids[txid].map((withdrawal) => {
77-
return withdrawal.update(
78-
{
79-
waiting: false,
80-
status: true
81-
},
82-
{
83-
attributes: ['waiting', 'status'],
84-
transaction,
85-
returning: true
26+
});
27+
Deposit.findAll({
28+
where: {
29+
type: 'withdrawal',
30+
status: false,
31+
dismissed: false,
32+
rejected: false,
33+
processing: false,
34+
waiting: true,
35+
$or: vaultCoins
36+
},
37+
include: [
38+
{
39+
model: User,
40+
attributes: ['email', 'settings']
41+
}
42+
]
43+
})
44+
.then((withdrawals) => {
45+
if (withdrawals.length === 0) {
46+
loggerDeposits.info('No withdrawals need checking');
47+
resolve();
48+
}
49+
let txids = {};
50+
each(withdrawals, (withdrawal) => {
51+
txids[withdrawal.transaction_id]
52+
? txids[withdrawal.transaction_id].push(withdrawal)
53+
: txids[withdrawal.transaction_id] = [withdrawal];
54+
});
55+
return all(Object.keys(txids).map((txid, i) => {
56+
const option = {
57+
method: 'GET',
58+
headers: {
59+
key: VAULT_KEY(),
60+
secret: VAULT_SECRET()
61+
},
62+
qs: {
63+
txid
64+
},
65+
uri: `${VAULT_ENDPOINT}/${VAULT_WALLET(txids[txid][0].currency)}/transactions`,
66+
json: true
67+
};
68+
return delay(500 * i)
69+
.then(() => {
70+
return rp(option);
71+
})
72+
.then((tx) => {
73+
if (tx.data[0]) {
74+
if (tx.data[0].is_confirmed) {
75+
loggerDeposits.info(`Transaction ${txid} was confirmed`);
76+
return sequelize.transaction((transaction) => {
77+
return all(txids[txid].map((withdrawal) => {
78+
return withdrawal.update(
79+
{
80+
waiting: false,
81+
status: true
82+
},
83+
{
84+
attributes: ['waiting', 'status'],
85+
transaction,
86+
returning: true
87+
}
88+
)
89+
.then((data) => {
90+
return {
91+
success: true,
92+
status: true,
93+
data
94+
};
95+
});
96+
}));
97+
});
98+
} else if (tx.data[0].is_rejected) {
99+
loggerDeposits.info(`Transaction ${txid} was rejected`);
100+
return {
101+
success: true,
102+
status: false,
103+
info: {
104+
type: 'Vault Withdrawal Rejected',
105+
data: {
106+
description: 'This Vault withdrawal was rejected on the blockchain. You can double-check this transaction and proceed to confirm or dismiss the withdrawal through your admin panel.',
107+
transaction_id: txid,
108+
withdrawals: txids[txid].map((wd) => wd.dataValues)
86109
}
87-
)
88-
.then((data) => {
89-
return {
90-
success: true,
91-
status: true,
92-
data
93-
};
94-
});
95-
}));
96-
});
97-
} else if (tx.data[0].is_rejected) {
98-
loggerDeposits.info(`Transaction ${txid} was rejected`);
110+
}
111+
};
112+
} else {
113+
loggerDeposits.info(`Transaction ${txid} is not processed yet`);
114+
return {};
115+
}
116+
} else {
117+
loggerDeposits.warn(`Transaction ${txid} is not found`);
99118
return {
100-
success: true,
101-
status: false,
119+
success: false,
102120
info: {
103-
type: 'Vault Withdrawal Rejected',
121+
type: 'Vault Transaction Not Found',
104122
data: {
105-
description: 'This Vault withdrawal was rejected on the blockchain. You can double-check this transaction and proceed to confirm or dismiss the withdrawal through your admin panel.',
123+
description: 'This withdrawal was not found in Vault.',
106124
transaction_id: txid,
107125
withdrawals: txids[txid].map((wd) => wd.dataValues)
108126
}
109127
}
110128
};
111-
} else {
112-
loggerDeposits.info(`Transaction ${txid} is not processed yet`);
113-
return {};
114129
}
115-
} else {
116-
loggerDeposits.warn(`Transaction ${txid} is not found`);
117-
return {
118-
success: false,
119-
info: {
120-
type: 'Vault Transaction Not Found',
121-
data: {
122-
description: 'This withdrawal was not found in Vault.',
123-
transaction_id: txid,
124-
withdrawals: txids[txid].map((wd) => wd.dataValues)
125-
}
126-
}
127-
};
128-
}
129-
});
130-
}));
131-
})
132-
.then((results) => {
133-
if (Array.isArray(results)) {
130+
});
131+
}));
132+
})
133+
.then((results) => {
134134
return all(results.map((result) => {
135135
if (Array.isArray(result)) {
136136
return all(result.map((data) => {
@@ -169,16 +169,16 @@ const checkWithdrawals = () => {
169169
return;
170170
}
171171
}));
172-
} else {
173-
return;
174-
}
175-
})
176-
.then(() => {
177-
loggerDeposits.info('checkWithdrawals finished');
178-
})
179-
.catch((err) => {
180-
loggerDeposits.error(err.message);
181-
});
172+
})
173+
.then(() => {
174+
loggerDeposits.info('checkWithdrawals finished');
175+
resolve();
176+
})
177+
.catch((err) => {
178+
loggerDeposits.error('plugins/vault/cron/checkWithdrawals catch', err.message);
179+
reject(err);
180+
});
181+
});
182182
};
183183

184184
module.exports = {

0 commit comments

Comments
 (0)