Basic Implementation

LiquidScheduler Consumer Example Contract used in unit tests

in zeus_boxes/contracts/eos/cronconsumer/cronconsumer.cpp The consumer contract is a great starting point for playing around with the LiquidScheduler syntax.
1
#include "../dappservices/cron.hpp"
2
​
3
#define DAPPSERVICES_ACTIONS() \
4
XSIGNAL_DAPPSERVICE_ACTION \
5
CRON_DAPPSERVICE_ACTIONS \
6
​
7
#define DAPPSERVICE_ACTIONS_COMMANDS() \
8
CRON_SVC_COMMANDS()
9
​
10
#define CONTRACT_NAME() cronconsumer
11
​
12
CONTRACT_START()
13
​
14
TABLE stat {
15
uint64_t counter = 0;
16
};
17
typedef eosio::singleton<"stat"_n, stat> stats_def;
18
​
19
TABLE payloadtbl {
20
string payload = "";
21
};
22
typedef eosio::singleton<"payloadtbl"_n, payloadtbl> payload_def;
23
24
bool timer_callback(name timer, std::vector<char> payload, uint32_t seconds){
25
if(timer.to_string() == "aborttest") eosio::check(false, "{abort_service_request}");
26
stats_def statstable(_self, timer.value);
27
stat newstats;
28
if(!statstable.exists()){
29
statstable.set(newstats, _self);
30
}
31
else{
32
newstats = statstable.get();
33
}
34
if(payload.size() > 0) {
35
payload_def payloadtable(_self, timer.value);
36
payloadtbl newpayload;
37
string payload_string(payload.begin(), payload.end());
38
newpayload.payload = payload_string;
39
payloadtable.set(newpayload, _self);
40
return false;
41
}
42
newstats.counter++;
43
statstable.set(newstats, _self);
44
// reschedule
45
return (newstats.counter < 45);
46
}
47
​
48
// test scheduling timer scoped to _self with 2s interval
49
[[eosio::action]] void testschedule(uint32_t interval) {
50
// optional payload for data to be passed to timer_callback
51
std::vector<char> payload;
52
schedule_timer(_self, payload, interval);
53
}
54
​
55
// test setting interval
56
[[eosio::action]] void testinterval(uint32_t interval) {
57
// optional payload for data to be passed to timer_callback
58
std::vector<char> payload;
59
start_interval(_self, payload, interval);
60
}
61
​
62
// remove interval
63
[[eosio::action]] void rminterval() {
64
remove_interval(_self);
65
}
66
​
67
// test multiple timers by scoping each timer by account with 2s interval
68
[[eosio::action]] void multitimer(name account, uint32_t interval) {
69
// optional payload for data to be passed to timer_callback
70
std::vector<char> payload;
71
schedule_timer(account, payload, interval);
72
}
73
​
74
// remove timer by scope
75
[[eosio::action]] void removetimer(name account) {
76
remove_timer(account);
77
}
78
​
79
// test passing payload to timer_callback
80
[[eosio::action]] void testpayload(name account, std::vector<char> payload, uint32_t seconds) {
81
schedule_timer(account, payload, seconds);
82
}
83
​
84
// test passing payload to timer_callback
85
[[eosio::action]] void testabort(name account) {
86
std::vector<char> payload;
87
schedule_timer(account, payload, 2);
88
}
89
CONTRACT_END((testschedule)(multitimer)(removetimer)(testpayload)(testabort)(testinterval)(rminterval))
90
​
Copied!
Last modified 8mo ago
Copy link