-
Notifications
You must be signed in to change notification settings - Fork 260
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d08359
Moving convertToUint to utils
3456853
pre-commit fix
11f5132
reversing OracleSwap example
aditya520 4ecad58
pre-commit]
aditya520 0f906a5
added test
aditya520 9d57e84
abi-gen
aditya520 1b41cde
Added solc to sdk
aditya520 72f3f56
resolved comments
aditya520 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 | ||
/// @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); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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" | ||
} | ||
] |
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
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.
can you give an example here to clarify what this means (use ethereum and 18 decimals)
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.
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.
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.
Added the tests PythTestUtils.t.sol