From 0fa0c8dc47dc3f30fe0fb6d70cf0404209919cec Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 11:26:02 +0200 Subject: [PATCH 01/14] - implement SDR price calculation from sdr basket settings --- feeder/Dockerfile | 2 ++ feeder/src/index.ts | 16 +++++++++++++++ feeder/src/vote.ts | 49 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/feeder/Dockerfile b/feeder/Dockerfile index 1b44251..e57111b 100644 --- a/feeder/Dockerfile +++ b/feeder/Dockerfile @@ -10,6 +10,8 @@ ENV ORACLE_FEEDER_KEY_PATH=voter.json ENV ORACLE_FEEDER_COIN_TYPE=330 ENV ORACLE_FEEDER_ADDR_PREFIX=terra +ENV ORACLE_SDR_BASKET="{USD: 0.57813, EUR: 0.37379,JPY: 13.452, CNY: 1.0993, GBP: 0.080870}" + WORKDIR /app COPY package*.json ./ diff --git a/feeder/src/index.ts b/feeder/src/index.ts index 8a5e55f..bbfe7b2 100644 --- a/feeder/src/index.ts +++ b/feeder/src/index.ts @@ -64,6 +64,12 @@ function registerCommands(parser: ArgumentParser): void { defaultValue: `voter`, }) + voteCommand.addArgument(['-s', '--sdr-basket'], { + help: `SDR basket information`, + dest: `sdrBasket`, + defaultValue: '', + }) + // Updating Key command const keyCommand = subparsers.addParser(`add-key`, { addHelp: true }) @@ -123,6 +129,16 @@ async function main(): Promise { args.validators || (process.env.ORACLE_FEEDER_VALIDATORS && process.env.ORACLE_FEEDER_VALIDATORS.split(',')) args.keyName = process.env.ORACLE_FEEDER_KEY_NAME ? process.env.ORACLE_FEEDER_KEY_NAME : args.keyName + args.sdrBasket = args.sdrBasket || process.env.ORACLE_SDR_BASKET || '' + // convert basket data into json object + if (args.sdrBasket !== '') { + args.sdrBasket = args.sdrBasket.split(',').reduce((acc: string, curr: string) => { + const [key, value] = curr.split(':') + acc[key] = value + return acc + }, {}) + } + await vote(args) } else if (args.subparser_name === `add-key`) { await addKey(args.keyPath, args.coinType, args.keyName) diff --git a/feeder/src/vote.ts b/feeder/src/vote.ts index 0da576b..f70bb6e 100644 --- a/feeder/src/vote.ts +++ b/feeder/src/vote.ts @@ -74,7 +74,46 @@ interface Price { price: string } -async function getPrices(sources: string[]): Promise { +function calculateSDR(prices: Price[], sdrBasket: string): Price | undefined { + if (!sdrBasket) { + return undefined + } + + // check if all prices from the basket are available + for (const denom of Object.keys(sdrBasket)) { + if (!prices.find((p) => p.denom === denom)) { + logger.error(`getPrices: price for ${denom} not found`) + return undefined + } + } + + // calculate SDR price + let sdrPrice: BigNumber = undefined + + try { + sdrPrice = Object.entries(sdrBasket).reduce((acc, [denom, weight]) => { + const price = prices.find((p) => p.denom === denom) + if (!price) { + throw new Error(`price for ${denom} not found`) + } + return acc.plus(new BigNumber(price.price).times(weight)) + }, new BigNumber(0)) + } catch (err) { + logger.error(`getPrices: error calculating SDR price: ${err.message}`) + return undefined + } + + if (!sdrPrice) { + return undefined + } + + return { + denom: 'SDR', + price: sdrPrice.toString(), + } +} + +async function getPrices(sources: string[], sdrBasket: string): Promise { const results = await Bluebird.some( sources.map((s) => ax.get(s)), 1 @@ -99,6 +138,11 @@ async function getPrices(sources: string[]): Promise { return [] } + const sdr = calculateSDR(results[0].data.prices, sdrBasket) + if (sdr) { + results[0].data.prices.push(sdr) + } + return results[0].data.prices } @@ -189,7 +233,7 @@ export async function processVote( // Print timestamp before start logger.info(`[VOTE] Requesting prices from price server ${args.dataSourceUrl.join(',')}`) - const _prices = await getPrices(args.dataSourceUrl) + const _prices = await getPrices(args.dataSourceUrl, args.sdrBasket) // Removes non-whitelisted currencies and abstain for not fetched currencies const prices = preparePrices(_prices, oracleWhitelist) @@ -302,6 +346,7 @@ interface VoteArgs { password: string keyPath: string keyName: string + sdrBasket: string } function buildLCDClientConfig(args: VoteArgs, lcdIndex: number): Record { From 2f915c100459c558b577adb44b549e189f83b87f Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 11:30:24 +0200 Subject: [PATCH 02/14] - added log output --- feeder/src/vote.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/feeder/src/vote.ts b/feeder/src/vote.ts index f70bb6e..d40cabd 100644 --- a/feeder/src/vote.ts +++ b/feeder/src/vote.ts @@ -139,6 +139,8 @@ async function getPrices(sources: string[], sdrBasket: string): Promise } const sdr = calculateSDR(results[0].data.prices, sdrBasket) + logger.info(`[VOTE] SDR price: ${sdr?.price}`) + if (sdr) { results[0].data.prices.push(sdr) } From 0f3333d2fa2217ec841ce589bf74f38904e616ea Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 11:35:36 +0200 Subject: [PATCH 03/14] - only calculate SDR if not provided from price server --- feeder/src/vote.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/feeder/src/vote.ts b/feeder/src/vote.ts index d40cabd..cb34996 100644 --- a/feeder/src/vote.ts +++ b/feeder/src/vote.ts @@ -138,6 +138,11 @@ async function getPrices(sources: string[], sdrBasket: string): Promise return [] } + if (results[0].data.prices.find((p) => p.denom === 'SDR')) { + logger.info(`[VOTE] SDR price found from price server`) + return results[0].data.prices + } + const sdr = calculateSDR(results[0].data.prices, sdrBasket) logger.info(`[VOTE] SDR price: ${sdr?.price}`) From 91afcdd9dc7ba3571205a054b67fcf3b0cc86992 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 11:40:43 +0200 Subject: [PATCH 04/14] - wrong type of variable --- feeder/src/vote.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feeder/src/vote.ts b/feeder/src/vote.ts index cb34996..a2b5389 100644 --- a/feeder/src/vote.ts +++ b/feeder/src/vote.ts @@ -88,7 +88,7 @@ function calculateSDR(prices: Price[], sdrBasket: string): Price | undefined { } // calculate SDR price - let sdrPrice: BigNumber = undefined + let sdrPrice: BigNumber | undefined = undefined try { sdrPrice = Object.entries(sdrBasket).reduce((acc, [denom, weight]) => { From 23419cbd5326ad0165fc730bc8dca97ef9d15e62 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 11:45:33 +0200 Subject: [PATCH 05/14] - fixed typo - add fixed price for USD --- feeder/Dockerfile | 2 +- feeder/src/vote.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/feeder/Dockerfile b/feeder/Dockerfile index e57111b..8e57d5b 100644 --- a/feeder/Dockerfile +++ b/feeder/Dockerfile @@ -10,7 +10,7 @@ ENV ORACLE_FEEDER_KEY_PATH=voter.json ENV ORACLE_FEEDER_COIN_TYPE=330 ENV ORACLE_FEEDER_ADDR_PREFIX=terra -ENV ORACLE_SDR_BASKET="{USD: 0.57813, EUR: 0.37379,JPY: 13.452, CNY: 1.0993, GBP: 0.080870}" +ENV ORACLE_SDR_BASKET=USD:0.57813,EUR:0.37379,JPY:13.452,CNY:1.0993,GBP:0.080870 WORKDIR /app diff --git a/feeder/src/vote.ts b/feeder/src/vote.ts index a2b5389..934c968 100644 --- a/feeder/src/vote.ts +++ b/feeder/src/vote.ts @@ -81,6 +81,9 @@ function calculateSDR(prices: Price[], sdrBasket: string): Price | undefined { // check if all prices from the basket are available for (const denom of Object.keys(sdrBasket)) { + if (denom === 'USD') { + continue + } if (!prices.find((p) => p.denom === denom)) { logger.error(`getPrices: price for ${denom} not found`) return undefined @@ -92,7 +95,7 @@ function calculateSDR(prices: Price[], sdrBasket: string): Price | undefined { try { sdrPrice = Object.entries(sdrBasket).reduce((acc, [denom, weight]) => { - const price = prices.find((p) => p.denom === denom) + const price = denom === 'USD' ? { price: '1.0' } : prices.find((p) => p.denom === denom) if (!price) { throw new Error(`price for ${denom} not found`) } From a9806525e4d82208f32e3920b0307c1829355227 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 13:25:50 +0200 Subject: [PATCH 06/14] - type fixed --- feeder/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feeder/src/index.ts b/feeder/src/index.ts index bbfe7b2..d3e854a 100644 --- a/feeder/src/index.ts +++ b/feeder/src/index.ts @@ -132,7 +132,7 @@ async function main(): Promise { args.sdrBasket = args.sdrBasket || process.env.ORACLE_SDR_BASKET || '' // convert basket data into json object if (args.sdrBasket !== '') { - args.sdrBasket = args.sdrBasket.split(',').reduce((acc: string, curr: string) => { + args.sdrBasket = args.sdrBasket.split(',').reduce((acc: { string: string }, curr: string) => { const [key, value] = curr.split(':') acc[key] = value return acc From 84e51929bd4969d9297f0fd3d4927919c8cf1f1f Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 14:09:28 +0200 Subject: [PATCH 07/14] - move logic from feeder to price server --- feeder/Dockerfile | 2 - feeder/src/index.ts | 16 ----- feeder/src/vote.ts | 59 +------------------ price-server/config/default-sample.js | 8 +++ price-server/config/docker.js | 8 +++ .../src/provider/fiat/FiatProvider.ts | 55 ++++++++++++++++- 6 files changed, 71 insertions(+), 77 deletions(-) diff --git a/feeder/Dockerfile b/feeder/Dockerfile index 8e57d5b..1b44251 100644 --- a/feeder/Dockerfile +++ b/feeder/Dockerfile @@ -10,8 +10,6 @@ ENV ORACLE_FEEDER_KEY_PATH=voter.json ENV ORACLE_FEEDER_COIN_TYPE=330 ENV ORACLE_FEEDER_ADDR_PREFIX=terra -ENV ORACLE_SDR_BASKET=USD:0.57813,EUR:0.37379,JPY:13.452,CNY:1.0993,GBP:0.080870 - WORKDIR /app COPY package*.json ./ diff --git a/feeder/src/index.ts b/feeder/src/index.ts index d3e854a..8a5e55f 100644 --- a/feeder/src/index.ts +++ b/feeder/src/index.ts @@ -64,12 +64,6 @@ function registerCommands(parser: ArgumentParser): void { defaultValue: `voter`, }) - voteCommand.addArgument(['-s', '--sdr-basket'], { - help: `SDR basket information`, - dest: `sdrBasket`, - defaultValue: '', - }) - // Updating Key command const keyCommand = subparsers.addParser(`add-key`, { addHelp: true }) @@ -129,16 +123,6 @@ async function main(): Promise { args.validators || (process.env.ORACLE_FEEDER_VALIDATORS && process.env.ORACLE_FEEDER_VALIDATORS.split(',')) args.keyName = process.env.ORACLE_FEEDER_KEY_NAME ? process.env.ORACLE_FEEDER_KEY_NAME : args.keyName - args.sdrBasket = args.sdrBasket || process.env.ORACLE_SDR_BASKET || '' - // convert basket data into json object - if (args.sdrBasket !== '') { - args.sdrBasket = args.sdrBasket.split(',').reduce((acc: { string: string }, curr: string) => { - const [key, value] = curr.split(':') - acc[key] = value - return acc - }, {}) - } - await vote(args) } else if (args.subparser_name === `add-key`) { await addKey(args.keyPath, args.coinType, args.keyName) diff --git a/feeder/src/vote.ts b/feeder/src/vote.ts index 934c968..0da576b 100644 --- a/feeder/src/vote.ts +++ b/feeder/src/vote.ts @@ -74,49 +74,7 @@ interface Price { price: string } -function calculateSDR(prices: Price[], sdrBasket: string): Price | undefined { - if (!sdrBasket) { - return undefined - } - - // check if all prices from the basket are available - for (const denom of Object.keys(sdrBasket)) { - if (denom === 'USD') { - continue - } - if (!prices.find((p) => p.denom === denom)) { - logger.error(`getPrices: price for ${denom} not found`) - return undefined - } - } - - // calculate SDR price - let sdrPrice: BigNumber | undefined = undefined - - try { - sdrPrice = Object.entries(sdrBasket).reduce((acc, [denom, weight]) => { - const price = denom === 'USD' ? { price: '1.0' } : prices.find((p) => p.denom === denom) - if (!price) { - throw new Error(`price for ${denom} not found`) - } - return acc.plus(new BigNumber(price.price).times(weight)) - }, new BigNumber(0)) - } catch (err) { - logger.error(`getPrices: error calculating SDR price: ${err.message}`) - return undefined - } - - if (!sdrPrice) { - return undefined - } - - return { - denom: 'SDR', - price: sdrPrice.toString(), - } -} - -async function getPrices(sources: string[], sdrBasket: string): Promise { +async function getPrices(sources: string[]): Promise { const results = await Bluebird.some( sources.map((s) => ax.get(s)), 1 @@ -141,18 +99,6 @@ async function getPrices(sources: string[], sdrBasket: string): Promise return [] } - if (results[0].data.prices.find((p) => p.denom === 'SDR')) { - logger.info(`[VOTE] SDR price found from price server`) - return results[0].data.prices - } - - const sdr = calculateSDR(results[0].data.prices, sdrBasket) - logger.info(`[VOTE] SDR price: ${sdr?.price}`) - - if (sdr) { - results[0].data.prices.push(sdr) - } - return results[0].data.prices } @@ -243,7 +189,7 @@ export async function processVote( // Print timestamp before start logger.info(`[VOTE] Requesting prices from price server ${args.dataSourceUrl.join(',')}`) - const _prices = await getPrices(args.dataSourceUrl, args.sdrBasket) + const _prices = await getPrices(args.dataSourceUrl) // Removes non-whitelisted currencies and abstain for not fetched currencies const prices = preparePrices(_prices, oracleWhitelist) @@ -356,7 +302,6 @@ interface VoteArgs { password: string keyPath: string keyName: string - sdrBasket: string } function buildLCDClientConfig(args: VoteArgs, lcdIndex: number): Record { diff --git a/price-server/config/default-sample.js b/price-server/config/default-sample.js index 1925c42..f786b68 100644 --- a/price-server/config/default-sample.js +++ b/price-server/config/default-sample.js @@ -578,4 +578,12 @@ module.exports = { apiKey: '', // necessary }, }, + sdrBasket: { + // to calculate SDR value if not available from fiat providers + USD: '0.57813', + EUR: '0.37379', + JPY: '13.452', + CNY: '1.0993', + GBP: '0.080870', + }, } diff --git a/price-server/config/docker.js b/price-server/config/docker.js index f284a48..8a2f7bb 100644 --- a/price-server/config/docker.js +++ b/price-server/config/docker.js @@ -57,4 +57,12 @@ module.exports = { apiKey: process.env.FIAT_PROVIDER_ALPHA_VANTAGE_API_KEY || '', // necessary }, }, + sdrBasket: { + // to calculate SDR value if not available from fiat providers + USD: '0.57813', + EUR: '0.37379', + JPY: '13.452', + CNY: '1.0993', + GBP: '0.080870', + }, } diff --git a/price-server/src/provider/fiat/FiatProvider.ts b/price-server/src/provider/fiat/FiatProvider.ts index 43e76ae..9c5a7e7 100644 --- a/price-server/src/provider/fiat/FiatProvider.ts +++ b/price-server/src/provider/fiat/FiatProvider.ts @@ -1,6 +1,8 @@ import * as config from 'config' -import { Provider, ProviderOptions } from 'provider/base' +import { PriceBySymbol, Provider, ProviderOptions } from 'provider/base' +import * as logger from 'lib/logger' import { CurrencyLayer, AlphaVantage, Fixer, ExchangeRate, Fer, Frankfurter, Fastforex } from './quoter' +import BigNumber from 'bignumber.js' class FiatProvider extends Provider { constructor(options: ProviderOptions) { @@ -28,7 +30,56 @@ class FiatProvider extends Provider { await super.initialize() - await this.tick(Date.now()) + const isUpdated = await this.tick(Date.now()) + + if (isUpdated && !this.priceBySymbol['SDR']) { + logger.info(`No SDR price found, falling back to calculation.`) + const sdrPrice = this.calculateSDR(this.priceBySymbol) + if (sdrPrice && sdrPrice.isNaN() === false) { + this.priceBySymbol['SDR'] = sdrPrice + } else { + logger.error(`No SDR price found, calculation failed.`) + } + } + } + + protected calculateSDR(prices: PriceBySymbol): BigNumber | undefined { + if (!config.sdrBasket) { + return undefined + } + + // check if all prices from the basket are available + for (const denom of Object.keys(config.sdrBasket)) { + if (denom === 'USD') { + continue + } + if (!prices[denom]) { + logger.error(`calculateSDR price for ${denom} not found`) + return undefined + } + } + + // calculate SDR price + let sdrPrice: BigNumber | undefined = undefined + + try { + sdrPrice = Object.entries(config.sdrBasket).reduce((acc, [denom, weight]: [string, string]) => { + const price = denom === 'USD' ? BigNumber(1) : prices[denom] || BigNumber(0) + if (!price) { + throw new Error(`price for ${denom} not found`) + } + return acc.plus(new BigNumber(price).times(weight)) + }, new BigNumber(0)) + } catch (err) { + logger.error(`getPrices: error calculating SDR price: ${err.message}`) + return undefined + } + + if (!sdrPrice) { + return undefined + } + + return sdrPrice } protected adjustPrices(): void { From dea01ce4bb18d28dd8491702ca960ab27ed40aec Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 14:15:19 +0200 Subject: [PATCH 08/14] - move to adjustPrices --- .../src/provider/fiat/FiatProvider.ts | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/price-server/src/provider/fiat/FiatProvider.ts b/price-server/src/provider/fiat/FiatProvider.ts index 9c5a7e7..d81e729 100644 --- a/price-server/src/provider/fiat/FiatProvider.ts +++ b/price-server/src/provider/fiat/FiatProvider.ts @@ -30,21 +30,12 @@ class FiatProvider extends Provider { await super.initialize() - const isUpdated = await this.tick(Date.now()) - - if (isUpdated && !this.priceBySymbol['SDR']) { - logger.info(`No SDR price found, falling back to calculation.`) - const sdrPrice = this.calculateSDR(this.priceBySymbol) - if (sdrPrice && sdrPrice.isNaN() === false) { - this.priceBySymbol['SDR'] = sdrPrice - } else { - logger.error(`No SDR price found, calculation failed.`) - } - } + await this.tick(Date.now()) } protected calculateSDR(prices: PriceBySymbol): BigNumber | undefined { if (!config.sdrBasket) { + logger.error(`calculateSDR: config.sdrBasket not found`) return undefined } @@ -76,9 +67,11 @@ class FiatProvider extends Provider { } if (!sdrPrice) { + logger.error(`getPrices: error calculating SDR price`) return undefined } + logger.info(`getPrices: calculated SDR price: ${sdrPrice.toString()}`) return sdrPrice } @@ -96,6 +89,16 @@ class FiatProvider extends Provider { } } } + + if (!this.priceBySymbol['SDR']) { + logger.info(`No SDR price found, falling back to calculation.`) + const sdrPrice = this.calculateSDR(this.priceBySymbol) + if (sdrPrice && sdrPrice.isNaN() === false) { + this.priceBySymbol['SDR'] = sdrPrice + } else { + logger.error(`No SDR price found, calculation failed.`) + } + } } } From 3ed652b69ab31f890a536a0265aa2f3355c9b375 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 14:21:28 +0200 Subject: [PATCH 09/14] - fixed invalid structure of prices --- price-server/src/provider/fiat/FiatProvider.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/price-server/src/provider/fiat/FiatProvider.ts b/price-server/src/provider/fiat/FiatProvider.ts index d81e729..d526896 100644 --- a/price-server/src/provider/fiat/FiatProvider.ts +++ b/price-server/src/provider/fiat/FiatProvider.ts @@ -3,6 +3,7 @@ import { PriceBySymbol, Provider, ProviderOptions } from 'provider/base' import * as logger from 'lib/logger' import { CurrencyLayer, AlphaVantage, Fixer, ExchangeRate, Fer, Frankfurter, Fastforex } from './quoter' import BigNumber from 'bignumber.js' +import { getBaseCurrency } from 'lib/currency' class FiatProvider extends Provider { constructor(options: ProviderOptions) { @@ -39,12 +40,18 @@ class FiatProvider extends Provider { return undefined } + const priceList = Object.keys(prices).map((symbol) => ({ + denom: getBaseCurrency(symbol), + price: prices[symbol].toFixed(8), + })) + // check if all prices from the basket are available for (const denom of Object.keys(config.sdrBasket)) { if (denom === 'USD') { continue } - if (!prices[denom]) { + + if (!priceList.find((p) => p.denom === denom)) { logger.error(`calculateSDR price for ${denom} not found`) return undefined } @@ -55,7 +62,7 @@ class FiatProvider extends Provider { try { sdrPrice = Object.entries(config.sdrBasket).reduce((acc, [denom, weight]: [string, string]) => { - const price = denom === 'USD' ? BigNumber(1) : prices[denom] || BigNumber(0) + const price = denom === 'USD' ? BigNumber(1) : priceList.find((p) => p.denom === denom)?.price || BigNumber(0) if (!price) { throw new Error(`price for ${denom} not found`) } From c12a909c03f0089dc8ed6a16b805921c51a4084c Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 14:27:02 +0200 Subject: [PATCH 10/14] - allow sdr basket being passed by env --- price-server/config/docker.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/price-server/config/docker.js b/price-server/config/docker.js index 8a2f7bb..4d79786 100644 --- a/price-server/config/docker.js +++ b/price-server/config/docker.js @@ -57,12 +57,14 @@ module.exports = { apiKey: process.env.FIAT_PROVIDER_ALPHA_VANTAGE_API_KEY || '', // necessary }, }, - sdrBasket: { - // to calculate SDR value if not available from fiat providers - USD: '0.57813', - EUR: '0.37379', - JPY: '13.452', - CNY: '1.0993', - GBP: '0.080870', - }, + sdrBasket: process.env.SDR_BASKET + ? JSON.parse(process.env.SDR_BASKET) + : { + // to calculate SDR value if not available from fiat providers + USD: '0.57813', + EUR: '0.37379', + JPY: '13.452', + CNY: '1.0993', + GBP: '0.080870', + }, } From 17c352d34dcd6a7abec50b1efddec2e5010b1c11 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Fri, 29 Sep 2023 14:40:41 +0200 Subject: [PATCH 11/14] - make SDR a correct fiat pair --- price-server/src/provider/fiat/FiatProvider.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/price-server/src/provider/fiat/FiatProvider.ts b/price-server/src/provider/fiat/FiatProvider.ts index d526896..bf574d7 100644 --- a/price-server/src/provider/fiat/FiatProvider.ts +++ b/price-server/src/provider/fiat/FiatProvider.ts @@ -97,11 +97,11 @@ class FiatProvider extends Provider { } } - if (!this.priceBySymbol['SDR']) { + if (!this.priceBySymbol['SDR/USD']) { logger.info(`No SDR price found, falling back to calculation.`) const sdrPrice = this.calculateSDR(this.priceBySymbol) if (sdrPrice && sdrPrice.isNaN() === false) { - this.priceBySymbol['SDR'] = sdrPrice + this.priceBySymbol['SDR/USD'] = sdrPrice } else { logger.error(`No SDR price found, calculation failed.`) } From 29c463d264fcfa90a50f5745f2e5c73e3c9f85b2 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Tue, 17 Sep 2024 21:39:04 +0200 Subject: [PATCH 12/14] - rework parsing of website --- price-server/src/provider/fiat/quoter/IMF.ts | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/price-server/src/provider/fiat/quoter/IMF.ts b/price-server/src/provider/fiat/quoter/IMF.ts index 9b49d0f..43bce1a 100644 --- a/price-server/src/provider/fiat/quoter/IMF.ts +++ b/price-server/src/provider/fiat/quoter/IMF.ts @@ -9,16 +9,27 @@ const SDR_VALUATION_URL = 'https://www.imf.org/external/np/fin/data/rms_sdrv.asp async function fetchQuote() { const text = await fetch(SDR_VALUATION_URL).then((res) => res.text()) - const doc = parse(text) - const tds = doc.querySelectorAll('.tightest td') - const idx = tds.findIndex((el) => el.structuredText === ' SDR1 = US$') - - if (idx === -1) { - throw new Error('cannot find SDR/USD element from HTML document') + let idx = -1 + const tables = text.match(/]*>([\s\S]*?)<\/table>/gi) || [] + console.log('Tables:', tables.length) // Debugging line + for (const table of tables) { + const doc = parse(table.trim()) + const tds = doc.querySelectorAll('td') + + console.log('TDs:', 'cnt', tds.length) // Debugging line + idx = tds.findIndex((el) => { + console.log('ST', el.structuredText) + return el.structuredText.trim() === 'SDR1 = US$' + }) + + if (idx !== -1) { + // sample format: ' 1.32149 2' + return num(tds[idx + 1].structuredText.split(' ')[1]) + } } - // sample format: ' 1.32149 2' - return num(tds[idx + 1].structuredText.split(' ')[1]) + // nothing found + throw new Error('cannot find SDR/USD element from HTML document') } // fetchQuote().then(console.log).catch(console.error) // For test From 0f57053927d40da9c67fcb3c25d720d1f61438ec Mon Sep 17 00:00:00 2001 From: StrathCole Date: Tue, 17 Sep 2024 21:48:16 +0200 Subject: [PATCH 13/14] - remove logs --- price-server/src/provider/fiat/quoter/IMF.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/price-server/src/provider/fiat/quoter/IMF.ts b/price-server/src/provider/fiat/quoter/IMF.ts index 43bce1a..898995f 100644 --- a/price-server/src/provider/fiat/quoter/IMF.ts +++ b/price-server/src/provider/fiat/quoter/IMF.ts @@ -11,14 +11,11 @@ async function fetchQuote() { let idx = -1 const tables = text.match(/]*>([\s\S]*?)<\/table>/gi) || [] - console.log('Tables:', tables.length) // Debugging line for (const table of tables) { const doc = parse(table.trim()) const tds = doc.querySelectorAll('td') - console.log('TDs:', 'cnt', tds.length) // Debugging line idx = tds.findIndex((el) => { - console.log('ST', el.structuredText) return el.structuredText.trim() === 'SDR1 = US$' }) From 9200b5cf3b546656378ad1d21a01368669bba1d1 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Tue, 17 Sep 2024 21:55:22 +0200 Subject: [PATCH 14/14] - bump version --- feeder/package-lock.json | 4 ++-- feeder/package.json | 2 +- price-server/package-lock.json | 4 ++-- price-server/package.json | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/feeder/package-lock.json b/feeder/package-lock.json index 7a333ae..132b3ed 100644 --- a/feeder/package-lock.json +++ b/feeder/package-lock.json @@ -1,12 +1,12 @@ { "name": "@classic-terra/oracle-feeder", - "version": "3.1.4", + "version": "3.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@classic-terra/oracle-feeder", - "version": "3.1.4", + "version": "3.1.5", "license": "Apache-2.0", "dependencies": { "@terra-money/terra.js": "^3.1.7", diff --git a/feeder/package.json b/feeder/package.json index 9e46939..07a8137 100644 --- a/feeder/package.json +++ b/feeder/package.json @@ -1,6 +1,6 @@ { "name": "@classic-terra/oracle-feeder", - "version": "3.1.4", + "version": "3.1.5", "main": "src/index.ts", "license": "Apache-2.0", "scripts": { diff --git a/price-server/package-lock.json b/price-server/package-lock.json index 13d457a..d754468 100644 --- a/price-server/package-lock.json +++ b/price-server/package-lock.json @@ -1,12 +1,12 @@ { "name": "@classic-terra/price-server", - "version": "3.1.4", + "version": "3.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@classic-terra/price-server", - "version": "3.1.4", + "version": "3.1.5", "license": "Apache-2.0", "dependencies": { "@opentelemetry/api": "^1.0.2", diff --git a/price-server/package.json b/price-server/package.json index 7053989..26c7a6a 100644 --- a/price-server/package.json +++ b/price-server/package.json @@ -1,6 +1,6 @@ { "name": "@classic-terra/price-server", - "version": "3.1.4", + "version": "3.1.5", "main": "src/main.ts", "license": "Apache-2.0", "scripts": {