remove dependency from price config (#638)
This commit is contained in:
parent
6bd4e2d3b8
commit
89139850b4
|
@ -1,6 +1,10 @@
|
|||
import { Contract, EventData } from "web3-eth-contract";
|
||||
import { PriceConfig } from "./price-config";
|
||||
import { ChainPricePusher, PriceInfo, ChainPriceListener } from "./interface";
|
||||
import {
|
||||
ChainPricePusher,
|
||||
PriceInfo,
|
||||
ChainPriceListener,
|
||||
PriceItem,
|
||||
} from "./interface";
|
||||
import { TransactionReceipt } from "ethereum-protocol";
|
||||
import { addLeading0x, DurationInSeconds, removeLeading0x } from "./utils";
|
||||
import AbstractPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/AbstractPyth.json";
|
||||
|
@ -17,23 +21,15 @@ import {
|
|||
export class EvmPriceListener extends ChainPriceListener {
|
||||
private pythContractFactory: PythContractFactory;
|
||||
private pythContract: Contract;
|
||||
private priceIdToAlias: Map<HexString, string>;
|
||||
|
||||
constructor(
|
||||
pythContractFactory: PythContractFactory,
|
||||
priceConfigs: PriceConfig[],
|
||||
priceItems: PriceItem[],
|
||||
config: {
|
||||
pollingFrequency: DurationInSeconds;
|
||||
}
|
||||
) {
|
||||
super(
|
||||
"Evm",
|
||||
config.pollingFrequency,
|
||||
priceConfigs.map((priceConfig) => priceConfig.id)
|
||||
);
|
||||
this.priceIdToAlias = new Map(
|
||||
priceConfigs.map((priceConfig) => [priceConfig.id, priceConfig.alias])
|
||||
);
|
||||
super("Evm", config.pollingFrequency, priceItems);
|
||||
|
||||
this.pythContractFactory = pythContractFactory;
|
||||
this.pythContract = this.pythContractFactory.createPythContract();
|
||||
|
@ -57,7 +53,7 @@ export class EvmPriceListener extends ChainPriceListener {
|
|||
}
|
||||
|
||||
private async startSubscription() {
|
||||
for (const priceId of this.priceIds) {
|
||||
for (const { id: priceId } of this.priceItems) {
|
||||
this.pythContract.events.PriceFeedUpdate(
|
||||
{
|
||||
filter: {
|
||||
|
@ -106,6 +102,12 @@ export class EvmPriceListener extends ChainPriceListener {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Polled an EVM on chain price for feed ${this.priceIdToAlias.get(
|
||||
priceId
|
||||
)} (${priceId}).`
|
||||
);
|
||||
|
||||
return {
|
||||
conf: priceRaw.conf,
|
||||
price: priceRaw.price,
|
||||
|
|
|
@ -77,6 +77,7 @@ const argv = yargs(hideBin(process.argv))
|
|||
.parseSync();
|
||||
|
||||
const priceConfigs = readPriceConfigFile(argv.priceConfigFile);
|
||||
const priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias }));
|
||||
|
||||
// TODO: name ChainPricePusher -> IPricePusher in a clean up PR
|
||||
// TODO: update listeners to not depend on the whole priceConfig
|
||||
|
@ -120,7 +121,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
|
|||
argv.pythContract
|
||||
);
|
||||
|
||||
return new EvmPriceListener(pythContractFactory, priceConfigs, {
|
||||
return new EvmPriceListener(pythContractFactory, priceItems, {
|
||||
pollingFrequency: argv.pollingFrequency,
|
||||
});
|
||||
}
|
||||
|
@ -129,7 +130,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
|
|||
return new InjectivePriceListener(
|
||||
argv.pythContract,
|
||||
argv.endpoint,
|
||||
priceConfigs,
|
||||
priceItems,
|
||||
{ pollingFrequency: argv.pollingFrequency }
|
||||
);
|
||||
default:
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
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 { PriceConfig } from "./price-config";
|
||||
import {
|
||||
ChainGrpcAuthApi,
|
||||
ChainGrpcWasmApi,
|
||||
|
@ -36,16 +40,12 @@ export class InjectivePriceListener extends ChainPriceListener {
|
|||
constructor(
|
||||
private contractAddress: string,
|
||||
private grpcEndpoint: string,
|
||||
priceConfigs: PriceConfig[],
|
||||
priceItems: PriceItem[],
|
||||
config: {
|
||||
pollingFrequency: DurationInSeconds;
|
||||
}
|
||||
) {
|
||||
super(
|
||||
"Injective",
|
||||
config.pollingFrequency,
|
||||
priceConfigs.map((priceConfig) => priceConfig.id)
|
||||
);
|
||||
super("Injective", config.pollingFrequency, priceItems);
|
||||
}
|
||||
|
||||
async getOnChainPriceInfo(
|
||||
|
@ -67,6 +67,12 @@ export class InjectivePriceListener extends ChainPriceListener {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Polled an Injective on chain price for feed ${this.priceIdToAlias.get(
|
||||
priceId
|
||||
)} (${priceId}).`
|
||||
);
|
||||
|
||||
return {
|
||||
conf: priceQueryResponse.price_feed.price.conf,
|
||||
price: priceQueryResponse.price_feed.price.price,
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
import { HexString, UnixTimestamp } from "@pythnetwork/pyth-common-js";
|
||||
import { DurationInSeconds } from "./utils";
|
||||
|
||||
export type PriceItem = {
|
||||
id: HexString;
|
||||
alias: string;
|
||||
};
|
||||
|
||||
export type PriceInfo = {
|
||||
price: string;
|
||||
conf: string;
|
||||
|
@ -15,13 +20,17 @@ export interface IPriceListener {
|
|||
|
||||
export abstract class ChainPriceListener implements IPriceListener {
|
||||
private latestPriceInfo: Map<HexString, PriceInfo>;
|
||||
protected priceIdToAlias: Map<HexString, string>;
|
||||
|
||||
constructor(
|
||||
private chain: string,
|
||||
private pollingFrequency: DurationInSeconds,
|
||||
protected priceIds: HexString[]
|
||||
protected priceItems: PriceItem[]
|
||||
) {
|
||||
this.latestPriceInfo = new Map();
|
||||
this.priceIdToAlias = new Map(
|
||||
priceItems.map(({ id, alias }) => [id, alias])
|
||||
);
|
||||
}
|
||||
|
||||
async start() {
|
||||
|
@ -33,7 +42,7 @@ export abstract class ChainPriceListener implements IPriceListener {
|
|||
|
||||
private async pollPrices() {
|
||||
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);
|
||||
if (currentPriceInfo !== undefined) {
|
||||
this.updateLatestPriceInfo(priceId, currentPriceInfo);
|
||||
|
|
Loading…
Reference in New Issue