Skip to content

Conversation

@0xphilipp
Copy link
Contributor

@0xphilipp 0xphilipp commented Feb 25, 2023

This pr will allow some very powerful schedules based on a cron timer. It is only a PoC and if interested I will also adjust the tests to run properly.

It only supports basic crons (with numbers) for performance / gas reasons.

It uses chrono (https://github.com/chronotope/chrono) to convert a unix timestamp to a datetime with components to start the search and convert the result back)

For the search structure the tm structure has been copied: https://docs.rs/mysql/14.0.0/mysql/time/struct.Tm.html

The implementation is based on https://crates.io/crates/crontab with adjustments for weekday support.
This is the major difference to normal cron, that weekdays are complimentary to all other options, which is a lot more sane in my opinion than in the normal implementation. It allows for powerful options like recurring jobs executing exactly first monday of a month (what we will need for our amp governance tune+redelegation)

There is this for blockchains optimized version: https://github.com/CronCats/Schedule
It is a bit more powerful, but not as lightweight as what could be enough for warp.

I feel like the used version is a very clean, straight forward and performant implementation. That is why I always prefer direct integrations for dependencies. Not sure about chrono, but it seems it is okay to run on the chain.

Usage is quite similar to the example in #15

        let result = await context.sdk.createJob(context.accountAddress, {
          condition: {
            expr: {
              uint: {
                left: {
                  // access current_time when executing
                  env: 'time',
                },
                op: 'gt',
                right: {
                  ref: 'next_execution'
                }
              }
            },
          },
          recurring: true,
          requeue_on_evict: true,
          vars: [
            {
              static: {
                kind: 'uint',
                name: 'next_execution',
                value: '1',
                update_fn: {
                  on_error: { ... }
                  on_success: {
                    uint: {
                      env: {
                        // execute 5:00 (0 5) on first - seventh day of a month (1-7), but only on monday (1)
                        // That means execute first monday of a month at 5:00
                        next_time_for_cron: '0 5 1-7 * 1'
                      }
                    },
                  },
                },
              },
            },
          ],
          msgs: [
            {
              wasm: {
                execute: {
                  contract_addr: context.hub,
                  funds: [],
                  msg: base64encode({ harvest: {} }),
                },
              },
            },
          ],
          name: 'eris-harvest',
          reward: '10000',
        });

@simke9445
Copy link
Contributor

simke9445 commented Mar 2, 2023

What'd be good here is to extract the cron utils to a package (i.e. crontab-plus) and use it as a dependency in warp contracts. More practical in terms of audits.

@0xphilipp
Copy link
Contributor Author

yes I have thought about that and started doing it. It is deeper nested than expected. If you want I can move everything into the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants