-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(target_chains/ethereum/sdk/solidity): add convertToUint method t…
…o the sdk (#1390) * Moving convertToUint to utils * pre-commit fix * reversing OracleSwap example * pre-commit] * added test * abi-gen * Added solc to sdk * resolved comments
- Loading branch information
Showing
5 changed files
with
173 additions
and
4 deletions.
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 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 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 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 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