# Ic-cron library tutorials

**URL:** https://forum.dfinity.org/t/ic-cron-library-tutorials/10714
**Category:** Developers
**Created:** [February 5, 2022, 12:32pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714 "2022-02-05T12:32:03Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 5, 2022, 12:32pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/1 "2022-02-05T12:32:03Z")

</div>

Hey everybody. I’m working on a series of tutorials about [`ic-cron`](https://github.com/seniorjoinu/ic-cron). This is the first one about how one can extend Sonic exchange by leveraging Open Internet Services paradigm.

Please, take a look. I would be very thankful if you could provide any feedback (post it here or in comments) and if you could repost it through your social media channels.

> **[Tutorial: Extending Sonic With Limit Orders Using ic-cron Library | HackerNoon](https://hackernoon.com/tutorial-extending-sonic-with-limit-orders-using-ic-cron-library)**
>
> In this tutorial we build a crypto trading bot canister on Internet Computer (Dfinity) which can execute limit orders on AMM-based exchange - Sonic.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 5, 2022, 12:35pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/2 "2022-02-05T12:35:41Z")

</div>

By the way, `ic-cron` was promoted to version `0.5`!

What’s new:

- simplified API (no more task `kind` - use your own type to differentiate between tasks);
- added `delay` scheduling argument (now you can schedule a task to execute each monday 3PM at any other day of week);
- cron state is now candid-serializable, which means that you can persist it between canister upgrades (github readme contains an example of this).

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [February 5, 2022, 3:38pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/3 "2022-02-05T15:38:57Z")

</div>

What would it take to have ic-cron library in Motoko instead of Rust?

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 5, 2022, 3:41pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/4 "2022-02-05T15:41:31Z")

</div>

I don’t know actually.  
Some simple (and non-efficient) version should be pretty easy to implement. But `ic-cron` uses `BinaryHeap` collection, that makes it a lot more faster. So if you want a Motoko version of this library to have the same performance it will take same effort.

---

<div class="post-metadata">

### Author: ![coin\_master](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/coin_master/32/15963_2.png) [@coin\_master](https://forum.dfinity.org/u/coin_master)
#### Post date: [February 6, 2022, 6:36am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/5 "2022-02-06T06:36:05Z")

</div>

Thanks that’s really helpful, I have learned many things that I wasn’t aware of.

---

<div class="post-metadata">

### Author: ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)
#### Post date: [February 6, 2022, 6:40am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/6 "2022-02-06T06:40:09Z")

</div>

FWIW there is a [Heap](https://smartcontracts.org/docs/base-libraries/Heap.html) library in Motoko, but I haven’t personally tried it.

---

<div class="post-metadata">

### Author: ![ComputerInternetMan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/computerinternetman/32/1848_2.png) [@ComputerInternetMan](https://forum.dfinity.org/u/ComputerInternetMan)
#### Post date: [February 6, 2022, 11:45pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/7 "2022-02-06T23:45:05Z")

</div>

Appreciate your effort on this Alexander. A very nice contribution!

---

<div class="post-metadata">

### Author: ![davidxiao](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/davidxiao/32/4554_2.png) [@davidxiao](https://forum.dfinity.org/u/davidxiao)
#### Post date: [February 7, 2022, 10:24am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/8 "2022-02-07T10:24:06Z")

</div>

@senior.joinu , really valuable, it’s exactly what I am looking for as a starter for IC,

hope to have more such similar articles.

I feel it’s not easy to get the deep/practical knowledge for starter, reviewing official docs is not enough.

Getting a proper job in the field seems not easy as well at present(any other guys have similar concerns? ),  
BTW, pls drop me a msg for details if there are proper opportunities(fullstack or backend), I think i am professional and love IC, Rust 🙂

---

<div class="post-metadata">

### Author: ![coin\_master](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/coin_master/32/15963_2.png) [@coin\_master](https://forum.dfinity.org/u/coin_master)
#### Post date: [February 9, 2022, 4:26am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/9 "2022-02-09T04:26:03Z")

</div>

@senior.joinu  
I have tried to deploy the example locally but I keep getting some error

```auto
The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister rrkah-fqaaa-aaaaa-aaaaq-cai trapped explicitly: Custom(No more values on the wire, the expected type principal is not opt, reserved or null)

```

dfx version `0.9.0`

Steps I did.

`dfx start --clean`

Create canisters to get the local wallet canister needed for the next command  
`dfx canister create --all`

`dfx deploy --wallet rwlgt-iiaaa-aaaaa-aaaaa-cai --argument '(principal "my_principal")'`

This builds successfully but fail at installing  
`Installing canisters... Creating UI canister on the local network. The UI canister on the "local" network is "ryjl3-tyaaa-aaaaa-aaaba-cai" Installing code for canister ic-cron-sonic-bot-example, with canister_id rrkah-fqaaa-aaaaa-aaaaq-cai The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister rrkah-fqaaa-aaaaa-aaaaq-cai trapped explicitly: Custom(No more values on the wire, the expected type principal is not opt, reserved or null)`

Also deploying to production --network ic returns the same error.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 9, 2022, 12:01pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/10 "2022-02-09T12:01:08Z")

</div>

Did you tried that?

> [@Upgrade canister on ic with new dfx version](https://forum.dfinity.org/t/upgrade-canister-on-ic-with-new-dfx-version/10546/7):
>
> You’ll have to upgrade your wallet canister and run a command to upgrade your other canisters in order to use dfx 0.9.0. This is documented in the [changelog](https://github.com/dfinity/sdk/blob/master/CHANGELOG.adoc#feat-remove-the-wallet-proxy-and-the---no-wallet-flag) and in the [release notes](https://smartcontracts.org/docs/release-notes/0.9.0-rn.html). After running the steps to upgrade, you should not need the --wallet parameter. Unfortunately we’ve published the dfx 0.9.0 release notes, as well as updated the docs to use dfx 0.9.0, but have not yet promoted dfx 0.9.0. Breaking change: Canister commands, except for dfx canister create, will make the call dir…

This posts says that there is no need in `--wallet` argument if you did upgrade your dfx now.

---

<div class="post-metadata">

### Author: ![coin\_master](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/coin_master/32/15963_2.png) [@coin\_master](https://forum.dfinity.org/u/coin_master)
#### Post date: [February 9, 2022, 6:12pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/11 "2022-02-09T18:12:30Z")

</div>

Yes unfortunately this doesn’t work and also upgrading the wallet fails for some reason.  
Most probably it’s related to the latest breaking changes with dfx 0.9.0

---

<div class="post-metadata">

### Author: ![coin\_master](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/coin_master/32/15963_2.png) [@coin\_master](https://forum.dfinity.org/u/coin_master)
#### Post date: [February 10, 2022, 2:13pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/12 "2022-02-10T14:13:01Z")

</div>

I found a work around, not sure it’s correct but it unblocked me for now.

```auto
pub fn init() {
    let controller = caller();
....
}

```

I have another question, is there a way to test our code locally against the deployed Sonic canisters?  
If I deployed that code locally it will try to find sonic canisters locally not on the mainnet and of course all the calls fails.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 10, 2022, 7:28pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/13 "2022-02-10T19:28:11Z")

</div>

Hmm, I’m not sure if this workaround will work. Or at least you have to only call your canister from your own principal or only from your wallet.

Sonic’s code is not open sourced yet, so there is no way of testing locally.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 16, 2022, 12:22pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/14 "2022-02-16T12:22:25Z")

</div>

![dj-khaled-another-one (1)](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/0/011266cfcf565c0666af8986359a415b8c230b34.gif)

> **[How to Execute Background Tasks on Particular Weekdays with IC-Cron and Chrono...](https://hackernoon.com/how-to-execute-background-tasks-on-particular-weekdays-with-ic-cron-and-chrono)**
>
> In this tutorial we use ic-cron library in order to execute tasks on Internet Computer (Dfinity) at a specific days of week.

---

<div class="post-metadata">

### Author: ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)
#### Post date: [February 22, 2022, 7:18am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/15 "2022-02-22T07:18:33Z")

</div>

Random question: do you know if ic-cron is cycle-intensive or is cheaper than the Motoko heartbeat functionality? See [here](https://forum.dfinity.org/t/why-does-a-canister-keep-consuming-cycles/10821/12) for context.

My guess is that it’s equally as cycle-intensive (since the issue is on the system level and not programming language dependent), but I could be wrong.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [February 22, 2022, 11:52am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/16 "2022-02-22T11:52:29Z")

</div>

Hmm… I don’t notice any problem with expensiveness. For a tutorial about Sonic I left my canister with cron task to keep polling exchange rate for a week. And it only wasted around $1 within that time.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [March 1, 2022, 7:39pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/17 "2022-03-01T19:39:47Z")

</div>

![dj-khaled-another-one (1)](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/0/011266cfcf565c0666af8986359a415b8c230b34.gif)

> **[Introduction To ic-cron Library](https://dev.to/seniorjoinu/introduction-to-ic-cron-library-17g1)**
>
> In this tutorial we’ll go through this functionality of ic-cron in more details, covering advantages of using this library comparing to standard API as well as some examples of how task scheduling with ic-cron works.

---

<div class="post-metadata">

### Author: ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)
#### Post date: [March 1, 2022, 7:40pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/18 "2022-03-01T19:40:18Z")

</div>

![dj-khaled-another-one (1)](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/0/011266cfcf565c0666af8986359a415b8c230b34.gif)

> **[Tutorial: How To Build A Token With Recurrent Payments On The Internet...](https://dev.to/seniorjoinu/tutorial-how-to-build-a-token-with-recurrent-payments-on-the-internet-computer-using-ic-cron-library-3l2h)**
>
> In this tutorial, I’ll show you how to extend your token in order to add recurrent payment functionality to it using ic-cron library.

---

<div class="post-metadata">

### Author: ![lshoo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lshoo/32/5847_2.png) [@lshoo](https://forum.dfinity.org/u/lshoo)
#### Post date: [March 19, 2022, 12:05pm UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/19 "2022-03-19T12:05:15Z")

</div>

excellent guide, thanks!

---

<div class="post-metadata">

### Author: ![Iceypee](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/iceypee/32/5362_2.png) [@Iceypee](https://forum.dfinity.org/u/Iceypee)
#### Post date: [May 10, 2022, 3:46am UTC](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714/20 "2022-05-10T03:46:56Z")

</div>

Thats wierd that @senior.joinu found it cheap. Now im not famliar with the rust side of things but is the heartbeat the same heartbeat timeframe as motoko (I think every block)?

If so, It doesnt seem right that on motoko its much more expensive if this iccron library calls the heartbeat call as frequently as on motoko.

[Next page](https://forum.dfinity.org/t/ic-cron-library-tutorials/10714.md?page=2)
