From fa36e9abf398b350c350f3452713826d54ad5cf5 Mon Sep 17 00:00:00 2001 From: Dust Date: Fri, 13 Dec 2024 22:52:21 -0300 Subject: [PATCH] CU-86a5x0q43 - BS Swap - Bug - availableTokensToUse has tokens that do not have decimals --- .../CU-86a5x0q43_2024-12-14-01-53.json | 10 ++++++++ packages/bs-swap/src/apis/SimpleSwapApi.ts | 25 ++++++++++++------- .../bs-swap/src/services/SimpleSwapService.ts | 5 +++- 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 common/changes/@cityofzion/bs-swap/CU-86a5x0q43_2024-12-14-01-53.json diff --git a/common/changes/@cityofzion/bs-swap/CU-86a5x0q43_2024-12-14-01-53.json b/common/changes/@cityofzion/bs-swap/CU-86a5x0q43_2024-12-14-01-53.json new file mode 100644 index 0000000..d7d0aa5 --- /dev/null +++ b/common/changes/@cityofzion/bs-swap/CU-86a5x0q43_2024-12-14-01-53.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-swap", + "comment": "Fix bug where availableTokensToUse has tokens that do not have decimals", + "type": "patch" + } + ], + "packageName": "@cityofzion/bs-swap" +} \ No newline at end of file diff --git a/packages/bs-swap/src/apis/SimpleSwapApi.ts b/packages/bs-swap/src/apis/SimpleSwapApi.ts index be301e8..cd6a990 100644 --- a/packages/bs-swap/src/apis/SimpleSwapApi.ts +++ b/packages/bs-swap/src/apis/SimpleSwapApi.ts @@ -10,6 +10,7 @@ import { SimpleSwapApiGetRangeResponse, SimpleSwapServiceInitParams, } from '../types/simpleSwap' +import { Token } from '@cityofzion/blockchain-service' export class SimpleSwapApi { #axios: AxiosInstance @@ -42,17 +43,23 @@ export class SimpleSwapApi { if (chainsByServiceNameEntry) { blockchain = chainsByServiceNameEntry[0] - if (!hash) { - const token = options.blockchainServicesByName[blockchain].tokens.find( - token => currency.ticker?.toLowerCase().startsWith(token.symbol.toLowerCase()) + let token: Token | undefined + + if (hash) { + token = options.blockchainServicesByName[blockchain].tokens.find(item => hash!.replace('0x', '') === item.hash) + } + + if (!token) { + token = options.blockchainServicesByName[blockchain].tokens.find( + item => currency.ticker?.toLowerCase().startsWith(item.symbol.toLowerCase()) ) + } - if (token) { - hash = token.hash - decimals = token.decimals - name = token.name - symbol = token.symbol - } + if (token) { + hash = token.hash + decimals = token.decimals + name = token.name + symbol = token.symbol } } diff --git a/packages/bs-swap/src/services/SimpleSwapService.ts b/packages/bs-swap/src/services/SimpleSwapService.ts index 5f74533..313e114 100644 --- a/packages/bs-swap/src/services/SimpleSwapService.ts +++ b/packages/bs-swap/src/services/SimpleSwapService.ts @@ -198,7 +198,10 @@ export class SimpleSwapService implements SwapSe blockchainServicesByName: this.#blockchainServicesByName, chainsByServiceName: this.#chainsByServiceName, }) - this.#availableTokensToUse = { loading: false, value: tokens } + + const filteredTokens = tokens.filter(token => token.blockchain && token.decimals && token.hash) + + this.#availableTokensToUse = { loading: false, value: filteredTokens } } async setTokenToUse(token: SwapServiceToken | null): Promise {