-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifting.js
51 lines (45 loc) · 1.45 KB
/
lifting.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
const ethers = require("ethers");
const ABI = require("./abi.json");
const cron = require("node-cron");
require("dotenv").config();
async function getLifted(callback) {
const provider = new ethers.providers.JsonRpcProvider(
process.env.EWC_RPC_URL,
);
const contract = new ethers.Contract(
process.env.EWC_LIFTING_CONTRACT,
ABI,
provider,
);
let lastBlockChecked = await provider.getBlockNumber();
cron.schedule("*/1 * * * *", async () => {
// This cron job runs every minutes
const currentBlock = await provider.getBlockNumber();
const filter = contract.filters.LogLifted();
const events = await contract.queryFilter(
filter,
lastBlockChecked + 1,
currentBlock,
);
if (events.length > 0) {
events.forEach((event) => {
const amountInEth = ethers.utils.formatEther(event.args.amount);
// Check if the amount is greater than or equal to 500 ETH
if (parseFloat(amountInEth) >= 500) {
callback({
token: event.args.token,
t1Address: event.args.t1Address,
t2PublicKey: event.args.t2PublicKey,
amount: amountInEth,
});
} else {
console.log(
`${amountInEth} has been lifted, but notification didn't fire because it is under the threshold.`,
);
}
});
lastBlockChecked = currentBlock; // Update the last checked block
}
});
}
module.exports = { getLifted };