diff --git a/common/changes/@cityofzion/blockchain-service/CU-86a5x0q43-3_2024-12-16-19-34.json b/common/changes/@cityofzion/blockchain-service/CU-86a5x0q43-3_2024-12-16-19-34.json new file mode 100644 index 0000000..50c188a --- /dev/null +++ b/common/changes/@cityofzion/blockchain-service/CU-86a5x0q43-3_2024-12-16-19-34.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/blockchain-service", + "comment": "Add new function to format number", + "type": "minor" + } + ], + "packageName": "@cityofzion/blockchain-service" +} \ No newline at end of file diff --git a/common/changes/@cityofzion/bs-swap/CU-86a5x0q43-3_2024-12-16-19-34.json b/common/changes/@cityofzion/bs-swap/CU-86a5x0q43-3_2024-12-16-19-34.json new file mode 100644 index 0000000..7097f6f --- /dev/null +++ b/common/changes/@cityofzion/bs-swap/CU-86a5x0q43-3_2024-12-16-19-34.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-swap", + "comment": "Fix incorrect amount formatting", + "type": "patch" + } + ], + "packageName": "@cityofzion/bs-swap" +} \ No newline at end of file diff --git a/packages/blockchain-service/src/functions.ts b/packages/blockchain-service/src/functions.ts index 34e2546..ab6a8f8 100644 --- a/packages/blockchain-service/src/functions.ts +++ b/packages/blockchain-service/src/functions.ts @@ -139,3 +139,29 @@ export async function fetchAccountsForBlockchainServices decimals) { + newValue = newValue.slice(0, newValue.length - countedDecimals + decimals) + } + } + + return newValue.replace(/\s|-/g, '').replace(/^([^.]*\.)(.*)$/, function (_a, b, c) { + return b + c.replace(/\./g, '') + }) +} diff --git a/packages/bs-swap/src/services/SimpleSwapService.ts b/packages/bs-swap/src/services/SimpleSwapService.ts index 812a08a..afc183a 100644 --- a/packages/bs-swap/src/services/SimpleSwapService.ts +++ b/packages/bs-swap/src/services/SimpleSwapService.ts @@ -1,6 +1,7 @@ import { Account, BlockchainService, + formatNumber, isCalculableFee, SwapService, SwapServiceEvents, @@ -167,11 +168,11 @@ export class SimpleSwapService implements SwapSe const apiRange = await this.#api.getRange(this.#tokenToUse.value, this.#tokenToReceive.value!) range = { min: this.#tokenToUse.value.decimals - ? Number(apiRange.min).toFixed(this.#tokenToUse.value.decimals) + ? formatNumber(apiRange.min, this.#tokenToUse.value.decimals) : apiRange.min, max: this.#tokenToUse.value.decimals && apiRange.max - ? Number(apiRange.max).toFixed(this.#tokenToUse.value.decimals) + ? formatNumber(apiRange.max, this.#tokenToUse.value.decimals) : apiRange.max, } } @@ -181,7 +182,7 @@ export class SimpleSwapService implements SwapSe if (shouldRecalculateAmountToUse) { this.#amountToUse = { value: this.#tokenToUse.value.decimals - ? Number(range.min).toFixed(this.#tokenToUse.value.decimals) + ? formatNumber(range.min, this.#tokenToUse.value.decimals) : range.min, } } @@ -243,7 +244,8 @@ export class SimpleSwapService implements SwapSe async setAmountToUse(amount: string | null): Promise { this.#amountToUse = { - value: this.#tokenToUse.value?.decimals ? Number(amount).toFixed(this.#tokenToUse.value.decimals) : amount, + value: + this.#tokenToUse.value?.decimals && amount ? formatNumber(amount, this.#tokenToUse.value.decimals) : amount, } debounce(this.#recalculateValues.bind(this), 500)(['amountToReceive'])