Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 494bd02

Browse files
committed
fix price
1 parent 9652fa8 commit 494bd02

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/oracle/IOracle.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
pragma solidity >=0.8.0;
55

66
interface IOracle {
7-
function getNativePrice() external view returns (uint256);
7+
function getNativePrice() external view returns (uint256, uint256);
88
}

src/oracle/UniswapV2Oracle.sol

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ contract UniswapV2Oracle is IOracle {
3131
USDT = _usdt;
3232
}
3333

34-
function getNativePrice() external view override returns (uint256) {
34+
function getNativePrice() external view override returns (uint256, uint256) {
3535
return getTokenPrice(WETH, USDT, 1 ether);
3636
}
3737

38-
function getTokenPrice(address tokenA, address tokenB, uint256 amount) public view returns (uint256) {
38+
function getTokenPrice(address tokenA, address tokenB, uint256 amount) public view returns (uint256, uint256) {
3939
address pairAddress = IUniswapV2Factory(factory).getPair(tokenA, tokenB);
4040
console.log("pair address: ", pairAddress);
4141
require(pairAddress != address(0), "Pair does not exist");
@@ -66,10 +66,15 @@ contract UniswapV2Oracle is IOracle {
6666
uint8 decimalsB = IERC20(tokenB).decimals();
6767
console.log("decimal b", decimalsB);
6868

69-
// fix price computation
70-
uint156 price = 0;
69+
// e.g. lets say we wanna get price fo 1eth
70+
// and pool have 10 eth and 30000 usd then
71+
// 1e18 * 30000USD * 10 ** 1e18 / 10eth * 10 ** 1e6
72+
uint256 price = (amount * reserveB * (10 ** decimalsA)) / (reserveA * (10 ** decimalsB));
7173

72-
return price;
74+
uint256 scale = 10 ** decimalsA;
75+
uint256 integer_part = price / scale;
76+
uint256 fraction = price % scale;
77+
return (integer_part, fraction);
7378
}
7479

7580
function getPairAddress(address tokenA, address tokenB) external view returns (address) {

test/UniswapV2Oracle.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ contract UniswapV2OracleTest is Test {
2121
}
2222

2323
function testGetNativePrice() public view {
24-
uint256 price = oracle.getNativePrice();
25-
console.log("WETH/USDT Price:", price);
24+
(uint256 ints, uint256 fraction) = oracle.getNativePrice();
25+
console.log("WETH/USDT Price:", ints, fraction);
2626
}
2727
}

0 commit comments

Comments
 (0)