Skip to content

Commit

Permalink
param override
Browse files Browse the repository at this point in the history
  • Loading branch information
Subarna-Singh committed Dec 13, 2024
1 parent eb94f47 commit 90fc3af
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
59 changes: 31 additions & 28 deletions packages/composites/glv-token/src/transport/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Token,
Market,
mapSymbol,
unsupportedAssets,
mapParameter,
} from './utils'
import abi from '../config/readerAbi.json'
import glvAbi from '../config/glvReaderAbi.json'
Expand Down Expand Up @@ -79,6 +79,11 @@ export class GlvTokenTransport extends SubscriptionTransport<GlvTokenTransportTy

await this.tokenInfo()
await this.marketInfo()

setInterval(() => {
this.tokenInfo()
this.marketInfo()
}, 60 * 60 * 3 * 1000) // 3 hours
}

async tokenInfo() {
Expand All @@ -92,6 +97,7 @@ export class GlvTokenTransport extends SubscriptionTransport<GlvTokenTransportTy
JSON.stringify(requestConfig),
requestConfig,
)

const data: Token[] = response.response.data.tokens
data.map((token) => {
this.tokensMap[token.address] = token
Expand All @@ -110,6 +116,7 @@ export class GlvTokenTransport extends SubscriptionTransport<GlvTokenTransportTy
JSON.stringify(requestConfig),
requestConfig,
)

const data: Market[] = response.response.data.markets
data.map((market) => {
this.marketsMap[market.marketToken] = market
Expand Down Expand Up @@ -235,7 +242,8 @@ export class GlvTokenTransport extends SubscriptionTransport<GlvTokenTransportTy
for (let i = 0; i < sources.length; i++) {
const source = sources[i]
const assetPromises = assets.map(async (asset) => {
const base = this.unwrapAsset(asset)
const mappedAsset = mapParameter(source.name, asset)
const base = this.unwrapAsset(mappedAsset)
const requestConfig = {
url: source.url,
method: 'POST',
Expand All @@ -248,33 +256,28 @@ export class GlvTokenTransport extends SubscriptionTransport<GlvTokenTransportTy
},
}

if (unsupportedAssets[source.name].includes(asset)) {
// skip assets not supported by EA to avoid Rate Limiting Error
undefined
} else {
// try/catch is needed in a case if one of source EAs fails to return a response,
// we will still get and calculate the median price based on responses of remaining EAs (based on MIN_REQUIRED_SOURCE_SUCCESS setting)
try {
const response = await this.requester.request<{ data: LwbaResponseDataFields['Data'] }>(
JSON.stringify(requestConfig),
requestConfig,
)
const { bid, ask } = response.response.data.data

priceData[asset] = {
bids: [...(priceData[asset]?.bids || []), bid],
asks: [...(priceData[asset]?.asks || []), ask],
}

priceProviders[base] = priceProviders[base]
? [...new Set([...priceProviders[base], source.name])]
: [source.name]
} catch (error) {
const e = error as Error
logger.error(
`Error fetching data for ${asset} from ${source.name}, url - ${source.url}: ${e.message}`,
)
// try/catch is needed in a case if one of source EAs fails to return a response,
// we will still get and calculate the median price based on responses of remaining EAs (based on MIN_REQUIRED_SOURCE_SUCCESS setting)
try {
const response = await this.requester.request<{ data: LwbaResponseDataFields['Data'] }>(
JSON.stringify(requestConfig),
requestConfig,
)
const { bid, ask } = response.response.data.data

priceData[asset] = {
bids: [...(priceData[asset]?.bids || []), bid],
asks: [...(priceData[asset]?.asks || []), ask],
}

priceProviders[asset] = priceProviders[asset]
? [...new Set([...priceProviders[asset], source.name])]
: [source.name]
} catch (error) {
const e = error as Error
logger.error(
`Error fetching data for ${asset} from ${source.name}, url - ${source.url}: ${e.message}`,
)
}
})

Expand Down
22 changes: 16 additions & 6 deletions packages/composites/glv-token/src/transport/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const median = (values: number[]): number => {
Formats a given number with a specified precision without leading zeros and decimal point.
Price decimals = SIGNED_PRICE_DECIMALS - token decimals
toFixed(14.84329267, 6) -> '14843292670000'
toFixed(0.99999558, 18) -> '999995580000000000000000'
toFixed(14.84329267, 18) -> '14843292670000'
toFixed(0.99999558, 6) -> '999995580000000000000000'
*/
export const toFixed = (number: number, decimals: number): string => {
const n = new Decimal(number)
Expand Down Expand Up @@ -53,8 +53,18 @@ export function mapSymbol(address: string, symbolMap: Record<string, any>) {
return symbolMap[address]
}

export const unsupportedAssets: Record<string, string[]> = {
coinmetrics: ['TAO'],
tiingo: ['FLOKI'],
ncfx: [],
const adapterParamOverride: Record<string, Record<string, string>> = {
coinmetrics: {
TAO: 'tao_bittensor',
},
tiingo: {
FLOKI: 'floki2',
},
}

export function mapParameter(source: string, param: string) {
if (source in adapterParamOverride && param in adapterParamOverride[source]) {
return adapterParamOverride[source][param]
}
return param
}

0 comments on commit 90fc3af

Please sign in to comment.