|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Analog's Contracts (last updated v0.1.0) (src/Gateway.sol) |
| 3 | + |
| 4 | +pragma solidity >=0.8.0; |
| 5 | + |
| 6 | +import "src/oracle/IOracle.sol"; |
| 7 | +import {Test, console} from "forge-std/Test.sol"; |
| 8 | + |
| 9 | +interface IUniswapV2Factory { |
| 10 | + function getPair(address tokenA, address tokenB) external view returns (address pair); |
| 11 | +} |
| 12 | + |
| 13 | +interface IUniswapV2Pair { |
| 14 | + function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); |
| 15 | + function token0() external view returns (address); |
| 16 | + function token1() external view returns (address); |
| 17 | +} |
| 18 | + |
| 19 | +interface IERC20 { |
| 20 | + function decimals() external view returns (uint8); |
| 21 | +} |
| 22 | + |
| 23 | +contract UniswapV2Oracle is IOracle { |
| 24 | + address public immutable factory; |
| 25 | + address public immutable WETH; |
| 26 | + address public immutable USDT; |
| 27 | + |
| 28 | + constructor(address _factory, address _weth, address _usdt) { |
| 29 | + factory = _factory; |
| 30 | + WETH = _weth; |
| 31 | + USDT = _usdt; |
| 32 | + } |
| 33 | + |
| 34 | + function getNativePrice() external view override returns (uint256) { |
| 35 | + return getTokenPrice(WETH, USDT, 1 ether); |
| 36 | + } |
| 37 | + |
| 38 | + function getTokenPrice(address tokenA, address tokenB, uint256 amount) public view returns (uint256) { |
| 39 | + address pairAddress = IUniswapV2Factory(factory).getPair(tokenA, tokenB); |
| 40 | + console.log("pair address: ", pairAddress); |
| 41 | + require(pairAddress != address(0), "Pair does not exist"); |
| 42 | + |
| 43 | + IUniswapV2Pair pair = IUniswapV2Pair(pairAddress); |
| 44 | + |
| 45 | + (uint112 reserve0, uint112 reserve1,) = pair.getReserves(); |
| 46 | + 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; |
| 55 | + |
| 56 | + if (tokenA == token0) { |
| 57 | + reserveA = uint256(reserve0); |
| 58 | + reserveB = uint256(reserve1); |
| 59 | + } else { |
| 60 | + reserveA = uint256(reserve1); |
| 61 | + reserveB = uint256(reserve0); |
| 62 | + } |
| 63 | + |
| 64 | + uint8 decimalsA = IERC20(tokenA).decimals(); |
| 65 | + console.log("decimal a", decimalsA); |
| 66 | + uint8 decimalsB = IERC20(tokenB).decimals(); |
| 67 | + console.log("decimal b", decimalsB); |
| 68 | + |
| 69 | + uint256 price = (amount * reserveB * (10 ** decimalsB)) / (reserveA * (10 ** decimalsA)); |
| 70 | + console.log("price calculated is", price); |
| 71 | + |
| 72 | + return price; |
| 73 | + } |
| 74 | + |
| 75 | + function getPairAddress(address tokenA, address tokenB) external view returns (address) { |
| 76 | + return IUniswapV2Factory(factory).getPair(tokenA, tokenB); |
| 77 | + } |
| 78 | +} |
0 commit comments