Skip to content

Commit

Permalink
fix: bring tickers to this branch
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed May 7, 2024
1 parent cee27d7 commit 7e6cfc6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
30 changes: 19 additions & 11 deletions src/services/OracleServiceV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface PriceFeedEntry {
trusted?: boolean;
}

interface RedstoneFeed {
export interface RedstoneFeed {
token: string;
dataFeedId: string;
reserve: boolean;
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class OracleServiceV3 {
addressProvider: AddressProviderService;

@Inject(PROVIDER)
providerr: Provider;
provider: Provider;

@Inject(CONFIG)
config: ConfigSchema;
Expand All @@ -75,7 +75,7 @@ export default class OracleServiceV3 {
public async launch(block: number): Promise<void> {
this.#lastBlock = ORACLE_START_BLOCK[this.addressProvider.network];
const oracle = await this.addressProvider.findService("PRICE_ORACLE", 300);
this.#oracle = IPriceOracleV3__factory.connect(oracle, this.providerr);
this.#oracle = IPriceOracleV3__factory.connect(oracle, this.provider);
this.log.debug(`starting oracle v3 at ${block}`);
await this.#updateFeeds(block);
this.log.info(`started with ${Object.keys(this.#feeds).length} tokens`);
Expand Down Expand Up @@ -118,7 +118,7 @@ export default class OracleServiceV3 {
}
}
this.log.debug(`need to peform convert on ${calls.length} feeds`);
const resp = await safeMulticall<bigint>(calls, this.providerr);
const resp = await safeMulticall<bigint>(calls, this.provider);

for (let i = 0; i < resp.length; i++) {
const { value, error } = resp[i];
Expand Down Expand Up @@ -159,16 +159,24 @@ export default class OracleServiceV3 {

/**
* Returns currenly used redstone feeds
* For single token, it can include multiple feeds (main and/or reserve)
*/
public getRedstoneFeeds(activeOnly: boolean): RedstoneFeed[] {
const result: RedstoneFeed[] = [];
for (const [token, entry] of Object.entries(this.#feeds)) {
public getRedstoneFeeds(activeOnly: boolean): Record<string, RedstoneFeed[]> {
const result: Record<string, RedstoneFeed[]> = {};
for (const [t, entry] of Object.entries(this.#feeds)) {
const token = t.toLowerCase();
const { active, main, reserve } = entry;
if (main.dataFeedId && (!activeOnly || active === "main")) {
result.push({ token, dataFeedId: main.dataFeedId, reserve: false });
result[token] = [
...result[token],
{ token, dataFeedId: main.dataFeedId, reserve: false },
];
}
if (reserve?.dataFeedId && (!activeOnly || active === "reserve")) {
result.push({ token, dataFeedId: reserve.dataFeedId, reserve: true });
result[token] = [
...result[token],
{ token, dataFeedId: reserve.dataFeedId, reserve: true },
];
}
}
return result;
Expand All @@ -179,7 +187,7 @@ export default class OracleServiceV3 {
return;
}
this.log.debug(`updating price feeds in [${this.#lastBlock}, ${toBlock}]`);
const logs = await this.providerr.getLogs({
const logs = await this.provider.getLogs({
address: this.oracle.getAddress(),
fromBlock: this.#lastBlock,
toBlock,
Expand Down Expand Up @@ -245,7 +253,7 @@ export default class OracleServiceV3 {
}
}
this.log.debug(`need to get redstone data ids on ${calls.length} feeds`);
const resp = await safeMulticall(calls, this.providerr);
const resp = await safeMulticall(calls, this.provider);
for (let i = 0; i < resp.length; i++) {
let dataFeedId = resp[i].value || null;
let feedAddress = calls[i].address;
Expand Down
37 changes: 31 additions & 6 deletions src/services/RedstoneServiceV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
type PriceFeedData,
type PriceFeedType,
REDSTONE_SIGNERS,
tickerInfoTokensByNetwork,
tokenSymbolByAddress,
} from "@gearbox-protocol/sdk-gov";
import type { MultiCall } from "@gearbox-protocol/types/v3";
import { ICreditFacadeV3Multicall__factory } from "@gearbox-protocol/types/v3";
Expand All @@ -14,7 +16,9 @@ import { CONFIG, type ConfigSchema } from "../config";
import { Logger, type LoggerInterface } from "../log";
import { PROVIDER } from "../utils";
import type { CreditAccountData } from "../utils/ethers-6-temp";
import { AddressProviderService } from "./AddressProviderService";
import type { PriceOnDemandExtras, PriceUpdate } from "./liquidate/types";
import type { RedstoneFeed } from "./OracleServiceV3";
import OracleServiceV3 from "./OracleServiceV3";

const cfMulticall = ICreditFacadeV3Multicall__factory.createInterface();
Expand All @@ -35,6 +39,9 @@ export class RedstoneServiceV3 {
@Inject()
oracle: OracleServiceV3;

@Inject()
addressProvider: AddressProviderService;

@Inject(PROVIDER)
provider: Provider;

Expand All @@ -46,16 +53,34 @@ export class RedstoneServiceV3 {
tokens: string[],
activeOnly: boolean,
): Promise<PriceOnDemandExtras[]> {
const tokenz = tokens.map(t => t.toLowerCase());
const redstoneFeeds = this.oracle
.getRedstoneFeeds(activeOnly)
.filter(f => tokenz.includes(f.token));
const redstoneFeeds = this.oracle.getRedstoneFeeds(activeOnly);
const tickers = tickerInfoTokensByNetwork[this.addressProvider.network];

const redstoneUpdates: RedstoneFeed[] = [];
for (const t of tokens) {
const token = t.toLowerCase();
const feeds = redstoneFeeds[token];
if (feeds?.length) {
redstoneUpdates.push(...feeds);
continue;
}
const symb = tokenSymbolByAddress[token];
const ticker = tickers[symb];
if (ticker) {
this.log.debug(`found ticker ${ticker.symbol} for ${symb}`);
redstoneUpdates.push({
dataFeedId: ticker.dataId,
token: ticker.address,
reserve: false, // TODO: check this
});
}
}

this.log?.debug(
`need to update ${redstoneFeeds.length} redstone feeds: ${redstoneFeeds.map(({ dataFeedId }) => dataFeedId).join(", ")}`,
`need to update ${redstoneUpdates.length} redstone feeds: ${redstoneUpdates.map(({ dataFeedId }) => dataFeedId).join(", ")}`,
);
const result = await Promise.all(
redstoneFeeds.map(({ token, dataFeedId, reserve }) =>
redstoneUpdates.map(({ token, dataFeedId, reserve }) =>
this.#getRedstonePayloadForManualUsage(
token,
reserve,
Expand Down

0 comments on commit 7e6cfc6

Please sign in to comment.