The time-based actions feature in Hive Stream is one of my favourite features. It allows you to perform cron jobs inside your Hive Stream contracts. If you wanted to act every hour, you could add a time-based action that would execute code every hour (provided the server stream is running, of course).
Fortunately, Hive Stream comes with some contracts out of the box already. Notably a Coinflip and Lotto contract. Our lotto contract already has time-based functionality for running draws every X amount of time.
In our contract, we have two types of lottery. An hourly and daily lottery.
A simple example of how we can take our lotto contract and run hourly or daily draws can be seen below.
import { TimeAction, Streamer, LottoContract } from 'hive-stream';
// Create a new streamer instance
const streamer = new Streamer({
ACTIVE_KEY: ''
});
// Register our lotto contract as "hivelotto"
streamer.registerContract('hivelotto', new LottoContract());
// Create an hourly action for drawing the hourly lottery
const hourlyLottoDrawAction = new TimeAction('1h', 'hourlylottery', 'hivelotto', 'drawHourlyLottery');
// Create a daily action for drawing the daily lottery
const dailyLottoDrawAction = new TimeAction('24h', 'dailylottery', 'hivelotto', 'drawDailyLottery');
// Register our time-based actions
streamer.registerAction(hourlyLottoDrawAction);
streamer.registerAction(dailyLottoDrawAction);
// Start the streamer
streamer.start();
We have two time-based actions every hour and every 24 hours (one day).
What is happening here might be mostly self-explanatory. But let's break down what a TimeAction instance accepts.
The TimeAction instance accepts the following values (all values are in the order they are supplied to the instance):
timeValue - When should this action be run?uniqueId - A unique ID to describe your actioncontractName - The name of the contractcontractMethod - The method we are calling inside of the contractdate - An optional final parameter that accepts a date of creationAt the moment, the timeValue passed in as the first argument to TimeAction cannot accept just any value. However, there are many available out-of-the-box with more flexibility to come in the future.
3s or block will run a task every block (3 seconds, approximately)30s will run a task every 30 seconds1m or minute will run a task every 60 seconds (1 minute)15m or quarter will run a task every 15 minutes30m or halfhour will run a task every 30 minutes1h or hourly will run a task every 60 minutes (every hour)12h or halfday will run a task every 12 hours (half a day)24h or day will run a task every 24 hours (day)Remember, while time-based actions can be helpful, don't rely on them to always go to plan. If your streamer goes down for whatever reason and a time-based action elapses (say an hourly one), it might not run until the following hour (or whatever time value you choose).
Also, please note that the contracts with Hive Stream should be tested first if you plan to use them. They haven't been used in a production environment before. I am working on test coverage for these, which will be in a future release.
Happy time actioning!