@@ -22,56 +22,40 @@ interface IERC20 {
2222
2323contract UniswapV2Oracle is IOracle {
2424 address public immutable factory;
25- address public immutable WETH;
2625 address public immutable USDT;
2726
28- constructor (address _factory , address _weth , address _usdt ) {
27+ constructor (address _factory , address _usdt ) {
2928 factory = _factory;
30- WETH = _weth;
3129 USDT = _usdt;
3230 }
3331
34- function getNativePrice ( ) external view override returns (uint256 , uint256 ) {
35- return getTokenPrice (WETH, USDT, 1 ether );
32+ function getPrice ( address token ) external view returns (uint256 , uint256 ) {
33+ return getTokenValueInUSDT (token, 10 ** IERC20 (token). decimals () );
3634 }
3735
38- function getTokenPrice (address tokenA , address tokenB , uint256 amount ) public view returns (uint256 , uint256 ) {
39- address pairAddress = IUniswapV2Factory (factory).getPair (tokenA, tokenB);
40- console.log ("pair address: " , pairAddress);
36+ function getTokenValueInUSDT (address token , uint256 amount ) public view returns (uint256 , uint256 ) {
37+ address pairAddress = IUniswapV2Factory (factory).getPair (token, USDT);
4138 require (pairAddress != address (0 ), "Pair does not exist " );
4239
4340 IUniswapV2Pair pair = IUniswapV2Pair (pairAddress);
44-
4541 (uint112 reserve0 , uint112 reserve1 ,) = pair.getReserves ();
4642 require (reserve0 > 0 && reserve1 > 0 , "Insufficient liquidity " );
47- console.log ("reserve0 is, " , reserve0);
48- console.log ("reserve1 is, " , reserve1);
49-
50- address token0 = pair.token0 ();
51- // address token1 = pair.token1();
52-
53- uint256 reserveA;
54- uint256 reserveB;
5543
56- if (tokenA == token0) {
57- reserveA = uint256 (reserve0);
58- reserveB = uint256 (reserve1);
59- } else {
60- reserveA = uint256 (reserve1);
61- reserveB = uint256 (reserve0);
62- }
44+ (uint256 reserveToken , uint256 reserveUSDT ) =
45+ pair.token0 () == USDT ? (reserve1, reserve0) : (reserve0, reserve1);
6346
64- uint8 decimalsA = IERC20 (tokenA).decimals ();
65- console.log ("decimal a " , decimalsA);
66- uint8 decimalsB = IERC20 (tokenB).decimals ();
67- console.log ("decimal b " , decimalsB);
47+ uint8 tokenDecimals = IERC20 (token).decimals ();
48+ uint8 usdtDecimals = IERC20 (USDT).decimals (); // address token1 = pair.token1();
6849
6950 // e.g. lets say we wanna get price fo 1eth
7051 // 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));
52+ // 1e18 * 30000USD * 10 ** 1e18 / 10eth * 10 ** 1e6
53+ uint256 price = (amount * reserveUSDT * (10 ** tokenDecimals)) / (reserveToken * (10 ** usdtDecimals));
54+ console.log ("price: {} " , price);
55+ console.log ("reserve0: {} " , reserve0);
56+ console.log ("reserve1: {} " , reserve1);
7357
74- uint256 scale = 10 ** decimalsA ;
58+ uint256 scale = 10 ** tokenDecimals ;
7559 uint256 integer_part = price / scale;
7660 uint256 fraction = price % scale;
7761 return (integer_part, fraction);
0 commit comments