Skip to content

Commit

Permalink
CU-86a5ypu8q - BS Lib - Fix issue that is limiting tokens we can swap…
Browse files Browse the repository at this point in the history
… FROM to just a couple options
  • Loading branch information
dustr94 committed Dec 18, 2024
1 parent e27ee22 commit d572be2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
24 changes: 22 additions & 2 deletions packages/bs-swap/src/services/SimpleSwapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
#chainsByServiceName: Partial<Record<BSName, string[]>>

#internalAvailableTokensToUse: SwapServiceLoadableValue<SimpleSwapApiCurrency<BSName>[]> = {
loading: true,
loading: false,
value: null,
}
#internalTokenToUse: SwapServiceLoadableValue<SimpleSwapApiCurrency<BSName>> = { loading: false, value: null }
Expand Down Expand Up @@ -232,15 +232,18 @@ export class SimpleSwapService<BSName extends string = string> 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
}
Expand All @@ -249,6 +252,7 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
async setTokenToUse(token: SwapServiceToken<BSName> | null): Promise<void> {
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')

Expand All @@ -259,6 +263,22 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
if (!simpleSwapCurrency) throw new Error('You are trying to use a token that is not available')
}

if (simpleSwapCurrency && simpleSwapCurrency.decimals === undefined) {
if (!simpleSwapCurrency?.blockchain || !simpleSwapCurrency.hash) throw new Error('Token is not valid')

let decimals = 6

try {
const service = this.#blockchainServicesByName[simpleSwapCurrency.blockchain]
const tokenInfo = await service.blockchainDataService.getTokenInfo(simpleSwapCurrency.hash)
decimals = tokenInfo.decimals
} catch {
/* empty */
}

simpleSwapCurrency.decimals = decimals
}

this.#tokenToUse = { loading: false, value: simpleSwapCurrency }

if (this.#accountToUse.value?.blockchain !== simpleSwapCurrency?.blockchain) {
Expand Down

0 comments on commit d572be2

Please sign in to comment.