Skip to content

Commit

Permalink
Use AaveOracle instead of configured chainlink oracles
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann committed Jul 31, 2023
1 parent d9a653f commit bed0e34
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
20 changes: 10 additions & 10 deletions contracts/adapters/AaveV3LeverageStrategyExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Math } from "@openzeppelin/contracts/math/Math.sol";

import {AaveLeverageStrategyExtension} from "./AaveLeverageStrategyExtension.sol";
import {IBaseManager} from "../interfaces/IBaseManager.sol";
import { IChainlinkAggregatorV3 } from "../interfaces/IChainlinkAggregatorV3.sol";
import { IAaveOracle } from "../interfaces/IAaveOracle.sol";
import {IPool} from "../interfaces/IPool.sol";
import {IPoolAddressesProvider} from "../interfaces/IPoolAddressesProvider.sol";
import { DataTypes } from "../interfaces/Datatypes.sol";
Expand All @@ -42,9 +42,11 @@ contract AaveV3LeverageStrategyExtension is AaveLeverageStrategyExtension {
IPoolAddressesProvider public lendingPoolAddressesProvider;
uint256 public maxOraclePriceAge;
bool public overrideNoRebalanceInProgress;
IAaveOracle public aaveOracle;

constructor(
IBaseManager _manager,
IAaveOracle _aaveOracle,
ContractSettings memory _strategy,
MethodologySettings memory _methodology,
ExecutionSettings memory _execution,
Expand All @@ -67,6 +69,7 @@ contract AaveV3LeverageStrategyExtension is AaveLeverageStrategyExtension {
{
lendingPoolAddressesProvider = _lendingPoolAddressesProvider;
maxOraclePriceAge = _maxOraclePriceAge;
aaveOracle = _aaveOracle;
}

/* ============ Modifiers ============ */
Expand Down Expand Up @@ -195,11 +198,11 @@ contract AaveV3LeverageStrategyExtension is AaveLeverageStrategyExtension {
function _createActionInfo() internal view override returns(ActionInfo memory) {
ActionInfo memory rebalanceInfo;

// Calculate prices from chainlink. Chainlink returns prices with 8 decimal places, but we need 36 - underlyingDecimals decimal places.
// Calculate prices from chainlink. AaveOracle returns prices with 8 decimal places, but we need 36 - underlyingDecimals decimal places.
// This is so that when the underlying amount is multiplied by the received price, the collateral valuation is normalized to 36 decimals.
// To perform this adjustment, we multiply by 10^(36 - 8 - underlyingDecimals)
rebalanceInfo.collateralPrice = _getAssetPrice(strategy.collateralPriceOracle, strategy.collateralDecimalAdjustment);
rebalanceInfo.borrowPrice = _getAssetPrice(strategy.borrowPriceOracle, strategy.borrowDecimalAdjustment);
rebalanceInfo.collateralPrice = _getAssetPrice(strategy.collateralAsset, strategy.collateralDecimalAdjustment);
rebalanceInfo.borrowPrice = _getAssetPrice(strategy.borrowAsset, strategy.borrowDecimalAdjustment);

rebalanceInfo.collateralBalance = strategy.targetCollateralAToken.balanceOf(address(strategy.setToken));
rebalanceInfo.borrowBalance = strategy.targetBorrowDebtToken.balanceOf(address(strategy.setToken));
Expand All @@ -210,12 +213,9 @@ contract AaveV3LeverageStrategyExtension is AaveLeverageStrategyExtension {
return rebalanceInfo;
}

function _getAssetPrice(IChainlinkAggregatorV3 _priceOracle, uint256 _decimalAdjustment) internal view returns (uint256) {
(,int256 rawPrice,, uint256 updatedAt,) = _priceOracle.latestRoundData();
if(maxOraclePriceAge > 0) {
require(updatedAt > block.timestamp.sub(maxOraclePriceAge), "Price data is stale");
}
return rawPrice.toUint256().mul(10 ** _decimalAdjustment);
function _getAssetPrice(address _asset, uint256 _decimalAdjustment) internal view returns (uint256) {
uint256 rawPrice = aaveOracle.getAssetPrice(_asset);
return rawPrice.mul(10 ** _decimalAdjustment);
}

}
2 changes: 1 addition & 1 deletion contracts/interfaces/IAaveOracle.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.8.10;
pragma solidity 0.6.10;

interface IAaveOracle {
event AssetSourceUpdated(address indexed asset, address indexed source);
Expand Down
24 changes: 4 additions & 20 deletions test/integration/ethereum/aaveV3LeverageStrategyExtension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ if (process.env.INTEGRATIONTEST) {
maxOraclePriceAge = 600;
leverageStrategyExtension = await deployer.extensions.deployAaveV3LeverageStrategyExtension(
baseManagerV2.address,
aaveOracle.address,
strategy,
methodology,
execution,
Expand All @@ -405,6 +406,7 @@ if (process.env.INTEGRATIONTEST) {

describe("#constructor", async () => {
let subjectManagerAddress: Address;
let subjectAaveOracleAddress: Address;
let subjectContractSettings: AaveContractSettings;
let subjectMethodologySettings: MethodologySettings;
let subjectExecutionSettings: ExecutionSettings;
Expand All @@ -418,6 +420,7 @@ if (process.env.INTEGRATIONTEST) {

beforeEach(async () => {
subjectAaveAddressesProvider = contractAddresses.aaveV3AddressProvider;
subjectAaveOracleAddress = aaveOracle.address;
subjectManagerAddress = baseManagerV2.address;
subjectMaxOraclePriceAge = maxOraclePriceAge;
subjectContractSettings = {
Expand Down Expand Up @@ -466,6 +469,7 @@ if (process.env.INTEGRATIONTEST) {
async function subject(): Promise<AaveV3LeverageStrategyExtension> {
return await deployer.extensions.deployAaveV3LeverageStrategyExtension(
subjectManagerAddress,
subjectAaveOracleAddress,
subjectContractSettings,
subjectMethodologySettings,
subjectExecutionSettings,
Expand Down Expand Up @@ -1748,26 +1752,6 @@ if (process.env.INTEGRATIONTEST) {
await weth.transfer(tradeAdapterMock.address, sendQuantity);
});

describe("when collateralPrice is outdated", async () => {
beforeEach(async () => {
await chainlinkCollateralPriceMock.setPriceAge(maxOraclePriceAge + 1);
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Price data is stale");
});
});

describe("when borrowPrice is outdated", async () => {
beforeEach(async () => {
await chainlinkBorrowPriceMock.setPriceAge(maxOraclePriceAge + 1);
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Price data is stale");
});
});

it("should set the last trade timestamp", async () => {
await subject();

Expand Down
2 changes: 2 additions & 0 deletions utils/deploys/deployExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export default class DeployExtensions {

public async deployAaveV3LeverageStrategyExtension(
manager: Address,
aaveOracle: Address,
contractSettings: AaveContractSettings,
methdologySettings: MethodologySettings,
executionSettings: ExecutionSettings,
Expand All @@ -380,6 +381,7 @@ export default class DeployExtensions {
): Promise<AaveV3LeverageStrategyExtension> {
return await new AaveV3LeverageStrategyExtension__factory(this._deployerSigner).deploy(
manager,
aaveOracle,
contractSettings,
methdologySettings,
executionSettings,
Expand Down

0 comments on commit bed0e34

Please sign in to comment.