Skip to content

Commit

Permalink
Update ExchangeRate, Adjust default Configuration File (particularly …
Browse files Browse the repository at this point in the history
…default-test.js)
  • Loading branch information
aeuser999 committed Sep 29, 2023
1 parent 55a2b64 commit 141872a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
1 change: 1 addition & 0 deletions price-server/config/default-sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ module.exports = {
symbols: fiatSymbols,
interval: 30 * 1000,
timeout: 5000,
apiKey: '', // optional
},
// https://fer.ee/
fer: {
Expand Down
1 change: 1 addition & 0 deletions price-server/config/default-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ module.exports = {
symbols: fiatSymbols,
interval: 30 * 1000,
timeout: 5000,
apiKey: '', // optional
},
// https://fer.ee/
fer: {
Expand Down
72 changes: 57 additions & 15 deletions price-server/src/provider/fiat/quoter/ExchangeRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import { num } from 'lib/num'
import * as logger from 'lib/logger'
import { Quoter } from 'provider/base'

interface ResponseOpen {
result: string
rates: { [symbol: string]: number }
'error-type'?: Record<string, unknown> | string
}

interface Response {
success: boolean
rates: { [symbol: string]: number }
error?: Record<string, unknown> | string
result: boolean
conversion_rates: { [symbol: string]: number }
'error-type'?: Record<string, unknown> | string
}

/**
Expand All @@ -27,25 +33,61 @@ interface Response {
export class ExchangeRate extends Quoter {
private async updatePrices(): Promise<void> {
const params = {
api_key: this.options.apiKey,
base: 'USD',
symbols: this.symbols.map((symbol) => (symbol === 'SDR/USD' ? 'XDR' : symbol.replace('/USD', ''))).join(','),
}

const response: Response = await nodeFetch(`https://api.exchangerate.host/latest?${toQueryString(params)}`, {
timeout: this.options.timeout,
}).then((res) => res.json())
if (!response || !response.success || !response.rates) {
logger.error(`${this.constructor.name}: wrong api response`, response ? JSON.stringify(response) : 'empty')
throw new Error('Invalid response from ExchangeRate')
}
if (typeof params.api_key === 'undefined' || params.api_key.length == 0) {
const responseOpen: ResponseOpen = await nodeFetch(`https://open.er-api.com/v6/latest/${params.base}`, {
timeout: this.options.timeout,
}).then((res) => res.json())

if (!responseOpen || !responseOpen.result || !responseOpen.rates) {
logger.error(`${this.constructor.name}: wrong api response`, responseOpen ? JSON.stringify(response) : 'empty')
throw new Error('Invalid response from ExchangeRate')
}

// Update the latest trades (Open Version)
for (const symbol of Object.keys(responseOpen.rates)) {
const convertedSymbol = (symbol == 'XDR') ? 'SDR' + '/' + params.base : symbol + '/' + params.base

if (this.symbols.includes(convertedSymbol)) {
const convertedPrice = num(1).dividedBy(num(responseOpen.rates[symbol]))

console.log ('ExchangeRate - Open: ' + convertedSymbol + ' - ' + convertedPrice)

// Update the latest trades
for (const symbol of Object.keys(response.rates)) {
const convertedSymbol = symbol + '/USD'
const convertedPrice = num(1).dividedBy(num(response.rates[symbol]))
this.setPrice(convertedSymbol, convertedPrice)
}
}

this.setPrice(convertedSymbol === 'XDR/USD' ? 'SDR/USD' : convertedSymbol, convertedPrice)
} else {
const response: Response = await nodeFetch(`https://v6.exchangerate-api.com/v6/${params.api_key}/latest/${params.base}`, {
timeout: this.options.timeout,
}).then((res) => res.json())

if (!response || !response.result || !response.conversion_rates) {
logger.error(`${this.constructor.name}: wrong api response`, response ? JSON.stringify(response) : 'empty')
throw new Error('Invalid response from ExchangeRate')
}

// Update the latest trades (API Version)
for (const symbol of Object.keys(response.conversion_rates)) {
const convertedSymbol = (symbol == 'XDR') ? 'SDR' + '/' + params.base : symbol + '/' + params.base

if (this.symbols.includes(convertedSymbol)) {
const convertedPrice = num(1).dividedBy(num(response.conversion_rates[symbol]))

console.log ('ExchangeRate - API: ' + convertedSymbol + ' - ' + convertedPrice)

this.setPrice(convertedSymbol, convertedPrice)
}
}
}




}

protected async update(): Promise<boolean> {
Expand Down

1 comment on commit 141872a

@StrathCole
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeuser999 could you provide that addition as a MR against the main oracle feeder at https://github.com/classic-terra/oracle-feeder ?

Please sign in to comment.