Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Created September 11, 2024 20:52
Show Gist options
  • Save Turupawn/cb390dbc5e1bf54ee8a920cd99a30850 to your computer and use it in GitHub Desktop.
Save Turupawn/cb390dbc5e1bf54ee8a920cd99a30850 to your computer and use it in GitHub Desktop.
run with `node 4TK_bot.js`
const Web3 = require('web3');
const cron = require('node-cron');
// Replace with your Ethereum node provider URL
const web3 = new Web3('https://rpc.garnetchain.com');
// Replace with your smart contract ABI and address
const contractABI = [
{
"inputs": [
{ "internalType": "uint256", "name": "param1", "type": "uint256" },
{ "internalType": "uint256", "name": "param2", "type": "uint256" },
{ "internalType": "uint256", "name": "param3", "type": "uint256" }
],
"name": "startFarming",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint256", "name": "param1", "type": "uint256" }
],
"name": "finishFarming",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
const contractAddress = '0x00E38DC13bBCBd0F83d32d5D763407b5966a5506';
const contract = new web3.eth.Contract(contractABI, contractAddress);
// Replace with your wallet's private key
const privateKey = ''; // Add your private key
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
// Replace with the desired parameters
const param1 = 281;
const param2 = 10;
const param3 = 1496;
// Function to call startFarming
async function callStartFarming() {
try {
const data = contract.methods.startFarming(param1, param2, param3).encodeABI();
// Fetch nonce manually
const nonce = await web3.eth.getTransactionCount(account.address, 'latest');
const tx = {
from: account.address,
to: contractAddress,
gas: 3000000, // Increased gas limit
data: data,
nonce: nonce,
};
// Simulate the transaction with eth_call before sending
const simulation = await contract.methods.startFarming(param1, param2, param3).call();
console.log('Simulation result:', simulation);
// Sign the transaction
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
// Send the transaction
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log('Transaction successful with hash:', receipt.transactionHash);
} catch (error) {
console.error('Error calling startFarming:', error);
}
}
// Function to call finishFarming
async function callFinishFarming() {
try {
const data = contract.methods.finishFarming(param1).encodeABI();
const tx = {
from: account.address,
to: contractAddress,
gas: 2000000,
data: data,
};
// Sign and send the transaction
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log('finishFarming transaction hash:', receipt.transactionHash);
} catch (error) {
console.error('Error calling finishFarming:', error);
}
}
// Cron job that starts farming, finishes after 30s, and starts again after another 30s
cron.schedule('* * * * *', async () => {
// Call startFarming first
await callStartFarming();
// Wait for 30 seconds before calling finishFarming
setTimeout(async () => {
await callFinishFarming();
}, 30000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment