recenter service

This commit is contained in:
JulesCrown 2024-09-11 12:59:35 +02:00
parent 2caef7ecb2
commit 6ee65f4834
3 changed files with 21 additions and 25 deletions

View file

@ -0,0 +1,4 @@
## RUN
node service.js

View file

@ -5,7 +5,7 @@
"license": "GPL3",
"dependencies": {
"dotenv": "^16.4.5",
"ethers": "5",
"ethers": "^6.13.2",
"express": "^4.19.2"
}
}

View file

@ -3,33 +3,32 @@ const { ethers } = require('ethers');
const express = require('express');
// Load environment variables
const INFURA_PROJECT_ID = process.env.INFURA_PROJECT_ID;
const PROVIDER_URL = process.env.PROVIDER_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const ABI = [
// Add your contract's ABI here
"function shilft() public",
"function slide() public"
{"type":"function","name":"recenter","inputs":[],"outputs":[],"stateMutability":"nonpayable"}
];
// Initialize the provider
const provider = new ethers.providers.InfuraProvider('sepolia', INFURA_PROJECT_ID);
const provider = new ethers.JsonRpcProvider(PROVIDER_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, wallet);
let startTime = new Date();
let lastShilftTime = null;
let lastSlideTime = null;
let lastCallTime = null;
async function checkFunds() {
const balance = await wallet.getBalance();
return ethers.utils.formatEther(balance);
const balance = await provider.getBalance(wallet.address);
return ethers.formatEther(balance);
}
async function canCallFunction(func) {
async function canCallFunction() {
try {
// Simulate calling the function
await func.callStatic();
// this will throw if the function is not callable
await contract.recenter.estimateGas();
return true;
} catch (error) {
return false;
@ -54,18 +53,12 @@ async function main() {
while (true) {
try {
if (await canCallFunction(contract.shilft)) {
console.log('Calling shilft...');
const tx = await contract.shilft();
if (await canCallFunction()) {
console.log('Calling recenter...');
const tx = await contract.recenter();
await tx.wait();
lastShilftTime = new Date();
console.log('shilft called successfully.');
} else if (await canCallFunction(contract.slide)) {
console.log('Calling slide...');
const tx = await contract.slide();
await tx.wait();
lastSlideTime = new Date();
console.log('slide called successfully.');
lastCallTime = new Date();
console.log('recenter called successfully.');
} else {
console.log('No function can be called at the moment.');
}
@ -94,8 +87,7 @@ app.get('/status', async (req, res) => {
const status = {
balance: `${balance} ETH`,
uptime: uptime,
lastShilftTime: lastShilftTime ? lastShilftTime.toString() : 'Never',
lastSlideTime: lastSlideTime ? lastSlideTime.toString() : 'Never'
lastCallTime: lastCallTime ? lastCallTime.toString() : 'Never'
};
if (parseFloat(balance) < 0.1) {