From 614338e14f10313804b9312b20d759606634642b Mon Sep 17 00:00:00 2001 From: Alcibiades Athens Date: Fri, 23 Sep 2022 21:54:08 -0400 Subject: [PATCH 1/3] update price oracle interface from documentation effort --- package.json | 2 +- src/ChainlinkPriceOracle.sol | 4 +- src/interfaces/IERC20.sol | 77 +++++++++++++++++++++++++++++++++ src/interfaces/IPriceOracle.sol | 3 +- 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/interfaces/IERC20.sol diff --git a/package.json b/package.json index a1e155f..507670e 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "fmt:check": "forge fmt --check", "solhint": "solhint --config ./.solhint.json 'src/**/*.sol' --fix", "solhint:check": "solhint --config ./.solhint.json 'src/**/*.sol'", - "lint": "npm run fmt && npm run solhint", + "lint": "npm run solhint && npm run fmt", "lint:check": "npm run fmt:check && npm run solhint:check" }, "devDependencies": { diff --git a/src/ChainlinkPriceOracle.sol b/src/ChainlinkPriceOracle.sol index b502597..853268f 100644 --- a/src/ChainlinkPriceOracle.sol +++ b/src/ChainlinkPriceOracle.sol @@ -26,10 +26,10 @@ contract ChainlinkPriceOracle is IPriceOracle { * @param token The ERC20 token to retrieve the USD price for * @return price The price of the token in USD */ - function getPriceUSD(address token) external view returns (uint256) { + function getPriceUSD(IERC20 token) external view returns (uint256) { (, int256 price,,,) = chainlinkPriceOracle.latestRoundData(); // get rid of warnings - uint256 tmp = uint256(uint160(token)); + uint256 tmp = uint256(uint160(address(token))); uint256 price2 = uint256(price); return tmp + price2; } diff --git a/src/interfaces/IERC20.sol b/src/interfaces/IERC20.sol new file mode 100644 index 0000000..5556216 --- /dev/null +++ b/src/interfaces/IERC20.sol @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.8.0; + +/** + * @dev Interface of the ERC20 standard as defined in the EIP. + */ +interface IERC20 { + /** + * @dev Emitted when `value` tokens are moved from one account (`from`) to + * another (`to`). + * + * Note that `value` may be zero. + */ + event Transfer(address indexed from, address indexed to, uint256 value); + + /** + * @dev Emitted when the allowance of a `spender` for an `owner` is set by + * a call to {approve}. `value` is the new allowance. + */ + event Approval(address indexed owner, address indexed spender, uint256 value); + + /** + * @dev Returns the amount of tokens in existence. + */ + function totalSupply() external view returns (uint256); + + /** + * @dev Returns the amount of tokens owned by `account`. + */ + function balanceOf(address account) external view returns (uint256); + + /** + * @dev Moves `amount` tokens from the caller's account to `to`. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * Emits a {Transfer} event. + */ + function transfer(address to, uint256 amount) external returns (bool); + + /** + * @dev Returns the remaining number of tokens that `spender` will be + * allowed to spend on behalf of `owner` through {transferFrom}. This is + * zero by default. + * + * This value changes when {approve} or {transferFrom} are called. + */ + function allowance(address owner, address spender) external view returns (uint256); + + /** + * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * IMPORTANT: Beware that changing an allowance with this method brings the risk + * that someone may use both the old and the new allowance by unfortunate + * transaction ordering. One possible solution to mitigate this race + * condition is to first reduce the spender's allowance to 0 and set the + * desired value afterwards: + * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 + * + * Emits an {Approval} event. + */ + function approve(address spender, uint256 amount) external returns (bool); + + /** + * @dev Moves `amount` tokens from `from` to `to` using the + * allowance mechanism. `amount` is then deducted from the caller's + * allowance. + * + * Returns a boolean value indicating whether the operation succeeded. + * + * Emits a {Transfer} event. + */ + function transferFrom(address from, address to, uint256 amount) external returns (bool); +} diff --git a/src/interfaces/IPriceOracle.sol b/src/interfaces/IPriceOracle.sol index 5289821..98dded5 100644 --- a/src/interfaces/IPriceOracle.sol +++ b/src/interfaces/IPriceOracle.sol @@ -2,6 +2,7 @@ pragma solidity 0.8.13; import "solmate/tokens/ERC20.sol"; +import "./IERC20.sol"; /** * @notice This is an interface for contracts providing the price of a token, in @@ -16,7 +17,7 @@ interface IPriceOracle { * @param token The ERC20 token to retrieve the USD price for * @return price The price of the token in USD */ - function getPriceUSD(address token) external view returns (uint256 price); + function getPriceUSD(IERC20 token) external view returns (uint256 price); /** * @notice Returns the scaling factor for the price From 218b55c9c69cb8be476112e560dd509cc1f18f6d Mon Sep 17 00:00:00 2001 From: Alcibiades Athens Date: Fri, 23 Sep 2022 22:06:41 -0400 Subject: [PATCH 2/3] document price oracle --- src/interfaces/IPriceOracle.sol | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/interfaces/IPriceOracle.sol b/src/interfaces/IPriceOracle.sol index 98dded5..fff6ae7 100644 --- a/src/interfaces/IPriceOracle.sol +++ b/src/interfaces/IPriceOracle.sol @@ -12,8 +12,10 @@ import "./IERC20.sol"; * by being wrapped in a contract implementing this interface. */ interface IPriceOracle { + error PriceNotAvailable(); + /** - * @notice + * @notice Reverts PriceNotAvailable if the USD price cannot be derived * @param token The ERC20 token to retrieve the USD price for * @return price The price of the token in USD */ From a2425fb05621e286bada218e1caeb9bbd2e0f831 Mon Sep 17 00:00:00 2001 From: Alcibiades Athens Date: Fri, 23 Sep 2022 22:26:15 -0400 Subject: [PATCH 3/3] remove uniswap v3 details from the agnostic volatility interface --- src/interfaces/IVolatilityOracle.sol | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/interfaces/IVolatilityOracle.sol b/src/interfaces/IVolatilityOracle.sol index 1d5e449..59c0a45 100644 --- a/src/interfaces/IVolatilityOracle.sol +++ b/src/interfaces/IVolatilityOracle.sol @@ -9,13 +9,6 @@ pragma solidity 0.8.13; * by being wrapped in a contract implementing this interface. */ interface IVolatilityOracle { - enum UniswapV3FeeTier { - PCT_POINT_01, - PCT_POINT_05, - PCT_POINT_3, - PCT_1 - } - /** * @notice Retrieves the historical volatility of a ERC20 token. * @param token The ERC20 token for which to retrieve historical volatility. @@ -27,14 +20,9 @@ interface IVolatilityOracle { * @notice Retrieves the implied volatility of a ERC20 token. * @param tokenA The ERC20 token for which to retrieve historical volatility. * @param tokenB The ERC20 token for which to retrieve historical volatility. - * @param tier The Uniswap fee tier for the desired pool on which to derive a - * volatility measurement. * @return impliedVolatility The implied volatility of the token, scaled by 1e18 */ - function getImpliedVolatility(address tokenA, address tokenB, UniswapV3FeeTier tier) - external - view - returns (uint256 impliedVolatility); + function getImpliedVolatility(address tokenA, address tokenB) external view returns (uint256 impliedVolatility); /** * @notice Returns the scaling factor for the volatility