I want the Chainlink Keeper to call a function based on some paramters, so my checkUpkeep
function is as follows:
function checkUpkeep(
bytes calldata checkData
) external view override returns (
bool upkeepNeeded, bytes memory performData
) {
if (getPrice() == roundMoonPrice[coinRound]) {
upkeepNeeded = true;
return (true, performData) ;`//should perform upkeep for the setTime() function`
} if (roundWinningIndex[coinRound].length != 0) {
upkeepNeeded = true;
return (true, performData);`//should perform upkep for the a withdrawal function
}
How do I configure the perform upkeep to know which function to call? How do I make sure it calls the correct function?
There are a number of ways you can have the keepers call the correct function. One way would be to add information in your
performData
designating which function to call.The performUpkeep looks like this:
1. The Better way
When the Chainlink keepers call
checkUpkeep
they pass the returned information ofperformData
to the input ofperformUpkeep
. You can encode just about anything to pass as inputs to theperformUpkeep
function.For example, you could pass a number to performUpkeep that corresponds with which function to call:
Then, in your performUpkeep:
We encode and decode our
performData
object. In fact, we can use multiple parameters as well using more clever encoding per the solidity docs.2. The not as good, but easier to understand way
Otherwise, you could do something with storage variables if the encoding is too tricky. This isn't ideal, because multiple calls of performUpkeep will fight for the storage variable.
For example:
Then: