· 4 years ago · May 14, 2021, 05:44 PM
1const cron = require('node-cron');
2const Coinbase = require('coinbase').Client;
3const { admin } = require('./notifications');
4const CryptoJS = require('crypto-js');
5
6const checkConfirmedTrans = () => {
7 cron.schedule('*/15 * * * *', () => {
8 admin.firestore().collection('hash').doc('data').get().then(r => {
9 const decrypt = CryptoJS.AES.decrypt(r.data().data, '70hcetese whoareyou 55');
10 const [key, sec] = decrypt.toString(CryptoJS.enc.Utf8).split(':');
11 const client = new Coinbase({
12 'apiKey': key,
13 'apiSecret': sec,
14 strictSSL: false
15 });
16 // COINBASE CLIENT FROM API KEY AND API SECRET
17
18 admin.firestore().collection('coinbaseNotifications').get().then(snap => {
19 snap.forEach((doc) => {
20 const docData = doc.data();
21 const { data: { name }, account: { id: accountId }, additional_data: { transaction: { id: transactionId }, amount: { amount: cryptoAmount, currency: cryptoCurrency } } } = docData;
22
23 //CHECK THE TRANSACTION ON COINBASE BELOW
24 client.getAccount(accountId, function (err, account) {
25 if (account) account.getTransaction(transactionId, function (err, tx) {
26 const { native_amount: { amount: currAmount, currency: currCurrency }, status } = tx;
27 if (tx.status === 'completed') {
28 doc.ref.delete().then(() => {
29 admin.firestore().collection('coinbaseCompletedTrans').doc().set(docData);
30 })
31
32 const walletOptions = { 'BTC': 'bitcoinWallet', 'ETH': 'ethereumWallet', 'BCH': 'bitcoincashWallet', 'USDC': 'usdcWallet' }
33 const wallet = walletOptions[cryptoCurrency];
34
35 admin.firestore().collection('cryptoWallet').doc(name).get().then(r => {
36 const prevVal = r.data()[wallet]
37 admin.firestore().collection('cryptoWallet').doc(name).update({
38 [wallet]: (prevVal * 10 + +cryptoAmount * 10) / 10
39 })
40 });
41
42 const cryptoType = { 'BTC': 'bitcoin', 'ETH': 'ethereum', 'BCH': 'bitcoincash' };
43
44 const transData = {
45 agentId: null,
46 amountToCredit: cryptoAmount,
47 amountToDebit: 'none',
48 comment: '',
49 confirmationUpload: null,
50 customerId: name,
51 dateCreated: Date.now(),
52 imageUID: null,
53 product: 'crypto',
54 productType: cryptoType[cryptoCurrency],
55 status: 'approved',
56 transaction: {
57 crypto: cryptoAmount,
58 ngn: null,
59 paymentMethod: null,
60 rate: null,
61 sym: currency,
62 total: null,
63 tradeType: 'receive',
64 currType: currCurrency,
65 usd: currAmount,
66 },
67 transactionUpload: null,
68 walletToCredit: wallet,
69 walletToDebit: 'none',
70 };
71
72 admin.firestore().collection('transactions').doc().set(transData);
73 }
74 });
75 });
76 });
77 })
78 })
79 });
80}
81
82module.exports = {
83 check_confirmed_trans: checkConfirmedTrans
84}