92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
require('dotenv').config();
|
|
const { ethers } = require('ethers');
|
|
const express = require('express');
|
|
const { execute } = require('./.graphclient');
|
|
const { bytesToUint256 } = require('harb-lib');
|
|
|
|
const myQuery = `
|
|
query GetPositions {
|
|
positions(first: 5000, where: {status: "Active"}) {
|
|
id
|
|
share
|
|
lastTaxTime
|
|
taxRate
|
|
status
|
|
}
|
|
}
|
|
`
|
|
|
|
// Load environment variables
|
|
const PROVIDER_URL = process.env.PROVIDER_URL;
|
|
const PRIVATE_KEY = process.env.PRIVATE_KEY;
|
|
const STAKE_CONTRACT_ADDRESS = process.env.STAKE_CONTRACT_ADDRESS;
|
|
const ABI = [
|
|
// Add your contract's ABI here
|
|
{"inputs":[{"internalType":"uint256","name":"positionId","type":"uint256"}],"name":"payTax","outputs":[],"stateMutability":"nonpayable","type":"function"}
|
|
];
|
|
|
|
// Initialize the provider
|
|
const provider = new ethers.JsonRpcProvider(PROVIDER_URL);
|
|
|
|
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
|
|
const contract = new ethers.Contract(STAKE_CONTRACT_ADDRESS, ABI, wallet);
|
|
|
|
let startTime = new Date();
|
|
let lastCallTime = null;
|
|
|
|
async function checkPosition(position) {
|
|
let taxRate = parseFloat(position.taxRate);
|
|
let lastTaxTime = position.lastTaxTime;
|
|
let passed = (Date.now() / 1000) - lastTaxTime;
|
|
if (passed > 365 * 24 * 60 * 60 / taxRate ) {
|
|
const hexString = position.id;
|
|
const cleanHexString = hexString.startsWith('0x') ? hexString.substring(2) : hexString;
|
|
const bytes = new Uint8Array(Math.ceil(cleanHexString.length / 2));
|
|
for (let i = 0, j = 0; i < cleanHexString.length; i += 2, j++) {
|
|
bytes[j] = parseInt(cleanHexString.slice(i, i + 2), 16);
|
|
}
|
|
let positionId = bytesToUint256(bytes);
|
|
console.log(`Calling payTax on ${positionId}`);
|
|
const tx = await contract.payTax(positionId);
|
|
await tx.wait();
|
|
lastCallTime = new Date();
|
|
} else {
|
|
console.log("not liquidated: ");
|
|
console.log(position);
|
|
}
|
|
}
|
|
|
|
|
|
async function main() {
|
|
console.log('Service started...');
|
|
|
|
while (true) {
|
|
// do stuff here
|
|
|
|
// Wait for some time before checking again
|
|
await new Promise(resolve => setTimeout(resolve, 300000)); // 5 minute
|
|
}
|
|
}
|
|
|
|
// Start the main loop
|
|
main().catch(async (error) => {
|
|
console.error('Fatal error:', error);
|
|
});
|
|
|
|
// Set up the Express server
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
app.get('/query', async (req, res) => {
|
|
const result = await execute(myQuery, {});
|
|
res.status(200).json(result);
|
|
for (const position of result.data.positions) {
|
|
await checkPosition(position);
|
|
}
|
|
});
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`HTTP server running on port ${PORT}`);
|
|
});
|
|
|