[Chainlink] Smart Contract Automation: How to Get Started With Chainlink Keepers
The utility of smart contracts continues to evolve over time. The initial wave of smart contracts were used to issue and assign rights to blockchain-based tokens. Later, developers began leveraging oracle networks to create hybrid smart contracts, which use external data and off-chain computation within on-chain applications to enable new markets such as DeFi, dynamic NFTs, and GameFi. Now, an additional piece of infrastructure is coming to the fore, known as “keepers,” bringing with it new capabilities around smart contract automation.
This article will explore smart contract automation and the possibilities it unlocks. We start by defining smart contract automation, explaining how keepers enable it, and looking at key security and cost considerations around automating smart contract execution, before highlighting the benefits of Chainlink Keepers and showcasing various use cases unlocked by smart contract automation.
If you are a developer and want to dive immediately into the code and technical components, skip to the beginner’s guide tutorial below or visit the Chainlink Keepers documentation.
What Is Smart Contract Automation?
To start, let’s establish a working understanding of smart contract automation in terms of the problem it helps developers overcome.
Problem: Smart Contracts Cannot Auto-Execute
Smart contracts are deterministic programs that run on blockchains. They contain code that states “if x event happens, then trigger y action.” However, smart contracts are not self-executing, meaning their code will not run and make state changes on a blockchain until triggered by an on-chain transaction. The external transaction serves as a “poke” to wake the smart contract up and initiate its logic, similar to how clicking a mouse can start a computer program.
For example, a blockchain-based lending protocol cannot liquidate an under-collateralized loan until an on-chain transaction is made that calls its liquidation function. Once called, the protocol’s liquidation smart contract will verify if the loan is under-collateralized by referencing on-chain price feeds. If under-collateralized, the user’s collateral is liquidated to pay down their debt—otherwise, the transaction reverts.
In some use cases, end-users call on-chain functions directly through their own smart contract interactions. For example, when a user wants to take out a loan in a decentralized money market, their transaction to borrow tokens against their collateral directly triggers the loan issuance function to execute. This function will determine the user’s maximum loan size and then transfer borrowed tokens to their address. However, there are many instances in which smart contracts must perform on-chain functions to maintain the health and utility of the protocol—with no direct user interactions to trigger them.
Solution: Keepers as a Transaction Automation Service
Keepers are externally owned accounts (EOAs) that are incentivized to trigger the execution of smart contracts based on predefined conditions. Conditions are defined in jobs and submitted by a development team, DAO, or protocol users to a keeper network, along with rewards subject to keeper performance. Conditions for smart contract automation are generally based on time (e.g. trigger x function every day at 5:00 pm EST) or events (e.g. trigger y function only when the asset’s price crosses a certain threshold).
Keepers effectively serve as smart contract automation bots that check conditions and make transactions once those predefined conditions have been satisfied. This process often involves the keeper using off-chain computation to execute the same smart contract function that it may eventually call on-chain. Once the function returns true, then the keeper calls that function on-chain by issuing an on-chain transaction. When the function is called, the conditions can be verified on-chain by the protocol’s smart contract before it undergoes a state change, helping ensure that the keeper is correct. The end result is smart contracts that only run on blockchains when needed and according to clearly defined conditions.
While the purpose of keepers is relatively straightforward, it’s important to look at some of the critical technical considerations around keeper implementations in order to understand the advantages of designs such as Chainlink Keepers.
Security and Cost Considerations With Automating Smart Contract Execution
Below are some of the key security risks and cost considerations to take into account when seeking to automate smart contract executions within your dApp.
Manual DevOps and Centralized Servers
One type of keeper implementation involves running a script on a centralized server or having a development team manually monitor conditions and make on-chain transactions. In such a setup, the keeper becomes a centralized point of failure that introduces risk around the untimely execution of smart contract functions (e.g. downtime or delays). Smart contracts that don’t execute when required can lead to asymmetrical exploits and missed opportunities, such as trade slippage, protocol insolvency, and the loss of user funds.
Manual developer operations (DevOps) also place demands on projects’ limited time and resources, which could otherwise go to core product development and ecosystem expansion. Manual DevOps are also likely to become more burdensome over time as projects seek to simplify user experience and add advanced utility to their dApps through smart contract automation. In the end, smart contracts need to be decentralized end to end, including the off-chain keeper infrastructure responsible for triggering their execution.
Costly and Unpredictable Bounties
Another way to design keepers is by offering bounties, with a financial reward allotted to the first keeper to call an on-chain function when certain conditions are met. While this method improves upon the centralized keeper model, it introduces challenges around cost-efficiency, centralization, and unpredictability.
The primary issue with bounties is that keeper nodes end up engaging in direct competition for the winner-takes-all reward, driving priority gas auction (PGA) bidding wars. Competing keepers will keep raising the gas price they’re willing to pay in order to incentivize miners to process their transactions first. Because the smart contract function can only be called once after its conditions are met, only the first keeper succeeds and gets paid for its work. Every other keeper is unsuccessful and incurs a loss when its transaction fails as compensation for gas consumption is not issued. Since most keeper job costs involve a base cost plus a gas fee, PGAs lead to increased costs for end-users, who have to cover the higher gas fees.
Keeper implementations designed around public bounties also fuel several other unintended consequences. For one, PGAs can increase network congestion on blockchains, driving even higher gas prices for keepers and everyone else on the network. Additionally, competition can result in the number of keepers naturally reducing over time to a select few well-capitalized actors who submit aggressive gas prices. Centralized keeper networks decrease reliability by reducing the number of keepers monitoring and submitting transactions.
Another risk with public bounties is that the smart contract has no direct commitment from the keeper to provide timely service. This introduces a level of uncertainty, especially during periods of extreme market volatility and network congestion when keepers are needed most. For example, lending protocols that don’t liquidate toxic positions can become insolvent if the small set of competitive keepers don’t act on time because gas prices are too high, their gas funds run out, or they become preoccupied with other activities.
The takeaway from these keeper designs is that keeper networks need to perform smart contract automation in a cost-effective, tamper-proof, and highly available manner.
Chainlink Keepers: Decentralized, Low-Cost, and Reliable Smart Contract Automation
Instead of relying on centralized keeper setups or taking a risk on competitive public bounties, projects can outsource their smart contract automation to Chainlink Keepers—a decentralized transaction automation service with a proven track record of hyper-reliability and incentive alignment. Chainlink Keepers are already running live on Ethereum and Polygon, with support for many more chains to be added in the future.
Some of the benefits of Chainlink Keepers include:
- Incentivized Jobs—Chainlink Keepers offer a simple framework where users can clearly outline jobs and rewards that a decentralized network of Chainlink nodes commits to, removing competition and creating predictable financial incentives.
- High Uptime—Chainlink Keepers are run by the same professional DevOps teams and enterprises that have a verifiable history of reliability during extreme network congestion and market volatility. Chainlink Keeper nodes already help secure $80+ billion dollars in smart contract value across other Chainlink services (e.g. Price Feeds).
- Low Cost—Chainlink Keepers have several gas-optimizing features that lower the costs of dApp automation, including a rotating node selection process to prevent PGA wars and stabilize costs for users.
- Decentralized Execution—Chainlink leverages a decentralized and transparent pool of Keepers for strong guarantees around secure and timely smart contract execution, saving teams time and mitigating manual intervention or centralized server risks.
- Increased Utility—Chainlink Keepers can perform advanced off-chain computation and generate calldata that’s verifiable by smart contracts, allowing developers to build advanced functionality that was not possible before and without any additional trust assumptions.
- Seamless Integration—Chainlink Keepers can be integrated in a matter of hours to automate smart contracts, with developers provided with simple documentation and step-by-step setup guides.
Through the use of Chainlink Keepers, many projects are now able to push the boundaries of smart contract utility and user experience.
Smart Contract Automation Use Cases Involving Chainlink Keepers
Below are just a few of the many use cases enabled by Chainlink Keepers. There are a plethora of other use cases still yet to be created that are waiting on innovative and creative developers to pioneer.
Automated Yield Harvesting and Debt Repayment
Alchemix is a self-repaying lending protocol that integrated Chainlink Keepers to harvest yield generated by users’ collateral on a daily basis. The yield is then used to repay portions of the debt created by loans without any manual input or developer overhead required.
Decentralized Rebasing of Elastic Supply Tokens
COTI is using Chainlink Keepers to rebase the supply of a volatility token pegged to the Crypto Volatility Index (CVI) each day at midnight UTC time. This establishes stronger assurances that the tokens remain pegged to the underlying CVI index in a fully decentralized manner.
Optimal Rebalancing of Liquidity Provisions
Visor Finance is a management protocol for Uniswap v3 liquidity providers (LPs) that aims to optimize yield returns. Visor Finance uses Chainlink Keepers to reinvest earned fees and new capital deposits into active liquidity positions and single asset limit orders when certain predefined thresholds are crossed. The end result is LPs being able to maintain high asset utilization via efficiently and timely deployed capital.
Minting of Limited-Edition NFTs When Specific Events Happen
The Curse NFT is a dynamic NFT art project that displays positive or negative 3D renderings of model Krystal Schott based on the price movement of ETH. The Curse NFT is using Chainlink Keepers to trigger the NFT to display its final “blessed” form should the price of ETH ever reach $20K USD.
How to Start Creating an Automated dApp With Chainlink Keepers
If you want to start building hybrid smart contract applications today and need some type of external data or computation, refer to our documentation, ask a technical question in Discord, or set up a call with one of our experts.
The post Smart Contract Automation: How to Get Started With Chainlink Keepers appeared first on Chainlink Blog.