Skip to content

Commit

Permalink
Merge pull request #120 from CityOfZion/CU-86a5x0q43
Browse files Browse the repository at this point in the history
CU-86a5x0q43 - BS Swap - Bug - availableTokensToUse has tokens that d…
  • Loading branch information
hotequil authored Dec 14, 2024
2 parents cdc79f4 + 2f62471 commit 463803a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": "Add normalizeHash function",
"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 bug where availableTokensToUse has tokens that do not have decimals",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-swap"
}
4 changes: 4 additions & 0 deletions packages/blockchain-service/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ export async function fetchAccountsForBlockchainServices<BSName extends string =

return accountsByBlockchainService
}

export function normalizeHash(hash: string): string {
return hash.replace('0x', '').toLowerCase()
}
23 changes: 12 additions & 11 deletions packages/bs-swap/src/apis/SimpleSwapApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SimpleSwapApiGetRangeResponse,
SimpleSwapServiceInitParams,
} from '../types/simpleSwap'
import { normalizeHash } from '@cityofzion/blockchain-service'

export class SimpleSwapApi<BSName extends string = string> {
#axios: AxiosInstance
Expand Down Expand Up @@ -42,17 +43,17 @@ export class SimpleSwapApi<BSName extends string = string> {
if (chainsByServiceNameEntry) {
blockchain = chainsByServiceNameEntry[0]

if (!hash) {
const token = options.blockchainServicesByName[blockchain].tokens.find(
token => currency.ticker?.toLowerCase().startsWith(token.symbol.toLowerCase())
)

if (token) {
hash = token.hash
decimals = token.decimals
name = token.name
symbol = token.symbol
}
const token = options.blockchainServicesByName[blockchain].tokens.find(
item =>
(hash && normalizeHash(hash) === normalizeHash(item.hash)) ||
(currency.ticker && currency.ticker.toLowerCase().startsWith(item.symbol.toLowerCase()))
)

if (token) {
hash = token.hash
decimals = token.decimals
name = token.name
symbol = token.symbol
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/bs-swap/src/services/SimpleSwapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ export class SimpleSwapService<BSName extends string = string> implements SwapSe
blockchainServicesByName: this.#blockchainServicesByName,
chainsByServiceName: this.#chainsByServiceName,
})
this.#availableTokensToUse = { loading: false, value: tokens }

const filteredTokens = tokens.filter(token => token.blockchain && token.decimals !== undefined && token.hash)

this.#availableTokensToUse = { loading: false, value: filteredTokens }
}

async setTokenToUse(token: SwapServiceToken<BSName> | null): Promise<void> {
Expand Down

0 comments on commit 463803a

Please sign in to comment.