59 lines
2.1 KiB
Solidity
59 lines
2.1 KiB
Solidity
// SPDX-License-Identifier: MIT
|
||
pragma solidity ^0.8.20;
|
||
|
||
import "@uniswap/v3-core/contracts/libraries/FullMath.sol";
|
||
|
||
/// @notice Minimal interface for a Uniswap V3 pool (to access slot0)
|
||
interface IUniswapV3Pool {
|
||
function safelyGetStateOfAMM()
|
||
external
|
||
view
|
||
returns (
|
||
uint160 sqrtPrice, // The current sqrt(price) as a Q64.96 value
|
||
int24 tick,
|
||
uint16 lastFee,
|
||
uint8 pluginConfig,
|
||
uint128 activeLiquidity,
|
||
uint24 nextTick,
|
||
uint24 previousTick
|
||
);
|
||
}
|
||
|
||
/// @title USDC Quote for SWAPx
|
||
/// @notice Returns the USDC price (scaled to 18 decimals) of 1 token,
|
||
/// assuming a pool where token0 is USDC and token1 is the token of interest.
|
||
/// The pool is hardcoded as well as the token addresses.
|
||
contract UsdcQuoteV3 {
|
||
// Hardcoded addresses
|
||
address public constant POOL_ADDRESS = 0x467865E7Ce29E7ED8f362D51Fd7141117B234b44;
|
||
// token0 is USDC
|
||
address public constant USDC = 0x29219dd400f2Bf60E5a23d13Be72B486D4038894;
|
||
// token1 is the token to be priced (assumed to have 18 decimals)
|
||
address public constant TOKEN = 0xA04BC7140c26fc9BB1F36B1A604C7A5a88fb0E70;
|
||
|
||
// Constant representing 2^192 used for fixed‑point math
|
||
uint256 public constant Q192 = 2**192;
|
||
|
||
function getSqrtPrice() public view returns (uint256 price) {
|
||
IUniswapV3Pool pool = IUniswapV3Pool(POOL_ADDRESS);
|
||
(uint160 sqrtPrice,,,,,,) = pool.safelyGetStateOfAMM();
|
||
|
||
price = sqrtPrice;
|
||
}
|
||
/**
|
||
* @notice Returns the USDC price (scaled to 18 decimals) for exactly 1 TOKEN.
|
||
*
|
||
* @return amount18 The USDC amount (scaled to 18 decimals) per 1 TOKEN.
|
||
*/
|
||
function getLatestPrice(address token) external view returns (uint256 amount18) {
|
||
uint256 sqrtPrice = getSqrtPrice();
|
||
|
||
uint256 numerator1 = uint256(sqrtPrice) * uint256(sqrtPrice);
|
||
uint256 directPrice = FullMath.mulDiv(numerator1, 10**6, 1 << 192);
|
||
require(directPrice > 0, "Direct price is zero");
|
||
amount18 = 1e36 / directPrice;
|
||
|
||
}
|
||
|
||
}
|