Utility Functions

This directory contains utility functions used throughout the SDK.

Files Overview

File
Purpose

decimals.ts

Decimal conversion utilities

errors.ts

Error creation and handling

logger.ts

Logger implementations

validation.ts

Input validation functions


Decimal Utilities (decimals.ts)

Constants

const DECIMALS = {
  USDC: 6,           // USDC token decimals
  WETH: 18,          // Wrapped Ether decimals
  cbBTC: 8,          // Coinbase BTC decimals
  PRICE: 8,          // Chainlink price feed decimals
  OPTION_SIZE: 18,   // Option contract size decimals
};

Functions

toBigInt

Convert a human-readable number to bigint with decimals.

fromBigInt

Convert a raw bigint to human-readable string.

scaleDecimals

Scale a value from one decimal precision to another.

formatAmount

Format a bigint for display with optional decimal places.

parseAmount

Parse a string amount to bigint.


Error Utilities (errors.ts)

createError

Create a typed ThetanutsError.

wrapError

Wrap an unknown error as ThetanutsError.

isThetanutsError

Type guard to check if an error is a ThetanutsError.

mapHttpError

Map HTTP/Axios errors to ThetanutsError.

mapContractError

Map ethers.js contract errors to ThetanutsError.


Logger Utilities (logger.ts)

noopLogger

A no-op logger that does nothing (default).

consoleLogger

A logger that outputs to console.

Custom Logger

Create your own logger implementation.


Validation Utilities (validation.ts)

validateAddress

Validate an Ethereum address.

validateOrderExpiry

Check if an order will expire soon.

validateFillSize

Validate fill size against available amount.

validateBuySlippage

Validate actual price doesn't exceed max for buys.

validateSellSlippage

Validate actual price meets minimum for sells.

calculateSlippagePrice

Calculate price with slippage applied.


Complete Example

Last updated