Skip to content

Commit

Permalink
fix: fix app crash on sendflow (#6830)
Browse files Browse the repository at this point in the history
* chore: update wallet version

* chore: apply sdk renames

* fix: set amount to undefined on form switch

* fix: fix tests

* chore: add @types/big.js

* chore: add strict type checking

---------

Co-authored-by: Tuditi <[email protected]>
  • Loading branch information
MarkNerdi996 and Tuditi authored May 17, 2023
1 parent c495923 commit 5d3bc63
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/shared/components/inputs/AssetAmountInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$: bigAmount = convertToRawAmount(amount, asset?.metadata, unit)
$: marketAmount = getMarketAmountFromAssetValue(bigAmount, asset)
$: max = parseCurrency(formatTokenAmountDefault(availableBalance, asset?.metadata, unit, false))
$: rawAmount = bigAmount.toString()
$: rawAmount = bigAmount?.toString()
function onClickAvailableBalance(): void {
const isRawAmount = asset?.metadata?.decimals && getUnitFromTokenMetadata(asset?.metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const INVALID_TOKEN_METADATA: TokenMetadata = {

describe('File: convertToRawAmount.ts', () => {
it('should return undefined if amount is empty', () => {
expect(convertToRawAmount('', INVALID_TOKEN_METADATA)).toStrictEqual(-1)
expect(convertToRawAmount('', INVALID_TOKEN_METADATA)).toStrictEqual(undefined)
})

describe('given the tokenMetadata standard is BaseToken', () => {
Expand Down Expand Up @@ -73,10 +73,10 @@ describe('File: convertToRawAmount.ts', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer], 'glow')).toStrictEqual(Big('1'))
})
it('should return undefined if a unit is not provided', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer])).toStrictEqual(-1)
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer])).toStrictEqual(undefined)
})
it('should return undefined if provided unit does not match the tokenMetadata unit or subunit', () => {
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer], 'test')).toStrictEqual(-1)
expect(convertToRawAmount('1', DEFAULT_BASE_TOKEN[NetworkId.Shimmer], 'test')).toStrictEqual(undefined)
})
})
})
Expand Down
16 changes: 10 additions & 6 deletions packages/shared/lib/core/wallet/utils/convertToRawAmount.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import Big from 'big.js'

import { parseCurrency } from '@core/i18n'
import { IOTA_UNIT_MAP } from '@core/utils'
import { IOTA_UNIT_MAP, IotaUnit } from '@core/utils'

import { TokenMetadata } from '../types'
import { MAX_SUPPORTED_DECIMALS } from '../constants/max-supported-decimals.constants'
import { TokenStandard } from '../enums'

export function convertToRawAmount(amount: string, tokenMetadata: TokenMetadata, unit?: string): Big {
export function convertToRawAmount(amount: string, tokenMetadata: TokenMetadata, unit?: string): Big | undefined {
if (amount) {
const parsedAmount = parseCurrency(amount)
return convertToRawAmountFromMetadata(parsedAmount, tokenMetadata, unit)
} else {
return -1
return undefined
}
}

function convertToRawAmountFromMetadata(amount: number, tokenMetadata: TokenMetadata, selectedUnit: string): Big {
function convertToRawAmountFromMetadata(
amount: number,
tokenMetadata: TokenMetadata,
selectedUnit?: string
): Big | undefined {
if (tokenMetadata?.standard === TokenStandard.BaseToken) {
if (tokenMetadata.useMetricPrefix) {
const decimals = IOTA_UNIT_MAP?.[selectedUnit?.substring(0, 1)]?.decimalPlaces ?? 0
const decimals = IOTA_UNIT_MAP?.[selectedUnit?.substring(0, 1) as IotaUnit]?.decimalPlaces ?? 0
return convertAmountToMatchUnit(amount, decimals)
} else {
if (selectedUnit === tokenMetadata.unit) {
Expand All @@ -28,7 +32,7 @@ function convertToRawAmountFromMetadata(amount: number, tokenMetadata: TokenMeta
} else if (selectedUnit === tokenMetadata.subunit) {
return Big(amount)
} else {
return -1
return undefined
}
}
} else if (tokenMetadata?.standard === TokenStandard.Irc30) {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@swc/core": "^1.3.44",
"@swc/jest": "^0.2.24",
"@tsconfig/svelte": "^1.0.10",
"@types/big.js": "^6.1.6",
"@types/jest": "^29.5.0",
"autoprefixer": "^10.0.2",
"jest": "^29.5.0",
Expand Down
35 changes: 26 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/big.js@^6.1.6":
version "6.1.6"
resolved "https://registry.yarnpkg.com/@types/big.js/-/big.js-6.1.6.tgz#3d417e758483d55345a03a087f7e0c87137ca444"
integrity sha512-0r9J+Zz9rYm2hOTwiMAVkm3XFQ4u5uTK37xrQMhc9bysn/sf/okzovWMYYIBMFTn/yrEZ11pusgLEaoarTlQbA==

"@types/bignumber.js@^5.0.0":
version "5.0.0"
resolved "https://registry.npmjs.org/@types/bignumber.js/-/bignumber.js-5.0.0.tgz"
Expand Down Expand Up @@ -2607,11 +2612,16 @@ ansi-html-community@^0.0.8:
resolved "https://registry.npmjs.org/ansi-html-community/-/ansi-html-community-0.0.8.tgz"
integrity sha512-1APHAyr3+PCamwNw3bXCPp4HFLONZt/yIH0sZp0/469KWNTEy+qN5jQ3GVX6DMZ1UXAi34yVwtTeaG/HpBuuzw==

ansi-regex@5.0.1, ansi-regex@^5.0.1, ansi-regex@^6.0.1:
ansi-regex@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==

ansi-regex@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a"
integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==

ansi-styles@^3.2.1:
version "3.2.1"
resolved "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz"
Expand Down Expand Up @@ -5267,7 +5277,14 @@ [email protected]:
resolved "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz"
integrity sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==

"glob-parent@ >=5.1.2", glob-parent@^5.1.2, glob-parent@^6.0.1, glob-parent@^6.0.2, glob-parent@~5.1.2:
glob-parent@^5.1.2, glob-parent@~5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==
dependencies:
is-glob "^4.0.1"

glob-parent@^6.0.1, glob-parent@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-6.0.2.tgz#6d237d99083950c79290f24c7642a3de9a28f9e3"
integrity sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==
Expand Down Expand Up @@ -6935,7 +6952,7 @@ lodash.topath@^4.5.2:
resolved "https://registry.npmjs.org/lodash.topath/-/lodash.topath-4.5.2.tgz"
integrity sha512-1/W4dM+35DwvE/iEd1M9ekewOSTlpFekhw9mhAtrwjVqUr83/ilQiyAvmg4tVX7Unkcfl1KC+i9WdaT4B6aQcg==

lodash@>=4.17.21, lodash@^4.17.15, lodash@^4.17.21:
lodash@^4.17.15, lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down Expand Up @@ -7417,7 +7434,7 @@ nano-json-stream-parser@^0.1.2:
resolved "https://registry.yarnpkg.com/nano-json-stream-parser/-/nano-json-stream-parser-0.1.2.tgz#0cc8f6d0e2b622b479c40d499c46d64b755c6f5f"
integrity sha512-9MqxMH/BSJC7dnLsEMPyfN5Dvoo49IsPFYMcHw3Bcfc2kN0lpHRBSzlMSVx4HGyJ7s9B31CyBTVehWJoQ8Ctew==

nanoid@^3.1.31, nanoid@^3.3.4:
nanoid@^3.3.4:
version "3.3.6"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==
Expand Down Expand Up @@ -7474,7 +7491,7 @@ nice-try@^1.0.4:
resolved "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

node-abi@^3.0.0, node-abi@^3.3.0, node-abi@^3.8.0:
node-abi@^3.0.0, node-abi@^3.3.0:
version "3.40.0"
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-3.40.0.tgz#51d8ed44534f70ff1357dfbc3a89717b1ceac1b4"
integrity sha512-zNy02qivjjRosswoYmPi8hIKJRr8MpQyeKT6qlcq/OnOgA3Rhoae+IYOqsM9V5+JnHWmxKnWOT2GxvtqdtOCXA==
Expand Down Expand Up @@ -7666,7 +7683,7 @@ [email protected]:
bn.js "4.11.6"
strip-hex-prefix "1.0.0"

nwsapi@^2.2.1, nwsapi@^2.2.2:
nwsapi@^2.2.2:
version "2.2.4"
resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.2.4.tgz#fd59d5e904e8e1f03c25a7d5a15cfa16c714a1e5"
integrity sha512-NHj4rzRo0tQdijE9ZqAx6kYDcoRwYwSYzCA8MY3JzfxlrvEU0jhnhJT9BhqhJs7I/dKcrDm6TyulaRqZPIhN5g==
Expand Down Expand Up @@ -8237,7 +8254,7 @@ promise-retry@^2.0.1:
err-code "^2.0.2"
retry "^0.12.0"

prompts@^2.0.1, prompts@^2.3.2, prompts@^2.4.2:
prompts@^2.0.1, prompts@^2.3.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
integrity sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==
Expand Down Expand Up @@ -8802,7 +8819,7 @@ select-hose@^2.0.0:
resolved "https://registry.npmjs.org/select-hose/-/select-hose-2.0.0.tgz"
integrity sha512-mEugaLK+YfkijB4fx0e6kImuJdCIt2LxCRcbEYPqRGCs4F2ogyfZU5IAZRdjCP8JPq2AtdNoC/Dux63d9Kiryg==

selfsigned@^2.0.1, selfsigned@^2.1.1:
selfsigned@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/selfsigned/-/selfsigned-2.1.1.tgz#18a7613d714c0cd3385c48af0075abf3f266af61"
integrity sha512-GSL3aowiF7wa/WtSFwnUrludWFoNhftq8bUkH9pkzjpN2XSPOAYEgg6e0sS9s0rZwgJzJiQRPU18A6clnoW5wQ==
Expand Down Expand Up @@ -8969,7 +8986,7 @@ shebang-regex@^3.0.0:
resolved "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==

shell-quote@1.7.3, shell-quote@^1.6.1, shell-quote@^1.7.3:
shell-quote@^1.6.1, shell-quote@^1.7.3:
version "1.7.3"
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.7.3.tgz#aa40edac170445b9a431e17bb62c0b881b9c4123"
integrity sha512-Vpfqwm4EnqGdlsBFNmHhxhElJYrdfcxPThu+ryKS5J8L/fhAwLazFZtq+S+TWZ9ANj2piSQLGj6NQg+lKPmxrw==
Expand Down

0 comments on commit 5d3bc63

Please sign in to comment.