import { AccountId } from '@polkadot/types/interfaces' ; import { encodeAddress } from '@polkadot/util-crypto' ; import { writeStorage } from '@substrate/local-storage' ; export function toShortAddress ( address ) { if ( typeof address !== 'string' ) { address = address . toString ( ) ; } return address . slice ( 0 , 8 ) . concat ( '...' ) . concat ( address . slice ( address . length - 5 ) ) ; } export function isCartItem ( item ) { return item . startsWith ( 'cart:' ) ; } export function stripAddressFromCartItem ( item ) { return item . slice ( 5 ) ; } export function getCurrCartCount ( ) { let count = 0 ; for ( const key in localStorage ) { if ( isCartItem ( key ) ) { count += 1 ; } } return count ; } export function addToCart ( stashPubKey ) { const stashid = encodeAddress ( stashPubKey , 2 ) ; writeStorage ( `cart:${ stashid }` , stashid ) ; const currCartCount = getCurrCartCount ( ) ; writeStorage ( `cartItemsCount` , currCartCount ) ; } export function getCartItems ( ) { const result = [ ] ; for ( const key in localStorage ) { if ( isCartItem ( key ) ) { result . push ( key ) ; } } return result ; } export function clearCart ( ) { for ( const key in localStorage ) { if ( isCartItem ( key ) ) { localStorage . removeItem ( key ) ; } } writeStorage ( 'cartItemsCount' , 0 ) ; } export function removeCartItem ( itemKey , currCartCount ) { localStorage . removeItem ( itemKey ) ; writeStorage ( 'cartItemsCount' , currCartCount - 1 ) ; }