DEV Community

Cover image for Learning Xahau: Time as a Variable for Your Business
Ekiserrepe
Ekiserrepe

Posted on

Learning Xahau: Time as a Variable for Your Business

In this Learning Xahau article, we’ll talk about how we can use time as a variable in our project or business without being forced to use an Escrow-type transaction, thanks to the many features of Hooks, the smart contract technology used by Xahau.

If you're new here and don’t yet know what the Xahau blockchain is, it’s a fork of the XRP Ledger base code that includes some improvements not available in the XRP Ledger—like Hooks, which allow Xahau users to implement complex logic easily (smart contracts). If you want more information, I recommend checking out the first article in this series: "Learning Xahau 1".

If you're coming from the XRP Ledger world, you know that you can’t implement custom logic directly on-chain. The only native transaction that allows the use of time in XRP Ledger (and also in Xahau) would be Escrow. Escrows allow locking amounts of the native token (in XRP Ledger) or any token (in Xahau), based on two possible conditions: time-based (lock these funds until a specific date/time), or conditional release, which is somewhat inaccurately named—it essentially unlocks when a "secret" is submitted. Escrows can also combine both conditions. One could argue that the Check transaction type is also time-based since you can add an expiration condition, but that’s not the focus of this article.

In XRP Ledger, if you want to create time-based (or any custom logic) for your transactions, events, accounts, etc., you must build your own backend outside the blockchain. This means you need:

  1. A server and configure it.
  2. A backend that stays connected and doesn't fail at critical moments.
  3. To store secret keys of your accounts on your own or someone else’s server—with all the risks that entails.

In short:

  • Server management.
  • Persistent connection to the network.
  • Private key storage risk.

In Xahau, however, you can create all the conditional logic you want inside a hook, deploy it to your own Xahau account, and everything works on Xahau. The hook can always listen to relevant transactions and execute your desired logic—without your interaction.

This means:

  1. You don’t need a server.
  2. You don’t need backend knowledge.
  3. Your secret keys are never exposed.
  4. Your service only stops if Xahau stops working.

Given these advantages, it makes sense to think of use cases or businesses where time is an important or necessary variable. Some examples:

  • An event happens on a specific date (or multiple).
  • An event is triggered only after some delay.
  • An event occurs only during a defined window of time.
  • An event stops after a certain period or date.

Time isn’t just a useful variable—it can be combined with other conditions, such as:

  • Triggering different transactions depending on the amount.
  • Allowlisting or blocklisting sender or recipient addresses.
  • Requiring a fee for acting as an intermediary.
  • Creating custom rules or constraints.

The possibilities are nearly endless.

How to test time in Xahau?

You can start using the Xahau Hooks Builder, an online IDE to develop and test hooks. There’s also the Hooks Toolkit, a desktop SDK.

Let’s create a hook in the Xahau Hooks Builder and try the ledger_last_time() function, which returns the timestamp at the moment the hook is called.

Here’s the full hook code so you can copy, paste, and modify it in the Xahau Hooks Builder:

//Get timestamp from the last ledger
    int64_t ts = ledger_last_time();
    TRACEVAR(ts);
Enter fullscreen mode Exit fullscreen mode

Image description

As you can see, we've created a variable called ts that stores the execution timestamp (technically of the last closed ledger, but we’ll keep it simple). The timestamp is the number of seconds since a reference date. In our example, the timestamp is 802547943, which corresponds to June 6, 2025.

If we go to XRPL Time Visualizer by Denis Angell, enter the timestamp, and click "From XRPL Time", it will return the current date.

Note: The Xahau/XRPL timestamp is not the same as Unix time. To convert it to Unix time, you must add 946684800 to your timestamp, since Xahau’s base date is Jan 1, 2000, while Unix time starts on Jan 1, 1970 at 00:00:00 UTC.

Image description

Let’s create a variable representing 2 minutes. Since the timestamp is in seconds, we can set it to 120 seconds or 2 * 60. We can use this variable later to add it to other timestamp variables and create conditions.

//Set a variable representing 2 minutes
    int64_t two_min = 2*60;
    TRACEVAR(two_min);
Enter fullscreen mode Exit fullscreen mode

Image description

We can also declare a fixed timestamp for use as a base. In this case, we assign the value 802549410 to my_ts.

    //Set my own ts
    int64_t my_ts = 802549410;
    TRACEVAR(my_ts);
Enter fullscreen mode Exit fullscreen mode

Image description

You can also save a timestamp in a namespace—"simple key-value tables where we store data on Xahau". For example:

    //Save my_ts in a namespace
    state_set(SVAR(my_ts), SBUF(dest_param));
Enter fullscreen mode Exit fullscreen mode

You can verify on the Xahau Testnet de XRPLWin that your account has a declared namespace where the timestamp was saved:

Image description

As you can see, the stored value is A2EED52F00000000, which we need to convert from HEX to a readable format. Using the XRPL Hex Visualizer, enter the value and click "From Hex". You’ll see it returns 802549410, just like the value we saved in the hook.

Image description

Of course, we can also retrieve our timestamp from the namespace later, using this code:

    //Get my_ts from my namespace
    int64_t get_ts;
    state(SVAR(get_ts), SBUF(dest_param));
    TRACEVAR(get_ts);
Enter fullscreen mode Exit fullscreen mode

Image description

From here on, with these tools, your imagination is the limit. If you want to continue building and have any questions you can join to the Discord Xahau Builders server.

Links

Official Website
Official Docs
Hooks Builder
Discord Server
Xahaud
X Account

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.