diff --git a/contracts/ERC4626RateProvider.sol b/contracts/ERC4626RateProvider.sol new file mode 100644 index 0000000..3f79c56 --- /dev/null +++ b/contracts/ERC4626RateProvider.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/interfaces/IERC4626.sol"; + +import "./interfaces/IRateProvider.sol"; + +/** + * @title ERC4626 Rate Provider + * @notice Returns an 18 decimal fixed point number that is the exchange rate of the + * shares of an ERC4626 to the underlying asset + */ +contract ERC4626RateProvider is IRateProvider { + IERC4626 public immutable erc4626; + uint256 public immutable fixedPointOne; + + constructor(IERC4626 _erc4626) { + erc4626 = _erc4626; + uint256 underlyingDecimals = IERC4626(_erc4626.asset()).decimals(); + // Balancer does not support tokens with more than 18 decimals so this will never underflow + fixedPointOne = 10**(18 + _erc4626.decimals() - underlyingDecimals); + } + + /** + * @return An 18 decimal fixed point number that is the exchange rate of the + * shares of an ERC4626 to the underlying asset + */ + function getRate() external view override returns (uint256) { + return erc4626.convertToAssets(fixedPointOne); + } +} diff --git a/contracts/ERC4626RateProviderFactory.sol b/contracts/ERC4626RateProviderFactory.sol new file mode 100644 index 0000000..4fe732a --- /dev/null +++ b/contracts/ERC4626RateProviderFactory.sol @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +pragma solidity ^0.8.0; + +import "./BaseRateProviderFactory.sol"; +import "./ERC4626RateProvider.sol"; + +/** + * @title ERC4626 Rate Provider Factory + * @notice Factory for creating ERC4626RateProviders + * @dev This contract is used to create ERC4626RateProvider contracts. + */ +contract ERC4626RateProviderFactory is BaseRateProviderFactory { + /** + * @notice Deploys a new ERC4626RateProvider contract using an ERC4626 contract. + * @param erc4626 - The ERC4626 contract. + */ + function create(IERC4626 erc4626) external returns (ERC4626RateProvider) { + ERC4626RateProvider rateProvider = new ERC4626RateProvider(erc4626); + _onCreate(address(rateProvider)); + return rateProvider; + } +} diff --git a/package.json b/package.json index 6116288..7932f70 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@chainlink/contracts": "^0.2.1", "@nomiclabs/hardhat-ethers": "^2.0.1", "@nomiclabs/hardhat-waffle": "^2.0.0", - "@openzeppelin/contracts": "^4.3.0", + "@openzeppelin/contracts": "^4.7.3", "@types/chai": "^4.2.12", "@types/mocha": "^8.0.3", "@types/node": "^14.6.0", diff --git a/yarn.lock b/yarn.lock index ed25654..04d93c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -620,10 +620,10 @@ "@types/sinon-chai" "^3.2.3" "@types/web3" "1.0.19" -"@openzeppelin/contracts@^4.3.0": - version "4.3.0" - resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.3.0.tgz#345236d4ec73ef381ab4907c6ef66fd55e5dedad" - integrity sha512-+uBDl/TrmR0Kch6mq3tuxMex/fK7huR6+fQMae+zJk1K5T+dp0pFl12Hbc+1L6oYMXoyDSBJ8zqhRIntrREDFA== +"@openzeppelin/contracts@^4.7.3": + version "4.9.3" + resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.9.3.tgz#00d7a8cf35a475b160b3f0293a6403c511099364" + integrity sha512-He3LieZ1pP2TNt5JbkPA4PNT9WC3gOTOlDcFGJW4Le4QKqwmiNJCRt44APfxMxvq7OugU/cqYuPcSBzOw38DAg== "@resolver-engine/core@^0.3.3": version "0.3.3"