fixed proper decimal submissions in feeds

This commit is contained in:
dd 2021-06-05 12:11:13 -04:00
parent 208759f577
commit b1318349e6
2 changed files with 33 additions and 30 deletions

View File

@ -44,13 +44,13 @@ const priceFeedMapping = {
pairNames: ['ETH-USD', 'live_trades_ethusd', 'ETH/USD', 'ETHBUSD', 'ETH-USDC'] pairNames: ['ETH-USD', 'live_trades_ethusd', 'ETH/USD', 'ETHBUSD', 'ETH-USDC']
}, },
'sol:usd': { 'sol:usd': {
minValueChangeForNewRound: 4, minValueChangeForNewRound: 400,
useFeeds: [2,3,4], useFeeds: [2,3,4],
pairNames: ['SOL/USD', 'SOLBUSD', 'SOL-USDT'] pairNames: ['SOL/USD', 'SOLBUSD', 'SOL-USDT']
// uses USDT for OKEx // uses USDT for OKEx
}, },
'srm:usd': { 'srm:usd': {
minValueChangeForNewRound: 1, minValueChangeForNewRound: 50,
useFeeds: [2,3,4], useFeeds: [2,3,4],
pairNames: ['SRM/USD', 'SRMBUSD', 'SRM-USDT'] pairNames: ['SRM/USD', 'SRMBUSD', 'SRM-USDT']
// uses USDT for OKEx // uses USDT for OKEx

View File

@ -25,7 +25,6 @@ export interface IPriceFeed {
export abstract class PriceFeed { export abstract class PriceFeed {
public emitter = new EventEmitter() public emitter = new EventEmitter()
protected conn!: ReconnectingWebSocket protected conn!: ReconnectingWebSocket
protected connected!: Promise<void> protected connected!: Promise<void>
@ -91,6 +90,10 @@ export abstract class PriceFeed {
this.emitter.emit(UPDATE, price) this.emitter.emit(UPDATE, price)
} }
/**
* Every implementation must return IPrice with the correct amount of decimal transformation done on the incoming price
* @param data
*/
abstract parseMessage(data: any): IPrice | undefined abstract parseMessage(data: any): IPrice | undefined
// abstract parseMessage(pair: string) // abstract parseMessage(pair: string)
// abstract parseMessage(pair: string) // abstract parseMessage(pair: string)
@ -131,8 +134,8 @@ export class CoinBase extends PriceFeed {
const price: IPrice = { const price: IPrice = {
source: CoinBase.name, source: CoinBase.name,
pair, pair,
decimals: 2, decimals: 0,
value: Math.floor(payload.price * 100), value: payload.price,
} }
return price return price
@ -182,8 +185,8 @@ export class BitStamp extends PriceFeed {
const price: IPrice = { const price: IPrice = {
source: BitStamp.name, source: BitStamp.name,
pair, pair,
decimals: 2, decimals: 0,
value: Math.floor(payload.data.price * 100), value: payload.data.price,
} }
return price return price
@ -231,8 +234,8 @@ export class FTX extends PriceFeed {
const price: IPrice = { const price: IPrice = {
source: FTX.name, source: FTX.name,
pair, pair,
decimals: 2, decimals: 0,
value: Math.floor(payload.data.last * 100), value: payload.data.last,
} }
return price return price
@ -276,12 +279,11 @@ export class Binance extends PriceFeed {
const pair = payload.s; const pair = payload.s;
const price: IPrice = { const price: IPrice = {
source: Binance.name, source: Binance.name,
pair, pair,
decimals: 2, decimals: 0,
value: Math.floor(payload.p * 100), value: payload.p,
} }
return price return price
@ -341,8 +343,8 @@ export class OKEx extends PriceFeed {
const price: IPrice = { const price: IPrice = {
source: OKEx.name, source: OKEx.name,
pair, pair,
decimals: 2, decimals: 0,
value: Math.floor(payload.data[0].last * 100), value: payload.data[0].last,
} }
return price return price
@ -378,20 +380,21 @@ export class AggregatedFeed {
let j = 0 let j = 0
for (let i = 0; i < this.feeds.length; i++) { for (let i = 0; i < this.feeds.length; i++) {
const feed = this.feeds[i]; const feed = this.feeds[i];
feed.subscribe(pairMappings[i]); feed.subscribe(pairMappings[i]);
const index = j; const index = j;
j++; j++;
// store the price updates in the ith position of `this.prices` // store the price updates in the ith position of `this.prices`
feed.emitter.on(UPDATE, (price: IPrice) => { feed.emitter.on(UPDATE, (price: IPrice) => {
if (price.pair != pairMappings[i]) { if (price.pair != pairMappings[i]) {
return return
} }
price.timestamp = Date.now() price.timestamp = Date.now()
price.decimals = decimals; price.value = Math.floor(Math.pow(10, decimals - price.decimals) * price.value)
this.prices[index] = price price.decimals = decimals;
this.onPriceUpdate(price) this.prices[index] = price
}) this.onPriceUpdate(price)
})
} }
} }
@ -421,8 +424,8 @@ export class AggregatedFeed {
// filter out prices that are older than 10 seconds // filter out prices that are older than 10 seconds
recentPrices() : IPrice[] { recentPrices() : IPrice[] {
return this.prices.filter((p) => p && return this.prices.filter((p) => p &&
p.timestamp && p.timestamp &&
(p.timestamp - Date.now()) < 10*SECONDS) (p.timestamp - Date.now()) < 10*SECONDS)
} }
get median(): IPrice | undefined { get median(): IPrice | undefined {