Formatting Functions

Formatting functions of Azoth SDK for working with numbers and blockchain data

The SDK provides functions for formatting amounts between different representations.

formatInput()

Converts a human-readable amount into a bigint for use in blockchain transactions.

function formatInput(amount: string, unit?: string | ethers.Numeric): bigint

Parameters:

  • amount: The amount as a string

  • unit: (Optional) The unit of the amount (e.g., "18" for 18 decimal places)

Returns: The formatted bigint amount.

Example:

// Format 10 USDT for transaction (USDT has 6 decimal places)
const amount = formatInput('10', 6);
console.log(amount); // 10000000n

// Format 1.5 ETH for transaction (ETH has 18 decimal places)
const ethAmount = formatInput('1.5', 18);
console.log(ethAmount); // 1500000000000000000n

formatOutput()

Converts a bigint amount from blockchain to a human-readable number.

Parameters:

  • amount: The amount as bigint or number

  • unit: (Optional) The unit of the amount (e.g., "18" for 18 decimal places)

Returns: The formatted amount as a string.

Example:

Practical Examples

Working with Balances

Working with Different Tokens

Error Handling

Last updated