Yes. You can use a Chainlink external adapter, Gelato Network, or Chainlink alarm clock to achieve this.
In all 3, you'll find that some kind of oracle is always needed. Let's look at an example of using a Chainlink external adapter to do this.
Let's say you have a function called addOne
that you want to call at a random time, and a global variable called count
.
public uint256 count;
function addOne() public {
count = count + 1;
}
And you wanted to call this at random times. You could make a Chainlink call to a node that has a sleep
parameter identified. You'd identify that oracle's address and jobid in a function called randomlyCall
which makes the request, and you'd specify addOne
as the callback function when the oracle finishes the sleep duration. We would need to add some syntax to the addOne
function to make sure only the chainlink oracle can randomly call it. We specify the addOne is the callback function in the 3rd parameter of the buildChainlinkRequest
with:
this.addOne.selector
public uint256 count;
function addOne(bytes32 _requestId) public recordChainlinkFulfillment(_requestId){
count = count + 1;
}
function randomlyCall() public {
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.addOne.selector);
sendChainlinkRequestTo(oracle, req, fee);
}
On the Chainlink side, we add an external adapter to the job specifications with some code to pick a random time. You could do this in nodejs for example with something like
index.js
const duration = Math.random() * ceiling
const wait = async () => {
await sleep(duration)
}
wait()
You can learn more about external adapters here. Once you have this defined, you could have a network of Chainlink nodes run similar functions so that you're not depending on a centralized oracle to randomly call your function.
The only issue with this method is proving that the number the oracles come up with is random. You could use a Chainlink VRF to get a provably random number and use that as an input to your external adapter.
Note: I am a Chainlink developer advocate