Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
wolverineks committed Feb 10, 2023
1 parent b2707a7 commit 5c7231e
Show file tree
Hide file tree
Showing 26 changed files with 454 additions and 611 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {Boundary, TwoActionView} from '../../../components'
import {useSignWithHwAndSubmitTx} from '../../../hooks'
import {LedgerConnect, LedgerTransportSwitch} from '../../../HW'
import {confirmationMessages, txLabels} from '../../../i18n/global-messages'
import type {DeviceId, DeviceObj} from '../../../legacy/ledgerUtils'
import {walletManager, withBLE, withUSB, YoroiWallet} from '../../../yoroi-wallets'
import {DeviceId, DeviceObj, walletManager, withBLE, withUSB, YoroiWallet} from '../../../yoroi-wallets'
import {YoroiUnsignedTx} from '../../../yoroi-wallets/types'
import {TransferSummary} from '../TransferSummary'

Expand Down
3 changes: 1 addition & 2 deletions src/HW/LedgerConnect/LedgerConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import usbImage from '../../assets/img/ledger-nano-usb.png'
import {BulletPointItem, Button, Text} from '../../components'
import globalMessages, {confirmationMessages, ledgerMessages} from '../../i18n/global-messages'
import LocalizableError from '../../i18n/LocalizableError'
import type {DeviceId, DeviceObj} from '../../legacy/ledgerUtils'
import {BluetoothDisabledError, RejectedByUserError} from '../../legacy/ledgerUtils'
import {Logger} from '../../legacy/logging'
import {COLORS, spacing} from '../../theme'
import {BluetoothDisabledError, DeviceId, DeviceObj, RejectedByUserError} from '../../yoroi-wallets'
import {Device} from '../../yoroi-wallets/types'
import {DeviceItem} from './DeviceItem'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import DeviceInfo from 'react-native-device-info'
import {Button, Modal, Text} from '../../components'
import globalMessages from '../../i18n/global-messages'
import {CONFIG} from '../../legacy/config'
import {useLedgerPermissions} from '../../legacy/ledgerUtils'
import {spacing} from '../../theme'
import {useLedgerPermissions} from '../../yoroi-wallets'

type Props = {
onSelectUSB: () => void
Expand Down
4 changes: 1 addition & 3 deletions src/WalletInit/ConnectNanoX/ConnectNanoXScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import {showErrorDialog} from '../../dialogs'
import {LedgerConnect} from '../../HW'
import {errorMessages} from '../../i18n/global-messages'
import LocalizableError from '../../i18n/LocalizableError'
import type {DeviceId, DeviceObj} from '../../legacy/ledgerUtils'
import {getHWDeviceInfo} from '../../legacy/ledgerUtils'
import {Logger} from '../../legacy/logging'
import {WalletInitRouteNavigation, WalletInitRoutes} from '../../navigation'
import {NetworkId, WalletImplementationId} from '../../yoroi-wallets'
import {DeviceId, DeviceObj, getHWDeviceInfo, NetworkId, WalletImplementationId} from '../../yoroi-wallets'
import {Device} from '../../yoroi-wallets/types'

export type Params = {
Expand Down
3 changes: 1 addition & 2 deletions src/components/ConfirmTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {confirmationMessages, errorMessages, txLabels} from '../../i18n/global-m
import LocalizableError from '../../i18n/LocalizableError'
import {CONFIG} from '../../legacy/config'
import {WrongPassword} from '../../legacy/errors'
import {DeviceId, DeviceObj} from '../../legacy/ledgerUtils'
import {isEmptyString} from '../../legacy/utils'
import {useSelectedWallet} from '../../SelectedWallet'
import {COLORS} from '../../theme'
import {CardanoTypes, walletManager, withBLE, withUSB} from '../../yoroi-wallets'
import {CardanoTypes, DeviceId, DeviceObj, walletManager, withBLE, withUSB} from '../../yoroi-wallets'
import {YoroiUnsignedTx} from '../../yoroi-wallets/types'
import {Button, ButtonProps, ValidatedTextInput} from '..'
import {Dialog, Step as DialogStep} from './Dialog'
Expand Down
2 changes: 1 addition & 1 deletion src/components/ConfirmTx/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useIntl} from 'react-intl'
import {ErrorView, Modal, PleaseWaitView} from '../../components'
import {LedgerConnect, LedgerTransportSwitchView} from '../../HW'
import globalMessages, {ledgerMessages, txLabels} from '../../i18n/global-messages'
import type {DeviceId, DeviceObj} from '../../legacy/ledgerUtils'
import {DeviceId, DeviceObj} from '../../yoroi-wallets'

type ErrorData = {
errorMessage: string
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
} from 'react-query'

import {isNightly} from '../legacy/config'
import {HWDeviceInfo} from '../legacy/ledgerUtils'
import {useStorage} from '../Storage'
import {parseWalletMeta} from '../Storage/migrations/walletMeta'
import {useWalletManager} from '../WalletManager'
import {
calcLockedDeposit,
HWDeviceInfo,
NetworkId,
toToken,
TxSubmissionStatus,
Expand Down
14 changes: 1 addition & 13 deletions src/legacy/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import {BigNumber} from 'bignumber.js'

import {getCardanoDefaultAsset} from './config'
import {
ASSET_DENOMINATION,
formatTokenAmount,
formatTokenFractional,
formatTokenInteger,
getAssetDenomination,
} from './format'
import {formatTokenAmount, formatTokenFractional, formatTokenInteger} from './format'

const defaultCardanoAsset = getCardanoDefaultAsset()

describe('getAssetDenomination', () => {
it('correctly represents ADA symbol', () => {
expect(getAssetDenomination(defaultCardanoAsset, ASSET_DENOMINATION.SYMBOL)).toEqual('₳')
})
})

describe('formatAda', () => {
it('formats zero', () => {
expect(formatTokenAmount(new BigNumber(0), defaultCardanoAsset)).toEqual('0.000000')
Expand Down
88 changes: 44 additions & 44 deletions src/legacy/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,47 @@ import {defineMessages} from 'react-intl'
import {DefaultAsset, Token} from '../yoroi-wallets/types'
import {getCardanoDefaultAsset} from './config'
import utfSymbols from './utfSymbols'
const messages = defineMessages({
today: {
id: 'utils.format.today',
defaultMessage: '!!!Today',
},
yesterday: {
id: 'utils.format.yesterday',
defaultMessage: '!!!Yesterday',
},
unknownAssetName: {
id: 'utils.format.unknownAssetName',
defaultMessage: '!!![Unknown asset name]',
},
})
export const getTokenFingerprint = ({policyId, assetNameHex}) => {

const getTokenFingerprint = ({policyId, assetNameHex}) => {
const assetFingerprint = new AssetFingerprint(Buffer.from(policyId, 'hex'), Buffer.from(assetNameHex, 'hex'))
return assetFingerprint.fingerprint()
}

export const ASSET_DENOMINATION = {
TICKER: 'ticker',
SYMBOL: 'symbol',
NAME: 'name',
FINGERPRINT: 'fingerprint',
}
export type AssetDenomination = typeof ASSET_DENOMINATION[keyof typeof ASSET_DENOMINATION]
export const decodeHexAscii = (text: string) => {

type AssetDenomination = typeof ASSET_DENOMINATION[keyof typeof ASSET_DENOMINATION]

const decodeHexAscii = (text: string) => {
const bytes = [...Buffer.from(text, 'hex')]
const isAscii = bytes.every((byte) => byte > 32 && byte < 127)
return isAscii ? String.fromCharCode(...bytes) : undefined
}
export const getTicker = (token: Token | DefaultAsset) => token.metadata.ticker
export const getSymbol = (token: Token | DefaultAsset) =>

const getTicker = (token: Token | DefaultAsset) => token.metadata.ticker

const getSymbol = (token: Token | DefaultAsset) =>
token.metadata.ticker
? utfSymbols.CURRENCIES[token.metadata.ticker]
? utfSymbols.CURRENCIES[token.metadata.ticker]
: token.metadata.ticker
: null
export const getName = (token: Token | DefaultAsset) =>

const getName = (token: Token | DefaultAsset) =>
token.metadata.longName ||
decodeHexAscii(token.metadata.assetName) ||
getTokenFingerprint({
policyId: token.metadata.policyId,
assetNameHex: token.metadata.assetName,
}) ||
undefined

// NOTE: There is a bug when starting fresh, the metadata is empty
export const getAssetDenomination = (
const getAssetDenomination = (
token: Token | DefaultAsset,
denomination: AssetDenomination,
): string | null | undefined => {
Expand All @@ -78,6 +72,7 @@ export const getAssetDenomination = (
return null
}
}

export const getAssetDenominationOrId = (token: Token | DefaultAsset, denomination?: AssetDenomination): string => {
if (denomination !== undefined) {
return (
Expand All @@ -98,35 +93,36 @@ export const getAssetDenominationOrId = (token: Token | DefaultAsset, denominati
})
)
}
export const getAssetDenominationOrUnknown = (
token: Token | DefaultAsset,
denomination: AssetDenomination,
intl: IntlShape,
): string => getAssetDenomination(token, denomination) ?? intl.formatMessage(messages.unknownAssetName)

export const normalizeTokenAmount = (amount: BigNumber, token: Token | DefaultAsset): BigNumber => {
const normalizationFactor = Math.pow(10, token.metadata.numberOfDecimals)
return amount.dividedBy(normalizationFactor).decimalPlaces(token.metadata.numberOfDecimals)
}

export const formatTokenAmount = (amount: BigNumber, token: Token | DefaultAsset): string => {
const normalized = normalizeTokenAmount(amount, token)
const amountStr = normalized.toFormat(token.metadata.numberOfDecimals)

return amountStr
}

export const formatTokenWithSymbol = (amount: BigNumber, token: Token | DefaultAsset): string => {
const denomination = getAssetDenominationOrId(token, ASSET_DENOMINATION.SYMBOL)
return `${formatTokenAmount(amount, token)}${utfSymbols.NBSP}${denomination}`
}
// We assume that tickers are non-localized. If ticker doesn't exist, default
// to identifier

export const formatTokenWithText = (amount: BigNumber, token: Token | DefaultAsset) => {
const tickerOrId = getAssetDenominationOrId(token)
return `${formatTokenAmount(amount, token)}${utfSymbols.NBSP}${tickerOrId}`
}

export const formatTokenWithTextWhenHidden = (text: string, token: Token | DefaultAsset) => {
const tickerOrId = getAssetDenominationOrId(token)
return `${text}${utfSymbols.NBSP}${tickerOrId}`
}

export const formatTokenInteger = (amount: BigNumber, token: Token | DefaultAsset) => {
const normalizationFactor = Math.pow(10, token.metadata.numberOfDecimals)
const num = amount.dividedToIntegerBy(normalizationFactor)
Expand All @@ -138,12 +134,14 @@ export const formatTokenInteger = (amount: BigNumber, token: Token | DefaultAsse
return num.toFormat(0)
}
}

export const formatTokenFractional = (amount: BigNumber, token: Token | DefaultAsset) => {
const normalizationFactor = Math.pow(10, token.metadata.numberOfDecimals)
const fractional = amount.abs().modulo(normalizationFactor).dividedBy(normalizationFactor)
// remove leading '0'
return fractional.toFormat(token.metadata.numberOfDecimals).substring(1)
}

export const truncateWithEllipsis = (s: string, n: number) => {
if (s.length > n) {
return `${s.substr(0, Math.floor(n / 2))}...${s.substr(s.length - Math.floor(n / 2))}`
Expand All @@ -154,36 +152,23 @@ export const truncateWithEllipsis = (s: string, n: number) => {
// TODO(multi-asset): consider removing these
const defaultAssetMeta = getCardanoDefaultAsset().metadata
const normalizationFactor = Math.pow(10, defaultAssetMeta.numberOfDecimals)
export const formatAda = (amount: BigNumber) => {

const formatAda = (amount: BigNumber) => {
const num = amount.dividedBy(normalizationFactor)
return num.toFormat(6)
}
export const formatAdaWithSymbol = (amount: BigNumber) =>
`${formatAda(amount)}${utfSymbols.NBSP}${utfSymbols.CURRENCIES.ADA}`
// We assume that "ADA" is non-localized

export const formatAdaWithText = (amount: BigNumber) =>
`${formatAda(amount)}${utfSymbols.NBSP}${defaultAssetMeta.ticker}`
export const formatAdaInteger = (amount: BigNumber) => {
const num = amount.dividedToIntegerBy(normalizationFactor)

if (amount.lt(0) && amount.gt(-normalizationFactor)) {
// -0 needs special handling
return '-0'
} else {
return num.toFormat(0)
}
}
export const formatAdaFractional = (amount: BigNumber) => {
const fractional = amount.abs().modulo(normalizationFactor).dividedBy(normalizationFactor)
// remove leading '0'
return fractional.toFormat(6).substring(1)
}
export const formatTimeToSeconds = (ts: string | any) => {
return moment(ts).format((moment(0) as any)._locale._format.timeToSeconds)
}

export const formatDateToSeconds = (ts: string | any) => {
return moment(ts).format((moment(0) as any)._locale._format.dateToSeconds)
}

export const formatDateRelative = (ts: string | any, intl: IntlShape) => {
const config = {
sameDay: `[${intl.formatMessage(messages.today)}]`,
Expand All @@ -196,3 +181,18 @@ export const formatDateRelative = (ts: string | any, intl: IntlShape) => {
}
return moment(ts).calendar(null, config)
}

const messages = defineMessages({
today: {
id: 'utils.format.today',
defaultMessage: '!!!Today',
},
yesterday: {
id: 'utils.format.yesterday',
defaultMessage: '!!!Yesterday',
},
unknownAssetName: {
id: 'utils.format.unknownAssetName',
defaultMessage: '!!![Unknown asset name]',
},
})

0 comments on commit 5c7231e

Please sign in to comment.