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
4 changes: 2 additions & 2 deletions third_party/pyth/price-service/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 third_party/pyth/price-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/pyth-price-service",
"version": "2.2.2",
"version": "2.2.3",
"description": "Pyth Price Service",
"main": "index.js",
"scripts": {
Expand Down
80 changes: 39 additions & 41 deletions third_party/pyth/price-service/src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ export class Listener implements PriceStore {
}
}

isNewPriceInfo(
cachedInfo: PriceInfo | undefined,
observedInfo: PriceInfo
): boolean {
if (cachedInfo === undefined) {
return true;
}

if (cachedInfo.attestationTime < observedInfo.attestationTime) {
return true;
}

if (
cachedInfo.attestationTime === observedInfo.attestationTime &&
cachedInfo.seqNum < observedInfo.seqNum
) {
return true;
}

return false;
}

async processVaa(vaa: Buffer) {
const { parse_vaa } = await importCoreWasm();

Expand Down Expand Up @@ -184,55 +206,31 @@ export class Listener implements PriceStore {
return;
}

const isAnyPriceNew = batchAttestation.priceAttestations.some(
(priceAttestation) => {
const key = priceAttestation.priceId;
const lastAttestationTime =
this.priceFeedVaaMap.get(key)?.attestationTime;
return (
lastAttestationTime === undefined ||
lastAttestationTime < priceAttestation.attestationTime
);
}
);

if (!isAnyPriceNew) {
Copy link
Collaborator Author

@ali-behjati ali-behjati Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this one to make the code more clear. I believe the performance overhead of the computations after that are negligible; specially now that we drop duplicate VAAs before this part.

return;
}

for (const priceAttestation of batchAttestation.priceAttestations) {
const key = priceAttestation.priceId;

const lastAttestationTime =
this.priceFeedVaaMap.get(key)?.attestationTime;

if (
lastAttestationTime === undefined ||
lastAttestationTime < priceAttestation.attestationTime
) {
const priceFeed = priceAttestationToPriceFeed(priceAttestation);
const priceInfo = {
seqNum: parsedVaa.sequence,
vaa,
publishTime: priceAttestation.publishTime,
attestationTime: priceAttestation.attestationTime,
priceFeed,
emitterChainId: parsedVaa.emitter_chain,
priceServiceReceiveTime: Math.floor(new Date().getTime() / 1000),
};
const priceFeed = priceAttestationToPriceFeed(priceAttestation);
const priceInfo = {
seqNum: parsedVaa.sequence,
vaa,
publishTime: priceAttestation.publishTime,
attestationTime: priceAttestation.attestationTime,
priceFeed,
emitterChainId: parsedVaa.emitter_chain,
priceServiceReceiveTime: Math.floor(new Date().getTime() / 1000),
};

const cachedPriceInfo = this.priceFeedVaaMap.get(key);

if (this.isNewPriceInfo(cachedPriceInfo, priceInfo)) {
this.priceFeedVaaMap.set(key, priceInfo);

if (lastAttestationTime !== undefined) {
if (cachedPriceInfo !== undefined) {
this.promClient?.addPriceUpdatesAttestationTimeGap(
priceAttestation.attestationTime - lastAttestationTime
priceAttestation.attestationTime - cachedPriceInfo.attestationTime
);
}

const lastPublishTime = this.priceFeedVaaMap.get(key)?.publishTime;

if (lastPublishTime !== undefined) {
this.promClient?.addPriceUpdatesPublishTimeGap(
priceAttestation.publishTime - lastPublishTime
priceAttestation.publishTime - cachedPriceInfo.publishTime
);
}

Expand Down