NumberType Conversions
A problem with creating a JavaScript SDK for a Cosmos SDK based blockchain is that JavaScript's number primitive cannot natively handle numbers > Number.MAX_SAFE_INTEGER, but the blockchain allows amounts greater than that.
To combat this, we have made all TypeScript types in the SDK generic via a NumberType interface.
Types that you will find in the SDK that are number-based will all be generically typed, so that you can use any of the above NumberTypes, according to your preferences.
It is recommended that you use bigint and/or stringified because these can represent all possible numbers and do not lose precision. Also, note that for almost all SDK functions, we only take bigints.
For example, the following will represent a BadgeMetadata type where all numbers are stringified (i.e. "100" or "123").
Converting Between NumberTypes
To convert between different number types, all types come with a converter function (.convert()). This allows you to convert all the stringified numbers to another format (such as JS number or JS bigint). To convert, you can simply do the following:
We export the following types and converter functions for your convenience.
Example Application
In our API, JS bigints cannot be natively sent over HTTP. So, we use the following execution flow:
Before sending to the client, stringify everything before sending over HTTP
The client can use the converter functions to coonvert all types to their preferred method
Last updated