> ## Documentation Index
> Fetch the complete documentation index at: https://docs.leokit.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Utilities

> Amount math, chain helpers, asset parsing, error handling, events, and CDN icons

Most utilities live under the `leokit-sdk/utils` and `leokit-sdk/cdn` subpath imports. They're tree-shakeable — only what you import gets bundled.

## Amount Math

```typescript theme={null}
import {
  toBaseUnits,
  fromBaseUnits,
  formatAmount,
  formatWithSeparators,
  applySlippage,
  addAmounts,
  subtractAmounts,
  multiplyAmounts,
  divideAmounts,
  percentageOf,
  compareAmounts,
  isPositive,
  isZero,
  parseAmount,
  truncateDecimals,
  getMinimumAmount,
} from "leokit-sdk/utils";

toBaseUnits("1.5", 18);                  // "1500000000000000000"
fromBaseUnits("1500000000000000000", 18); // "1.5"
formatAmount("1234.5678", 2);            // "1234.57"
formatWithSeparators("1234567.89");      // "1,234,567.89"
applySlippage("1000", 150);              // "985"  (150 bps = 1.5%)
```

All amount helpers accept and return strings to avoid `number` precision loss. Internally they use `bignumber.js`.

## Chain Helpers

```typescript theme={null}
import {
  getChainFromNetwork,
  getNetworkFromChain,
  getNativeAsset,
  isEvmChain,
  isUtxoChain,
  isCosmosChain,
  getEvmChainId,
  getChainFromEvmId,
  getProtocolType,
  EVM_CHAINS,
  UTXO_CHAINS,
  COSMOS_CHAINS,
} from "leokit-sdk/utils";

getNativeAsset("ETH");          // "ETH.ETH"
getNativeAsset("BTC");          // "BTC.BTC"
isEvmChain("ARB");              // true
getEvmChainId("ETH");           // 1
getChainFromEvmId(42161);       // "ARB"
getProtocolType("ETH");         // "evm"
```

`CHAIN_CONFIGS` (from the main entry point) holds the full per-chain config used by the SDK — RPC URLs, explorer URLs, native asset, decimals.

```typescript theme={null}
import { CHAIN_CONFIGS, getChainConfig, getRpcUrl, getTxExplorerUrl } from "leokit-sdk";

const cfg = getChainConfig("ETH");
const rpc = getRpcUrl("ETH", "mainnet");
const url = getTxExplorerUrl("ETH", "0xabc...");
```

## Asset Parsing

LeoKit asset IDs follow `CHAIN.SYMBOL[-ADDRESS]` (e.g. `ETH.USDC-0xA0b...`). Helpers:

```typescript theme={null}
import {
  getParsedAsset,
  getAssetContractAddress,
  isNativeAsset,
  isHiveEngineAsset,
  constructAsset,
  normalizeAddress,
  NULL_ADDRESS,
} from "leokit-sdk/utils";

getParsedAsset("ETH.USDC-0xA0b...");
// { network: "ETH", symbol: "USDC", address: "0xA0b..." }

isNativeAsset("BTC.BTC");                                   // true
isNativeAsset("ETH.USDC-0xA0b...");                         // false

getAssetContractAddress("ETH.USDC-0xA0b...");               // "0xA0b..."
constructAsset("ETH", "USDC", "0xA0b...");                  // "ETH.USDC-0xA0b..."

normalizeAddress("ETH", "0xA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48");
// → checksummed lowercase form
```

The full asset-format spec is at [Asset Format](/reference/asset-format).

## Errors

```typescript theme={null}
import { LeoKitError, isLeoKitError, type ErrorCode } from "leokit-sdk/utils";

try {
  await sdk.executeSwap(quote);
} catch (err) {
  if (isLeoKitError(err)) {
    console.log(err.code, err.statusCode, err.message);
  } else {
    throw err;
  }
}
```

The full `ErrorCode` catalog is at [Error Codes](/reference/error-codes). `LeoKitError` mirrors the backend's error class so you get matching codes on both sides.

## Events

The `LeoKitClient` is itself an `EventEmitter`. For your own internal pub/sub, the SDK exposes the same primitive:

```typescript theme={null}
import { EventEmitter, createEventEmitter, type LeoKitEvents } from "leokit-sdk/utils";

const bus = createEventEmitter<{ ready: void; error: Error }>();
const off = bus.on("ready", () => console.log("ready"));
bus.emit("ready");
off(); // unsubscribe
```

Built-in `LeoKitEvents` shape: `error`, `transaction`, `walletChange`, `connectionChange`. Type-safe `on`/`off`/`emit`.

## CDN Icons

```typescript theme={null}
import { getAssetIcon, getNetworkIcon, getWalletIcon, getProtocolIcon } from "leokit-sdk/cdn";

getAssetIcon("ETH.USDC-0xA0b...");       // "https://cdn.leokit.dev/assets/eth/usdc.png"
getNetworkIcon("ETH");                   // "https://cdn.leokit.dev/networks/eth.png"
getWalletIcon("metamask");               // "https://cdn.leokit.dev/wallets/metamask.png"
getProtocolIcon("thorchain");            // "https://cdn.leokit.dev/protocols/thorchain.png"
```

The CDN is also exposed as `LEOKIT_CDN_BASE_URL` if you want to compose URLs yourself. Custom CDN bases can be set via `cdnUrl` in `LeoKitConfig`.

## EVM-Specific

```typescript theme={null}
import { normalizeHexValue } from "leokit-sdk/utils";

normalizeHexValue("0x1");       // "0x1"
normalizeHexValue("0x01");      // "0x1"
normalizeHexValue("0X0123");    // "0x123"
```

Useful when comparing `eth_chainId` values across providers that disagree on padding.
