Skip to content

Commit

Permalink
Example provider: fix error handling on init
Browse files Browse the repository at this point in the history
  • Loading branch information
adg-flare committed Apr 3, 2024
1 parent 3de282a commit b8ad155
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
21 changes: 14 additions & 7 deletions apps/example_provider/src/data-feeds/ccxt-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { FeedId, FeedValueData } from "../dto/provider-requests.dto";
import { BaseDataFeed } from "./base-feed";
import { FeedCategory } from "../../../../libs/ftso-core/src/voting-types";

export const CCXT_FALLBACK_VALUE = 0.01;
const CONFIG_PREFIX = "apps/example_provider/src/config/";
const RETRY_BACKOFF_MS = 10_000;

Expand Down Expand Up @@ -49,19 +48,29 @@ export class CcxtFeed implements BaseDataFeed {
}
}

this.logger.log(`Connecting to exchanges: ${JSON.stringify(Array.from(exchangeToSymbols.keys()))}`);
const loadExchanges = [];
for (const exchangeName of exchangeToSymbols.keys()) {
try {
const exchange: Exchange = new ccxt.pro[exchangeName]({ newUpdates: true });
this.exchangeByName.set(exchangeName, exchange);
loadExchanges.push(retry(async () => exchange.loadMarkets(), 3, RETRY_BACKOFF_MS, this.logger));
loadExchanges.push([exchangeName, retry(async () => exchange.loadMarkets(), 2, RETRY_BACKOFF_MS, this.logger)]);
} catch (e) {
this.logger.warn(`Failed to load markets for ${exchangeName}: ${e}`);
this.logger.warn(`Failed to initialize exchange ${exchangeName}, ignoring: ${e}`);
}
}

for (const [exchangeName, loadExchange] of loadExchanges) {
try {
await loadExchange;
this.logger.log(`Exchange ${exchangeName} initialized`);
} catch (e) {
this.logger.warn(`Failed to load markets for ${exchangeName}, ignoring: ${e}`);
}
}
await Promise.all(loadExchanges);
this.initialized = true;

this.logger.log(`Initialization done, watching trades...`);
void this.watchTrades(exchangeToSymbols);
}

Expand Down Expand Up @@ -199,9 +208,7 @@ export class CcxtFeed implements BaseDataFeed {
throw new Error("Must provide USDT feed sources, as it is used for USD conversion.");
}

config.forEach(feed => {
console.log(feed.feed);
});
this.logger.log(`Supported feeds: ${JSON.stringify(config.map(f => f.feed))}`);

return config;
} catch (err) {
Expand Down
5 changes: 3 additions & 2 deletions test/apps/integration/example_provider.controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from "chai";
import { ExampleProviderService } from "../../../apps/example_provider/src/example-provider-service";
import { ExampleProviderController } from "../../../apps/example_provider/src/example-provider.controller";
import { CcxtFeed, CCXT_FALLBACK_VALUE } from "../../../apps/example_provider/src/data-feeds/ccxt-provider-service";
import { CcxtFeed } from "../../../apps/example_provider/src/data-feeds/ccxt-provider-service";
import { RandomFeed } from "../../../apps/example_provider/src/data-feeds/random-feed";
import { sleepFor } from "../../../libs/ftso-core/src/utils/retry";
import { FeedId } from "../../../apps/example_provider/src/dto/provider-requests.dto";

export const DEFAULT_VALUE = 0.01;
const BTC_USD: FeedId = { category: 1, name: "4254430000000000" };

describe("ExampleProviderController Random", () => {
Expand Down Expand Up @@ -51,7 +52,7 @@ describe.skip("ExampleProviderController CCXT", () => {
expect(feedRes.votingRoundId).to.be.equal(123);
expect(feedRes.data.value).to.be.greaterThan(0);
expect(feedRes.data.feed).to.be.equal(BTC_USD);
expect(feedRes.data.value).not.to.be.equal(CCXT_FALLBACK_VALUE);
expect(feedRes.data.value).not.to.be.equal(DEFAULT_VALUE);
});

it.skip("should return all coston prices", async () => {
Expand Down

0 comments on commit b8ad155

Please sign in to comment.