Dheeraj Kathuria
Dheeraj Kathuria

Reputation: 1

Chainlink upkeep automation

contract UpkeepIDConditionalExample is AutomationCompatible { LinkTokenInterface public constant s_link = LinkTokenInterface(0x779877A7B0D9E8603169DdbD7836e478b4624789); AutomationRegistrarInterface public constant s_registrar = AutomationRegistrarInterface( 0xb0E49c5D0d05cbc241d68c05BC5BA1d1B7B72976 ); KeeperRegistryInterface public constant s_keeperRegistry = KeeperRegistryInterface(0x86EFBD0b6736Bed994962f9797049422A3A8E8Ad);

uint256 public numberOfPerformUpkeepCalls;
uint256 constant endDate = 1715781439;
uint256 public latestUpkeepId;

    // LINK must be approved for transfer - this can be done every time or once
    // with an infinite approval
    if (params.amount > 0)
        s_link.approve(address(s_registrar), params.amount);
    uint256 upkeepID = s_registrar.registerUpkeep(params);
    if (upkeepID != 0) {
        // DEV - Use the upkeepID however you see fit
        latestUpkeepId = upkeepID;
    } else {
        revert("auto-approve disabled");
    }
}

function fundUpkeep(uint96 amount) external {
    // Call the function using delegatecall or call
    // Using delegatecall to retain msg.sender as the caller
    (bool success, ) = address(s_keeperRegistry).call(
        abi.encodeWithSelector(
            s_keeperRegistry.addFunds.selector,
            latestUpkeepId,
            amount
        )
    );
    require(success, "Function call failed");
}

function pauseUpkeep() external {
    // Call the function using delegatecall or call
    // Using delegatecall to retain msg.sender as the caller
    (bool success, ) = address(s_keeperRegistry).call(
        abi.encodeWithSelector(
            s_keeperRegistry.pauseUpkeep.selector,
            latestUpkeepId
        )
    );
    require(success, "Function call failed");
}

function cancelUpkeep() external {
    // Call the function using delegatecall or call
    // Using delegatecall to retain msg.sender as the caller
    (bool success, ) = address(s_keeperRegistry).call(
        abi.encodeWithSelector(
            s_keeperRegistry.cancelUpkeep.selector,
            latestUpkeepId
        )
    );
    require(success, "Function call failed");
}

function editGasLimit(uint32 newGasLimit) external {
    // Call the function using delegatecall or call
    // Using delegatecall to retain msg.sender as the caller
    (bool success, ) = address(s_keeperRegistry).call(
        abi.encodeWithSelector(
            s_keeperRegistry.setUpkeepGasLimit.selector,
            latestUpkeepId,
            newGasLimit
        )
    );
    require(success, "Function call failed");
}

}

Upvotes: 0

Views: 33

Answers (0)

Related Questions