added service no call slide() and shift()

This commit is contained in:
JulesCrown 2024-06-26 17:50:43 +02:00
parent ed23b04bcd
commit e392690da5
4 changed files with 185 additions and 0 deletions

49
services/marketMaker/.gitignore vendored Normal file
View file

@ -0,0 +1,49 @@
# Node.js specific
node_modules/
npm-debug.log
yarn-error.log
package-lock.json
yarn.lock
# Environment variables
.env
# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# OS-specific files
.DS_Store
Thumbs.db
# IDE-specific files
.vscode/
.idea/
*.swp
*.swo
# Coverage directory used by tools like istanbul
coverage/
# Ignore build output
dist/
build/
# Ignore any other secret keys or sensitive information
secret-keys.json
# Ignore local environment configuration
.env.local
# Optional npm cache directory
.npm
# Ignore temporary files
tmp/
temp/
# Ignore .next directory if using Next.js
.next/

View file

@ -0,0 +1,11 @@
const { Wallet } = require('ethers');
// Generate a random wallet
const wallet = Wallet.createRandom();
// Extract the private key and address
const privateKey = wallet.privateKey;
const address = wallet.address;
console.log('Private Key:', privateKey);
console.log('Address:', address);

View file

@ -0,0 +1,11 @@
{
"name": "marketMaker",
"version": "0.0.1",
"main": "index.js",
"license": "GPL3",
"dependencies": {
"dotenv": "^16.4.5",
"ethers": "5",
"express": "^4.19.2"
}
}

View file

@ -0,0 +1,114 @@
require('dotenv').config();
const { ethers } = require('ethers');
const express = require('express');
// Load environment variables
const INFURA_PROJECT_ID = process.env.INFURA_PROJECT_ID;
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"
];
// Initialize the provider
const provider = new ethers.providers.InfuraProvider('sepolia', INFURA_PROJECT_ID);
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;
async function checkFunds() {
const balance = await wallet.getBalance();
return ethers.utils.formatEther(balance);
}
async function canCallFunction(func) {
try {
// Simulate calling the function
await func.callStatic();
return true;
} catch (error) {
return false;
}
}
function formatDuration(ms) {
let seconds = Math.floor(ms / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
let days = Math.floor(hours / 24);
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 24;
return `${days} days, ${hours} hours, ${minutes} minutes`;
}
async function main() {
console.log('Service started...');
while (true) {
try {
if (await canCallFunction(contract.shilft)) {
console.log('Calling shilft...');
const tx = await contract.shilft();
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.');
} else {
console.log('No function can be called at the moment.');
}
} catch (error) {
console.error('Error in main loop:', error);
}
// Wait for some time before checking again
await new Promise(resolve => setTimeout(resolve, 60000)); // 1 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('/status', async (req, res) => {
try {
const balance = await checkFunds();
const uptime = formatDuration(new Date() - startTime);
const status = {
balance: `${balance} ETH`,
uptime: uptime,
lastShilftTime: lastShilftTime ? lastShilftTime.toString() : 'Never',
lastSlideTime: lastSlideTime ? lastSlideTime.toString() : 'Never'
};
if (parseFloat(balance) < 0.1) {
res.status(500).send(`Low Ethereum Balance: ${balance} ETH`);
} else {
res.status(200).json(status);
}
} catch (error) {
res.status(500).send(`Error checking funds: ${error.message}`);
}
});
app.listen(PORT, () => {
console.log(`HTTP server running on port ${PORT}`);
});