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

Commit

Permalink
[pyth-evm-price-pusher]: Enable polling in evm WS RPC (#68)
Browse files Browse the repository at this point in the history
* Add polling in evm ws rpc as an addition to the existing subscription
  • Loading branch information
ali-bahjati authored Nov 2, 2022
1 parent 40f100e commit 77417cb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
9 changes: 5 additions & 4 deletions pyth-evm-price-pusher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ docker run public.ecr.aws/pyth-network/xc-evm-price-pusher:v<version> -- <above-

The program accepts the following command line arguments:

- `evm-endpoint`: RPC endpoint URL for the EVM network. If you provide a websocket RPC endpoint (`ws[s]://...`),
the price pusher will use event subscriptions to read the current EVM price. If you provide a normal
HTTP endpoint, the pusher will periodically poll for updates. The polling interval is configurable via
the `evm-polling-frequency` command-line argument (described below).
- `evm-endpoint`: RPC endpoint URL for the EVM network. If you provide a normal HTTP endpoint,
the pusher will periodically poll for updates. The polling interval is configurable via the
`evm-polling-frequency` command-line argument (described below). If you provide a websocket RPC endpoint
(`ws[s]://...`), the price pusher will use event subscriptions to read the current EVM
price in addition to polling.
- `mnemonic-file`: Path to payer mnemonic (private key) file.
- `pyth-contract`: The Pyth contract address. Provide the network name on which Pyth is deployed
or the Pyth contract address if you use a local network.
Expand Down
2 changes: 1 addition & 1 deletion pyth-evm-price-pusher/docker-compose.mainnet.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
spy:
image: ghcr.io/wormhole-foundation/guardiand:v2.10.3
image: ghcr.io/wormhole-foundation/guardiand:v2.13.1
command:
- "spy"
- "--nodeKey"
Expand Down
2 changes: 1 addition & 1 deletion pyth-evm-price-pusher/docker-compose.testnet.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
spy:
image: ghcr.io/wormhole-foundation/guardiand:v2.10.3
image: ghcr.io/wormhole-foundation/guardiand:v2.13.1
command:
- "spy"
- "--nodeKey"
Expand Down
4 changes: 2 additions & 2 deletions pyth-evm-price-pusher/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyth-evm-price-pusher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/pyth-evm-price-pusher",
"version": "1.0.1",
"version": "1.0.2",
"description": "Pyth EVM Price Pusher",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
33 changes: 24 additions & 9 deletions pyth-evm-price-pusher/src/evm-price-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ export class EvmPriceListener implements PriceListener {
this.startSubscription();
} else {
console.log(
"The target network RPC endpoint is not Websocket. Using polling instead..."
"The target network RPC endpoint is not Websocket. " +
"Listening for updates only via polling...."
);
setInterval(this.pollPrices.bind(this), this.pollingFrequency * 1000);
}

// Poll the prices to have values in the beginning until updates arrive.
console.log(
"Polling the prices in the beginning in order to set the initial values."
);
console.log(`Polling the prices every ${this.pollingFrequency} seconds...`);
setInterval(this.pollPrices.bind(this), this.pollingFrequency * 1000);

await this.pollPrices();
}

Expand Down Expand Up @@ -100,15 +99,15 @@ export class EvmPriceListener implements PriceListener {
publishTime: Number(event.returnValues.publishTime),
};

this.latestPriceInfo.set(priceId, priceInfo);
this.updateLatestPriceInfo(priceId, priceInfo);
}

private async pollPrices() {
console.log("Polling evm prices...");
for (const priceId of this.priceIds) {
const currentPriceInfo = await this.getOnChainPriceInfo(priceId);
if (currentPriceInfo !== undefined) {
this.latestPriceInfo.set(priceId, currentPriceInfo);
this.updateLatestPriceInfo(priceId, currentPriceInfo);
}
}
}
Expand All @@ -134,7 +133,23 @@ export class EvmPriceListener implements PriceListener {
return {
conf: priceRaw.conf,
price: priceRaw.price,
publishTime: priceRaw.publishTime,
publishTime: Number(priceRaw.publishTime),
};
}

private updateLatestPriceInfo(priceId: HexString, observedPrice: PriceInfo) {
const cachedLatestPriceInfo = this.getLatestPriceInfo(priceId);

// Ignore the observed price if the cache already has newer
// price. This could happen because we are using polling and
// subscription at the same time.
if (
cachedLatestPriceInfo !== undefined &&
cachedLatestPriceInfo.publishTime > observedPrice.publishTime
) {
return;
}

this.latestPriceInfo.set(priceId, observedPrice);
}
}
9 changes: 5 additions & 4 deletions pyth-evm-price-pusher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import { readPriceConfigFile } from "./price-config";
const argv = yargs(hideBin(process.argv))
.option("evm-endpoint", {
description:
"RPC endpoint URL for the EVM network. If you provide a websocket RPC endpoint (`ws[s]://...`), " +
"the price pusher will use event subscriptions to read the current EVM price. If you provide " +
"a normal HTTP endpoint, the pusher will periodically poll for updates. The polling interval " +
"is configurable via the `evm-polling-frequency` command-line argument",
"RPC endpoint URL for the EVM network. If you provide a normal HTTP endpoint, the pusher " +
"will periodically poll for updates. The polling interval is configurable via the " +
"`evm-polling-frequency` command-line argument. If you provide a websocket RPC " +
"endpoint (`ws[s]://...`), the price pusher will use event subscriptions to read " +
"the current EVM price in addition to polling. ",
type: "string",
required: true,
})
Expand Down
3 changes: 3 additions & 0 deletions pyth-evm-price-pusher/src/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ export class Pusher {

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

console.log("Source latest price: ", sourceLatestPrice);
console.log("Target latest price: ", targetLatestPrice);

console.log(
`Time difference: ${timeDifference} (< ${priceConfig.timeDifference}?)`
);
Expand Down

0 comments on commit 77417cb

Please sign in to comment.