remove dependency from price config (#638)

This commit is contained in:
Dev Kalra 2023-02-28 15:44:57 +05:30 committed by GitHub
parent 6bd4e2d3b8
commit 89139850b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 25 deletions

View File

@ -1,6 +1,10 @@
import { Contract, EventData } from "web3-eth-contract"; import { Contract, EventData } from "web3-eth-contract";
import { PriceConfig } from "./price-config"; import {
import { ChainPricePusher, PriceInfo, ChainPriceListener } from "./interface"; ChainPricePusher,
PriceInfo,
ChainPriceListener,
PriceItem,
} from "./interface";
import { TransactionReceipt } from "ethereum-protocol"; import { TransactionReceipt } from "ethereum-protocol";
import { addLeading0x, DurationInSeconds, removeLeading0x } from "./utils"; import { addLeading0x, DurationInSeconds, removeLeading0x } from "./utils";
import AbstractPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/AbstractPyth.json"; import AbstractPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/AbstractPyth.json";
@ -17,23 +21,15 @@ import {
export class EvmPriceListener extends ChainPriceListener { export class EvmPriceListener extends ChainPriceListener {
private pythContractFactory: PythContractFactory; private pythContractFactory: PythContractFactory;
private pythContract: Contract; private pythContract: Contract;
private priceIdToAlias: Map<HexString, string>;
constructor( constructor(
pythContractFactory: PythContractFactory, pythContractFactory: PythContractFactory,
priceConfigs: PriceConfig[], priceItems: PriceItem[],
config: { config: {
pollingFrequency: DurationInSeconds; pollingFrequency: DurationInSeconds;
} }
) { ) {
super( super("Evm", config.pollingFrequency, priceItems);
"Evm",
config.pollingFrequency,
priceConfigs.map((priceConfig) => priceConfig.id)
);
this.priceIdToAlias = new Map(
priceConfigs.map((priceConfig) => [priceConfig.id, priceConfig.alias])
);
this.pythContractFactory = pythContractFactory; this.pythContractFactory = pythContractFactory;
this.pythContract = this.pythContractFactory.createPythContract(); this.pythContract = this.pythContractFactory.createPythContract();
@ -57,7 +53,7 @@ export class EvmPriceListener extends ChainPriceListener {
} }
private async startSubscription() { private async startSubscription() {
for (const priceId of this.priceIds) { for (const { id: priceId } of this.priceItems) {
this.pythContract.events.PriceFeedUpdate( this.pythContract.events.PriceFeedUpdate(
{ {
filter: { filter: {
@ -106,6 +102,12 @@ export class EvmPriceListener extends ChainPriceListener {
return undefined; return undefined;
} }
console.log(
`Polled an EVM on chain price for feed ${this.priceIdToAlias.get(
priceId
)} (${priceId}).`
);
return { return {
conf: priceRaw.conf, conf: priceRaw.conf,
price: priceRaw.price, price: priceRaw.price,

View File

@ -77,6 +77,7 @@ const argv = yargs(hideBin(process.argv))
.parseSync(); .parseSync();
const priceConfigs = readPriceConfigFile(argv.priceConfigFile); const priceConfigs = readPriceConfigFile(argv.priceConfigFile);
const priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias }));
// TODO: name ChainPricePusher -> IPricePusher in a clean up PR // TODO: name ChainPricePusher -> IPricePusher in a clean up PR
// TODO: update listeners to not depend on the whole priceConfig // TODO: update listeners to not depend on the whole priceConfig
@ -120,7 +121,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
argv.pythContract argv.pythContract
); );
return new EvmPriceListener(pythContractFactory, priceConfigs, { return new EvmPriceListener(pythContractFactory, priceItems, {
pollingFrequency: argv.pollingFrequency, pollingFrequency: argv.pollingFrequency,
}); });
} }
@ -129,7 +130,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
return new InjectivePriceListener( return new InjectivePriceListener(
argv.pythContract, argv.pythContract,
argv.endpoint, argv.endpoint,
priceConfigs, priceItems,
{ pollingFrequency: argv.pollingFrequency } { pollingFrequency: argv.pollingFrequency }
); );
default: default:

View File

@ -1,7 +1,11 @@
import { HexString, PriceServiceConnection } from "@pythnetwork/pyth-common-js"; import { HexString, PriceServiceConnection } from "@pythnetwork/pyth-common-js";
import { ChainPricePusher, PriceInfo, ChainPriceListener } from "./interface"; import {
ChainPricePusher,
PriceInfo,
ChainPriceListener,
PriceItem,
} from "./interface";
import { DurationInSeconds } from "./utils"; import { DurationInSeconds } from "./utils";
import { PriceConfig } from "./price-config";
import { import {
ChainGrpcAuthApi, ChainGrpcAuthApi,
ChainGrpcWasmApi, ChainGrpcWasmApi,
@ -36,16 +40,12 @@ export class InjectivePriceListener extends ChainPriceListener {
constructor( constructor(
private contractAddress: string, private contractAddress: string,
private grpcEndpoint: string, private grpcEndpoint: string,
priceConfigs: PriceConfig[], priceItems: PriceItem[],
config: { config: {
pollingFrequency: DurationInSeconds; pollingFrequency: DurationInSeconds;
} }
) { ) {
super( super("Injective", config.pollingFrequency, priceItems);
"Injective",
config.pollingFrequency,
priceConfigs.map((priceConfig) => priceConfig.id)
);
} }
async getOnChainPriceInfo( async getOnChainPriceInfo(
@ -67,6 +67,12 @@ export class InjectivePriceListener extends ChainPriceListener {
return undefined; return undefined;
} }
console.log(
`Polled an Injective on chain price for feed ${this.priceIdToAlias.get(
priceId
)} (${priceId}).`
);
return { return {
conf: priceQueryResponse.price_feed.price.conf, conf: priceQueryResponse.price_feed.price.conf,
price: priceQueryResponse.price_feed.price.price, price: priceQueryResponse.price_feed.price.price,

View File

@ -1,6 +1,11 @@
import { HexString, UnixTimestamp } from "@pythnetwork/pyth-common-js"; import { HexString, UnixTimestamp } from "@pythnetwork/pyth-common-js";
import { DurationInSeconds } from "./utils"; import { DurationInSeconds } from "./utils";
export type PriceItem = {
id: HexString;
alias: string;
};
export type PriceInfo = { export type PriceInfo = {
price: string; price: string;
conf: string; conf: string;
@ -15,13 +20,17 @@ export interface IPriceListener {
export abstract class ChainPriceListener implements IPriceListener { export abstract class ChainPriceListener implements IPriceListener {
private latestPriceInfo: Map<HexString, PriceInfo>; private latestPriceInfo: Map<HexString, PriceInfo>;
protected priceIdToAlias: Map<HexString, string>;
constructor( constructor(
private chain: string, private chain: string,
private pollingFrequency: DurationInSeconds, private pollingFrequency: DurationInSeconds,
protected priceIds: HexString[] protected priceItems: PriceItem[]
) { ) {
this.latestPriceInfo = new Map(); this.latestPriceInfo = new Map();
this.priceIdToAlias = new Map(
priceItems.map(({ id, alias }) => [id, alias])
);
} }
async start() { async start() {
@ -33,7 +42,7 @@ export abstract class ChainPriceListener implements IPriceListener {
private async pollPrices() { private async pollPrices() {
console.log(`Polling ${this.chain} prices...`); console.log(`Polling ${this.chain} prices...`);
for (const priceId of this.priceIds) { for (const { id: priceId } of this.priceItems) {
const currentPriceInfo = await this.getOnChainPriceInfo(priceId); const currentPriceInfo = await this.getOnChainPriceInfo(priceId);
if (currentPriceInfo !== undefined) { if (currentPriceInfo !== undefined) {
this.updateLatestPriceInfo(priceId, currentPriceInfo); this.updateLatestPriceInfo(priceId, currentPriceInfo);