# Adding cycles to motoko futures

**URL:** <https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [October 18, 2024, 7:54pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323 "2024-10-18T19:54:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 18, 2024, 7:54pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/1 "2024-10-18T19:54:09Z")

</div>

A bit of a technical question here.

If I’m using futures and making a bunch of call before awaiting them later, do I do Cycles.add(xxxxxxx) before each future call? or do I wait and do it when I actually await the future later? I’m assuming it is before the future and that motoko will take care of adding each set of cycles to the correct call, but I thought it was worth asking.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [October 18, 2024, 9:10pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/2 "2024-10-18T21:10:08Z")

</div>

I’ve tested the approach you describe in production and it seems to work predictably, at least with where the follow-up call is to `deposit_cycles` on the management canister

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [October 18, 2024, 10:03pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/3 "2024-10-18T22:03:25Z")

</div>

At the call site when you send the message.  
Not at the await.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [October 23, 2024, 12:13pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/4 "2024-10-23T12:13:23Z")

</div>

Just a heads-up. I am in the process of re-working `Cycles.add`. The final solution might look like

```auto
actor {
  func bar(a : actor { foo : () -> async () }) : async () {
    await (with cycles = 999) a.foo();
    await (with cycles = 4242) async ();
  }
}

```

Here the `with`-note (parenthetical) applies to the message send to its right.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 23, 2024, 12:42pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/5 "2024-10-23T12:42:35Z")

</div>

Would this also work without the await? So we can create a future with reserved cycle allocation to be waited later? And would it work with async\*(this seems hard as there may be many awaits in an async\*.)

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [October 23, 2024, 12:54pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/6 "2024-10-23T12:54:10Z")

</div>

Yes, the `await` is irrelevant, as it (the note) augments the send itself. Yes, it will result in a future that has already sent out the message (with the cycles included as indicated). And, no, it doesn’t apply to `async*`, as that is not a send per se (but can expand to sends, which need to be annotated there).

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 23, 2024, 12:59pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/7 "2024-10-23T12:59:15Z")

</div>

Would it make sense to put this in \<\> like the system annotations? Or does that confuse the issue too much?

‘await a.foo\<withCycles(1\_000\_000)\>()’

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [October 23, 2024, 1:05pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/8 "2024-10-23T13:05:57Z")

</div>

I think that is a different concept. `<system, ...>` passes a capability to a library, while parentheticals modify the AST node that they are affixed to. Those syntax nodes can be calls or `async` expressions and (later, possibly) operator expressions too.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [October 23, 2024, 6:57pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/9 "2024-10-23T18:57:40Z")

</div>

How will this work for multiple inter-canister call settings, like those with the new scalable messaging model or extend to other message settings as the protocol adopts them? In this case, maybe some sort of record syntax might be more familiar?

I might imagine the call looking something like this

```auto
  await with({
    cycles = 999;
    // message type
    messageType = #best_effort;;
    // the call being made
    call = a.foo();
  });

```

And then not breaking current semantics

```auto
  await a.foo()

```

or

```auto
  ignore a.foo()

```

My point here is that parentheses aren’t as intuitive to use if there are multiple settings associated with the call.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [October 23, 2024, 9:58pm UTC](https://forum.dfinity.org/t/adding-cycles-to-motoko-futures/36323/10 "2024-10-23T21:58:02Z")

</div>

What I have implemented so far indeed borrows elements of the record syntax:

```js
    let defaults = { timeout = #seconds 3; retries = 4 };
    ...
    await (defaults with cycles = 495; sizeLimit = 1024) canister.call(<args>);

```

Note that all attributes other than `cycles` are speculative at this point.

Also the whole syntax thing is not cast in stone yet. I find it [cute](https://www.studiobinder.com/blog/what-is-a-parenthetical-in-a-script/), but there might be better solutions. Keep the ideas coming, but remember that it must be unambiguously parseable and intuitive too in order to make sense at all.
