schedule_timer

schedule_timer | code​

1
/**
2
* LiquidScheduler uses the schedule_timer macro to schedule a cron action on chain by adding a timer to the timerentry singleton
3
*
4
* @param {name} timer - account name to scope the timer within
5
* @param {std::vector<char>} payload - payload to be accessed within the timer_callback function in the consumer contract
6
* @param {uint32_t} seconds - seconds to repeat the cron
7
*
8
* Example:
9
*
10
* @code
11
* [[eosio::action]] void testschedule() {
12
* std::vector<char> payload;
13
* schedule_timer(_self, payload, 2);
14
* }
15
* @endcode
16
*/
17
​
18
TABLE timerentry { \
19
int64_t set_timestamp = 0; \
20
int64_t fired_timestamp = 0; \
21
};\
22
typedef eosio::singleton<"timer"_n, timerentry> timers_def;\
23
​
24
static void schedule_timer(name timer,std::vector<char> payload, uint32_t seconds){ \
25
timers_def timers(current_receiver(), timer.value); \
26
timerentry newtimer; \
27
if(timers.exists()){ \
28
newtimer = timers.get(); \
29
} \
30
newtimer.fired_timestamp = 0;\
31
newtimer.set_timestamp = eosio::current_time_point().time_since_epoch().count();\
32
timers.set(newtimer, current_receiver()); \
33
SEND_SVC_REQUEST(schedule, timer, payload, seconds); \
34
} \
35
​
36
SVC_RESP_CRON(schedule)(name timer,std::vector<char> payload, uint32_t seconds,name current_provider){ \
37
timers_def timers(current_receiver() , timer.value); \
38
if(!timers.exists()) \
39
return; \
40
auto current_timer = timers.get(); \
41
if(current_timer.fired_timestamp != 0 || (current_timer.set_timestamp + (seconds * 1000000) > eosio::current_time_point().time_since_epoch().count()))\
42
return; \
43
current_timer.fired_timestamp = eosio::current_time_point().time_since_epoch().count(); \
44
timers.set(current_timer, current_receiver()); \
45
if(!timer_callback(timer, payload, seconds)) \
46
return; \
47
schedule_timer(timer, payload, seconds);\
48
} \
Copied!
Last modified 4mo ago
Copy link