-
Notifications
You must be signed in to change notification settings - Fork 287
Update price feeds early if part of a batch #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ const PriceConfigFileSchema: Joi.Schema = Joi.array() | |
time_difference: Joi.number().required(), | ||
price_deviation: Joi.number().required(), | ||
confidence_ratio: Joi.number().required(), | ||
early_update: Joi.object({ | ||
time_difference: Joi.number().optional(), | ||
price_deviation: Joi.number().optional(), | ||
confidence_ratio: Joi.number().optional(), | ||
}).optional(), | ||
}) | ||
) | ||
.unique("id") | ||
|
@@ -27,6 +32,12 @@ export type PriceConfig = { | |
timeDifference: DurationInSeconds; | ||
priceDeviation: PctNumber; | ||
confidenceRatio: PctNumber; | ||
|
||
// An early update happens when another price has met the conditions to be pushed, so this | ||
// price can be included in a batch update for minimal gas cost. | ||
earlyUpdateTimeDifference: DurationInSeconds | undefined; | ||
earlyUpdatePriceDeviation: PctNumber | undefined; | ||
earlyUpdateConfidenceRatio: PctNumber | undefined; | ||
}; | ||
|
||
export function readPriceConfigFile(path: string): PriceConfig[] { | ||
|
@@ -44,11 +55,24 @@ export function readPriceConfigFile(path: string): PriceConfig[] { | |
timeDifference: priceConfigRaw.time_difference, | ||
priceDeviation: priceConfigRaw.price_deviation, | ||
confidenceRatio: priceConfigRaw.confidence_ratio, | ||
|
||
earlyUpdateTimeDifference: priceConfigRaw.early_update?.time_difference, | ||
earlyUpdatePriceDeviation: priceConfigRaw.early_update?.price_deviation, | ||
earlyUpdateConfidenceRatio: priceConfigRaw.early_update?.confidence_ratio, | ||
}; | ||
return priceConfig; | ||
}); | ||
} | ||
|
||
export enum UpdateCondition { | ||
// This price feed must be updated | ||
YES, | ||
// This price feed may be updated as part of a larger batch | ||
EARLY, | ||
// This price feed shouldn't be updated | ||
NO, | ||
} | ||
|
||
/** | ||
* Checks whether on-chain price needs to be updated with the latest pyth price information. | ||
* | ||
|
@@ -59,25 +83,25 @@ export function shouldUpdate( | |
priceConfig: PriceConfig, | ||
sourceLatestPrice: PriceInfo | undefined, | ||
targetLatestPrice: PriceInfo | undefined | ||
): boolean { | ||
): UpdateCondition { | ||
const priceId = priceConfig.id; | ||
|
||
// There is no price to update the target with. | ||
if (sourceLatestPrice === undefined) { | ||
return false; | ||
return UpdateCondition.YES; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a bug ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes unfortunately. fix is in #2730 |
||
} | ||
|
||
// It means that price never existed there. So we should push the latest price feed. | ||
if (targetLatestPrice === undefined) { | ||
console.log( | ||
`${priceConfig.alias} (${priceId}) is not available on the target network. Pushing the price.` | ||
); | ||
return true; | ||
return UpdateCondition.YES; | ||
} | ||
|
||
// The current price is not newer than the price onchain | ||
if (sourceLatestPrice.publishTime < targetLatestPrice.publishTime) { | ||
return false; | ||
return UpdateCondition.NO; | ||
} | ||
|
||
const timeDifference = | ||
|
@@ -99,19 +123,31 @@ export function shouldUpdate( | |
console.log("Target latest price: ", targetLatestPrice); | ||
|
||
console.log( | ||
`Time difference: ${timeDifference} (< ${priceConfig.timeDifference}?) OR ` + | ||
`Time difference: ${timeDifference} (< ${priceConfig.timeDifference}? / early: < ${priceConfig.earlyUpdateTimeDifference}) OR ` + | ||
`Price deviation: ${priceDeviationPct.toFixed(5)}% (< ${ | ||
priceConfig.priceDeviation | ||
}%?) OR ` + | ||
}%? / early: < ${priceConfig.earlyUpdatePriceDeviation}%?) OR ` + | ||
`Confidence ratio: ${confidenceRatioPct.toFixed(5)}% (< ${ | ||
priceConfig.confidenceRatio | ||
}%?)` | ||
}%? / early: < ${priceConfig.earlyUpdatePriceDeviation}%?)` | ||
); | ||
|
||
const result = | ||
if ( | ||
timeDifference >= priceConfig.timeDifference || | ||
priceDeviationPct >= priceConfig.priceDeviation || | ||
confidenceRatioPct >= priceConfig.confidenceRatio; | ||
|
||
return result; | ||
confidenceRatioPct >= priceConfig.confidenceRatio | ||
) { | ||
return UpdateCondition.YES; | ||
} else if ( | ||
(priceConfig.earlyUpdateTimeDifference !== undefined && | ||
timeDifference >= priceConfig.earlyUpdateTimeDifference) || | ||
(priceConfig.earlyUpdatePriceDeviation !== undefined && | ||
priceDeviationPct >= priceConfig.earlyUpdatePriceDeviation) || | ||
(priceConfig.earlyUpdateConfidenceRatio !== undefined && | ||
confidenceRatioPct >= priceConfig.earlyUpdateConfidenceRatio) | ||
) { | ||
return UpdateCondition.EARLY; | ||
} else { | ||
return UpdateCondition.NO; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by for ease of use