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

Commit 6491adf

Browse files
committed
improve interface
1 parent 494bd02 commit 6491adf

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
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, uint256);
7+
function getPrice(address token) external view returns (uint256, uint256);
88
}

src/oracle/UniswapV2Oracle.sol

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,40 @@ interface IERC20 {
2222

2323
contract 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);

test/UniswapV2Oracle.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ contract UniswapV2OracleTest is Test {
1717

1818
function setUp() public {
1919
vm.createSelectFork({urlOrAlias: "https://eth.meowrpc.com"});
20-
oracle = new UniswapV2Oracle(FACTORY, WETH, USDT);
20+
oracle = new UniswapV2Oracle(FACTORY, USDT);
2121
}
2222

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

0 commit comments

Comments
 (0)