2023-06-04 04:23:20 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
pragma solidity ^0.6.12;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
|
|
|
// OZ imports
|
|
|
|
|
|
|
|
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
|
2023-06-05 01:37:03 +03:00
|
|
|
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
|
|
|
|
|
|
// Uniswap imports
|
|
|
|
|
|
|
|
import { IUniswapV3Factory } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
|
|
|
|
import { IUniswapV3PoolState } from "@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3PoolState.sol";
|
|
|
|
|
|
|
|
// Tornado imports
|
|
|
|
|
|
|
|
import { ITornadoInstance } from "tornado-anonymity-mining/contracts/interfaces/ITornadoInstance.sol";
|
2023-06-04 04:23:20 +03:00
|
|
|
|
|
|
|
// Local imports
|
|
|
|
|
|
|
|
import { IFeeOracle } from "./interfaces/IFeeOracle.sol";
|
|
|
|
|
2023-06-05 01:37:03 +03:00
|
|
|
import { UniswapV3OracleHelper } from "./libraries/UniswapV3OracleHelper.sol";
|
|
|
|
|
|
|
|
import { InstanceData } from "./InstanceRegistry.sol";
|
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
/**
|
|
|
|
* @dev The global configuration for the `UniswapV3FeeOracle` contract. TORN is used in all `getFee()` calls,
|
|
|
|
* the interval seconds for the TWAP oracle are global, and the minimum observation cardinality assures the
|
|
|
|
* former interval can be requested.
|
|
|
|
*/
|
|
|
|
struct GlobalOracleConfig {
|
2023-06-05 01:37:03 +03:00
|
|
|
uint24 tornPoolFee;
|
2023-06-07 00:13:54 +03:00
|
|
|
uint32 twapIntervalSeconds;
|
|
|
|
uint16 minObservationCardinality;
|
2023-06-05 01:37:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title UniswapV3FeeOracle
|
|
|
|
* @author AlienTornadosaurusHex
|
|
|
|
* @notice A TORN fee oracle for any Uniswap V3 token pool.
|
|
|
|
*/
|
2023-06-04 04:23:20 +03:00
|
|
|
contract UniswapV3FeeOracle is IFeeOracle {
|
2023-06-05 01:37:03 +03:00
|
|
|
using SafeMath for uint256;
|
|
|
|
|
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ VARIABLES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
|
|
|
|
|
|
/* @dev The Governance Proxy address */
|
|
|
|
address public immutable governanceProxyAddress;
|
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
/* @dev Global configuration valid across all `getFee()` (and other) calls */
|
|
|
|
GlobalOracleConfig public globals;
|
2023-06-05 01:37:03 +03:00
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
/* @dev Uniswap pool fees for each token registered */
|
|
|
|
mapping(IERC20 => uint24) public poolFeesByToken;
|
2023-06-05 01:37:03 +03:00
|
|
|
|
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EVENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
event PoolFeeUpdated(IERC20 token, uint24 newPoolFee);
|
|
|
|
event GlobalTornPoolFeeUpdated(uint24 newPoolFee);
|
|
|
|
event GlobalTwapIntervalSecondsUpdated(uint32 newUniswapTimePeriod);
|
2023-06-05 01:37:03 +03:00
|
|
|
event GlobalMinObservationCardinalityUpdated(uint16 newMinObservationCardinality);
|
|
|
|
|
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LOGIC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
constructor(address _governanceProxyAddress) public {
|
2023-06-05 01:37:03 +03:00
|
|
|
governanceProxyAddress = _governanceProxyAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
modifier onlyGovernance() {
|
|
|
|
require(msg.sender == governanceProxyAddress, "UniswapV3FeeOracle: onlyGovernance");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GETTERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
2023-06-05 01:37:03 +03:00
|
|
|
|
|
|
|
function getFee(
|
|
|
|
IERC20 _torn,
|
|
|
|
ITornadoInstance _instance,
|
|
|
|
InstanceData memory _data,
|
|
|
|
uint64 _feePercent,
|
|
|
|
uint64 _feePercentDivisor
|
|
|
|
) public view virtual override returns (uint160) {
|
|
|
|
// If fee is 0 return
|
|
|
|
if (_feePercent == 0) return 0;
|
|
|
|
|
|
|
|
// If it's not an ERC20 it has to be ETH, use the WETH token
|
|
|
|
_data.token = _data.isERC20 ? _data.token : IERC20(UniswapV3OracleHelper.WETH);
|
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
// Get global config
|
|
|
|
GlobalOracleConfig memory global = globals;
|
2023-06-05 01:37:03 +03:00
|
|
|
|
2023-06-07 00:13:54 +03:00
|
|
|
// Get pool fee for the token and calc the price ratio
|
2023-06-05 01:37:03 +03:00
|
|
|
uint256 tokenPriceRatio = UniswapV3OracleHelper.getPriceRatioOfTokens(
|
|
|
|
[address(_torn), address(_data.token)],
|
2023-06-07 00:13:54 +03:00
|
|
|
[global.tornPoolFee, poolFeesByToken[_data.token]],
|
|
|
|
global.twapIntervalSeconds
|
2023-06-05 01:37:03 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// And now all according to legacy calculation
|
|
|
|
return uint160(
|
|
|
|
_instance.denomination().mul(UniswapV3OracleHelper.RATIO_DIVIDER).div(tokenPriceRatio).mul(
|
|
|
|
uint256(_feePercent)
|
2023-06-07 00:13:54 +03:00
|
|
|
).div(uint256(_feePercentDivisor))
|
2023-06-05 01:37:03 +03:00
|
|
|
);
|
|
|
|
}
|
2023-06-07 00:13:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice This function allows an external contract to rely on the selection mechanism of Uniswap pools
|
|
|
|
* based on this contracts minimum observation cardinality requirement.
|
|
|
|
*/
|
|
|
|
function getPriceRatioOfTokens(IERC20 target, IERC20 quote) public view virtual returns (uint256) {
|
|
|
|
return UniswapV3OracleHelper.getPriceRatioOfTokens(
|
|
|
|
[address(target), address(quote)],
|
|
|
|
[poolFeesByToken[target], poolFeesByToken[quote]],
|
|
|
|
globals.twapIntervalSeconds
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SETTERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
|
|
|
|
|
|
function setPoolFeeForToken(IERC20 _token, uint24 _tokenPoolFee) public virtual onlyGovernance {
|
|
|
|
// Get global config
|
|
|
|
|
|
|
|
GlobalOracleConfig memory global = globals;
|
|
|
|
|
|
|
|
// Check whether all globals needed are initialized
|
|
|
|
|
|
|
|
require(global.tornPoolFee != 0, "UniswapV3FeeOracle: torn pool fee not initialized");
|
|
|
|
require(global.twapIntervalSeconds != 0, "UniswapV3FeeOracle: time period not initialized");
|
|
|
|
|
|
|
|
// Check whether a pool exists for the token + fee combination
|
|
|
|
|
|
|
|
address poolAddress = UniswapV3OracleHelper.UniswapV3Factory.getPool(
|
|
|
|
address(_token), UniswapV3OracleHelper.WETH, _tokenPoolFee
|
|
|
|
);
|
|
|
|
|
|
|
|
require(poolAddress != address(0), "UniswapV3FeeOracle: pool for token and fee does not exist");
|
|
|
|
|
|
|
|
// Check whether the pool has a large enough observation cardinality
|
|
|
|
|
|
|
|
(,,,, uint16 observationCardinalityNext,,) = IUniswapV3PoolState(poolAddress).slot0();
|
|
|
|
|
|
|
|
require(
|
|
|
|
global.minObservationCardinality <= observationCardinalityNext,
|
|
|
|
"UniswapV3FeeOracle: pool observation cardinality low"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Store & log
|
|
|
|
|
|
|
|
poolFeesByToken[_token] = _tokenPoolFee;
|
|
|
|
|
|
|
|
emit PoolFeeUpdated(_token, _tokenPoolFee);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGlobalTornPoolFee(uint24 _newGlobalTornPoolFee) public virtual onlyGovernance {
|
|
|
|
globals.tornPoolFee = _newGlobalTornPoolFee;
|
|
|
|
|
|
|
|
// For `getPriceRatioOfTokens`
|
|
|
|
poolFeesByToken[IERC20(0x77777FeDdddFfC19Ff86DB637967013e6C6A116C)] = _newGlobalTornPoolFee;
|
|
|
|
|
|
|
|
emit GlobalTornPoolFeeUpdated(_newGlobalTornPoolFee);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGlobalTwapIntervalSeconds(uint32 _newGlobalTwapIntervalSeconds)
|
|
|
|
public
|
|
|
|
virtual
|
|
|
|
onlyGovernance
|
|
|
|
{
|
|
|
|
globals.twapIntervalSeconds = _newGlobalTwapIntervalSeconds;
|
|
|
|
emit GlobalTwapIntervalSecondsUpdated(_newGlobalTwapIntervalSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGlobalMinObservationCardinality(uint16 _newGlobalMinObservationCardinality)
|
|
|
|
public
|
|
|
|
virtual
|
|
|
|
onlyGovernance
|
|
|
|
{
|
|
|
|
globals.minObservationCardinality = _newGlobalMinObservationCardinality;
|
|
|
|
emit GlobalMinObservationCardinalityUpdated(_newGlobalMinObservationCardinality);
|
|
|
|
}
|
2023-06-04 04:23:20 +03:00
|
|
|
}
|