我想用 Chainlink price feed 和 Keepers 去实现一个卖单的自动化,就是当价格低于某一个数值的时候,就触发卖单。这两个函数分开运行分别都可以实现,但是当我把它们放在一起,就无法正常运行了。也就是说当把 Price Feed 放在 CheckUpkeep()
中,performUpKeep()
就没法被触发。
checkUpKeep() 函数可以去调用 priceFeed 吗?我应该怎么写这个函数?
代码如下:
function checkUpkeep(bytes memory /* checkData */) public view override returns (//,bytes memory value
bool upkeepNeeded,
bytes memory num
){
uint256 EthPrice = 0;
// uint256 i = 2;
EthPrice = getPrice();
num = abi.encodePacked(EthPrice);
if (EthPrice > 0){
upkeepNeeded = true;
}
return (upkeepNeeded, num);//, value
}
function performUpkeep(bytes calldata num) external override {//, bytes calldata value
(bool upkeepNeeded, ) = checkUpkeep("");
if (!upkeepNeeded) {
revert Order__UpkeepNotNeeded(
address(this).balance,
s_Wallets.length
);
}
//Byte conversion to uint
uint256 number;
number = abi.decode(num, (uint256));
// for(uint i=0;i<num.length;i++){
// number = number + uint(uint8(num[i]))*(2**(8*(num.length-(i+1))));
// }
s_nombre = number;
}