From e09ddeb77e53f6e1974f2d6462690cb44d33d03d Mon Sep 17 00:00:00 2001 From: ayushjhax Date: Sat, 22 Jun 2024 03:02:25 +0530 Subject: [PATCH 1/3] feat: Update PythUtils library to support int256 price type The PythUtils library was updated to support the int256 price type instead of int64. This change ensures that the library can handle larger price values accurately. --- target_chains/ethereum/sdk/solidity/PythUtils.sol | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/target_chains/ethereum/sdk/solidity/PythUtils.sol b/target_chains/ethereum/sdk/solidity/PythUtils.sol index 04b7f51f34..857255889b 100644 --- a/target_chains/ethereum/sdk/solidity/PythUtils.sol +++ b/target_chains/ethereum/sdk/solidity/PythUtils.sol @@ -1,4 +1,3 @@ -// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.0; library PythUtils { @@ -11,10 +10,10 @@ library PythUtils { /// This method will truncate any digits that cannot be represented by the targetDecimals. /// e.g. If the price is 0.000123 and the targetDecimals is 2, the result will be 0 function convertToUint( - int64 price, + int256 price, int32 expo, uint8 targetDecimals - ) public pure returns (uint256) { + ) internal pure returns (uint256) { if (price < 0 || expo > 0 || expo < -255) { revert(); } @@ -23,12 +22,12 @@ library PythUtils { if (targetDecimals >= priceDecimals) { return - uint(uint64(price)) * + uint(uint256(price)) * 10 ** uint32(targetDecimals - priceDecimals); } else { return - uint(uint64(price)) / + uint(uint256(price)) / 10 ** uint32(priceDecimals - targetDecimals); } } -} +} \ No newline at end of file From bb5919893396cc0cba11633f51746f9b4090dc20 Mon Sep 17 00:00:00 2001 From: ayushjhax Date: Sat, 22 Jun 2024 03:05:07 +0530 Subject: [PATCH 2/3] Update PythUtils library to support int256 price type in rust math library --- target_chains/ethereum/sdk/rust/PythUtilis.rs | 33 +++++++++++++++++++ .../ethereum/sdk/solidity/PythUtils.sol | 11 ++++--- 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 target_chains/ethereum/sdk/rust/PythUtilis.rs diff --git a/target_chains/ethereum/sdk/rust/PythUtilis.rs b/target_chains/ethereum/sdk/rust/PythUtilis.rs new file mode 100644 index 0000000000..857255889b --- /dev/null +++ b/target_chains/ethereum/sdk/rust/PythUtilis.rs @@ -0,0 +1,33 @@ +pragma solidity ^0.8.0; + +library PythUtils { + /// @notice Converts a Pyth price to a uint256 with a target number of decimals + /// @param price The Pyth price + /// @param expo The Pyth price exponent + /// @param targetDecimals The target number of decimals + /// @return The price as a uint256 + /// @dev Function will lose precision if targetDecimals is less than the Pyth price decimals. + /// This method will truncate any digits that cannot be represented by the targetDecimals. + /// e.g. If the price is 0.000123 and the targetDecimals is 2, the result will be 0 + function convertToUint( + int256 price, + int32 expo, + uint8 targetDecimals + ) internal pure returns (uint256) { + if (price < 0 || expo > 0 || expo < -255) { + revert(); + } + + uint8 priceDecimals = uint8(uint32(-1 * expo)); + + if (targetDecimals >= priceDecimals) { + return + uint(uint256(price)) * + 10 ** uint32(targetDecimals - priceDecimals); + } else { + return + uint(uint256(price)) / + 10 ** uint32(priceDecimals - targetDecimals); + } + } +} \ No newline at end of file diff --git a/target_chains/ethereum/sdk/solidity/PythUtils.sol b/target_chains/ethereum/sdk/solidity/PythUtils.sol index 857255889b..04b7f51f34 100644 --- a/target_chains/ethereum/sdk/solidity/PythUtils.sol +++ b/target_chains/ethereum/sdk/solidity/PythUtils.sol @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.0; library PythUtils { @@ -10,10 +11,10 @@ library PythUtils { /// This method will truncate any digits that cannot be represented by the targetDecimals. /// e.g. If the price is 0.000123 and the targetDecimals is 2, the result will be 0 function convertToUint( - int256 price, + int64 price, int32 expo, uint8 targetDecimals - ) internal pure returns (uint256) { + ) public pure returns (uint256) { if (price < 0 || expo > 0 || expo < -255) { revert(); } @@ -22,12 +23,12 @@ library PythUtils { if (targetDecimals >= priceDecimals) { return - uint(uint256(price)) * + uint(uint64(price)) * 10 ** uint32(targetDecimals - priceDecimals); } else { return - uint(uint256(price)) / + uint(uint64(price)) / 10 ** uint32(priceDecimals - targetDecimals); } } -} \ No newline at end of file +} From 7eeac95f60d68a75006f56758cd9ccac01eab7ba Mon Sep 17 00:00:00 2001 From: ayushjhax Date: Sat, 22 Jun 2024 03:09:38 +0530 Subject: [PATCH 3/3] feat(PythUtils): add support for int256 price type in Rust and Solidity libraries --- target_chains/ethereum/sdk/rust/PythUtilis.rs | 1 + target_chains/ethereum/sdk/solidity/PythUtils.sol | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/target_chains/ethereum/sdk/rust/PythUtilis.rs b/target_chains/ethereum/sdk/rust/PythUtilis.rs index 857255889b..a0c15a8709 100644 --- a/target_chains/ethereum/sdk/rust/PythUtilis.rs +++ b/target_chains/ethereum/sdk/rust/PythUtilis.rs @@ -9,6 +9,7 @@ library PythUtils { /// @dev Function will lose precision if targetDecimals is less than the Pyth price decimals. /// This method will truncate any digits that cannot be represented by the targetDecimals. /// e.g. If the price is 0.000123 and the targetDecimals is 2, the result will be 0 + function convertToUint( int256 price, int32 expo, diff --git a/target_chains/ethereum/sdk/solidity/PythUtils.sol b/target_chains/ethereum/sdk/solidity/PythUtils.sol index 04b7f51f34..8e221c5133 100644 --- a/target_chains/ethereum/sdk/solidity/PythUtils.sol +++ b/target_chains/ethereum/sdk/solidity/PythUtils.sol @@ -31,4 +31,4 @@ library PythUtils { 10 ** uint32(priceDecimals - targetDecimals); } } -} +} \ No newline at end of file