如何使用Chainlink Keepers实现智能合约执行的自动化

Chainlink2022-04-26 18:29


*本文含有大量链接,由于公众号设定,无法插入外部链接,需要查看具体内容的小伙伴请查看原文:

https://blog.chain.link/how-to-automate-smart-contract-execution-using-chainlink-keepers/,获取相关信息。

(文中链接请复制至浏览器打开)


Chainlink 2022春季黑客松已于4月22日拉开帷幕! 欢迎注册!请查看此页面以了解更多信息:

https://chain.link/hackathon?utm_medium=referral&utm_source=chainlink-docs&utm_campaign=spring-2022-hackathon&utm_content=developers


智能合约不能自己执行,这意味着它们需要一个外部拥有账户、预言机或合约来启动它们自己的功能。这给许多dApp带来了问题,比如这类dApp要求合约在固定时间间隔(如每24小时),或者满足预定条件(如以太坊达到特定价格),或者依据某种计算(如贷款被计算为抵押不足)执行。


在过去,开发者会创建并维护他们自己的中心化脚本或者手动触发智能合约的执行以解决问题。然而,这实际上破坏了构建去中心化区块链应用程序的目的,并且如果中心化脚本或手动触发过程失败,就会出现停机的可能。


在本教程中,你将学习如何使用Chainlink Keepers这种可靠的和去中心化的方式自动执行智能合约。


为什么每个智能合约开发者都应该使用去中心化的自动化方案?


Chainlink Keepers解锁了一种新形式的去中心化智能合约自动化方案,使开发者能够改变他们构建和维护dApp的方式。去中心化智能合约自动化有三个主要好处。


首先,有必要消除运行中存在的任何中心化故障点。Chainlink Keepers由Keeper节点组成的去中心化网络驱动--这与目前通过Chainlink Data Feeds守护DeFi数百亿价值的超可靠节点相同,消除了单点故障。


其次,开发者不需要投入时间和资源来创建链上监控和合约执行的脚本,只需要创建一个与Keeper兼容的合约并注册,就可以集成优化的Chainlink Keepers基础设施。这节省了时间,也减少了DevOps的工作量,使开发者能够专注于编写更多优秀的代码。


最后,通过使用Chainlink Keepers,开发者可以增强其协议的安全性。


开发者不再需要冒险从中心化服务器发起交易时暴露自己私钥的风险--Chainlink Keeper网络上的节点将签署链上交易。


开始使用Chainlink Keepers


你可以通过两个步骤用Chainlink Keepers自动化你的智能合约:

  1. 创建并部署一个与Keeper兼容的合约

    https://docs.chain.link/docs/chainlink-keepers/compatible-contracts/

  2. 在Chainlink Keepers应用程序上注册该合约,以创建一个Upkeep

    https://keepers.chain.link/


在这些步骤完成后,Chainlink Keepers将按照任务描述执行Upkeep工作,而不需要任何进一步的输入。


如果你是Solidity的新手,我们建议在继续之前先学习一些初级教程。这个教程特别全面和有用。我们现在将向你展示如何使你的合约与Keeper兼容。如果你更喜欢观看有关该主题的视频,请观看我们的视频教程:

https://www.youtube.com/watch?v=-Wkw5JVQGUo)


如何编写与Keeper兼容的合约


与Keeper兼容的合约有一个checkUpkeep函数和一个performUpkeep函数,并具有Chainlink Keepers所要求的必要输入和输出。为了防止错误,我们将在指定我们的checkUpkeep和performUpkeep函数应该做什么之前使用Keeper兼容的接口。


导入与Keeper兼容的接口


首先,将KeeperCompatibleInterface导入你的合约。


// SPDX-License-Identifier: MITpragma solidity ^0.8.0;
// KeeperCompatible.sol imports the functions from both ./KeeperBase.sol and// ./interfaces/KeeperCompatibleInterface.solimport "@chainlink/contracts/src/v0.8/KeeperCompatible.sol";


这个接口有两个函数:


checkUpkeep函数


Chainlink Keepers使用一个去中心化的网络,在每个区块中以安全和低成本的方式在链下监控checkUpkeep函数,然后在满足预定的条件时启动链上交易,执行智能合约功能。


function checkUpkeep(  bytes calldata checkData)  external  returns (    bool upkeepNeeded,    bytes memory performData  );


checkUpkeep函数需要一个名为checkData的字节参数,该参数在Keepers应用程序上注册Upkeep(https://keepers.chain.link/new)时被设置。这个值是可选的,可以在你的代码逻辑中使用,用于确定checkUpkeep是否返回true。


checkUpkeep返回一个名为upkeepNeeded的布尔值。当为真时,这将调用performUpkeep。它还返回字节格式的performData(可选的附加数据),如果需要Upkeep,Keeper应该调用performUpkeep。要了解更多信息,请参见开发者文档:

https://docs.chain.link/docs/chainlink-keepers/compatible-contracts/#performdata


performUpkeep函数


如果你的checkUpkeep的链下模拟确认预定义条件满足(upkeepNeeded == true from checkUpkeep),Keeper将向区块链广播一个交易,并将performData作为输入执行performUpkeep函数。


function performUpkeep(  bytes calldata performData) external;


轮流的节点选择过程可以防止节点之间的gas价格拍卖竞争,并能稳定你的合约自动化的成本。



下面是一个来自Chainlink Keepers用户Entropyfi的合约实例片段,checkUpkeep检查Entropyfi预测游戏是否要到期结算。


/*** @dev chainlink keeper checkUpkeep function to constantly check whether we need function call**/function checkUpkeep(bytes calldata checkData) external override returns (bool upkeepNeeded, bytes memory performData) {     PoolStatus currState = status.currState;     uint256 lastUpdateTimestamp = status.lastUpdateTimestamp;     uint256 durationOfGame = status.durationOfGame;     uint256 durationOfBidding = status.durationOfBidding;
if (currState == PoolStatus.Accepting && block.timestamp > lastUpdateTimestamp.add(durationOfBidding)) { upkeepNeeded = true; } else if (currState == PoolStatus.Locked && block.timestamp > lastUpdateTimestamp.add(durationOfGame)) { upkeepNeeded = true; } else { upkeepNeeded = false; } performData = checkData;}


Chainlink Keepers将不断调用checkUpkeep函数,如果upkeepNeeded为真,那么节点将执行执行performUpkeep函数。


/*** @dev once checkUpKeep been triggered, keeper will call performUpKeep**/function performUpkeep(bytes calldata performData) external override {     PoolStatus currState = status.currState;     uint256 lastUpdateTimestamp = status.lastUpdateTimestamp;     uint256 durationOfGame = status.durationOfGame;     uint256 durationOfBidding = status.durationOfBidding;
if (currState == PoolStatus.Accepting && block.timestamp > lastUpdateTimestamp.add(durationOfBidding)) { startGame(); } if (currState == PoolStatus.Locked && block.timestamp > lastUpdateTimestamp.add(durationOfGame)) { endGame(); } performData;}


从一些示例代码开始


无论你是创建一个新的合约,还是已经部署了一个包含需要自动化的函数的合约,在Chainlink Keepers开发者文档中都有一些指南可以帮助你开始。


从这里的合约示例开始。下面的例子代表了一个简单的计数器合约。


// SPDX-License-Identifier: MITpragma solidity ^0.7.0;// KeeperCompatible.sol imports the functions from both ./KeeperBase.sol and// KeeperCompatible.sol imports the functions from both ./KeeperBase.sol and// ./interfaces/KeeperCompatibleInterface.sol// ./interfaces/KeeperCompatibleInterface.solimport "@chainlink/contracts/src/v0.7/KeeperCompatible.sol";
contract Counter is KeeperCompatibleInterface { /** * Public counter variable */ uint public counter;
/** * Use an interval in seconds and a timestamp to slow execution of Upkeep */ uint public immutable interval; uint public lastTimeStamp;
constructor(uint updateInterval) { interval = updateInterval; lastTimeStamp = block.timestamp;
counter = 0;    }
function checkUpkeep(bytes calldata /* checkData */) external override returns (bool upkeepNeeded, bytes memory /* performData */) { upkeepNeeded = (block.timestamp - lastTimeStamp) > interval; // We don't use the checkData in this example. The checkData is defined when the Upkeep was registered. } function performUpkeep(bytes calldata /* performData */) external override { //We highly recommend revalidating the upkeep in the performUpkeep function if ((block.timestamp - lastTimeStamp) > interval ) { lastTimeStamp = block.timestamp; counter = counter + 1; } // We don't use the performData in this example. The performData is generated by the Keeper's call to your checkUpkeep function }}



Chainlink Keepers可以监控任何链上或链下条件的状态,如时间的流逝(如是否过去了24小时?)或某种计算(如贷款是否被计算为抵押不足?) 一旦条件得到满足,Chainlink Keepers就会在链上提交一个交易,以触发该函数的执行。


你也可以使用Chainlink Keepers的实用合约,比如EthBalanceMonitor合约或这些例子。


如何将你的合约注册为网络上的Upkeep


完成与Keeper兼容的合约后,就可以在Chainlink Keepers应用程序中点击“注册新的Upkeep”。



关于如何注册的详细步骤指南,请参阅Chainlink开发者文档:

https://docs.chain.link/docs/chainlink-keepers/register-upkeep/


重要提示(对于非以太坊链):你的Upkeep必须用ERC-677标准的LINK(而不是ERC-20,这在许多桥上是通用的)进行充值。可以使用PegSwap来将你的LINK转换为与ERC-677兼容的版本。


注册和批准后,你可以添加额外的资金,并在Chainlink Keepers应用程序上看到你的Upkeep的所有细节。




今天就开始用起来吧


现在你知道用Chainlink Keepers自动化你的智能合约是多么容易了,你可以开始集成Keepers并解锁大量的用例,如DEX限价单、跨链NFT铸造、重新调整和重新平衡通证等等。


访问开发者文档或加入Discord了解更多内容。要讨论集成问题,请点击此处联系专家。


要了解更多信息,请访问chain.link, 订阅Chainlink通讯,并在Twitter, YouTube和Reddit上关注Chainlink。


Chainlink 2.0白皮书中文版已于日前发布,想要了解更多关于Chainlink 2.0的内容细节,请扫描下方二维码▼


END


▲获取Chainlink官方最新资讯

加入 Chainlink官方社区▼


Chainlink 官方渠道
QQ群: 6135525 

微博:  https://weibo.com/chainlinkofficial
知乎:https://www.zhihu.com/people/chainlink
SegmentFault:https://segmentfault.com/u/chainlink
币乎:  https://bihu.com/people/1869894547
GitHub:  https://github.com/smartcontractkit/chainlink
Discord:   https://discord.gg/aSK4zew
Twitter:  https://twitter.com/chainlink
中文 Twitter: https://twitter.com/ChainlinkChina
Telegram:  https://t.me/chainlinkofficial
合作联系:  china@smartcontract.com

点击“阅读原文” 进入 Chainlink中文官网

【免责声明】市场有风险,投资需谨慎。本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况。据此投资,责任自负。

来源链接

相关Wiki

相关标签