使用ChainLink Keepers在Kovan测试网获取ETH/USD价格不更新问题求助
问题排查与解决方法
1 核心逻辑缺陷
- 你的
performUpkeep方法中仅调用了getLatestPrice()但没有存储返回的价格结果,该方法本身是view只读方法,调用不会修改合约状态,你自然无法看到更新的价格记录。如果需要存储每次Keeper执行时的价格,需要新增状态变量存储价格,修改后的逻辑如下:
// SPDX-License-Identifier: MIT pragma solidity 0.8.6; import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; interface KeeperCompatibleInterface { function checkUpkeep(bytes calldata checkData) external returns (bool upkeepNeeded, bytes memory performData); function performUpkeep(bytes calldata performData) external; } contract Counter is KeeperCompatibleInterface { AggregatorV3Interface internal priceFeed; uint public counter; uint public immutable interval; uint public lastTimeStamp; // 新增状态变量存储最新同步的价格 int public latestStoredPrice; constructor(uint updateInterval) { interval = updateInterval; lastTimeStamp = block.timestamp; counter = 0; // 更换为目标测试网对应的ETH/USD喂价聚合器地址 priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); } function getLatestPrice() public view returns (int) { ( uint80 roundID, int price, uint startedAt, uint timeStamp, uint80 answeredInRound ) = priceFeed.latestRoundData(); return price; } function checkUpkeep(bytes calldata checkData) external view override returns (bool upkeepNeeded, bytes memory performData) { upkeepNeeded = (block.timestamp - lastTimeStamp) > interval; performData = checkData; } function performUpkeep(bytes calldata performData) external override { lastTimeStamp = block.timestamp; counter = counter + 1; // 存储获取到的最新价格 latestStoredPrice = getLatestPrice(); } }
修改后调用latestStoredPrice()即可获取Keeper最近一次执行时同步的价格。如果你直接调用getLatestPrice()返回值一直不变,属于下述测试网兼容问题。
2 Keeper执行异常排查
如果你的counter变量也没有按间隔递增,说明Keepers服务没有正常触发你的合约执行,按以下步骤排查:
- 确认部署合约时传入的
updateInterval参数值合理,比如设置为300即5分钟执行一次,避免设置过长导致长时间看不到更新 - 确认你在Chainlink Keepers平台注册时使用的是对应测试网,转入的是对应测试网的LINK代币,且Upkeep余额充足
- 确认注册Upkeep时填写的目标合约地址、触发参数配置正确
3 测试网兼容问题
Kovan测试网已被官方弃用,Chainlink早已停止对Kovan测试网的喂价、Keepers服务支持,这也是你无法获取更新数据的核心原因之一,建议更换为Goerli或者Sepolia测试网进行测试,同时更换对应测试网的ETH/USD喂价聚合器地址。
内容的提问来源于stack exchange,提问作者PythonCoder1981
相关产品推荐
相关产品推荐

