值得信赖的区块链资讯!
比推数据  |  比推终端  |  比推英文  |  比推 APP  | 

下载比推 APP

值得信赖的区块链资讯!
iPhone
Android

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

Chainlink预言机

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应用程序注册Upkeephttps://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价格拍卖竞争,并能稳定你的合约自动化的成本。

image.png

下面是一个来自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铸造、重新调整和重新平衡通证等等。

说明:比推所有文章只代表作者观点,不构成投资建议
原文链接:https://www.bitpush.news/articles/2472432

比推快讯

更多 >>

下载比推 APP

24 小时追踪区块链行业资讯、热点头条、事实报道、深度洞察。

邮件订阅

金融科技决策者们都在看的区块链简报与深度分析,「比推」帮你划重点。