Enabling automated NFT lotteries with Chainlink Keepers and VRF
CZodiac’s novel use of Chainlink Keepers for the ‘VRF sandwich’ saving over 150 dev hours.
What is CZodiac?
We are known as the Global DeFi Bank on Binance Smart Chain. CZodiac issues lifetime passive rewards to NFT holders, a farming/staking platform, and a high-interest stabletoken all wrapped into one ecosystem.
How CZodiac uses Chainlink Keepers
We are using the term ‘VRF sandwich’ because the Lucky Rabbit contract uses TWO Chainlink Keepers Upkeeps with Chainlink VRF in between! ‘Keepers 1’ will watch and track the amount of CZUSD locked in liquidity via LRT burns, raising the price floor. When a certain amount is locked, it will initiate VRF and send a random number to ‘Keepers 2’ which will perform the minting/buying of a Rabbit NFT and then airdrop it to the random winner.
What is the VRF Sandwich feature?
The VRF sandwich separates the keeper into two parts around a central call to Chainlink VRF. The first keeper triggers the VRF function. Then to save gas and stay under the gas limit, the second keeper fires a second performUpkeep to distribute the NFT via lottery. This method reduces the gas cost while ensuring a safe and fun lottery game.
How CZodiac DeFi Bank integrated Chainlink Keepers
We used the documentation on Chainlink’s website which included great details on the specific process for implementation. All our contracts are developed and tested using hardhat, so adding the Keepers interface and VRF mock was as simple as adding Chainlink’s package to our repository using Yarn. Then we wrote the contracts in solidity inheriting from Chainlink’s interfaces. Testing was done in hardhat using mocks provided by the package.
Creating our Keepers-compatible contract
checkUpkeep Function: Runs off-chain at every block to determine if the performUpkeep function should be called on-chain.
Our checkUpkeep function below uses the checkData to determine the type of checkUpkeep requested, whether the upkeep type is to request the VRF or whether it is to request the final mint.
Similarly, performUpkeep accepts a performData that includes the type of Keeper request as returned by checkUpkeep, then routes the request to the appropriate logic in the smart contract to either request the VRF or to mint the NFT.
After writing our Keepers-compatible contract, we simply registered on the Chainlink Keepers app by following the tutorial here.
Our LSDT_VRF Upkeep registered on the Chainlink Keepers App
Advice for other developers integrating Chainlink Keepers
An important item to consider is whether your call to checkUpkeep is under the gas limit set by Chainlink. There’s a limit to the amount of computation checkUpkeep can do, so make sure your design is under that limit.
Results from the integration
The LRT performed quite well and had a positive reception from our community. Without the Chainlink integration, the dev team would need to constantly monitor the contract to call the performUpkeep manually. The Chainlink integration reduced a major headache for our dev team saving an estimated 150 to 180 hours a year. It also meant that our community would get their Rabbit NFT airdrops immediately instead of waiting for the dev to wake up.
Why CZodiac chose Chainlink Keepers
There are many things worth noting that the automation of Keepers can benefit. Our ecosystem is becoming deeper and more enriched every day, and that calls for more transparent and trustless systems. Chainlink Keepers fills that need for Web3 developers.
About CZodiac DeFi Bank
The Global Defi Investment Bank.
Passive income NFTs. Yields from assets backing CZUSD burn CZF, with profits building a rising price floor. See more at https://czodiac.com or trade CZodiac Official tokens and partner tokens directly on https://cz.cash.
About Chainlink Keepers
Automate your smart contracts using Chainlink Keepers, the decentralized, secure and highly reliable smart contract automation service.
Relying on Chainlink Keepers will help you reduce time spent on DevOps, eliminate automation infrastructure costs, build faster and more efficiently and unlock new dApp capabilities
Learn more about Chainlink Keepers:
Try the App: https://keepers.chain.link/