import { AssetWithMeta , NftMeta } from '@app/common/asset-types' ; import { mergeAssetBalances , mergeNftBalances , NftMetaRecord } from '@app/store/assets/tokens' ; import BigNumber from 'bignumber.js' ; describe ( mergeAssetBalances . name , ( ) => { test ( 'it merges fungible balances (anchored & unanchored) correctly' , ( ) => { const assets = [ { type : 'stx' , contractAddress : '' , contractName : '' , balance : new BigNumber ( '993992057' ) , subtitle : 'STX' , name : 'Stacks Token' , canTransfer : true , hasMemo : true , } , { type : 'ft' , subtitle : 'ST6G7…PSTK7.stella-the-cat' , contractAddress : 'ST6G7N19FKNW24XH5JQ5P5WR1DN10QWMKQSPSTK7' , contractName : 'stella-the-cat' , name : 'stella-token' , balance : new BigNumber ( '2469135782' ) , meta : { name : 'SteLLa the Cat' , symbol : 'CAT' , decimals : 9 , } , canTransfer : true , hasMemo : true , } , { type : 'ft' , subtitle : 'ST6G7…PSTK8.alf-the-dog' , contractAddress : 'ST6G7N19FKNW24XH5JQ5P5WR1DN10QWMKQSPSTK8' , contractName : 'alf-the-dog' , name : 'alf-token' , balance : new BigNumber ( '9469135782' ) , meta : { name : 'Alf the Dog' , symbol : 'ALF' , decimals : 9 , } , canTransfer : true , hasMemo : true , } , ] ; const unanchoredAssets = [ { type : 'stx' , contractAddress : '' , contractName : '' , balance : new BigNumber ( '991992057' ) , subtitle : 'STX' , name : 'Stacks Token' , canTransfer : true , hasMemo : true , } , { type : 'ft' , subtitle : 'ST6G7…PSTK7.stella-the-cat' , contractAddress : 'ST6G7N19FKNW24XH5JQ5P5WR1DN10QWMKQSPSTK7' , contractName : 'stella-the-cat' , name : 'stella-token' , balance : new BigNumber ( '1469135782' ) , meta : { name : 'SteLLa the Cat' , symbol : 'CAT' , decimals : 9 , } , canTransfer : true , hasMemo : true , } , { type : 'ft' , subtitle : 'ST6G7…PSTK9.boxer-the-horse' , contractAddress : 'ST6G7N19FKNW24XH5JQ5P5WR1DN10QWMKQSPSTK9' , contractName : 'boxer-the-horse' , name : 'boxer-token' , balance : new BigNumber ( '7000100702' ) , meta : { name : 'Boxer the Horse' , symbol : 'BOX' , decimals : 9 , } , canTransfer : true , hasMemo : true , } , ] ; const mergedAssets = mergeAssetBalances ( assets , unanchoredAssets , 'ft' ) ; expect ( mergedAssets . length ) . toEqual ( assets . length - 1 + 1 ) ; for ( const tokenName of [ 'stella-token' , 'alf-token' , 'boxer-token' ] ) { const token = assets . find ( asset => asset && asset . name === tokenName ) ; const tokenUnanchored = unanchoredAssets . find ( asset => asset && asset . name === tokenName ) ; const tokenMerged = mergedAssets . find ( asset => asset && asset . name === tokenName ) ; if ( ! tokenMerged ) return ; if ( token && tokenUnanchored ) { expect ( tokenMerged . balance ) . toEqual ( token . balance ) ; expect ( tokenMerged . subBalance ) . toEqual ( tokenUnanchored . balance ) ; } else if ( token && ! tokenUnanchored ) { expect ( tokenMerged . balance ) . toEqual ( token . balance ) ; expect ( tokenMerged ?. subBalance ?. toFixed ( ) ) . toEqual ( new BigNumber ( 0 ) . toFixed ( ) ) ; expect ( BigNumber . isBigNumber ( tokenMerged . subBalance ) ) . toBeTruthy ( ) ; } else if ( ! token && tokenUnanchored ) { expect ( tokenMerged . balance . toFixed ( ) ) . toEqual ( new BigNumber ( 0 ) . toFixed ( ) ) ; expect ( BigNumber . isBigNumber ( tokenMerged . balance ) ) . toBeTruthy ( ) ; expect ( tokenMerged . subBalance ) . toEqual ( tokenUnanchored . balance ) ; } } } ) ; } ) ; describe ( mergeAssetBalances . name , ( ) => { test ( 'it merges non-fungible balances (anchored & unanchored) correctly' , ( ) => { const anchoredNfts = { 'ST9VQ21ZEGG54JDFE39B99ZBTSFSWMEC323MENFG.animal::ANIMAL' : { count : '1' , total_sent : '1' , total_received : '2' , } , 'ST9VQ21ZEGG54JDFE39B99ZBTSFSWMEC323MENFF.animal::PLANET' : { count : '1' , total_sent : '0' , total_received : '1' , } , } ; const unanchoredNfts = { 'ST9VQ21ZEGG54JDFE39B99ZBTSFSWMEC323MENFG.animal::ANIMAL' : { count : '0' , total_sent : '2' , total_received : '2' , } , 'ST9VQ21ZEGG54JDFE39B99ZBTSFSWMEC323MENFF.animal::FLOWER' : { count : '1' , total_sent : '0' , total_received : '1' , } , } ; const mergedAssets = mergeNftBalances ( anchoredNfts , unanchoredNfts ) ; for ( const nftName of Object . keys ( anchoredNfts ) ) { const token = anchoredNfts [ nftName ] ; const tokenUnanchored = unanchoredNfts [ nftName ] ; const tokenMerged = mergedAssets . find ( asset => asset && asset . key === nftName ) ; if ( token && tokenUnanchored ) { expect ( tokenMerged ?. count ) . toEqual ( token . count ) ; expect ( tokenMerged ?. subCount ) . toEqual ( tokenUnanchored . count ) ; } else if ( token && ! tokenUnanchored ) { expect ( tokenMerged ?. count ) . toEqual ( token . count ) ; expect ( tokenMerged ?. subCount ) . toEqual ( '0' ) ; } else if ( ! token && tokenUnanchored ) { expect ( tokenMerged ?. count ) . toEqual ( '0' ) ; expect ( tokenMerged ?. subCount ) . toEqual ( tokenUnanchored . count ) ; } } } ) ; } ) ;