Soroban

Soroban

Soroban Soroban helper class to assist with formatting and parsing token amounts.

Constructor

new Soroban()

Source:

Methods

(static) formatTokenAmount(amount, decimals) → {string}

Given a whole number smart contract amount of a token and an amount of decimal places (if the token has any), it returns a "display" value.

All arithmetic inside the contract is performed on integers to avoid potential precision and consistency issues of floating-point.

Source:
Parameters:
Name Type Description
amount string

the token amount you want to display

decimals number

specify how many decimal places a token has

Throws:

if the given amount has a decimal point already

Type
TypeError
Returns:
Type:
string

the display value

Example
formatTokenAmount("123000", 4) === "12.3";

(static) parseTokenAmount(value, decimals) → {string}

Parse a token amount to use it on smart contract

This function takes the display value and its decimals (if the token has any) and returns a string that'll be used within the smart contract.

Source:
Parameters:
Name Type Description
value string

the token amount you want to use it on smart contract which you've been displaying in a UI

decimals number

the number of decimal places expected in the display value (different than the "actual" number, because suffix zeroes might not be present)

Returns:
Type:
string

the whole number token amount represented by the display value with the decimal places shifted over

Example
const displayValueAmount = "123.4560"
const parsedAmtForSmartContract = parseTokenAmount(displayValueAmount, 5);
parsedAmtForSmartContract === "12345600"