From e36d977e4072601254595bdbefa8f5f7c262d7bb Mon Sep 17 00:00:00 2001 From: De Facto Date: Tue, 16 Mar 2021 16:16:56 +0800 Subject: [PATCH] use aggregated cex median in PriceFeeder --- src/PriceFeeder.ts | 25 ++++++++++++++++++++++--- src/feeds.ts | 9 ++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/PriceFeeder.ts b/src/PriceFeeder.ts index cba7afc..2a9beed 100644 --- a/src/PriceFeeder.ts +++ b/src/PriceFeeder.ts @@ -2,7 +2,14 @@ import fs from "fs" import { Wallet } from "solray" import { AggregatorDeployFile } from "./Deployer" import { loadJSONFile } from "./json" -import { coinbase } from "./feeds" +import { + AggregatedFeed, + BitStamp, + CoinBase, + coinbase, + FTX, + PriceFeed, +} from "./feeds" import { Submitter, SubmitterConfig } from "./Submitter" import { log } from "./log" import { conn } from "./context" @@ -10,12 +17,21 @@ import { conn } from "./context" // Look at all the available aggregators and submit to those that the wallet can // act as an oracle. export class PriceFeeder { + private feeds: PriceFeed[] + constructor( private deployInfo: AggregatorDeployFile, private wallet: Wallet - ) {} + ) { + this.feeds = [new CoinBase(), new BitStamp(), new FTX()] + } async start() { + // connect to the price feeds + for (const feed of this.feeds) { + feed.connect() + } + // find aggregators that this wallet can act as oracle this.startAccessibleAggregators() } @@ -40,7 +56,10 @@ export class PriceFeeder { continue } - const priceFeed = coinbase(name) + const feed = new AggregatedFeed(this.feeds, name) + const priceFeed = feed.medians() + // const priceFeed = coinbase(name) + const submitter = new Submitter( this.deployInfo.programID, aggregatorInfo.pubkey, diff --git a/src/feeds.ts b/src/feeds.ts index 1e415c4..425d798 100644 --- a/src/feeds.ts +++ b/src/feeds.ts @@ -296,13 +296,16 @@ export class AggregatedFeed { this.emitter.emit(UPDATE, this) } - async* medians() { + async *medians() { for await (let _ of this.updates()) { - yield this.median + const price = this.median + if (price) { + yield price + } } } - async* updates() { + async *updates() { for await (let _ of eventsIter(this.emitter, "UPDATE")) { yield this }