Skip to content

Commit

Permalink
feat(target_chains/ethereum/sdk/solidity): add convertToUint method t…
Browse files Browse the repository at this point in the history
…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
aditya520 authored Mar 28, 2024
1 parent 9328b73 commit 77db9ee
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 4 deletions.
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
/// @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"
}
}

0 comments on commit 77db9ee

Please sign in to comment.