Skip to content

Commit

Permalink
chore: requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cucupac committed Feb 2, 2024
1 parent 15cbc05 commit f40b096
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 54 deletions.
7 changes: 7 additions & 0 deletions src/interfaces/IFeeCollector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ interface IFeeCollector {
*/
function balances(address _client, address _token) external view returns (uint256);

/**
* @notice Returns the take rate of the specified client operator; the percentage of the client rate that the operator keeps.
* @param _client A client operator address.
* @return clientTakeRate The percentage of the client rate that the operator keeps.
*/
function clientTakeRates(address _client) external returns (uint256);

/**
* @notice Collects fees from Position contracts when collateral is added.
* @param _client The address where a client operator will receive protocols fees.
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/IPosition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ interface IPosition {
*/
function B_TOKEN() external returns (address);

/**
* @notice Returns the address of the FeeCollector contract, which is responsible for collecting and allocating protocol fees.
*/
function FEE_COLLECTOR() external returns (address);

/**
* @notice Returns the maximum percentage of the collateral token that the protocol charges each revenue-generating transaction.
*/
function PROTOCOL_FEE_RATE() external returns (uint256);

/**
* @notice Returns the number of decimals for this position's collateral token.
*/
Expand Down
6 changes: 2 additions & 4 deletions test/Position.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DAI,
FEE_COLLECTOR,
TEST_CLIENT,
TEST_LTV,
TEST_POOL_FEE,
USDC,
WITHDRAW_BUFFER
Expand Down Expand Up @@ -225,9 +226,6 @@ contract PositionTest is Test, TokenUtils, DebtUtils {
/// @dev
// - It should revert with Unauthorized() error when called by an unauthorized sender.
function testFuzz_CannotClose(address _sender) public {
// Setup
uint256 ltv = 50;

// Assumptions
vm.assume(_sender != owner);

Expand All @@ -243,7 +241,7 @@ contract PositionTest is Test, TokenUtils, DebtUtils {
_fund(owner, positions[i].cToken, cAmt);
vm.startPrank(owner);
IERC20(positions[i].cToken).approve(addr, cAmt);
IPosition(addr).add(cAmt, ltv, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(addr).add(cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);
vm.stopPrank();

// Act
Expand Down
1 change: 1 addition & 0 deletions test/common/Constants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ uint256 constant PROTOCOL_FEE_RATE = 3;
uint256 constant CLIENT_RATE = 30;
uint256 constant CLIENT_TAKE_RATE = 50;
uint256 constant SUCCESSIVE_ITERATIONS = 5;
uint256 constant TEST_LTV = 50;

contract Assets {
address[4] public supported = [USDC, DAI, WETH, WBTC];
Expand Down
5 changes: 3 additions & 2 deletions test/integration/FeeCollector.add.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FEE_COLLECTOR,
PROTOCOL_FEE_RATE,
TEST_CLIENT,
TEST_LTV,
TEST_POOL_FEE,
USDC,
WBTC,
Expand Down Expand Up @@ -140,7 +141,7 @@ contract FeeCollectorAddTest is Test, TokenUtils, FeeUtils, DebtUtils {
assertEq(prePositionATokenBal, 0);

// Act: increase position
IPosition(positionAddr).add(_cAmt, 50, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(positionAddr).add(_cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Post-act balances
feeCollectorBalances.postFeeTokenBal = IERC20(cToken).balanceOf(FEE_COLLECTOR);
Expand Down Expand Up @@ -194,7 +195,7 @@ contract FeeCollectorAddTest is Test, TokenUtils, FeeUtils, DebtUtils {
assertEq(prePositionATokenBal, 0);

// Act: increase position
IPosition(positionAddr).add(_cAmt, 50, 0, TEST_POOL_FEE, address(0));
IPosition(positionAddr).add(_cAmt, TEST_LTV, 0, TEST_POOL_FEE, address(0));

// Post-act balances
feeCollectorBalances.postFeeTokenBal = IERC20(cToken).balanceOf(FEE_COLLECTOR);
Expand Down
1 change: 0 additions & 1 deletion test/integration/FeeCollector.clientFees.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ contract FeeCollectorClientFeesTest is Test, TokenUtils {

// Setup
address feeToken = supportedAssets[i];
// uint256 clientTakeRate = 50;

// Set client rate
vm.prank(CONTRACT_DEPLOYER);
Expand Down
18 changes: 11 additions & 7 deletions test/integration/Position.add.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ contract PositionAddTest is Test, TokenUtils, DebtUtils, FeeUtils {
assertEq(positionBalances.preAToken, 0);
assertEq(positionBalances.preVDToken, 0);

// Approve Position contract to spend collateral
IERC20(p.cToken).approve(p.addr, _cAmt);

// Act
vm.recordLogs();
IERC20(p.cToken).approve(p.addr, _cAmt);
IPosition(p.addr).add(_cAmt, _ltv, 0, TEST_POOL_FEE, TEST_CLIENT);

// Post-act balances
Expand Down Expand Up @@ -213,9 +215,9 @@ contract PositionAddTest is Test, TokenUtils, DebtUtils, FeeUtils {
}

/// @dev
// - The Position contract's bToken balance should increase by total bAmt receieved from swap across all add actions.
// - The Position contract's aToken balance should increase by emitted cAmt across all add actions.
// - The Position contract's variableDebtToken balance should increase by total dAmt received from borrow across all add actions.
// - The Position contract's bToken balance should increase by the sum of `bAmt`s receieved from swaps across all add actions.
// - The Position contract's aToken balance should increase by the sum of emitted `cAmt`s across all add actions.
// - The Position contract's variableDebtToken balance should increase by the sum of `dAmt`s received from borrowing across all add actions.
// - The above should be true for a wide range of LTVs.
// - The above should be true for a wide range of collateral amounts.
// - The above should be true for all supported tokens.
Expand Down Expand Up @@ -255,9 +257,11 @@ contract PositionAddTest is Test, TokenUtils, DebtUtils, FeeUtils {
// Fund owner with collateral
_fund(owner, p.cToken, _cAmt);

// Approve Position contract to spend collateral
IERC20(p.cToken).approve(p.addr, _cAmt);

// Act
vm.recordLogs();
IERC20(p.cToken).approve(p.addr, _cAmt);
IPosition(p.addr).add(_cAmt, _ltv, 0, TEST_POOL_FEE, TEST_CLIENT);

// Retrieve bAmt and dAmt from Add event
Expand Down Expand Up @@ -296,8 +300,8 @@ contract PositionAddTest is Test, TokenUtils, DebtUtils, FeeUtils {

// Assertions
assertEq(positionBalances.postBToken, sums.bAmt);
/// @dev The max delta per iteration is 1. Therefore, the max
// delta for all iterations is the number of iterations.
/// @dev Due to Aave interest, the max delta per iteration is 1.
// Therefore, the max delta for all iterations is the number of iterations.
assertApproxEqAbs(positionBalances.postAToken, sums.cAmt, SUCCESSIVE_ITERATIONS);
assertApproxEqAbs(positionBalances.postVDToken, sums.dAmt, SUCCESSIVE_ITERATIONS);

Expand Down
34 changes: 21 additions & 13 deletions test/integration/Position.addLeverage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import {
Assets,
AAVE_POOL,
AAVE_ORACLE,
CLIENT_RATE,
CLIENT_TAKE_RATE,
CONTRACT_DEPLOYER,
DAI,
FEE_COLLECTOR,
SUCCESSIVE_ITERATIONS,
TEST_CLIENT,
TEST_LTV,
TEST_POOL_FEE,
USDC,
WBTC
Expand All @@ -26,6 +29,7 @@ import { IAaveOracle } from "src/interfaces/aave/IAaveOracle.sol";
import { IPosition } from "src/interfaces/IPosition.sol";
import { IERC20 } from "src/interfaces/token/IERC20.sol";
import { IPool } from "src/interfaces/aave/IPool.sol";
import { IFeeCollector } from "src/interfaces/IFeeCollector.sol";

contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
/* solhint-disable func-name-mixedcase */
Expand All @@ -44,8 +48,6 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
uint256 postVDToken;
uint256 preAToken;
uint256 postAToken;
uint256 cTotalUSD;
uint256 dTotalUSD;
}

struct SuccessiveSums {
Expand Down Expand Up @@ -80,6 +82,14 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
vm.prank(CONTRACT_DEPLOYER);
deployCodeTo("FeeCollector.sol", abi.encode(CONTRACT_DEPLOYER), FEE_COLLECTOR);

// Set client rate
vm.prank(CONTRACT_DEPLOYER);
IFeeCollector(FEE_COLLECTOR).setClientRate(CLIENT_RATE);

// Set client take rate
vm.prank(TEST_CLIENT);
IFeeCollector(FEE_COLLECTOR).setClientTakeRate(CLIENT_TAKE_RATE);

// Deploy PositionFactory
vm.prank(CONTRACT_DEPLOYER);
positionFactory = new PositionFactory(CONTRACT_DEPLOYER);
Expand Down Expand Up @@ -144,7 +154,7 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
IERC20(p.cToken).approve(p.addr, _cAmt);

// Add initial position
IPosition(p.addr).add(_cAmt, 50, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(p.addr).add(_cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Pre-act balances
positionBalances.preBToken = IERC20(p.bToken).balanceOf(p.addr);
Expand Down Expand Up @@ -188,7 +198,7 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
// - The Position contract's variable debt token balance should increase by dAmt received from borrow across all add actions
// - The above should be true for a large range of LTVs and cAmts.
// - The above should be true for all positions where the collateral token is the same as the base token.
function testFuzz_AddLeverageSuccessive(uint256 _cAmt, uint256 _time) public {
function testFuzz_AddLeverageSuccessive(uint256 _ltv, uint256 _cAmt, uint256 _time) public {
// Setup
PositionBalances memory positionBalances;
TestPosition memory p;
Expand All @@ -206,15 +216,15 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
p.bToken = positions[i].bToken;

// Bound fuzzed variables
// _ltv = bound(_ltv, 1, 60);
_ltv = bound(_ltv, 1, 60);
_cAmt = bound(_cAmt, assets.minCAmts(p.cToken), assets.maxCAmts(p.cToken));

// Fund owner with collateral
_fund(owner, p.cToken, _cAmt);

// Add initial position
IERC20(p.cToken).approve(p.addr, _cAmt);
IPosition(p.addr).add(_cAmt, 50, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(p.addr).add(_cAmt, _ltv, 0, TEST_POOL_FEE, TEST_CLIENT);

/// @dev Initial balances to refect inital add
uint256 bAmtEndState;
Expand All @@ -230,14 +240,12 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
bool shallowLiquidity = (p.bToken == DAI && p.dToken == WBTC) || (p.bToken == WBTC && p.dToken == DAI);
if (!shallowLiquidity) {
for (uint256 j; j < SUCCESSIVE_ITERATIONS; j++) {
_time = bound(_time, 1 minutes, 2 minutes);

(positionBalances.cTotalUSD, positionBalances.dTotalUSD,,,,) =
IPool(AAVE_POOL).getUserAccountData(p.addr);
// Bound fuzzed variables
_time = bound(_time, 1 minutes, 12 weeks);

// Act
vm.recordLogs();
IPosition(p.addr).addLeverage(50, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(p.addr).addLeverage(TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Retrieve bAmt and dAmt from AddLeverage event
VmSafe.Log[] memory entries = vm.getRecordedLogs();
Expand Down Expand Up @@ -276,8 +284,8 @@ contract PositionAddLeverageTest is Test, TokenUtils, DebtUtils {
// Assertions

assertEq(positionBalances.postBToken, bAmtEndState);
/// @dev The max delta per iteration is 1. Therefore, the max
// delta for all iterations is the number of iterations.
/// @dev Due to Aave interest, the max delta per iteration is 1.
// Therefore, the max delta for all iterations is the number of iterations.
assertApproxEqAbs(positionBalances.postAToken, sums.cAmt, SUCCESSIVE_ITERATIONS);
assertApproxEqAbs(positionBalances.postVDToken, sums.dAmt, SUCCESSIVE_ITERATIONS);
}
Expand Down
13 changes: 5 additions & 8 deletions test/integration/Position.close.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
REPAY_PERCENT,
SWAP_ROUTER,
TEST_CLIENT,
TEST_LTV,
TEST_POOL_FEE,
USDC,
WITHDRAW_BUFFER
Expand Down Expand Up @@ -129,10 +130,9 @@ contract PositionCloseTest is Test, TokenUtils, DebtUtils {

// Setup: open position
uint256 cAmt = assets.maxCAmts(positions[i].cToken);
uint256 ltv = 50;
_fund(owner, positions[i].cToken, cAmt);
IERC20(positions[i].cToken).approve(addr, cAmt);
IPosition(addr).add(cAmt, ltv, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(addr).add(cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Get pre-act balances
contractBalances.preBToken = IERC20(positions[i].bToken).balanceOf(addr);
Expand Down Expand Up @@ -212,10 +212,9 @@ contract PositionCloseTest is Test, TokenUtils, DebtUtils {

// Setup: open position
uint256 cAmt = assets.maxCAmts(positions[i].cToken);
uint256 ltv = 50;
_fund(owner, positions[i].cToken, cAmt);
IERC20(positions[i].cToken).approve(addr, cAmt);
IPosition(addr).add(cAmt, ltv, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(addr).add(cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Get pre-act balances
contractBalances.preBToken = IERC20(positions[i].bToken).balanceOf(addr);
Expand Down Expand Up @@ -303,10 +302,9 @@ contract PositionCloseTest is Test, TokenUtils, DebtUtils {

// Setup: open position
uint256 cAmt = assets.maxCAmts(positions[i].cToken);
uint256 ltv = 50;
_fund(owner, positions[i].cToken, cAmt);
IERC20(positions[i].cToken).approve(addr, cAmt);
IPosition(addr).add(cAmt, ltv, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(addr).add(cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Get pre-act balances
contractBalances.preBToken = IERC20(positions[i].bToken).balanceOf(addr);
Expand Down Expand Up @@ -371,10 +369,9 @@ contract PositionCloseTest is Test, TokenUtils, DebtUtils {

// Setup: open position
uint256 cAmt = assets.maxCAmts(positions[i].cToken);
uint256 ltv = 50;
_fund(owner, positions[i].cToken, cAmt);
IERC20(positions[i].cToken).approve(addr, cAmt);
IPosition(addr).add(cAmt, ltv, 0, TEST_POOL_FEE, TEST_CLIENT);
IPosition(addr).add(cAmt, TEST_LTV, 0, TEST_POOL_FEE, TEST_CLIENT);

// Get pre-act balances
contractBalances.preVDToken = _getVariableDebtTokenBalance(addr, positions[i].dToken);
Expand Down
Loading

0 comments on commit f40b096

Please sign in to comment.