-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(wallet-mobile): added helpers to handle info
- Loading branch information
1 parent
3015ccb
commit 85a80e4
Showing
7 changed files
with
162 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {tokenMocks} from '../adapters/token.mocks' | ||
import {infoExtractName} from './info-extract-name' | ||
|
||
describe('infoExtractName', () => { | ||
it('should extract the ticker from FT token', () => { | ||
const result = infoExtractName(tokenMocks.primaryETH.info, { | ||
mode: 'name', | ||
}) | ||
|
||
expect(result).toBe('ETH') | ||
}) | ||
|
||
it('should extract the name from FT token when ticker is empty', () => { | ||
const result = infoExtractName( | ||
{ | ||
...tokenMocks.primaryETH.info, | ||
ticker: '', | ||
}, | ||
{ | ||
mode: 'name', | ||
}, | ||
) | ||
|
||
expect(result).toBe('Ethereum') | ||
}) | ||
|
||
it('should extract the fingerprint from FT token when ticker/name are empty', () => { | ||
const result = infoExtractName( | ||
{ | ||
...tokenMocks.primaryETH.info, | ||
ticker: '', | ||
name: '', | ||
}, | ||
{ | ||
mode: 'name', | ||
}, | ||
) | ||
|
||
expect(result).toBe('0x1234567890abcdef') | ||
}) | ||
|
||
it('should extract the name from NFT token', () => { | ||
const result = infoExtractName(tokenMocks.nftCryptoKitty.info, { | ||
mode: 'name', | ||
}) | ||
|
||
expect(result).toBe('CryptoKitty #1234') | ||
}) | ||
|
||
it('should extract the fingerprint from NFT token when name is empty', () => { | ||
const result = infoExtractName( | ||
{ | ||
...tokenMocks.nftCryptoKitty.info, | ||
name: '', | ||
}, | ||
{ | ||
mode: 'name', | ||
}, | ||
) | ||
|
||
expect(result).toBe('asset1s7nlt...eg483c6wu75') | ||
}) | ||
|
||
it('should extract the ticker from token', () => { | ||
const result = infoExtractName(tokenMocks.rnftWhatever.info, { | ||
mode: 'currency', | ||
}) | ||
|
||
expect(result).toBe('Whatever') | ||
}) | ||
|
||
it('should extract the ticker default', () => { | ||
const result = infoExtractName(tokenMocks.rnftWhatever.info) | ||
|
||
expect(result).toBe('Whatever #42') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {truncateString} from '@yoroi/common' | ||
import {Portfolio} from '@yoroi/types' | ||
|
||
// max name length is 50 and ticker is 9 in CIP26 | ||
// if the data is in metadatum there is "no limit" | ||
export function infoExtractName( | ||
info: Portfolio.Token.Info, | ||
{ | ||
mode = 'name', | ||
maxLength = mode === 'name' ? 25 : 9, | ||
}: {mode?: 'currency' | 'name'; maxLength?: number} = {}, | ||
) { | ||
if (mode === 'name') { | ||
if (info.type === Portfolio.Token.Type.FT) { | ||
return truncateString({ | ||
value: info.ticker || info.name || info.fingerprint, | ||
maxLength, | ||
}) | ||
} else { | ||
// NFTs without names usually are the "header" of collection | ||
return truncateString({value: info.name || info.fingerprint, maxLength}) | ||
} | ||
} | ||
|
||
return truncateString({value: info.ticker, maxLength}) | ||
} |