-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce0187c
commit c83fcbc
Showing
20 changed files
with
149 additions
and
174 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
4 changes: 2 additions & 2 deletions
4
packages/shared/lib/auxiliary/deep-link/types/wallet-context.type.ts
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Unit } from '@lib/units' | ||
import { IotaUnit } from '@core/utils' | ||
|
||
/** | ||
* The parameters of a send operation. | ||
*/ | ||
export type SendOperationParameters = { | ||
address: string | ||
amount: string | ||
unit: Unit | ||
unit: IotaUnit | ||
message: string | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './bech32-address-length.constant' | ||
export * from './hex.constants' | ||
export * from './iota-unit-map.constant' | ||
export * from './max-number-of-iotas.constant' | ||
export * from './pin-length.constant' |
11 changes: 11 additions & 0 deletions
11
packages/shared/lib/core/utils/constants/iota-unit-map.constant.ts
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,11 @@ | ||
import { IotaUnit } from '../enums' | ||
import { IotaUnitMap } from '../types' | ||
|
||
export const IOTA_UNIT_MAP: IotaUnitMap = { | ||
[IotaUnit._]: { value: 1, decimalPlaces: 0 }, | ||
[IotaUnit.K]: { value: 1000, decimalPlaces: 3 }, | ||
[IotaUnit.M]: { value: 1000000, decimalPlaces: 6 }, | ||
[IotaUnit.G]: { value: 1000000000, decimalPlaces: 9 }, | ||
[IotaUnit.T]: { value: 1000000000000, decimalPlaces: 12 }, | ||
[IotaUnit.P]: { value: 1000000000000000, decimalPlaces: 15 }, | ||
} |
1 change: 1 addition & 0 deletions
1
packages/shared/lib/core/utils/constants/max-number-of-iotas.constant.ts
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 @@ | ||
export const MAX_NUM_IOTAS = 2_779_530_283_277_761 |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './expiration-time.enum' | ||
export * from './iota-unit.enum' |
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,8 @@ | ||
export enum IotaUnit { | ||
_ = '_', | ||
K = 'k', | ||
M = 'M', | ||
G = 'G', | ||
T = 'T', | ||
P = 'P', | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './dropdown.interface' | ||
export * from './iota-unit-map.type' | ||
export * from './values-of.type' |
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,8 @@ | ||
import { IotaUnit } from '../enums' | ||
|
||
export type IotaUnitMap = { | ||
[unit in IotaUnit]: { | ||
value: number | ||
decimalPlaces: number | ||
} | ||
} |
File renamed without changes.
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,90 @@ | ||
import Big from 'big.js' | ||
|
||
import { formatNumber, getCurrencyPosition } from '@lib/currency' | ||
|
||
import { IOTA_UNIT_MAP } from './constants' | ||
import { IotaUnit } from './enums' | ||
|
||
/** | ||
* CAUTION: Set this to avoid small numbers switching to exponential format. | ||
*/ | ||
Big.NE = -20 | ||
|
||
export function convertIotaUnit(value: number, fromUnit: IotaUnit, toUnit: IotaUnit): number { | ||
if (value === 0) { | ||
return 0 | ||
} | ||
|
||
if (fromUnit === toUnit) { | ||
return value | ||
} | ||
|
||
const scaledValue = Number(new Big(value).times(IOTA_UNIT_MAP[fromUnit].value).div(IOTA_UNIT_MAP[toUnit].value)) | ||
|
||
return toUnit === IotaUnit._ ? Math.round(scaledValue) : scaledValue | ||
} | ||
|
||
export function formatIotaUnitPrecision( | ||
valueRaw: number, | ||
unit: IotaUnit, | ||
includeUnits: boolean = true, | ||
grouped: boolean = false, | ||
overrideDecimalPlaces?: number | ||
): string { | ||
// At the moment we have no symbol for IOTA so we always put the currency code | ||
// at the end, in the future when we have a symbol this can be updated to position | ||
// it correctly to the left when necessary | ||
const currencyPosition = getCurrencyPosition() | ||
|
||
if (!valueRaw) { | ||
return includeUnits ? (currencyPosition === 'left' ? `0 ${unit}` : `0 ${unit}`) : '0' | ||
} | ||
|
||
const converted = convertIotaUnit(valueRaw, IotaUnit._, unit) | ||
|
||
const formatted = formatNumber( | ||
converted, | ||
overrideDecimalPlaces ?? IOTA_UNIT_MAP[unit].decimalPlaces, | ||
overrideDecimalPlaces ?? IOTA_UNIT_MAP[unit].decimalPlaces, | ||
unit === IotaUnit._ ? 0 : 2, | ||
grouped | ||
) | ||
|
||
if (includeUnits) { | ||
return currencyPosition === 'left' ? `${formatted} ${unit}` : `${formatted} ${unit}` | ||
} else { | ||
return formatted | ||
} | ||
} | ||
|
||
export function formatIotaUnitBestMatch( | ||
value: number, | ||
includeUnits: boolean = true, | ||
overrideDecimalPlaces?: number | ||
): string { | ||
return formatIotaUnitPrecision(value, getIotaUnit(value), includeUnits, false, overrideDecimalPlaces) | ||
} | ||
|
||
export function getIotaUnit(value: number): IotaUnit { | ||
let bestUnits: IotaUnit = IotaUnit._ | ||
|
||
if (!value || value === 0) { | ||
return IotaUnit.M | ||
} | ||
|
||
const checkLength = Math.abs(value).toString().length | ||
|
||
if (checkLength > IOTA_UNIT_MAP.P.decimalPlaces) { | ||
bestUnits = IotaUnit.P | ||
} else if (checkLength > IOTA_UNIT_MAP.T.decimalPlaces) { | ||
bestUnits = IotaUnit.T | ||
} else if (checkLength > IOTA_UNIT_MAP.G.decimalPlaces) { | ||
bestUnits = IotaUnit.G | ||
} else if (checkLength > IOTA_UNIT_MAP.M.decimalPlaces) { | ||
bestUnits = IotaUnit.M | ||
} else if (checkLength > IOTA_UNIT_MAP.k.decimalPlaces) { | ||
bestUnits = IotaUnit.K | ||
} | ||
|
||
return bestUnits | ||
} |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import * as currency from './currency' | ||
import * as events from './events' | ||
import * as message from './message' | ||
import * as utils from './utils' | ||
import * as wallet from './dateDifference' | ||
|
||
export { currency, events, message, utils, wallet } | ||
export { currency, events, message, wallet } |
Oops, something went wrong.