Skip to content

Commit 9459107

Browse files
author
Dev Kalra
authored
[price-pusher]cleanup (#650)
* remove dependency pyth-common-js * PriceConfig -> PriceItem in pyth price listener * consistently name contract address variable * release a version after this pr * remove comment * improve logging * rename chain price pusher
1 parent f5620ec commit 9459107

File tree

11 files changed

+63
-64
lines changed

11 files changed

+63
-64
lines changed

price_pusher/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"typescript": "^4.6.3"
4848
},
4949
"dependencies": {
50+
"@pythnetwork/price-service-client": "*",
5051
"@injectivelabs/sdk-ts": "^1.0.457",
51-
"@pythnetwork/pyth-common-js": "^1.4.0",
5252
"@pythnetwork/pyth-sdk-solidity": "^2.2.0",
5353
"@truffle/hdwallet-provider": "^2.1.3",
5454
"joi": "^17.6.0",

price_pusher/src/controller.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { UnixTimestamp } from "@pythnetwork/pyth-common-js";
1+
import { UnixTimestamp } from "@pythnetwork/price-service-client";
22
import { DurationInSeconds, sleep } from "./utils";
3-
import { ChainPricePusher, IPriceListener } from "./interface";
3+
import { IPricePusher, IPriceListener } from "./interface";
44
import { PriceConfig, shouldUpdate } from "./price-config";
55

66
export class Controller {
@@ -9,7 +9,7 @@ export class Controller {
99
private priceConfigs: PriceConfig[],
1010
private sourcePriceListener: IPriceListener,
1111
private targetPriceListener: IPriceListener,
12-
private targetChainPricePusher: ChainPricePusher,
12+
private targetChainPricePusher: IPricePusher,
1313
config: {
1414
cooldownDuration: DurationInSeconds;
1515
}
@@ -39,9 +39,20 @@ export class Controller {
3939
pubTimesToPush.push((targetLatestPrice?.publishTime || 0) + 1);
4040
}
4141
}
42-
// note that the priceIds are without leading "0x"
43-
const priceIds = pricesToPush.map((priceConfig) => priceConfig.id);
44-
this.targetChainPricePusher.updatePriceFeed(priceIds, pubTimesToPush);
42+
if (pricesToPush.length !== 0) {
43+
console.log(
44+
"Some of the above values passed the threshold. Will push the price."
45+
);
46+
47+
// note that the priceIds are without leading "0x"
48+
const priceIds = pricesToPush.map((priceConfig) => priceConfig.id);
49+
this.targetChainPricePusher.updatePriceFeed(priceIds, pubTimesToPush);
50+
} else {
51+
console.log(
52+
"None of the above values passed the threshold. No push needed."
53+
);
54+
}
55+
4556
await sleep(this.cooldownDuration * 1000);
4657
}
4758
}

price_pusher/src/evm/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PriceServiceConnection } from "@pythnetwork/pyth-common-js";
1+
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
22
import * as options from "../options";
33
import { readPriceConfigFile } from "../price-config";
44
import fs from "fs";
@@ -68,7 +68,7 @@ export default {
6868

6969
const pythListener = new PythPriceListener(
7070
priceServiceConnection,
71-
priceConfigs
71+
priceItems
7272
);
7373

7474
const pythContractFactory = new PythContractFactory(

price_pusher/src/evm/evm.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Contract, EventData } from "web3-eth-contract";
22
import {
3-
ChainPricePusher,
3+
IPricePusher,
44
PriceInfo,
55
ChainPriceListener,
66
PriceItem,
@@ -16,7 +16,7 @@ import {
1616
PriceServiceConnection,
1717
HexString,
1818
UnixTimestamp,
19-
} from "@pythnetwork/pyth-common-js";
19+
} from "@pythnetwork/price-service-client";
2020
import { CustomGasStation } from "./custom-gas-station";
2121

2222
export class EvmPriceListener extends ChainPriceListener {
@@ -98,7 +98,7 @@ export class EvmPriceListener extends ChainPriceListener {
9898
.getPriceUnsafe(addLeading0x(priceId))
9999
.call();
100100
} catch (e) {
101-
console.error(`Getting on-chain price for ${priceId} failed. Error:`);
101+
console.error(`Polling on-chain price for ${priceId} failed. Error:`);
102102
console.error(e);
103103
return undefined;
104104
}
@@ -117,7 +117,7 @@ export class EvmPriceListener extends ChainPriceListener {
117117
}
118118
}
119119

120-
export class EvmPricePusher implements ChainPricePusher {
120+
export class EvmPricePusher implements IPricePusher {
121121
private customGasStation?: CustomGasStation;
122122
constructor(
123123
private connection: PriceServiceConnection,
@@ -221,7 +221,7 @@ export class PythContractFactory {
221221
constructor(
222222
private endpoint: string,
223223
private mnemonic: string,
224-
private pythContractAddr: string
224+
private pythContractAddress: string
225225
) {}
226226

227227
/**
@@ -243,7 +243,7 @@ export class PythContractFactory {
243243

244244
return new web3.eth.Contract(
245245
AbstractPythAbi as any,
246-
this.pythContractAddr,
246+
this.pythContractAddress,
247247
{
248248
from: provider.getAddress(0),
249249
}
@@ -259,7 +259,10 @@ export class PythContractFactory {
259259
createPythContract(): Contract {
260260
const provider = this.createWeb3Provider();
261261
const web3 = new Web3(provider);
262-
return new web3.eth.Contract(AbstractPythAbi as any, this.pythContractAddr);
262+
return new web3.eth.Contract(
263+
AbstractPythAbi as any,
264+
this.pythContractAddress
265+
);
263266
}
264267

265268
hasWebsocketProvider(): boolean {

price_pusher/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// #!/usr/bin/env node
2-
// // FIXME: update readme and compose files
3-
// // FIXME: release a new version
42
import yargs from "yargs";
53
import { hideBin } from "yargs/helpers";
64
import injective from "./injective/command";

price_pusher/src/injective/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PriceServiceConnection } from "@pythnetwork/pyth-common-js";
1+
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
22
import * as options from "../options";
33
import { readPriceConfigFile } from "../price-config";
44
import fs from "fs";
@@ -51,7 +51,7 @@ export default {
5151

5252
const pythListener = new PythPriceListener(
5353
priceServiceConnection,
54-
priceConfigs
54+
priceItems
5555
);
5656

5757
const injectiveListener = new InjectivePriceListener(

price_pusher/src/injective/injective.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { HexString, PriceServiceConnection } from "@pythnetwork/pyth-common-js";
21
import {
3-
ChainPricePusher,
2+
HexString,
3+
PriceServiceConnection,
4+
} from "@pythnetwork/price-service-client";
5+
import {
6+
IPricePusher,
47
PriceInfo,
58
ChainPriceListener,
69
PriceItem,
@@ -35,11 +38,10 @@ type UpdateFeeResponse = {
3538
amount: string;
3639
};
3740

38-
// FIXME: CLEANUP contractAddr variable name consistency
3941
// this use price without leading 0x
4042
export class InjectivePriceListener extends ChainPriceListener {
4143
constructor(
42-
private contractAddress: string,
44+
private pythContractAddress: string,
4345
private grpcEndpoint: string,
4446
priceItems: PriceItem[],
4547
config: {
@@ -56,14 +58,14 @@ export class InjectivePriceListener extends ChainPriceListener {
5658
try {
5759
const api = new ChainGrpcWasmApi(this.grpcEndpoint);
5860
const { data } = await api.fetchSmartContractState(
59-
this.contractAddress,
61+
this.pythContractAddress,
6062
Buffer.from(`{"price_feed":{"id":"${priceId}"}}`).toString("base64")
6163
);
6264

6365
const json = Buffer.from(data as string, "base64").toString();
6466
priceQueryResponse = JSON.parse(json);
6567
} catch (e) {
66-
console.error(`Getting on-chain price for ${priceId} failed. Error:`);
68+
console.error(`Polling on-chain price for ${priceId} failed. Error:`);
6769
console.error(e);
6870
return undefined;
6971
}
@@ -82,11 +84,11 @@ export class InjectivePriceListener extends ChainPriceListener {
8284
}
8385
}
8486

85-
export class InjectivePricePusher implements ChainPricePusher {
87+
export class InjectivePricePusher implements IPricePusher {
8688
private wallet: PrivateKey;
8789
constructor(
8890
private priceServiceConnection: PriceServiceConnection,
89-
private pythContract: string,
91+
private pythContractAddress: string,
9092
private grpcEndpoint: string,
9193
mnemonic: string
9294
) {
@@ -160,7 +162,7 @@ export class InjectivePricePusher implements ChainPricePusher {
160162
try {
161163
const api = new ChainGrpcWasmApi(this.grpcEndpoint);
162164
const { data } = await api.fetchSmartContractState(
163-
this.pythContract,
165+
this.pythContractAddress,
164166
Buffer.from(
165167
JSON.stringify({
166168
get_update_fee: {
@@ -178,11 +180,10 @@ export class InjectivePricePusher implements ChainPricePusher {
178180
return;
179181
}
180182

181-
// TODO: add specific error messages
182183
try {
183184
const executeMsg = MsgExecuteContract.fromJSON({
184185
sender: this.injectiveAddress(),
185-
contractAddress: this.pythContract,
186+
contractAddress: this.pythContractAddress,
186187
msg: priceFeedUpdateObject,
187188
funds: [updateFeeQueryResponse],
188189
});

price_pusher/src/interface.ts

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

44
export type PriceItem = {
@@ -34,14 +34,15 @@ export abstract class ChainPriceListener implements IPriceListener {
3434
}
3535

3636
async start() {
37-
console.log(`Polling the prices every ${this.pollingFrequency} seconds...`);
37+
console.log(
38+
`Polling the prices on ${this.chain} every ${this.pollingFrequency} seconds...`
39+
);
3840
setInterval(this.pollPrices.bind(this), this.pollingFrequency * 1000);
3941

4042
await this.pollPrices();
4143
}
4244

4345
private async pollPrices() {
44-
console.log(`Polling ${this.chain} prices...`);
4546
for (const { id: priceId } of this.priceItems) {
4647
const currentPriceInfo = await this.getOnChainPriceInfo(priceId);
4748
if (currentPriceInfo !== undefined) {
@@ -79,7 +80,7 @@ export abstract class ChainPriceListener implements IPriceListener {
7980
): Promise<PriceInfo | undefined>;
8081
}
8182

82-
export interface ChainPricePusher {
83+
export interface IPricePusher {
8384
updatePriceFeed(
8485
priceIds: string[],
8586
pubTimesToPush: UnixTimestamp[]

price_pusher/src/price-config.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HexString } from "@pythnetwork/pyth-common-js";
1+
import { HexString } from "@pythnetwork/price-service-client";
22
import Joi from "joi";
33
import YAML from "yaml";
44
import fs from "fs";
@@ -99,33 +99,19 @@ export function shouldUpdate(
9999
console.log("Target latest price: ", targetLatestPrice);
100100

101101
console.log(
102-
`Time difference: ${timeDifference} (< ${priceConfig.timeDifference}?)`
103-
);
104-
console.log(
105-
`Price deviation: ${priceDeviationPct.toFixed(5)}% (< ${
106-
priceConfig.priceDeviation
107-
}%?)`
108-
);
109-
console.log(
110-
`Confidence ratio: ${confidenceRatioPct.toFixed(5)}% (< ${
111-
priceConfig.confidenceRatio
112-
}%?)`
102+
`Time difference: ${timeDifference} (< ${priceConfig.timeDifference}?) OR ` +
103+
`Price deviation: ${priceDeviationPct.toFixed(5)}% (< ${
104+
priceConfig.priceDeviation
105+
}%?) OR ` +
106+
`Confidence ratio: ${confidenceRatioPct.toFixed(5)}% (< ${
107+
priceConfig.confidenceRatio
108+
}%?)`
113109
);
114110

115111
const result =
116112
timeDifference >= priceConfig.timeDifference ||
117113
priceDeviationPct >= priceConfig.priceDeviation ||
118114
confidenceRatioPct >= priceConfig.confidenceRatio;
119115

120-
if (result == true) {
121-
console.log(
122-
"Some of the above values passed the threshold. Will push the price."
123-
);
124-
} else {
125-
console.log(
126-
"None of the above values passed the threshold. No push needed."
127-
);
128-
}
129-
130116
return result;
131117
}

price_pusher/src/pyth-price-listener.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ import {
22
HexString,
33
PriceFeed,
44
PriceServiceConnection,
5-
} from "@pythnetwork/pyth-common-js";
6-
import { PriceConfig } from "./price-config";
7-
import { PriceInfo, IPriceListener } from "./interface";
5+
} from "@pythnetwork/price-service-client";
6+
import { PriceInfo, IPriceListener, PriceItem } from "./interface";
87

98
export class PythPriceListener implements IPriceListener {
109
private connection: PriceServiceConnection;
1110
private priceIds: HexString[];
1211
private priceIdToAlias: Map<HexString, string>;
1312
private latestPriceInfo: Map<HexString, PriceInfo>;
1413

15-
constructor(connection: PriceServiceConnection, priceConfigs: PriceConfig[]) {
14+
constructor(connection: PriceServiceConnection, priceItems: PriceItem[]) {
1615
this.connection = connection;
17-
this.priceIds = priceConfigs.map((priceConfig) => priceConfig.id);
16+
this.priceIds = priceItems.map((priceItem) => priceItem.id);
1817
this.priceIdToAlias = new Map(
19-
priceConfigs.map((priceConfig) => [priceConfig.id, priceConfig.alias])
18+
priceItems.map((priceItem) => [priceItem.id, priceItem.alias])
2019
);
2120
this.latestPriceInfo = new Map();
2221
}

0 commit comments

Comments
 (0)