Skip to content

Instantly share code, notes, and snippets.

@mushroomsforest
Last active August 14, 2021 04:52
Show Gist options
  • Save mushroomsforest/b14377af2c3fd6a709ced8f28a163116 to your computer and use it in GitHub Desktop.
Save mushroomsforest/b14377af2c3fd6a709ced8f28a163116 to your computer and use it in GitHub Desktop.
Chainlink Upkeep script template for Mushrooms
const ethers = require("ethers");
const contractAbi = require("MushroomsGenericKeep3rV2.abi.json");
const iface = new ethers.utils.Interface(contractAbi);

const provider = new ethers.providers.JsonRpcProvider("{your rpc node url}");
const keeper = new ethers.Wallet("{keeper private key}", provider);
const upKeepContract = new ethers.Contract("0x031D40daEbf78b12070e2BC8dde0050422746b06", contractAbi, keeper);

function earn() {
    upKeepContract.getVaults().then((result) => {
        for (i = 0; i < result.length; i++) {            
             let method = "earnable";
             let callData = iface.encodeFunctionData(method, [result[i]]);
             execute(callData);
        }
    }).catch(console.error);
}

function harvest() {
    upKeepContract.getStrategies().then((result) => {
        for (i = 0; i < result.length; i++) {        
             let method = "harvestable";
             let callData = iface.encodeFunctionData(method, [result[i]]);
             execute(callData);
        }
    }).catch(console.error);
}

function keepMinRatio() {
    upKeepContract.getCollateralizedStrategies().then((result) => {
        for (i = 0; i < result.length; i++) {        
             let method = "keepMinRatioMayday";
             let callData = iface.encodeFunctionData(method, [result[i]]);
             execute(callData);
        }
    }).catch(console.error);
}


function execute(callData) {
    console.log("callData: " + callData);
    upKeepContract.callStatic.checkUpkeep(callData).then(checkResult => {
        let upkeepNeeded = checkResult[0];
        let performData = checkResult[1];
        console.log(upkeepNeeded);
        console.log(performData);
        if (upkeepNeeded){
            upKeepContract.performUpkeep(performData).then(console.log);
        }
    }).catch(console.error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment