Contract Source Code:
File 1 of 1 : Oracle
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/SafeMath.sol)
pragma solidity ^0.8.0;
// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.
/**
* @dev Wrappers over Solidity's arithmetic operations.
*
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* now has built in overflow checking.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator.
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a / b;
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a % b;
}
}
}
pragma solidity ^0.8.26;
interface IPool {
error NOT_AUTHORIZED();
error UNSTABLE_RATIO();
/// @dev safe transfer failed
error STF();
error OVERFLOW();
/// @dev skim disabled
error SD();
/// @dev insufficient liquidity minted
error ILM();
/// @dev insufficient liquidity burned
error ILB();
/// @dev insufficient output amount
error IOA();
/// @dev insufficient input amount
error IIA();
error IL();
error IT();
error K();
event Mint(address indexed sender, uint256 amount0, uint256 amount1);
event Burn(
address indexed sender,
uint256 amount0,
uint256 amount1,
address indexed to
);
event Swap(
address indexed sender,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
/// @notice Same as prices with with an additional window argument.
/// Window = 2 means 2 * 30min (or 1 hr) between observations
/// @param tokenIn .
/// @param amountIn .
/// @param points .
/// @param window .
/// @return Array of TWAP prices
function sample(
address tokenIn,
uint256 amountIn,
uint256 points,
uint256 window
) external view returns (uint256[] memory);
function observations(uint256 index) external view returns (uint256 timestamp, uint256 reserve0Cumulative, uint256 reserve1Cumulative);
function current(address tokenIn, uint256 amountIn) external view returns (uint256 amountOut);
/// @notice Provides twap price with user configured granularity, up to the full window size
/// @param tokenIn .
/// @param amountIn .
/// @param granularity .
/// @return amountOut .
function quote(address tokenIn, uint256 amountIn, uint256 granularity) external view returns (uint256 amountOut);
/// @notice Get the number of observations recorded
function observationLength() external view returns (uint256);
/// @notice Address of token in the pool with the lower address value
function token0() external view returns (address);
/// @notice Address of token in the poool with the higher address value
function token1() external view returns (address);
/// @notice initialize the pool, called only once programatically
function initialize(
address _token0,
address _token1,
bool _stable
) external;
/// @notice calculate the current reserves of the pool and their last 'seen' timestamp
/// @return _reserve0 amount of token0 in reserves
/// @return _reserve1 amount of token1 in reserves
/// @return _blockTimestampLast the timestamp when the pool was last updated
function getReserves()
external
view
returns (
uint112 _reserve0,
uint112 _reserve1,
uint32 _blockTimestampLast
);
/// @notice mint the pair tokens (LPs)
/// @param to where to mint the LP tokens to
/// @return liquidity amount of LP tokens to mint
function mint(address to) external returns (uint256 liquidity);
/// @notice burn the pair tokens (LPs)
/// @param to where to send the underlying
/// @return amount0 amount of amount0
/// @return amount1 amount of amount1
function burn(
address to
) external returns (uint256 amount0, uint256 amount1);
/// @notice direct swap through the pool
function swap(
uint256 amount0Out,
uint256 amount1Out,
address to,
bytes calldata data
) external;
/// @notice force balances to match reserves, can be used to harvest rebases from rebasing tokens or other external factors
/// @param to where to send the excess tokens to
function skim(address to) external;
/// @notice force reserves to match balances, prevents skim excess if skim is enabled
function sync() external;
/// @notice set the pair fees contract address
function setFeeRecipient(address _pairFees) external;
/// @notice set the feesplit variable
function setFeeSplit(uint256 _feeSplit) external;
/// @notice sets the swap fee of the pair
/// @dev max of 10_000 (10%)
/// @param _fee the fee
function setFee(uint256 _fee) external;
/// @notice 'mint' the fees as LP tokens
/// @dev this is used for protocol/voter fees
function mintFee() external;
/// @notice calculates the amount of tokens to receive post swap
/// @param amountIn the token amount
/// @param tokenIn the address of the token
function getAmountOut(
uint256 amountIn,
address tokenIn
) external view returns (uint256 amountOut);
/// @notice returns various metadata about the pair
function metadata()
external
view
returns (
uint256 _decimals0,
uint256 _decimals1,
uint256 _reserve0,
uint256 _reserve1,
bool _stable,
address _token0,
address _token1
);
/// @notice returns the feeSplit of the pair
function feeSplit() external view returns (uint256);
/// @notice returns the fee of the pair
function fee() external view returns (uint256);
/// @notice returns the feeRecipient of the pair
function feeRecipient() external view returns (address);
}
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
pragma solidity ^0.8.0;
contract Operator is Context, Ownable {
address private _operator;
event OperatorTransferred(address indexed previousOperator, address indexed newOperator);
constructor() {
_operator = _msgSender();
emit OperatorTransferred(address(0), _operator);
}
function operator() public view returns (address) {
return _operator;
}
modifier onlyOperator() {
require(_operator == msg.sender, "operator: caller is not the operator");
_;
}
function isOperator() public view returns (bool) {
return _msgSender() == _operator;
}
function transferOperator(address newOperator_) public onlyOwner {
_transferOperator(newOperator_);
}
function _transferOperator(address newOperator_) internal {
require(newOperator_ != address(0), "operator: zero address given for new operator");
emit OperatorTransferred(address(0), newOperator_);
_operator = newOperator_;
}
function _renounceOperator() public onlyOwner {
emit OperatorTransferred(_operator, address(0));
_operator = address(0);
}
}
pragma solidity ^0.8.0;
contract Oracle is Operator {
using SafeMath for uint256;
address public token0;
address public token1;
IPool public pair;
constructor(IPool _pair) {
pair = _pair;
token0 = pair.token0();
token1 = pair.token1();
uint256 reserve0;
uint256 reserve1;
(reserve0, reserve1, ) = pair.getReserves();
require(reserve0 != 0 && reserve1 != 0, "Oracle: No reserves");
}
function update() external {
pair.sync();
}
function consult(
address _token,
uint256 _amountIn
) external view returns (uint256 amountOut) {
if (_token == token0) {
amountOut = _quote(_token, _amountIn, 12);
} else {
require(_token == token1, "Oracle: Invalid token");
amountOut = _quote(_token, _amountIn, 12);
}
}
function twap(
address _token,
uint256 _amountIn
) external view returns (uint256 amountOut) {
if (_token == token0) {
amountOut = _quote(_token, _amountIn, 2);
} else {
require(_token == token1, "Oracle: Invalid token");
amountOut = _quote(_token, _amountIn, 2);
}
}
// Note the window parameter is removed as its always 1 (30min), granularity at 12 for example is (12 * 30min) = 6 hours
function _quote(
address tokenIn,
uint256 amountIn,
uint256 granularity // number of observations to query
) internal view returns (uint256 amountOut) {
uint256 observationLength = IPool(pair).observationLength();
require(
granularity <= observationLength,
"Oracle: Not enough observations"
);
uint256 price = IPool(pair).quote(tokenIn, amountIn, granularity);
amountOut = price;
}
}