S Price: $0.464923 (+6.59%)

Contract Diff Checker

Contract Name:
OffchainOracle

Contract Source Code:

File 1 of 1 : OffchainOracle

// SPDX-License-Identifier: No

pragma solidity 0.7.6;

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

interface IERC20Metadata is IERC20 {
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
}
// For Solidly v3
interface ISolidlyV3Pool {
    function slot0() external view returns (uint160 sqrtPriceX96, int24, uint24, bool);
    function token0() external view returns (address);
    function token1() external view returns (address);
}
contract OffchainOracle {
    function getRate(address tokenFrom, address tokenTo, address poolAddress, uint8 ver) external view returns (uint256 rate, uint8 decimalsFrom, uint8 decimalsTo) {
        // version = 3 : Solidly v3
        if(ver == 3) {
            address token0 = ISolidlyV3Pool(poolAddress).token0();
            address token1 = ISolidlyV3Pool(poolAddress).token1();
            (uint256 sqrtPriceX96,,,) = ISolidlyV3Pool(poolAddress).slot0();
            if(token0 == tokenFrom) {
                decimalsFrom = IERC20Metadata(token0).decimals();
                decimalsTo = IERC20Metadata(token1).decimals();
                rate = (((1e18 * sqrtPriceX96) >> 96) * sqrtPriceX96) >> 96;
            } else {
                decimalsFrom = IERC20Metadata(token1).decimals();
                decimalsTo = IERC20Metadata(token0).decimals();
                rate = (1e18 << 192) / sqrtPriceX96 / sqrtPriceX96;
            }
        }
    }
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):