Skip to content
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

Add a Math Library for Combining Pyth Prices on Solidity #1748

Closed

Conversation

nirala-mehul
Copy link

This PR adds the Price Library, a Solidity library comibing prices using Price data types. The library includes functions for basic arithmetic operations, normalization, scaling, and fraction calculations on Price structs.

Changes Made

  • Price Library: Added a new Solidity library (PriceLibrary.sol) that provides essential operations for manipulating Pyth prices.
  • Forge Test Project: Introduced a test project (forge-test) to validate the functionality of the Price Library across various scenarios.
  • Test Cases: Included comprehensive test cases within the forge-test project to ensure robustness and accuracy of the implemented functions.

Summary of Public Functions Implemented in Price Library

  • getPriceInQuote: Calculates the price of a base asset in terms of a quote asset using division and scaling.
  • getCollateralValuationPrice: Computes an adjusted price based on deposits, applying rate discounts linearly interpolated between initial and final rates.
  • getBorrowValuationPrice: Computes an adjusted price based on borrows, applying rate premiums linearly interpolated between initial and final premiums.
  • affineCombination: Performs an affine combination of two prices at a query point, useful for interpolation.
  • normalize: Normalizes a Price struct by adjusting its price and confidence based on its exponent.
  • scaleToExponent: Scales a Price struct to a target exponent, adjusting its price and confidence accordingly.
  • div: Performs division between two Price structs, adjusting for precision and handling overflow conditions.
  • add: Adds two Price structs together, ensuring their exponents match.
  • cmul: Performs constant multiplication of a Price struct by a scalar constant.
  • mul: Multiplies two Price structs together, adjusting their exponents and handling overflow.

Testing
Added a forge project and tests for PriceLibrary.

Copy link

vercel bot commented Jun 30, 2024

@nirala-mehul is attempting to deploy a commit to the pyth-web Team on Vercel.

A member of the Team first needs to authorize it.

target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
PythStructs.Price memory right = mul(y1, frac2Q);
// If resulting price is zero, return zero price
if (right.price == 0) {
return PythStructs.Price(0, 0, 0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
target_chains/ethereum/sdk/solidity/PriceLibrary.sol Outdated Show resolved Hide resolved
@ali-bahjati
Copy link
Collaborator

We have decided to move forward with #1746

@ali-bahjati ali-bahjati closed this Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants