UMI Helpers
Amounts
An Amount is a special type that allows us to define big decimal numbers. It does this by representing the number in its lowest possible unit (e.g. lamports) and then keeping track of the decimal number of that unit (e.g. 9). This allows for a more accurate representation of the number and avoids JavaScript rounding errors caused by IEEE 754 floating point numbers. It also uses a string identifier to ensure that we are dealing with amounts in the same unit when performing operations. Here's how the Amount generic type is defined:
type AmountIdentifier = 'SOL' | 'USD' | '%' | 'splToken' | string;
type AmountDecimals = number;
type Amount<
I extends AmountIdentifier = AmountIdentifier,
D extends AmountDecimals = AmountDecimals
> = {
/** The amount in its lower possible unit such that it does not contain decimals. */
basisPoints: bigint;
/** The identifier of the amount. */
identifier: I;
/** The number of decimals in the amount. */
decimals: D;
};Umi also provides specific versions of this Amount type for specific cases like SOLs and USDs.
type SolAmount = Amount<'SOL', 9>;
type UsdAmount = Amount<'USD', 2>;
type PercentAmount<D extends AmountDecimals> = Amount<'%', D>;To make it easier for developers to handle amounts, Umi provides a set of helper functions that can be used to create, format, and perform operations on amounts.
You may want to check out the "Utils — Amounts" section of the API references to learn more about all these helpers but here's a quick list of functions that can help create new amount types.
Options
In Rust, we define optional values as an Option<T> enum which can either be Some(T) or None. This is usually represented as T | null in the JavaScript world. The issue with this approach is it doesn't work with nested options. For instance, an Option<Option<T>> in Rust would become a T | null | null in JavaScript which is equivalent to T | null. That means, there is no way for us to represent the Some(None) value in JavaScript or any other nested option.
To solve this issue, Umi provides an Option<T> union type that works very similarly to the Rust Option<T> type. It is defined as follows:
To improve the developer experience, Umi offers a some and none function to create options. The type T of the option can either be inferred by TypeScript or explicitly provided.
Umi also provides a set of helper functions to verify and manipulate options.
DateTimes
Umi provides a DateTime type that can be used to represent a date and time using a timestamp in seconds. It is simply defined as a bigint number and offers a set of helper functions to create and format date times.
GpaBuilders
To help prepare getProgramAccounts RPC requests, Umi provides an immutable GpaBuilder helper class. It can be used to add filters, slice data and fetch the raw accounts whilst mapping them to whatever we want. Here are some examples.
GpaBuilders can also be told how to deserialize a raw account into a deserialized account via the deserializeUsing method. Once a deserialization callback was provided, the getDeserialized method can be used to fetch the deserialized accounts.
Additionally, we can pass a set of fields with their offsets to a GpaBuilder to improve the developer experience around filtering and slicing data. To do so, we can use the registerFields method. For instance, say we know that starting from byte 16, the next 32 bytes represent a name via a fixed size string and the next 4 bytes after that represent an age. Here's how we could register those fields.
Once the fields are registered, we can use the whereField and sliceField methods to filter and slice data using fields. Not only it will know which offset to use but how to serialize its value.
Last updated