gpt4 book ai didi

chainlink - 如何配置 perform upkeep 以了解在 chainlink keepers 中调用哪个函数?

转载 作者:行者123 更新时间:2023-12-05 03:34:46 25 4
gpt4 key购买 nike

我希望 Chainlink Keeper 根据一些参数调用一个函数,所以我的 checkUpkeep 函数如下:

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
}

如何配置执行维护以了解调用哪个函数?我如何确保它调用正确的函数?

最佳答案

您可以通过多种方式让管理员调用正确的函数。一种方法是在您的 performData 中添加信息,指定要调用的函数。

performUpkeep 看起来像这样:

function performUpkeep(bytes calldata performData) external override {
// conditional to call one function
// conditional to call a different function
}

1。更好的方式

当 Chainlink 守护者调用 checkUpkeep 时,他们将 performData 的返回信息传递给 performUpkeep 的输入。你可以encode几乎任何东西都可以作为输入传递给 performUpkeep 函数。

例如,您可以将一个数字传递给与要调用的函数相对应的 performUpkeep:

function checkUpkeep(
bytes calldata checkData
) external view override returns (
bool upkeepNeeded, bytes memory performData
) {
if (getPrice() == roundMoonPrice[coinRound]) {
upkeepNeeded = true;
performData = abi.encodePacked(uint256(0); // This is the new line
return (true, performData) ;
} if (roundWinningIndex[coinRound].length != 0) {
upkeepNeeded = true;
performData = abi.encodePacked(uint256(1); // This is the new line
return (true, performData);
}

然后,在您的 performUpkeep 中:

function performUpkeep(bytes calldata performData) external override {
uint256 decodedValue = abi.decode(performData, (uint256));
if(decodedValue == 0){
setTime();
}
if(decodedValue == 1){
withdraw();
}
}

我们编码和解码我们的 performData 对象。事实上,我们也可以使用多个参数以及使用 more clever encoding per the solidity docs.

2。不太好,但更容易理解的方式

否则,如果编码太棘手,您可以对存储变量做一些事情。这并不理想,因为多次调用 performUpkeep 会争夺存储变量。

例如:

uint256 public functionToCall = 0;

function checkUpkeep(
bytes calldata checkData
) external view override returns (
bool upkeepNeeded, bytes memory performData
) {
if (getPrice() == roundMoonPrice[coinRound]) {
upkeepNeeded = true;
functionToCall = 0;
return (true, performData) ;
} if (roundWinningIndex[coinRound].length != 0) {
upkeepNeeded = true;
functionToCall = 1;
return (true, performData);
}

然后:

function performUpkeep(bytes calldata performData) external override {
if(functionToCall == 0){
setTime();
}
if(functionToCall == 1){
withdraw();
}
}

关于chainlink - 如何配置 perform upkeep 以了解在 chainlink keepers 中调用哪个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70091960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com