dayTrigger.js
Automated script that advances the protocol's day counter, triggers penalty calculations, and conditionally executes weekly airdrop generation based on the protocol schedule.
require('dotenv').config();
const { ethers } = require('ethers');
const { exec } = require('child_process');
// Load environment variables
const RPC_URL = process.env.RPC_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONTRACT_ADDRESS = process.env.WNMCONTRACT_ADDRESS;
const ABI = [
{
"inputs": [],
"name": "incrementDay",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "currentDay",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
];
const provider = new ethers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, wallet);
async function main() {
try {
console.log('Calling incrementDay()...');
const tx = await contract.incrementDay();
console.log(`Transaction submitted. Hash: ${tx.hash}`);
const receipt = await tx.wait();
if (receipt.status !== 1) {
console.error('❌ Transaction failed.');
return;
}
console.log('✅ incrementDay() transaction confirmed.');
console.log('Calling currentDay()...');
const result = await contract.currentDay();
console.log(`currentDay() returned: ${result}`);
const snapshotDay = result - 1n;
// ✅ Always run mongodbscript.js with snapshotDay
console.log(`Running mongodbscript.js with snapshotDay: ${snapshotDay}`);
exec(`node mongodbscript.js ${snapshotDay.toString()}`, (error, stdout, stderr) => {
if (error) console.error(`❌ Error running mongodbscript.js: ${error.message}`);
if (stderr) console.error(`stderr: ${stderr}`);
if (stdout) console.log(`stdout: ${stdout}`);
});
// ✅ Conditionally run example.js only on airdrop day
if ((result - 8n) % 7n === 0n && result >= 8n) {
console.log(`It's a snapshot day. Running airdropGenerator.js...`);
exec('node example.js', (error, stdout, stderr) => {
if (error) console.error(`❌ Error running airdropGenerator.js: ${error.message}`);
if (stderr) console.error(`stderr: ${stderr}`);
if (stdout) console.log(`stdout: ${stdout}`);
});
} else {
console.log(`Current day is not an airdrop day.`);
}
} catch (err) {
console.error('❌ Unexpected error:', err);
}
}
main();
Last updated