@@ -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 ) {
0 commit comments