Skip to content

Commit

Permalink
Merge pull request #133 from CityOfZion/CU-86a5z9zkg
Browse files Browse the repository at this point in the history
CU-86a5z9zkg - BS Lib - Enhance failure handling for native token has…
  • Loading branch information
thiagocbalducci authored Dec 19, 2024
2 parents 1b216f8 + 6e73001 commit 5874676
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-ethereum",
"comment": "Enhance failure handling for native token hash fetching",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-ethereum"
}
4 changes: 4 additions & 0 deletions packages/bs-ethereum/src/constants/BSEthereumConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class BSEthereumConstants {
'12227332': 'GAS',
}

static NATIVE_WRAPPED_HASH_BY_NETWORK_ID: Partial<Record<BSEthereumNetworkId, string>> = {
'1': '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
}

static RPC_LIST_BY_NETWORK_ID: Record<BSEthereumNetworkId, string[]> = {
'1': [
'https://eth.llamarpc.com',
Expand Down
4 changes: 2 additions & 2 deletions packages/bs-ethereum/src/helpers/BSEthereumHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Network } from '@cityofzion/blockchain-service'
import { Network, normalizeHash } from '@cityofzion/blockchain-service'
import { BSEthereumConstants, BSEthereumNetworkId } from '../constants/BSEthereumConstants'

export class BSEthereumHelper {
Expand All @@ -21,7 +21,7 @@ export class BSEthereumHelper {
}

static normalizeHash(hash: string): string {
return hash.startsWith('0x') ? hash.slice(2) : hash
return normalizeHash(hash)
}

static wait(duration: number): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TokenPricesHistoryResponse,
TokenPricesResponse,
} from '@cityofzion/blockchain-service'
import { BSEthereumNetworkId } from '../../constants/BSEthereumConstants'
import { BSEthereumConstants, BSEthereumNetworkId } from '../../constants/BSEthereumConstants'
import { BSEthereumHelper } from '../../helpers/BSEthereumHelper'
import { MoralisBDSEthereum } from '../blockchain-data/MoralisBDSEthereum'

Expand Down Expand Up @@ -47,10 +47,20 @@ export class MoralisEDSEthereum extends CryptoCompareEDS implements ExchangeData

async #getWrappedNativeToken(): Promise<Token> {
const nativeToken = BSEthereumHelper.getNativeAsset(this.#network)
const wrappedSymbol = `W${nativeToken.symbol}`
const localWrappedHash = BSEthereumConstants.NATIVE_WRAPPED_HASH_BY_NETWORK_ID[this.#network.id]
if (localWrappedHash) {
return {
...nativeToken,
symbol: wrappedSymbol,
hash: localWrappedHash,
}
}

const client = MoralisBDSEthereum.getClient(this.#network)
const { data } = await client.get<MoralisERC20MetadataResponse[]>('/erc20/metadata/symbols', {
params: {
symbols: [`W${nativeToken.symbol}`],
symbols: [wrappedSymbol],
},
})

Expand All @@ -74,18 +84,26 @@ export class MoralisEDSEthereum extends CryptoCompareEDS implements ExchangeData

let wrappedNativeToken: Token | undefined
if (params.tokens.some(token => token.symbol === nativeToken.symbol)) {
wrappedNativeToken = await this.#getWrappedNativeToken()
try {
wrappedNativeToken = await this.#getWrappedNativeToken()
} catch {
/* empty */
}
}

const tokensBody = params.tokens.map(token => {
const tokensBody: { token_address: string }[] = []

params.tokens.map(token => {
if (token.symbol !== nativeToken.symbol) {
return {
tokensBody.push({
token_address: token.hash,
}
})
}

return {
token_address: wrappedNativeToken!.hash,
if (wrappedNativeToken) {
tokensBody.push({
token_address: wrappedNativeToken.hash,
})
}
})

Expand All @@ -106,8 +124,9 @@ export class MoralisEDSEthereum extends CryptoCompareEDS implements ExchangeData
let token: Token

if (
wrappedNativeToken &&
BSEthereumHelper.normalizeHash(item.tokenAddress) ===
BSEthereumHelper.normalizeHash(wrappedNativeToken?.hash ?? '')
BSEthereumHelper.normalizeHash(wrappedNativeToken.hash)
) {
token = nativeToken
} else {
Expand Down

0 comments on commit 5874676

Please sign in to comment.