Skip to content

feat(target_chains/ethereum/sdk/solidity): add convertToUint method to the sdk #1390

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 8 commits into from
Mar 28, 2024
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
74 changes: 72 additions & 2 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
import "@pythnetwork/pyth-sdk-solidity/IPythEvents.sol";
import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythUtils.sol";

import "forge-std/Test.sol";
import "./WormholeTestUtils.t.sol";
Expand Down Expand Up @@ -275,3 +276,35 @@ abstract contract PythTestUtils is Test, WormholeTestUtils, RandTestUtils {
}
}
}

contract PythUtilsTest is Test, WormholeTestUtils, PythTestUtils, IPythEvents {
function testConvertToUnit() public {
// Price can't be negative
vm.expectRevert();
PythUtils.convertToUint(-100, -5, 18);

// Exponent can't be positive
vm.expectRevert();
PythUtils.convertToUint(100, 5, 18);

// Price with 18 decimals and exponent -5
assertEq(
PythUtils.convertToUint(100, -5, 18),
1000000000000000 // 100 * 10^13
);

// Price with 9 decimals and exponent -2
assertEq(
PythUtils.convertToUint(100, -2, 9),
1000000000 // 100 * 10^7
);

// Price with 4 decimals and exponent -5
assertEq(PythUtils.convertToUint(100, -5, 4), 10);

// Price with 5 decimals and exponent -2
// @note: We will lose precision here as price is
// 0.00001 and we are targetDecimals is 2.
assertEq(PythUtils.convertToUint(100, -5, 2), 0);
}
}
34 changes: 34 additions & 0 deletions target_chains/ethereum/sdk/solidity/PythUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

library PythUtils {
/// @notice Converts a Pyth price to a uint256 with a target number of decimals
Copy link
Contributor

Choose a reason for hiding this comment

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

can you give an example here to clarify what this means (use ethereum and 18 decimals)

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also note that this function can lose precision and you need to be careful about that? For example, if the price is 0.003 and you set targetdecimals to 2, then the result will be 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the tests PythTestUtils.t.sol

/// @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(
int64 price,
int32 expo,
uint8 targetDecimals
) public pure returns (uint256) {
if (price < 0 || expo > 0 || expo < -255) {
revert();
}

uint8 priceDecimals = uint8(uint32(-1 * expo));

if (targetDecimals >= priceDecimals) {
return
uint(uint64(price)) *
10 ** uint32(targetDecimals - priceDecimals);
} else {
return
uint(uint64(price)) /
10 ** uint32(priceDecimals - targetDecimals);
}
}
}
31 changes: 31 additions & 0 deletions target_chains/ethereum/sdk/solidity/abis/PythUtils.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"inputs": [
{
"internalType": "int64",
"name": "price",
"type": "int64"
},
{
"internalType": "int32",
"name": "expo",
"type": "int32"
},
{
"internalType": "uint8",
"name": "targetDecimals",
"type": "uint8"
}
],
"name": "convertToUint",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "pure",
"type": "function"
}
]
5 changes: 3 additions & 2 deletions target_chains/ethereum/sdk/solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"format": "npx prettier --write .",
"generate-abi": "npx generate-abis IPyth IPythEvents AbstractPyth MockPyth PythErrors",
"generate-abi": "npx generate-abis IPyth IPythEvents AbstractPyth MockPyth PythErrors PythUtils",
"check-abi": "git diff --exit-code abis",
"build": "solcjs --bin MockPyth.sol --base-path . -o build/"
},
Expand All @@ -25,8 +25,9 @@
},
"homepage": "https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity",
"devDependencies": {
"abi_generator": "*",
"prettier": "^2.7.1",
"prettier-plugin-solidity": "^1.0.0-rc.1",
"abi_generator": "*"
"solc": "^0.8.25"
}
}