-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (68 loc) · 2.25 KB
/
app.js
File metadata and controls
79 lines (68 loc) · 2.25 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
// @author: axelphunter
// https://github.com/axelphunter
require('dotenv').config();
const ethers = require('ethers');
const cron = require('node-cron');
const express = require('express');
const app = express();
const genericErc20Abi = require('./utils/genericErc20Abi.json')
const addresses = {
// Tokens
FXM: '0x132b56763C0e73F95BeCA9C452BadF89802ba05e',
// Contracts
fantasmContract: '0xC4510604504Fd50f64499fF6186AEf1F740dE38B',
// User wallet address
recipient: process.env.RECIPIENT
}
// Provide Chainstack or Quicknode websocket url.
const provider = new ethers.providers.WebSocketProvider(process.env.NETWORK_WEBSOCKET_URL);
// Add Mnemonic to .envs (DO NOT HARD CODE)
const wallet = ethers.Wallet.fromMnemonic(process.env.MNEMONIC);
const account = wallet.connect(provider);
// Fantasm contract methods
const fantasmContract = new ethers.Contract(
addresses.fantasmContract,
[
'function getReward() public',
'function stake(uint256 amount, bool lock) external',
'function totalBalance(address user) external view returns (uint256 amount)'
],
account
);
// Generic ERC 20 abi for tokens
const FXM = new ethers.Contract(
addresses.FXM,
genericErc20Abi,
account
)
app.listen(process.env.PORT || 4000, function () {
console.log("Let's gooo!");
const run = async () => {
try {
console.log('Claiming rewards...')
// Withdraw all available rewards.
const a = await fantasmContract.getReward()
await a.wait()
// Query available FXM balance.
let balance = await FXM.balanceOf(addresses.recipient);
// Stake available FXM balance.
const b = await fantasmContract.stake(balance, true, { gasLimit: 300000 });
await b.wait()
// Get total locked
let totalLocked = await fantasmContract.totalBalance(addresses.recipient)
totalLocked = parseFloat(ethers.utils.formatEther(totalLocked)).toFixed(3)
balance = parseFloat(ethers.utils.formatEther(balance)).toFixed(3)
console.log(`
==================\n
Reward claimed: ${balance} FXM\n
Total locked: ${totalLocked} FXM\n
==================\n`)
} catch (e) {
console.log(e, 'error')
}
};
// Run worker every hour
cron.schedule('0 0 */1 * * *', async () => {
run()
})
});