SDK Utilities

A handful of small helpers for the things you end up doing constantly when working with Starknet tokens: formatting addresses, dividing by decimals, truncating long strings.

Code
ts
1import {
2 bigintToLongAddress,
3 limitChars,
4 convertToReadableTokens,
5 removeTrailingZeros,
6 formatNumberInternational,
7 timeStampToDate,
8} from 'starknet-tokenkit';

bigintToLongAddress

Turns a BigInt string into a hex contract address.

Code
tsx
1bigintToLongAddress("3618502788666131213697322783095070105623107215331596699973092056135872020478")
2// → "0x7f..."

convertToReadableTokens

Converts a raw on-chain amount into something a human can read, using the token's decimals.

Code
tsx
1convertToReadableTokens(tokens: number | string, decimals: number)

Example:

Code
tsx
1const USDC_5 = 5000000;
2const decimals = 6;
3 
4const readable = convertToReadableTokens(USDC_5, decimals);
5// readable === "5.000000"

limitChars

Truncates a string to a max length, with an optional ellipsis.

Code
tsx
1limitChars(str: string, count: number, show_dots: boolean)

Example:

Code
tsx
1limitChars("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", 10, true)
2// → "0x049d3657 ..."
3 
4limitChars("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", 10, false)
5// → "0x049d3657"

removeTrailingZeros

Strips the leading zeros that sit between 0x and the rest of an address. Handy when comparing addresses that arrived in different formats.

Code
tsx
1removeTrailingZeros("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
2// → "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"

formatNumberInternational

Formats a number with Intl.NumberFormat, 4 decimal places, en-US locale.

Code
tsx
1formatNumberInternational(1234567.89)
2// → "1,234,567.8900"

timeStampToDate

Turns a Unix timestamp (in seconds) into a JavaScript Date.

Code
tsx
1timeStampToDate(1700000000)
2// → Date object: 2023-11-14T22:13:20.000Z