Skip to content

Commit

Permalink
FeeBatchV4 & improve reward pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kexleyBeefy committed Oct 5, 2023
1 parent 1fabb4f commit 85e6de8
Show file tree
Hide file tree
Showing 13 changed files with 934 additions and 223 deletions.
364 changes: 364 additions & 0 deletions contracts/BIFI/infra/BeefyFeeBatchV4.sol

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion contracts/BIFI/infra/BeefyOracle/BeefyOracle.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma solidity 0.8.19;

import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

import { ISubOracle } from "../../interfaces/oracle/ISubOracle.sol";

/// @title Beefy Oracle
Expand Down
11 changes: 4 additions & 7 deletions contracts/BIFI/infra/BeefyOracle/BeefyOracleChainlink.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma solidity 0.8.19;

import { IChainlink } from "../../interfaces/oracle/IChainlink.sol";
import { BeefyOracleHelper } from "./BeefyOracleHelper.sol";
import { BeefyOracleHelper, BeefyOracleErrors } from "./BeefyOracleHelper.sol";

/// @title Beefy Oracle using Chainlink
/// @author Beefy, @kexley
/// @notice On-chain oracle using Chainlink
library BeefyOracleChainlink {

/// @dev No response from the Chainlink feed
error NoAnswer();

/// @notice Fetch price from the Chainlink feed and scale to 18 decimals
/// @param _data Payload from the central oracle with the address of the Chainlink feed
/// @return price Retrieved price from the Chainlink feed
Expand All @@ -33,7 +30,7 @@ library BeefyOracleChainlink {
address chainlink = abi.decode(_data, (address));
try IChainlink(chainlink).decimals() returns (uint8) {
try IChainlink(chainlink).latestAnswer() returns (int256) {
} catch { revert NoAnswer(); }
} catch { revert NoAnswer(); }
} catch { revert BeefyOracleErrors.NoAnswer(); }
} catch { revert BeefyOracleErrors.NoAnswer(); }
}
}
25 changes: 25 additions & 0 deletions contracts/BIFI/infra/BeefyOracle/BeefyOracleErrors.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;

/// @title Beefy Oracle Errors
/// @author Beefy, @kexley
/// @notice Error list for Beefy Oracles
contract BeefyOracleErrors {

/// @dev No response from the Chainlink feed
error NoAnswer();

/// @dev No price for base token
/// @param token Base token
error NoBasePrice(address token);

/// @dev Token is not present in the pair
/// @param token Input token
/// @param pair Pair token
error TokenNotInPair(address token, address pair);

/// @dev Array length is not correct
error ArrayLength();

}
4 changes: 3 additions & 1 deletion contracts/BIFI/infra/BeefyOracle/BeefyOracleHelper.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma solidity 0.8.19;

import { IERC20MetadataUpgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol";

import { IBeefyOracle } from "../../interfaces/oracle/IBeefyOracle.sol";
import { BeefyOracleErrors } from "./BeefyOracleErrors.sol";

/// @title Beefy Oracle Helper
/// @author Beefy, @kexley
Expand Down
23 changes: 6 additions & 17 deletions contracts/BIFI/infra/BeefyOracle/BeefyOracleSolidly.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma solidity 0.8.19;

import { IERC20MetadataUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol";
import { BeefyOracleHelper } from "./BeefyOracleHelper.sol";

import { ISolidlyPair} from "../../interfaces/common/ISolidlyPair.sol";
import { BeefyOracleHelper, IBeefyOracle, BeefyOracleErrors } from "./BeefyOracleHelper.sol";

/// @title Beefy Oracle for Solidly
/// @author Beefy, @kexley
/// @notice On-chain oracle using Solidly
library BeefyOracleSolidly {

/// @dev Array length is not correct
error ArrayLength();

/// @dev No price for base token
/// @param token Base token
error NoBasePrice(address token);

/// @dev Token is not present in the pair
/// @param token Input token
/// @param pair Solidly pair
error TokenNotInPair(address token, address pair);

/// @notice Fetch price from the Solidly pairs using the TWAP observations
/// @param _data Payload from the central oracle with the addresses of the token route, pool
/// route and TWAP periods counted in 30 minute increments
Expand Down Expand Up @@ -50,17 +39,17 @@ library BeefyOracleSolidly {
abi.decode(_data, (address[], address[], uint256[]));

if (tokens.length != pools.length + 1 || tokens.length != twapPeriods.length + 1) {
revert ArrayLength();
revert BeefyOracleErrors.ArrayLength();
}

uint256 basePrice = IBeefyOracle(msg.sender).getPrice(tokens[0]);
if (basePrice == 0) revert NoBasePrice(tokens[0]);
if (basePrice == 0) revert BeefyOracleErrors.NoBasePrice(tokens[0]);

for (uint i; i < pools.length; i++) {
address token = tokens[i];
address pool = pools[i];
if (token != ISolidlyPair(pool).token0() || token != ISolidlyPair(pool).token1()) {
revert TokenNotInPair(token, pool);
revert BeefyOracleErrors.TokenNotInPair(token, pool);
}
}
}
Expand Down
23 changes: 6 additions & 17 deletions contracts/BIFI/infra/BeefyOracle/BeefyOracleUniswapV2.sol
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma solidity 0.8.19;

import { IERC20MetadataUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol";

import { IUniswapV2Pair } from "../../interfaces/common/IUniswapV2Pair.sol";
import { BeefyOracleHelper } from "./BeefyOracleHelper.sol";
import { BeefyOracleHelper, IBeefyOracle, BeefyOracleErrors } from "./BeefyOracleHelper.sol";

/// @title Beefy Oracle for UniswapV2
/// @author Beefy, @kexley
/// @notice On-chain oracle using UniswapV2
/// @dev Observations are stored here as UniswapV2 pairs do not store historical observations
contract BeefyOracleUniswapV2 {

/// @dev Array length is not correct
error ArrayLength();

/// @dev No price for base token
/// @param token Base token
error NoBasePrice(address token);

/// @dev Token is not present in the pair
/// @param token Input token
/// @param pair UniswapV2 pair
error TokenNotInPair(address token, address pair);

/// @dev Struct of stored price averages and the most recent observation of a pair
/// @param priceAverage0 Average price of token0
/// @param priceAverage1 Average price of token1
Expand Down Expand Up @@ -128,17 +117,17 @@ contract BeefyOracleUniswapV2 {
abi.decode(_data, (address[], address[], uint256[]));

if (tokens.length != pairs.length + 1 || tokens.length != twapPeriods.length + 1) {
revert ArrayLength();
revert BeefyOracleErrors.ArrayLength();
}

uint256 basePrice = IBeefyOracle(msg.sender).getPrice(tokens[0]);
if (basePrice == 0) revert NoBasePrice();
if (basePrice == 0) revert BeefyOracleErrors.NoBasePrice(tokens[0]);

for (uint i; i < pairs.length; i++) {
address token = tokens[i];
address pair = pairs[i];
if (token != IUniswapV2Pair(pair).token0() || token != IUniswapV2Pair(pair).token1()) {
revert TokenNotInPair();
revert BeefyOracleErrors.TokenNotInPair(token, pair);
}
}
}
Expand Down
27 changes: 9 additions & 18 deletions contracts/BIFI/infra/BeefyOracle/BeefyOracleUniswapV3.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma solidity 0.8.19;

import { IERC20MetadataUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol";
import { BeefyOracleHelper } from "./BeefyOracleHelper.sol";
import { UniswapV3OracleLibrary } from "../../utils/UniswapV3OracleLibrary.sol";

import { UniswapV3OracleLibrary, IUniswapV3Pool } from "../../utils/UniswapV3OracleLibrary.sol";
import { BeefyOracleHelper, IBeefyOracle, BeefyOracleErrors } from "./BeefyOracleHelper.sol";

/// @title Beefy Oracle for UniswapV3
/// @author Beefy, @kexley
/// @notice On-chain oracle using UniswapV3
library BeefyOracleUniswapV3 {

/// @dev Array length is not correct
error ArrayLength();

/// @dev No price for base token
/// @param token Base token
error NoBasePrice(address token);

/// @dev Token is not present in the pool
/// @param token Input token
/// @param pair UniswapV3 pool
error TokenNotInPool(address token, address pool);

/// @notice Fetch price from the UniswapV3 pools using the TWAP observations
/// @param _data Payload from the central oracle with the addresses of the token route, pool
/// route and TWAP periods in seconds
Expand Down Expand Up @@ -61,16 +50,18 @@ library BeefyOracleUniswapV3 {
(address[] memory tokens, address[] memory pools, uint256[] memory twapPeriods) =
abi.decode(_data, (address[], address[], uint256[]));

if (tokens.length != pools.length + 1 || tokens.length != twapPeriods.length + 1) revert ArrayLength();
if (tokens.length != pools.length + 1 || tokens.length != twapPeriods.length + 1) {
revert BeefyOracleErrors.ArrayLength();
}

uint256 basePrice = IBeefyOracle(msg.sender).getPrice(tokens[0]);
if (basePrice == 0) revert NoBasePrice(tokens[0]);
if (basePrice == 0) revert BeefyOracleErrors.NoBasePrice(tokens[0]);

for (uint i; i < pools.length; i++) {
address token = tokens[i];
address pool = pools[i];
if (token != IUniswapV3Pool(pool).token0() || token != IUniswapV3Pool(pool).token1()) {
revert TokenNotInPool(token, pool);
revert BeefyOracleErrors.TokenNotInPair(token, pool);
}
}
}
Expand Down
Loading

0 comments on commit 85e6de8

Please sign in to comment.