Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 77417cb

Browse files
authored
[pyth-evm-price-pusher]: Enable polling in evm WS RPC (#68)
* Add polling in evm ws rpc as an addition to the existing subscription
1 parent 40f100e commit 77417cb

8 files changed

+42
-22
lines changed

pyth-evm-price-pusher/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ docker run public.ecr.aws/pyth-network/xc-evm-price-pusher:v<version> -- <above-
6262

6363
The program accepts the following command line arguments:
6464

65-
- `evm-endpoint`: RPC endpoint URL for the EVM network. If you provide a websocket RPC endpoint (`ws[s]://...`),
66-
the price pusher will use event subscriptions to read the current EVM price. If you provide a normal
67-
HTTP endpoint, the pusher will periodically poll for updates. The polling interval is configurable via
68-
the `evm-polling-frequency` command-line argument (described below).
65+
- `evm-endpoint`: RPC endpoint URL for the EVM network. If you provide a normal HTTP endpoint,
66+
the pusher will periodically poll for updates. The polling interval is configurable via the
67+
`evm-polling-frequency` command-line argument (described below). If you provide a websocket RPC endpoint
68+
(`ws[s]://...`), the price pusher will use event subscriptions to read the current EVM
69+
price in addition to polling.
6970
- `mnemonic-file`: Path to payer mnemonic (private key) file.
7071
- `pyth-contract`: The Pyth contract address. Provide the network name on which Pyth is deployed
7172
or the Pyth contract address if you use a local network.

pyth-evm-price-pusher/docker-compose.mainnet.sample.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
spy:
3-
image: ghcr.io/wormhole-foundation/guardiand:v2.10.3
3+
image: ghcr.io/wormhole-foundation/guardiand:v2.13.1
44
command:
55
- "spy"
66
- "--nodeKey"

pyth-evm-price-pusher/docker-compose.testnet.sample.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
spy:
3-
image: ghcr.io/wormhole-foundation/guardiand:v2.10.3
3+
image: ghcr.io/wormhole-foundation/guardiand:v2.13.1
44
command:
55
- "spy"
66
- "--nodeKey"

pyth-evm-price-pusher/package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyth-evm-price-pusher/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-evm-price-pusher",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Pyth EVM Price Pusher",
55
"homepage": "https://pyth.network",
66
"main": "lib/index.js",

pyth-evm-price-pusher/src/evm-price-listener.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,14 @@ export class EvmPriceListener implements PriceListener {
5454
this.startSubscription();
5555
} else {
5656
console.log(
57-
"The target network RPC endpoint is not Websocket. Using polling instead..."
57+
"The target network RPC endpoint is not Websocket. " +
58+
"Listening for updates only via polling...."
5859
);
59-
setInterval(this.pollPrices.bind(this), this.pollingFrequency * 1000);
6060
}
6161

62-
// Poll the prices to have values in the beginning until updates arrive.
63-
console.log(
64-
"Polling the prices in the beginning in order to set the initial values."
65-
);
62+
console.log(`Polling the prices every ${this.pollingFrequency} seconds...`);
63+
setInterval(this.pollPrices.bind(this), this.pollingFrequency * 1000);
64+
6665
await this.pollPrices();
6766
}
6867

@@ -100,15 +99,15 @@ export class EvmPriceListener implements PriceListener {
10099
publishTime: Number(event.returnValues.publishTime),
101100
};
102101

103-
this.latestPriceInfo.set(priceId, priceInfo);
102+
this.updateLatestPriceInfo(priceId, priceInfo);
104103
}
105104

106105
private async pollPrices() {
107106
console.log("Polling evm prices...");
108107
for (const priceId of this.priceIds) {
109108
const currentPriceInfo = await this.getOnChainPriceInfo(priceId);
110109
if (currentPriceInfo !== undefined) {
111-
this.latestPriceInfo.set(priceId, currentPriceInfo);
110+
this.updateLatestPriceInfo(priceId, currentPriceInfo);
112111
}
113112
}
114113
}
@@ -134,7 +133,23 @@ export class EvmPriceListener implements PriceListener {
134133
return {
135134
conf: priceRaw.conf,
136135
price: priceRaw.price,
137-
publishTime: priceRaw.publishTime,
136+
publishTime: Number(priceRaw.publishTime),
138137
};
139138
}
139+
140+
private updateLatestPriceInfo(priceId: HexString, observedPrice: PriceInfo) {
141+
const cachedLatestPriceInfo = this.getLatestPriceInfo(priceId);
142+
143+
// Ignore the observed price if the cache already has newer
144+
// price. This could happen because we are using polling and
145+
// subscription at the same time.
146+
if (
147+
cachedLatestPriceInfo !== undefined &&
148+
cachedLatestPriceInfo.publishTime > observedPrice.publishTime
149+
) {
150+
return;
151+
}
152+
153+
this.latestPriceInfo.set(priceId, observedPrice);
154+
}
140155
}

pyth-evm-price-pusher/src/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import { readPriceConfigFile } from "./price-config";
1515
const argv = yargs(hideBin(process.argv))
1616
.option("evm-endpoint", {
1717
description:
18-
"RPC endpoint URL for the EVM network. If you provide a websocket RPC endpoint (`ws[s]://...`), " +
19-
"the price pusher will use event subscriptions to read the current EVM price. If you provide " +
20-
"a normal HTTP endpoint, the pusher will periodically poll for updates. The polling interval " +
21-
"is configurable via the `evm-polling-frequency` command-line argument",
18+
"RPC endpoint URL for the EVM network. If you provide a normal HTTP endpoint, the pusher " +
19+
"will periodically poll for updates. The polling interval is configurable via the " +
20+
"`evm-polling-frequency` command-line argument. If you provide a websocket RPC " +
21+
"endpoint (`ws[s]://...`), the price pusher will use event subscriptions to read " +
22+
"the current EVM price in addition to polling. ",
2223
type: "string",
2324
required: true,
2425
})

pyth-evm-price-pusher/src/pusher.ts

+3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ export class Pusher {
201201

202202
console.log(`Analyzing price ${priceConfig.alias} (${priceId})`);
203203

204+
console.log("Source latest price: ", sourceLatestPrice);
205+
console.log("Target latest price: ", targetLatestPrice);
206+
204207
console.log(
205208
`Time difference: ${timeDifference} (< ${priceConfig.timeDifference}?)`
206209
);

0 commit comments

Comments
 (0)