@xlabs-xyz/common

Glue layer that ties together the lower-level packages (const-utils, utils, binary-layout, amount) into practical building blocks.
- Layout Items – binary-layout items that serialize/deserialize to Amount types
- Units – predefined Amount kinds for common units (currencies, duration, bytes, etc.)
- Utilities
Layout Items
Binary-layout items that serialize to/from Amount and related types.
amountItem
Deserialize numeric bytes directly to an Amount:
const layout = [
{ name: "balance", ...amountItem(8, Sol) },
{ name: "timeout", ...amountItem(4, Duration, "second") },
] as const;
With a transform for scaled and/or shifted values:
import { linearTransform } from "@xlabs-xyz/common";
amountItem(4, Sol, linearTransform("stored", 1_000));
conversionItem
For exchange rates / conversion factors. Has many overloads – can wrap an existing amountItem or be built from scratch:
const priceItem = amountItem(8, Sol, "lamport");
conversionItem(priceItem, Usd);
conversionItem(8, Sol, "lamport", Usd);
Other Items
timestampItem("uint", 4);
hashItem;
paddingItem(4);
byteSwitchItem / enumSwitchVariants – helpers for byte-discriminated tagged unions; see source for details.
Units
Predefined Amount kinds for common use cases. These are sensible defaults – not authoritative definitions. Feel free to roll your own if they don't fit your needs.
- Currencies:
Usd, Usdt, Usdc, Btc, Eth, Sol – each with a CurrencyKind type and three formatting systems:
default – native symbols where available ($, ₿, Ξ)
uniform – ticker-style (USD, BTC, ETH)
fancy – unicode symbols (₿, Ξ, USD₮)
- Percentage – supports
%, bp (basis points), and x (scalar)
- Duration – seconds through years, with
long and short formatting systems
- Byte – SI (kB, MB, GB) and binary (KiB, MiB, GiB) systems
Unit Definition Helpers
For defining your own kinds:
toDecimalUnits([
[0, [{ symbol: "FOO" }]],
[-6, [{ symbol: "µFOO" }]],
]);
toCompoundUnits([
[1, [withPluralS("second")]],
[60, [withPluralS("minute")]],
]);
withPluralS("hour");
allowPluralS("hour");
allowOtherCap("Gwei");
Utilities
fromAtomicIfKind(1_000_000n);
fromAtomicIfKind(1_000_000n, Sol);