Skip to content

Commit 8913985

Browse files
author
Dev Kalra
authored
remove dependency from price config (#638)
1 parent 6bd4e2d commit 8913985

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

price_pusher/src/evm.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Contract, EventData } from "web3-eth-contract";
2-
import { PriceConfig } from "./price-config";
3-
import { ChainPricePusher, PriceInfo, ChainPriceListener } from "./interface";
2+
import {
3+
ChainPricePusher,
4+
PriceInfo,
5+
ChainPriceListener,
6+
PriceItem,
7+
} from "./interface";
48
import { TransactionReceipt } from "ethereum-protocol";
59
import { addLeading0x, DurationInSeconds, removeLeading0x } from "./utils";
610
import AbstractPythAbi from "@pythnetwork/pyth-sdk-solidity/abis/AbstractPyth.json";
@@ -17,23 +21,15 @@ import {
1721
export class EvmPriceListener extends ChainPriceListener {
1822
private pythContractFactory: PythContractFactory;
1923
private pythContract: Contract;
20-
private priceIdToAlias: Map<HexString, string>;
2124

2225
constructor(
2326
pythContractFactory: PythContractFactory,
24-
priceConfigs: PriceConfig[],
27+
priceItems: PriceItem[],
2528
config: {
2629
pollingFrequency: DurationInSeconds;
2730
}
2831
) {
29-
super(
30-
"Evm",
31-
config.pollingFrequency,
32-
priceConfigs.map((priceConfig) => priceConfig.id)
33-
);
34-
this.priceIdToAlias = new Map(
35-
priceConfigs.map((priceConfig) => [priceConfig.id, priceConfig.alias])
36-
);
32+
super("Evm", config.pollingFrequency, priceItems);
3733

3834
this.pythContractFactory = pythContractFactory;
3935
this.pythContract = this.pythContractFactory.createPythContract();
@@ -57,7 +53,7 @@ export class EvmPriceListener extends ChainPriceListener {
5753
}
5854

5955
private async startSubscription() {
60-
for (const priceId of this.priceIds) {
56+
for (const { id: priceId } of this.priceItems) {
6157
this.pythContract.events.PriceFeedUpdate(
6258
{
6359
filter: {
@@ -106,6 +102,12 @@ export class EvmPriceListener extends ChainPriceListener {
106102
return undefined;
107103
}
108104

105+
console.log(
106+
`Polled an EVM on chain price for feed ${this.priceIdToAlias.get(
107+
priceId
108+
)} (${priceId}).`
109+
);
110+
109111
return {
110112
conf: priceRaw.conf,
111113
price: priceRaw.price,

price_pusher/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const argv = yargs(hideBin(process.argv))
7777
.parseSync();
7878

7979
const priceConfigs = readPriceConfigFile(argv.priceConfigFile);
80+
const priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias }));
8081

8182
// TODO: name ChainPricePusher -> IPricePusher in a clean up PR
8283
// TODO: update listeners to not depend on the whole priceConfig
@@ -120,7 +121,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
120121
argv.pythContract
121122
);
122123

123-
return new EvmPriceListener(pythContractFactory, priceConfigs, {
124+
return new EvmPriceListener(pythContractFactory, priceItems, {
124125
pollingFrequency: argv.pollingFrequency,
125126
});
126127
}
@@ -129,7 +130,7 @@ function getNetworkPriceListener(network: string): IPriceListener {
129130
return new InjectivePriceListener(
130131
argv.pythContract,
131132
argv.endpoint,
132-
priceConfigs,
133+
priceItems,
133134
{ pollingFrequency: argv.pollingFrequency }
134135
);
135136
default:

price_pusher/src/injective.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { HexString, PriceServiceConnection } from "@pythnetwork/pyth-common-js";
2-
import { ChainPricePusher, PriceInfo, ChainPriceListener } from "./interface";
2+
import {
3+
ChainPricePusher,
4+
PriceInfo,
5+
ChainPriceListener,
6+
PriceItem,
7+
} from "./interface";
38
import { DurationInSeconds } from "./utils";
4-
import { PriceConfig } from "./price-config";
59
import {
610
ChainGrpcAuthApi,
711
ChainGrpcWasmApi,
@@ -36,16 +40,12 @@ export class InjectivePriceListener extends ChainPriceListener {
3640
constructor(
3741
private contractAddress: string,
3842
private grpcEndpoint: string,
39-
priceConfigs: PriceConfig[],
43+
priceItems: PriceItem[],
4044
config: {
4145
pollingFrequency: DurationInSeconds;
4246
}
4347
) {
44-
super(
45-
"Injective",
46-
config.pollingFrequency,
47-
priceConfigs.map((priceConfig) => priceConfig.id)
48-
);
48+
super("Injective", config.pollingFrequency, priceItems);
4949
}
5050

5151
async getOnChainPriceInfo(
@@ -67,6 +67,12 @@ export class InjectivePriceListener extends ChainPriceListener {
6767
return undefined;
6868
}
6969

70+
console.log(
71+
`Polled an Injective on chain price for feed ${this.priceIdToAlias.get(
72+
priceId
73+
)} (${priceId}).`
74+
);
75+
7076
return {
7177
conf: priceQueryResponse.price_feed.price.conf,
7278
price: priceQueryResponse.price_feed.price.price,

price_pusher/src/interface.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { HexString, UnixTimestamp } from "@pythnetwork/pyth-common-js";
22
import { DurationInSeconds } from "./utils";
33

4+
export type PriceItem = {
5+
id: HexString;
6+
alias: string;
7+
};
8+
49
export type PriceInfo = {
510
price: string;
611
conf: string;
@@ -15,13 +20,17 @@ export interface IPriceListener {
1520

1621
export abstract class ChainPriceListener implements IPriceListener {
1722
private latestPriceInfo: Map<HexString, PriceInfo>;
23+
protected priceIdToAlias: Map<HexString, string>;
1824

1925
constructor(
2026
private chain: string,
2127
private pollingFrequency: DurationInSeconds,
22-
protected priceIds: HexString[]
28+
protected priceItems: PriceItem[]
2329
) {
2430
this.latestPriceInfo = new Map();
31+
this.priceIdToAlias = new Map(
32+
priceItems.map(({ id, alias }) => [id, alias])
33+
);
2534
}
2635

2736
async start() {
@@ -33,7 +42,7 @@ export abstract class ChainPriceListener implements IPriceListener {
3342

3443
private async pollPrices() {
3544
console.log(`Polling ${this.chain} prices...`);
36-
for (const priceId of this.priceIds) {
45+
for (const { id: priceId } of this.priceItems) {
3746
const currentPriceInfo = await this.getOnChainPriceInfo(priceId);
3847
if (currentPriceInfo !== undefined) {
3948
this.updateLatestPriceInfo(priceId, currentPriceInfo);

0 commit comments

Comments
 (0)