import { Coin } from "@cosmjs/stargate" ; import { Decimal } from "@cosmjs/math" ; const thinSpace = "\u202F" ; export function printableCoin ( coin ? ) { if ( ! coin ) { return "0" ; } if ( coin . denom . startsWith ( "u" ) ) { const ticker = coin . denom . slice ( 1 ) . toUpperCase ( ) ; return Decimal . fromAtomics ( coin . amount , 6 ) . toString ( ) + thinSpace + ticker ; } else { return coin . amount + thinSpace + coin . denom ; } } export function printableBalance ( balance ? ) { if ( ! balance || balance . length === 0 ) return "–" ; return balance . map ( printableCoin ) . join ( ", " ) ; } export interface MappedCoin { readonly denom : string ; readonly fractionalDigits : number ; } export interface CoinMap { readonly [ key ] : MappedCoin ; } export function nativeCoinToDisplay ( coin , coinMap ) { if ( ! coinMap ) return coin ; const coinToDisplay = coinMap [ coin . denom ] ; if ( ! coinToDisplay ) return coin ; const amountToDisplay = Decimal . fromAtomics ( coin . amount , coinToDisplay . fractionalDigits ) . toString ( ) ; return { denom : coinToDisplay . denom , amount : amountToDisplay } ; } export function displayAmountToNative ( amountToDisplay , coinMap , nativeDenom ) { const fractionalDigits = coinMap [ nativeDenom ] ?. fractionalDigits ; if ( fractionalDigits ) { const decimalAmount = Decimal . fromUserInput ( amountToDisplay , fractionalDigits ) ; return decimalAmount . atomics ; } return amountToDisplay ; }