Smart Contract Triggering with Chainlink Automation

Joe
Web3 Writers Guild
Published in
5 min readNov 14, 2023

You might have properly written a smart contract before and you have been told that smart contracts functions must be triggered either by an EOA (Externally owned account) or another smart contract.

And then for every time, you want to make a change to your contract storage, you have to trigger a function using your EOA or writing another smart contract. If you are coming from the web2 space or you have a slight idea of how the backend system works in web2 space. You will know about cron jobs.

Cron jobs are a powerful tool for automating tasks in Node. js. They allow you to run code at specific intervals, such as every minute, hour, day, week, or month. This can be used to do things like send emails, run backups, or update data.

What if you can implement something like that in your smart contracts, and then you don’t have to be the one doing the job of calling functions yourself.

Welcome to Chainlink Automation. Let me take you through a summary of how it works

Chainlink Automation are more like triggers that trigger functions executions in smart contracts and it is categorized into three triggers and links to Chainlink to read more ont hem, and there are as follow;

Time-based trigger: Use a time-based trigger to execute your function according to a time schedule. This feature is also called the Job Scheduler and it is a throwback to the Ethereum Alarm Clock. Time-based trigger contracts don’t need to be compatible with theAutomationCompatibleInterface contract.

Custom logic trigger: Use a custom logic trigger to provide custom solidity logic that Automation Nodes evaluate (off-chain) to determine when to execute your function on-chain. Your contract must meet the requirements to be compatible with the AutomationCompatibleInterface contract. Custom logic examples include checking the balance on a contract, only executing limit orders when their levels are met, any one of our coded examples, and many more.

Log trigger: Use log data as both trigger and input. Your contract must meet the requirements to be compatible with the AutomationCompatibleInterface contract.

For this example, I will be showing you an implementation of chainlink Automation using Time-based trigger. I will take you through a simple contract written in solidity.

We will create a contract called automatedPay that allows

// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.18;

contract AutomatedPay {
address payee;
uint unlocked;
uint lockedTime;
address owner;

constructor(address _payee, uint _unlocked) {
payee = _payee;
unlocked = _unlocked;
lockedTime = block.timestamp;
owner = msg.sender;
}

error TimeNotReached();

function deposit() external payable {
require(msg.value > 0, "ZeroAmount");
}

function withdraw() external {
require(msg.sender == owner, "Unauthorized");
if (block.timestamp <= (lockedTime + unlocked)) {
revert TimeNotReached();
}
(bool sent, ) = payee.call{value: address(this).balance}("");
require(sent, "Failed to send Ether");
}

receive() external payable { }

}

The smart contract above allows withdrawal of funds after the locked time has been reached. The unlocked time is set at constructor level, the account to pay is also set at constructor level.

Deploy the contract above and save the address, you will needing it. Deposit any amount of ethers into the contract and then any account to receive the ethers when the time elapses.

The withdrawal function can only be call by the account that deployed the contract. Now in the case that we ain’t closed accessing our wallet, what then happens, we will then have to wait till the we do.

But with chainlink automation, we can automate the contract to pay out the funds when the time elapses.

Let’s get to that part.

First of all, we will need to set a Time-Based Upkeep to carry out the trigger,

If you are deploying to sepolia, then you will need some sepolia teest net. If its Polygon, then you will need some matic tetsnet.
I deployed my contract to Sepolia, so I will be preceeding with sepolia.

Proceed to Chainlink Automation to register a new upkeep.

Then select the type of trigger mechanism you want to implement for your contract. For this implementation, am using Time-based automation.

Moving on, you will add the deployed contract address, as well as the ABI of the compiled contract.

Then you will select the function you want chainlink to automatically trigger when the time you will set reaches.

Finally, you will set the time that you want the function to be triggered. Since we are locking the fund till a certain time. You have to add a time that will be after the unlocked time you set when deploying the contract.

From the image below, you can see that I set a time (30minutes after), then our function will be triggered.

After following every step, you should see an overview like the image below;

After 30 minutes, you will see that the withdraw function was triggered. The contract I wrote and deployed had the withdrawn function triggered by chainlink.

With the illustration above, you can do more with chainlink automation. Different concepts can be implemented using this feature provided by chainlink. You can at anytime, stop the time trigger action, pause and resume the trigger actions, etc.

Applications like staking contracts, lottery games, voting contracts, etc can all be built and have its functions triggered by time, an event or a customized logic.

You can explore other functionalities as well, also try implementing them too.

--

--

Joe
Web3 Writers Guild

Smart Contract. Pisces. Music. Books over People. above all, Gigantura Indica.