DOT.News » Kintsugi BTC

All the latest news about Polkadot, Kusama and its strong ecosystem

​​Handling Money Cross-Chain In JavaScript

Published date: August 16 2021

Introducing monetary-js: a library for dealing with the myriad of digital currencies in a safe multi-chain future

Digital Money in JavaScript Today

Today CoinGecko lists 8911 currencies on its website. Back when I started working on crypto there were far less currencies and also a lot less chains over which monetary amounts were transferred. Handling money is already hard in itself but with an increased number in currencies and chains, it becomes even more difficult.

You will likely feel my pain if you ever tried to implement an application handling more than two currencies. JavaScript’s native `Number` is not the way to go, floating point math usually ends up being wrong, and using strings also leaves you nowhere to go the moment you want to do arithmetic operations or comparisons. Moreover, there is clearly no common standard in terms of decimals for various currencies.

Add in the need to display numbers to users in a digestible way to your internal representation and you end up implementing a lot of custom logic per currency. Typically, 0.001 DAI are rather an irrelevant amount of money whereas 0.001 BTC are likely not.

It gets even more complicated when you envision a multi-chain architecture where a user might hold balances of a certain currency on multiple chains at the same time. These assets might be the same from a user’s perspective, but transferring e.g. USDC on different chains will require different methods to be called.

Introducing monetary-js

As we are building our cross-chain Bitcoin bridge, we discovered that we ran into exactly the same problems. To tackle these issues, we provide a monetary library that serves as a common interface to deal with monetary amounts in a safe and predictable way.

The overall goal of this library is two-fold:

  1. Provide a secure currency interface. This includes safe arithmetic, type checking to ensure conversions between currencies are done correctly, safe conversion between denominations, and correct rounding.
  2. Provide a universal currency interface. The interface should be able to handle different denominations, an identifiable name and ticker, and an “origin”.

Using monetary-js

Let’s provide an example of how to use the monetary library. It’s as easy as:

npm i @interlay/monetary-js

And then:

Out-of-the-Box Features

Type-checked currencies and denominations: Using monetary provides you with type-checked monetary amounts with a clear indication which denomination is used. Instead of some number or string type, a monetary amount always includes the currency and the denomination. This allows you to easily reason that “0.02” is of a certain currency and in a certain denomination without any additional logic. It will also prevent you from accidentally adding e.g., wei to ethers.

Exchange rate conversions: Monetary prevents you from doing arithmetic over mismatching currencies or denominations. However, it is often useful to display crypto amounts in fiat terms, e.g. BTC in USD. This is easily achievable by storing dedicated exchange rates between assets and converting currency using the exchange rate.

Extensibility: With the ever growing number of currencies, the monetary library is built to easily allow adding additional currencies. This consists of three parts: (1) adding the base units of the currency, (2) describing the currency including its name, base unit, smallest denomination, and a preferred human-friendly display option, and (3) generating the specific monetary amount type from steps 1 and 2. For a step-by-step example, check out:

Next steps

  1. Integrate monetary-js in your project: Please reach out to us if you have any questions on discord and checkout the monetary repository:
  2. Participate in the development: We are currently working on extending the capabilities of this library to handle different “locations” of assets. As we are building on Bitcoin and Polkadot with Cosmos and Ethereum being the next targets, we will likely add support for these chains first. Join the discussions:

We are hiring!

Senior UI/UX Designer

Lead Engineer

For more details, check our website

Stay tuned for more information on interBTC:

​​Handling Money Cross-Chain In JavaScript was originally published in Interlay on Medium, where people are continuing the conversation by highlighting and responding to this story.