Skip to content

Commit

Permalink
fixing overflow apr (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeNervoXS authored May 11, 2022
1 parent 664dadc commit a8604a3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,8 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
if (_totalAssets == 0 || totalVariableDebt == 0 || _aToken.totalSupply() == 0) return 0;

(uint256 deposits, uint256 borrows) = getCurrentPosition();
uint256 yearlyRewardsATokenInUSDC;
uint256 yearlyRewardsDebtTokenInUSDC;
uint256 yearlyRewardsATokenInUSD;
uint256 yearlyRewardsDebtTokenInUSD;
{
uint256 stkAavePriceInWant = _estimatedStkAaveToWant(1 ether);
(uint256 emissionPerSecondAToken, , ) = (_aToken.getIncentivesController()).assets(address(_aToken));
Expand All @@ -863,21 +863,21 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower

uint256 yearlyEmissionsAToken = emissionPerSecondAToken * 60 * 60 * 24 * 365; // BASE: 18
uint256 yearlyEmissionsDebtToken = emissionPerSecondDebtToken * 60 * 60 * 24 * 365; // BASE: 18
yearlyRewardsATokenInUSDC = ((deposits * yearlyEmissionsAToken * stkAavePriceInWant * 10**9) /
_aToken.totalSupply()); // BASE 27 + want
yearlyRewardsDebtTokenInUSDC = ((borrows * yearlyEmissionsDebtToken * stkAavePriceInWant * 10**9) /
totalVariableDebt); // BASE 27 + want
yearlyRewardsATokenInUSD =
((deposits * yearlyEmissionsAToken) / _aToken.totalSupply()) *
stkAavePriceInWant; // BASE 18 + want
yearlyRewardsDebtTokenInUSD =
((borrows * yearlyEmissionsDebtToken) / totalVariableDebt) *
stkAavePriceInWant; // BASE 18 + want
}

return
(liquidityRate *
deposits +
yearlyRewardsATokenInUSDC +
yearlyRewardsDebtTokenInUSDC -
variableBorrowRate *
borrows) /
_totalAssets /
10**9; // BASE 18
((liquidityRate * deposits) /
10**9 +
yearlyRewardsATokenInUSD +
yearlyRewardsDebtTokenInUSD -
(variableBorrowRate * borrows) /
10**9) / _totalAssets; // BASE 18
}

/// @notice Returns the `want` balance
Expand Down
2 changes: 1 addition & 1 deletion deploy/StETHStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const func: DeployFunction = async ({ deployments, ethers }) => {
curvePool,
wETH,
stETH,
parseUnits('3.9', 9),
parseUnits('3.9', 16),
]);

const proxyStrategy = await deploy('StETHStrategy', {
Expand Down
34 changes: 20 additions & 14 deletions test/strategyAaveFlashLoan/aaveFlashloanStrategyCoverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
PoolManager,
IAaveIncentivesController,
} from '../../typechain';
import { logBN } from '../utils-interaction';
import { setDaiBalanceFor } from './aaveFlashloanStrategy_random_DAI.test';
import { parseUnits } from 'ethers/lib/utils';

describe('AaveFlashloan Strat - Coverage', () => {
Expand All @@ -37,13 +39,12 @@ describe('AaveFlashloan Strat - Coverage', () => {
let poolManager: PoolManager;
let incentivesController: IAaveIncentivesController;
let flashMintLib: FlashMintLib;

let strategy: AaveFlashloanStrategy;

// ReserveInterestRateStrategy for USDC
const reserveInterestRateStrategyUSDC = '0x8Cae0596bC1eD42dc3F04c4506cfe442b3E74e27';
// const reserveInterestRateStrategy = '0x8Cae0596bC1eD42dc3F04c4506cfe442b3E74e27';
// ReserveInterestRateStrategy for DAI
// const reserveInterestRateStrategyDAI = '0xfffE32106A68aA3eD39CcCE673B646423EEaB62a';
const reserveInterestRateStrategy = '0xfffE32106A68aA3eD39CcCE673B646423EEaB62a';

beforeEach(async () => {
await network.provider.request({
Expand All @@ -58,7 +59,10 @@ describe('AaveFlashloan Strat - Coverage', () => {
],
});

wantToken = (await ethers.getContractAt(ERC20__factory.abi, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')) as ERC20;
// const tokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const tokenAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F';

wantToken = (await ethers.getContractAt(ERC20__factory.abi, tokenAddress)) as ERC20;
aave = (await ethers.getContractAt(ERC20__factory.abi, '0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9')) as ERC20;
stkAave = (await ethers.getContractAt(
IStakedAave__factory.abi,
Expand Down Expand Up @@ -89,28 +93,26 @@ describe('AaveFlashloan Strat - Coverage', () => {
]);
strategy = new Contract(proxy.address, AaveFlashloanStrategy__factory.abi, deployer) as AaveFlashloanStrategy;

await strategy.initialize(
poolManager.address,
reserveInterestRateStrategyUSDC,
governor.address,
guardian.address,
[keeper.address],
);
await strategy.initialize(poolManager.address, reserveInterestRateStrategy, governor.address, guardian.address, [
keeper.address,
]);

aToken = (await ethers.getContractAt(ERC20__factory.abi, '0xBcca60bB61934080951369a648Fb03DF4F96263C')) as ERC20;
debtToken = (await ethers.getContractAt(ERC20__factory.abi, '0x619beb58998eD2278e08620f97007e1116D5D25b')) as ERC20;
});

describe('Strategy', () => {
const _startAmountUSDC = utils.parseUnits((2_000_000).toString(), 6);
const _startAmount = 1_000_000_000;

beforeEach(async () => {
await (await poolManager.addStrategy(strategy.address, utils.parseUnits('0.75', 9))).wait();

await impersonate('0x6262998Ced04146fA42253a5C0AF90CA02dfd2A3', async acc => {
await wantToken.connect(acc).transfer(user.address, _startAmountUSDC);
await wantToken.connect(acc).transfer(user.address, _startAmount);
});

await setDaiBalanceFor(user.address, _startAmount);

// sending funds to emission controller
await network.provider.send('hardhat_setBalance', [
'0xEE56e2B3D491590B5b31738cC34d5232F378a8D5',
Expand All @@ -123,7 +125,7 @@ describe('AaveFlashloan Strat - Coverage', () => {
utils.parseEther('100').toHexString().replace('0x0', '0x'),
]);

await wantToken.connect(user).transfer(poolManager.address, _startAmountUSDC);
await wantToken.connect(user).transfer(poolManager.address, _startAmount);

await strategy.connect(keeper)['harvest()']({ gasLimit: 3e6 });
});
Expand Down Expand Up @@ -250,5 +252,9 @@ describe('AaveFlashloan Strat - Coverage', () => {

expect(aaveBalanceAfterRedeem).to.be.closeTo(aaveBalanceBefore, 0.1);
});
it('estimatedAPR', async () => {
const estimatedAPR = await strategy.estimatedAPR();
expect(estimatedAPR).to.be.closeTo(parseUnits('0.0406', 18), parseUnits('0.005', 18));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { network } from 'hardhat';
import { utils } from 'ethers';
import { setup } from './setup_tests';

async function setDaiBalanceFor(account: string, amount: number) {
export async function setDaiBalanceFor(account: string, amount: number) {
const balanceStorage = utils.solidityKeccak256(['uint256', 'uint256'], [account, 2]);

await network.provider.send('hardhat_setStorageAt', [
Expand Down

0 comments on commit a8604a3

Please sign in to comment.