Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-86a5x0q43 - BS Swap - Bug - availableTokensToUse has tokens that d… #123

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading