algorandfoundation / algokit-utils-ts

MIT License
19 stars 8 forks source link

Time management utilities #313

Open cusma opened 2 months ago

cusma commented 2 months ago

Problem

For some use cases, particularly when Smart Contract are based on time or blocks' height conditions, it is useful to conveniently "wait" for a given timestamp or block.

Moreover, when things run on a LocalNet in DevMode it is possible to "fast forward" both time and blocks.

It would be very useful if such methods could have a nice UX (a less boilerplate code).

Solution

Add to AlgorandClient and LocalNet (or some better place) convenient methods to fetch time/blocks, wait for time/blocks conditions, jump to time/block (in DevMode).

Proposal

Add to the AlgorandClient the following methods:

In order to speed up tests when things run LocalNet (in DevMode), the following methods can be added (not in AlgorandClient, since it's DevMode specific):

Specifically for timeWarp I'd suggest the following implementation to jump directly to toTimestamp:

  1. Get the latestTimestamp with getLatestTimestamp
  2. Set the timestamp offset with the Algod method to: toTimestamp - latestTimestamp
  3. Generate a block to commit the desired offset (this could use blockWarp with a single block generation)
  4. Reset the timestamp offset to 0

Pros and Cons

Pros: speed up test, less boilerplate code, nicer UX. Cons: none.

Dependencies

Algorand SDK and LocalNet in DevMode

robdmoore commented 2 months ago

My suggestion for the design of this would be a new NetworkManager class is added (similar to AppManager, AssetManager, etc.) with the following signatures from AlgorandClient:

We could also proxy or maybe even move some of the methods on ClientManager like isTestNet() and potentially proxy some of the localnet specific things to hang off algorand.network.localNet. in other classes like localNetDispenser on AccountManager as one of a number of examples.

robdmoore commented 2 months ago

All of them should wrap the underlying algosdk types, similar to getById in AssetManager and getInformation in AccountManager and do the similar treatment like convert round numbers to bigint etc. to follow AlgorandClient conventions.