-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckWallet.js
More file actions
112 lines (101 loc) · 3.44 KB
/
checkWallet.js
File metadata and controls
112 lines (101 loc) · 3.44 KB
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
require("dotenv").config();
const connectDB = require("./config/db.js");
connectDB();
const axios = require("axios");
const WalletModel = require("./models/walletModel.js");
const { getBalance, getSPLBalance } = require("./utils/transfer");
const { solNativeAddress } = require("./constant/constants.js");
const { getBalanceFromDB } = require("./utils/others.js");
const checkWallet = async () => {
try {
setInterval(async () => {
const walletAccounts = await fetchWalletList();
if (walletAccounts.length === 0) {
return;
}
await checkWalletBalanceAndRequestDeposit(walletAccounts);
}, process.env.CHECK_WALLET_INTERVAL * 1000);
} catch (err) {
console.log("CheckWallet Error: ", err);
}
};
const fetchWalletList = async () => {
try {
const walletAccounts = await WalletModel.find({ archived: false });
return walletAccounts;
} catch (err) {
console.log("fetchWalletList error: ", err);
}
};
// SOL
const checkWalletBalanceAndRequestDeposit = async (walletAccounts) => {
try {
const promises = Promise.all(walletAccounts.map((d) => handleSingleWallet(d)));
await promises.then();
} catch (err) {
console.log("checkWalletBalanceAndDeposit error: ", err);
}
};
// SOL
const handleSingleWallet = async (walletAccount) => {
try {
const wallet = await WalletModel.findOne({ address: walletAccount.address });
const userId = wallet.userId;
const balance = await getBalance(walletAccount.address);
const dbBalance = await getBalanceFromDB(userId, walletAccount.address, solNativeAddress);
const depositAmount = (balance - dbBalance).toFixed(9);
console.log(`deposit amount at ${walletAccount.address}`, depositAmount);
if (balance > 0 && depositAmount > 0) {
try {
await axios
.post(`${process.env.HOST_ENDPOINT}/user/deposit`, {
userId,
walletAddress: walletAccount.address,
tokenAddress: solNativeAddress,
amountIn: depositAmount,
})
.then((res) => {
console.log(res.data);
});
} catch (err) {
console.log("Deposit SOL Error: ", err);
}
}
const splAccounts = await getSPLBalance(walletAccount.address);
// Deposit SPL
if (splAccounts.length !== 0) {
try {
splAccounts.forEach(async (account) => {
console.log(
`${account.tokenAddress} Balance of ${walletAccount.address} is ${account.balance}`
);
const dbBalance = await getBalanceFromDB(
userId,
walletAccount.address,
account.tokenAddress
);
const depositAmount = (account.balance - dbBalance).toFixed(account.decimal);
if(depositAmount !== 0) console.log(`deposit amount at ${walletAccount.address}`, depositAmount);
if (account.balance > 0 && depositAmount > 0) {
await axios
.post(`${process.env.HOST_ENDPOINT}/user/deposit`, {
userId,
walletAddress: walletAccount.address,
tokenAddress: account.tokenAddress,
amountIn: depositAmount,
})
.then((res) => {
console.log(res.data);
});
}
});
} catch (err) {
console.log("Deposit SPL Error: ", err);
}
}
console.log("End of a cycle");
} catch (err) {
console.log("handleSingleWallet error", err);
}
};
checkWallet();