Ledger

Assets & currency conversion

Model asset exchanges and currency conversions with Ledger and Numscript.

The ledger handles assets — the generic term for anything tracked on the ledger, including currencies, crypto, stocks (like AAPL), loyalty points, and more. Each asset has a name and an optional decimal precision. For example, USD/2 means US dollars stored as integer cents. If you're not familiar with asset notation, read Assets first.

Funded vs non-funded#

Decide first whether the conversion is funded. That is the difference between a conversion and an exchange.

Non-funded conversion#

When you're exchanging currency A for currency B, the latter needs to come from somewhere. If you're trading against the @world account, you're effectively introducing new units of B in an unbounded way into the ledger - which the ledger treats as a non-funded conversion. It is the shortest way to perform a conversion. You still set the amount, but no reserve balance caps how much currency B enters the ledger.

Funded exchange#

The other way to perform a conversion is to trade against a funding account that you control, and you can ensure that the amount of currency B you're trading is limited by the amount of currency B you previously introduced in the ledger in a separate process, which the ledger treats as a funded conversion.

An example case where a funded conversion is preferable could be a BTC buying platform. In this example, users acquire BTC from you and you will enable them to hold both BTC and USD in their accounts, letting them exchange their USD for BTC. In this case, the total amount of BTC held by your users should be limited by the amount of BTC you actually own in your reserves. By using a funded conversion and sourcing BTC granted to users from a previously provisioned account in the ledger, you can ensure that you are not accidentally creating BTC out of thin air.

Single-phase vs multi-stages#

The other thing to consider is whether you want to perform the asset conversion in a single step, or in multiple stages. In the case of a single step conversion, you will be performing the conversion in a single atomic transaction posted to the ledger. This is the simplest way to perform a conversion, but it is not always possible and assumes that the desired asset is already available for exchange.

A typical good case for multi-stage conversion, is a conversion that involves the intervention of different parties or a delay in the conversion process. For example, if you enable your users to trade some USD for AAPL, and you are using a broker behind the scenes for the acquiring of AAPL. In this scenario, the best practice is to use a swap account as described below:

In this example, each edge is labeled with the step it belongs to:

  1. The first step represents the user placing some USD in the swap account.
  2. The second step represents a step where the logic of acquiring the AAPL would be happening in your backend, with them eventually being sent to the swap account in exchange for its whole balance of USD.
  3. Finally, the third step is the user exchanging the AAPL acquired by the broker for AAPL in their account.

Each of these steps can be performed in a separate ledger transaction, as the swap account is being used as a temporary holding location for the exchange process.

Additional best practices#

Exchange rate#

For auditability purposes, it is recommended that you store the exchange rate used for the conversion in the transaction metadata. This will enable you to audit the conversion process and ensure that the conversion rate used is the one you intended to use.