From 36324f700e84c3ee33f1fcfecbf5d947fad5bdac Mon Sep 17 00:00:00 2001 From: Dust Date: Wed, 18 Dec 2024 18:06:10 -0300 Subject: [PATCH] CU-86a5ypu8q - BS Lib - Fix issue that is limiting tokens we can swap FROM to just a couple options --- .../CU-86a5ypu8q_2024-12-18-20-59.json | 10 ++++++++ .../bs-swap/src/services/SimpleSwapService.ts | 24 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 common/changes/@cityofzion/bs-swap/CU-86a5ypu8q_2024-12-18-20-59.json diff --git a/common/changes/@cityofzion/bs-swap/CU-86a5ypu8q_2024-12-18-20-59.json b/common/changes/@cityofzion/bs-swap/CU-86a5ypu8q_2024-12-18-20-59.json new file mode 100644 index 0000000..1435051 --- /dev/null +++ b/common/changes/@cityofzion/bs-swap/CU-86a5ypu8q_2024-12-18-20-59.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-swap", + "comment": "Remove mandatory decimals and fetch it in setTokenToUse function if it doesn't exist", + "type": "patch" + } + ], + "packageName": "@cityofzion/bs-swap" +} \ No newline at end of file diff --git a/packages/bs-swap/src/services/SimpleSwapService.ts b/packages/bs-swap/src/services/SimpleSwapService.ts index 61c5156..bf5b131 100644 --- a/packages/bs-swap/src/services/SimpleSwapService.ts +++ b/packages/bs-swap/src/services/SimpleSwapService.ts @@ -27,7 +27,7 @@ export class SimpleSwapService implements SwapSe #chainsByServiceName: Partial> #internalAvailableTokensToUse: SwapServiceLoadableValue[]> = { - loading: true, + loading: false, value: null, } #internalTokenToUse: SwapServiceLoadableValue> = { loading: false, value: null } @@ -232,15 +232,18 @@ export class SimpleSwapService implements SwapSe async init() { try { + this.#availableTokensToUse = { loading: true } + const tokens = await this.#api.getCurrencies({ blockchainServicesByName: this.#blockchainServicesByName, chainsByServiceName: this.#chainsByServiceName, }) - const filteredTokens = tokens.filter(token => token.blockchain && token.decimals !== undefined && token.hash) + const filteredTokens = tokens.filter(token => token.blockchain && token.hash) this.#availableTokensToUse = { loading: false, value: filteredTokens } } catch (error: any) { + this.#availableTokensToUse = { loading: false, value: [] } this.eventEmitter.emit('error', error.message) throw error } @@ -249,6 +252,7 @@ export class SimpleSwapService implements SwapSe async setTokenToUse(token: SwapServiceToken | null): Promise { this.#amountToReceive = { loading: false, value: null } this.#amountToUseMinMax = { loading: false, value: null } + this.#tokenToUse = { loading: true } if (!this.#availableTokensToUse.value) throw new Error('Available tokens to use is not set') @@ -259,6 +263,22 @@ export class SimpleSwapService implements SwapSe if (!simpleSwapCurrency) throw new Error('You are trying to use a token that is not available') } + if (!token?.blockchain || !token.hash) throw new Error('Token is not valid') + + if (simpleSwapCurrency && simpleSwapCurrency.decimals === undefined) { + let decimals = 6 + + try { + const service = this.#blockchainServicesByName[token.blockchain] + const tokenInfo = await service.blockchainDataService.getTokenInfo(token.hash) + decimals = tokenInfo.decimals + } catch { + /* empty */ + } + + simpleSwapCurrency.decimals = decimals + } + this.#tokenToUse = { loading: false, value: simpleSwapCurrency } if (this.#accountToUse.value?.blockchain !== simpleSwapCurrency?.blockchain) {