import { ChainID } from '@stacks/transactions' ; import BigNumber from 'bignumber.js' ; import { c32addressDecode } from 'c32check' ; import { Network , STX_DECIMALS } from '@shared/constants' ; import { abbreviateNumber , initBigNumber } from '@app/common/utils' ; export const stacksValue = ( { value , fixedDecimals = true , withTicker = true , abbreviate = false , } ) => { const stacks = microStxToStx ( value ) ; const stxAmount = stacks . toNumber ( ) ; return `${ abbreviate && stxAmount > 10000 ? abbreviateNumber ( stxAmount ) : stxAmount . toLocaleString ( 'en-US' , { maximumFractionDigits : fixedDecimals ? STX_DECIMALS : 3 , } ) }${ withTicker ? ' STX' : '' }` ; } ; export const microStxToStx = ( mStx ) => { const microStacks = initBigNumber ( mStx ) ; return microStacks . shiftedBy ( - STX_DECIMALS ) ; } ; export const ftDecimals = ( value , decimals ) => { const amount = initBigNumber ( value ) ; return amount . shiftedBy ( - decimals ) . toNumber ( ) . toLocaleString ( 'en-US' , { maximumFractionDigits : decimals } ) ; } ; export const ftUnshiftDecimals = ( value , decimals ) => { const amount = initBigNumber ( value ) ; return amount . shiftedBy ( decimals ) . toString ( 10 ) ; } ; export const stxToMicroStx = ( stx ) => { const stxBN = initBigNumber ( stx ) ; return stxBN . shiftedBy ( STX_DECIMALS ) ; } ; export const validateStacksAddress = ( stacksAddress ) : boolean => { try { c32addressDecode ( stacksAddress ) ; return true ; } catch ( e ) { return false ; } } ; export function validateAddressChain ( address , currentNetwork ) { const prefix = address . substr ( 0 , 2 ) ; if ( currentNetwork . chainId === ChainID . Testnet ) return prefix === 'SN' || prefix === 'ST' ; if ( currentNetwork . chainId === ChainID . Mainnet ) return prefix === 'SM' || prefix === 'SP' ; return false ; }