Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions price_pusher/src/evm.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions price_pusher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,7 +121,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
argv.pythContract
);

return new EvmPriceListener(pythContractFactory, priceConfigs, {
return new EvmPriceListener(pythContractFactory, priceItems, {
pollingFrequency: argv.pollingFrequency,
});
}
Expand All @@ -129,7 +130,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
return new InjectivePriceListener(
argv.pythContract,
argv.endpoint,
priceConfigs,
priceItems,
{ pollingFrequency: argv.pollingFrequency }
);
default:
Expand Down
22 changes: 14 additions & 8 deletions price_pusher/src/injective.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions price_pusher/src/interface.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand All @@ -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);
Expand Down