Skip to content

Commit

Permalink
CU-86a5x0q43 - BS Swap - Bug - availableTokensToUse has tokens that d…
Browse files Browse the repository at this point in the history
…o not have decimals
  • Loading branch information
dustr94 committed Dec 16, 2024
1 parent 9d29c3e commit 0348f92
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": "Add new function to format number",
"type": "minor"
}
],
"packageName": "@cityofzion/blockchain-service"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-swap",
"comment": "Fix incorrect amount formatting",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-swap"
}
26 changes: 26 additions & 0 deletions packages/blockchain-service/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,29 @@ export async function fetchAccountsForBlockchainServices<BSName extends string =
export function normalizeHash(hash: string): string {
return hash.replace('0x', '').toLowerCase()
}

export function countDecimals(value: string | number) {
const [, decimals] = value.toString().split('.')
return decimals?.length ?? 0
}

export function formatNumber(value: string | number, decimals: number = 0) {
let newValue = typeof value === 'number' ? value.toFixed(decimals) : value

newValue = newValue.replace(/,|\.\.|\.,/g, '.')

if (decimals === 0) {
newValue = newValue.split('.')[0]
} else {
newValue = newValue.replace(/[^\d.]/g, '')
const countedDecimals = countDecimals(newValue)

if (countedDecimals > decimals) {
newValue = newValue.slice(0, newValue.length - countedDecimals + decimals)
}
}

return newValue.replace(/\s|-/g, '').replace(/^([^.]*\.)(.*)$/, function (_a, b, c) {
return b + c.replace(/\./g, '')
})
}
10 changes: 6 additions & 4 deletions packages/bs-swap/src/services/SimpleSwapService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Account,
BlockchainService,
formatNumber,
isCalculableFee,
SwapService,
SwapServiceEvents,
Expand Down Expand Up @@ -167,11 +168,11 @@ export class SimpleSwapService<BSName extends string = string> 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,
}
}
Expand All @@ -181,7 +182,7 @@ export class SimpleSwapService<BSName extends string = string> 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,
}
}
Expand Down Expand Up @@ -243,7 +244,8 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe

async setAmountToUse(amount: string | null): Promise<void> {
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'])
Expand Down

0 comments on commit 0348f92

Please sign in to comment.