Skip to content

Commit 0f906a5

Browse files
committed
added test
1 parent 4ecad58 commit 0f906a5

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

target_chains/ethereum/contracts/forge-test/utils/PythTestUtils.t.sol

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,33 @@ contract PythTestUtilsTest is
283283
PythTestUtils,
284284
IPythEvents
285285
{
286-
using PythUtils for *;
287-
288286
function testConvertToUnit() public {
287+
// Price can't be negative
288+
vm.expectRevert();
289+
PythUtils.convertToUint(-100, -5, 18);
290+
291+
// Exponent can't be positive
289292
vm.expectRevert();
290-
uint256 price = PythUtils.convertToUnit(-100, -5, 18);
293+
PythUtils.convertToUint(100, 5, 18);
294+
295+
// Price with 18 decimals and exponent -5
296+
assertEq(
297+
PythUtils.convertToUint(100, -5, 18),
298+
1000000000000000 // 100 * 10^13
299+
);
300+
301+
// Price with 9 decimals and exponent -2
302+
assertEq(
303+
PythUtils.convertToUint(100, -2, 9),
304+
1000000000 // 100 * 10^7
305+
);
306+
307+
// Price with 4 decimals and exponent -5
308+
assertEq(PythUtils.convertToUint(100, -5, 4), 10);
309+
310+
// Price with 5 decimals and exponent -2
311+
// @note: We will lose precision here as price is
312+
// 0.00001 and we are targetDecimals is 2.
313+
assertEq(PythUtils.convertToUint(100, -5, 2), 0);
291314
}
292315
}

target_chains/ethereum/sdk/solidity/PythUtils.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ library PythUtils {
77
/// @param expo The Pyth price exponent
88
/// @param targetDecimals The target number of decimals
99
/// @return The price as a uint256
10+
/// @dev Function will loose precision if targetDecimals is less than the Pyth price decimals
11+
/// e.g. If the price is 0.000123 and the targetDecimals is 2, the result will be 0
1012
function convertToUint(
1113
int64 price,
1214
int32 expo,

0 commit comments

Comments
 (0)