Skip to content

Commit

Permalink
test: add successive testing for add and addLeverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cucupac committed Feb 2, 2024
1 parent d76bcc8 commit 15cbc05
Show file tree
Hide file tree
Showing 21 changed files with 433 additions and 262 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ The following outlines principles for core protocol funcitonality.

Logic:

- None for now.
- None at the moment🙂

Tests:

- [ ] Create integration test for multiple calls `add()`
- [ ] Create integration test for multiple calls `addLeverage()`
- [ ] Create integration test for multiple calls `addWithPermit()`
- [ ] Remove all test_ActiveFork
- None at the moment🙂

Considerations:

- None for now.
- None at the moment🙂
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
},
"scripts": {
"commit": "cz",
"coverage": "forge coverage --report lcov && npm run coverage:filter && npx lcov-summary lcov.info",
"coverage": "forge coverage --fork-url https://arbitrum.llamarpc.com --report lcov && npm run coverage:filter && npx lcov-summary lcov.info",
"coverage:filter": "lcov -r lcov.info 'test/*' 'src/dependencies/*' -o lcov.info",
"format": "forge fmt",
"format:check": "forge fmt --check",
"lint:check": "solhint --config ./.solhint.json 'src/**/*.sol'",
"prepare": "husky install",
"test": "forge test --optimize && npm run coverage"
"test": "forge test --fork-url https://arbitrum.llamarpc.com --optimize && npm run coverage"
},
"commitlint": {
"extends": [
Expand Down
7 changes: 5 additions & 2 deletions src/FeeCollector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ contract FeeCollector is Ownable {

// 2. Update client balances
if (_client != address(0)) {
balances[_client][_token] += _clientFee;
totalClientBalances[_token] += _clientFee;
// Cannot overflow because the sum of all client balances can't exceed the max uint256 value.
unchecked {
balances[_client][_token] += _clientFee;
totalClientBalances[_token] += _clientFee;
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract Position is DebtService, SwapService {
}

/**
* @notice Adds to this contract's short position.
* @notice Adds to this contract's position.
* @param _cAmt The amount of collateral token to be supplied for this transaction-specific loan (units: C_DECIMALS).
* @param _ltv The desired loan-to-value ratio for this transaction-specific loan (ex: 75 is 75%).
* @param _swapAmtOutMin The minimum amount of output tokens from swap for the tx to go through.
Expand Down Expand Up @@ -63,7 +63,7 @@ contract Position is DebtService, SwapService {
}

/**
* @notice Adds to this contract's short position with permit, obviating the need for a separate approve tx.
* @notice Adds to this contract's position with permit, obviating the need for a separate approve tx.
* This function can only be used for ERC-2612-compliant tokens.
* @param _cAmt The amount of collateral token to be supplied for this transaction-specific loan (units: C_DECIMALS).
* @param _ltv The desired loan-to-value ratio for this transaction-specific loan (ex: 75 is 75%).
Expand Down Expand Up @@ -94,7 +94,7 @@ contract Position is DebtService, SwapService {
}

/**
* @notice Adds leverage to this contract's short position. This function can only be used for positions where the
* @notice Adds leverage to this contract's position. This function can only be used for positions where the
* collateral token is the same as the base token.
* @param _ltv The desired loan-to-value ratio for this transaction-specific loan (ex: 75 is 75%).
* @param _swapAmtOutMin The minimum amount of output tokens from swap for the tx to go through.
Expand Down Expand Up @@ -123,7 +123,7 @@ contract Position is DebtService, SwapService {
}

/**
* @notice Fully closes the short position.
* @notice Fully closes the position.
* @param _poolFee The fee of the Uniswap pool.
* @param _exactOutput Whether to swap exact output or exact input (true for exact output, false for exact input).
* @param _swapAmtOutMin The minimum amount of output tokens from swap for the tx to go through (only used if _exactOutput is false, supply 0 if true).
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/IPosition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface IPosition {
**
******************************************************************************/
/**
* @notice Adds to this contract's short position.
* @notice Adds to this contract's position.
* @param _cAmt The amount of collateral token to be supplied for this transaction-specific loan (units: C_DECIMALS).
* @param _ltv The desired loan-to-value ratio for this transaction-specific loan (ex: 75 is 75%).
* @param _swapAmtOutMin The minimum amount of output tokens from swap for the tx to go through.
Expand All @@ -58,7 +58,7 @@ interface IPosition {
payable;

/**
* @notice Adds to this contract's short position with permit, obviating the need for a separate approve tx.
* @notice Adds to this contract's position with permit, obviating the need for a separate approve tx.
* This function can only be used for ERC-2612-compliant tokens.
* @param _cAmt The amount of collateral token to be supplied for this transaction-specific loan (units: C_DECIMALS).
* @param _ltv The desired loan-to-value ratio for this transaction-specific loan (ex: 75 is 75%).
Expand All @@ -83,7 +83,7 @@ interface IPosition {
) external payable;

/**
* @notice Adds leverage to this contract's short position. This function can only be used for positions where the
* @notice Adds leverage to this contract's position. This function can only be used for positions where the
* collateral token is the same as the base token.
* @param _ltv The desired loan-to-value ratio for this transaction-specific loan (ex: 75 is 75%).
* @param _swapAmtOutMin The minimum amount of output tokens from swap for the tx to go through.
Expand All @@ -93,7 +93,7 @@ interface IPosition {
function addLeverage(uint256 _ltv, uint256 _swapAmtOutMin, uint24 _poolFee, address _client) external payable;

/**
* @notice Fully closes the short position.
* @notice Fully closes the position.
* @param _poolFee The fee of the Uniswap pool.
* @param _exactOutput Whether to swap exact output or exact input (true for exact output, false for exact input).
* @param _swapAmtOutMin The minimum amount of output tokens from swap for the tx to go through (only used if _exactOutput is false, supply 0 if true).
Expand Down
11 changes: 0 additions & 11 deletions test/FeeCollector.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ contract FeeCollectorTest is Test, TokenUtils, FeeUtils {
TestPosition[] public positions;

// Test Storage
uint256 public mainnetFork;
address public positionOwner = address(this);
address public feeCollectorAddr;

function setUp() public {
// Setup: use mainnet fork
mainnetFork = vm.createFork(vm.envString("RPC_URL"));
vm.selectFork(mainnetFork);

// Deploy assets
assets = new Assets();
address[4] memory supportedAssets = assets.getSupported();
Expand Down Expand Up @@ -74,12 +69,6 @@ contract FeeCollectorTest is Test, TokenUtils, FeeUtils {
positions.push(newPosition);
}

/// @dev
// - The active fork should be the forked network created in the setup
function test_ActiveFork() public {
assertEq(vm.activeFork(), mainnetFork, "vm.activeFork() != mainnetFork");
}

/// @dev
// - The FeeCollector's feeToken balance should increase by _protocolFee
// - The feeToken totalClientBalances should increase by clientFee
Expand Down
13 changes: 1 addition & 12 deletions test/Position.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ contract PositionTest is Test, TokenUtils, DebtUtils {
// Test Storage
VmSafe.Wallet public wallet;
address public positionAddr;
uint256 public mainnetFork;
address public owner;

function setUp() public {
// Setup: use mainnet fork
mainnetFork = vm.createFork(vm.envString("RPC_URL"));
vm.selectFork(mainnetFork);

// Set contract owner
wallet = vm.createWallet(uint256(keccak256(abi.encodePacked(uint256(1)))));
owner = wallet.addr;
Expand Down Expand Up @@ -98,12 +93,6 @@ contract PositionTest is Test, TokenUtils, DebtUtils {
}
}

/// @dev
// - The active fork should be the forked network created in the setup
function test_ActiveFork() public {
assertEq(vm.activeFork(), mainnetFork, "vm.activeFork() != mainnetFork");
}

/// @dev
// - It should revert with Unauthorized() error when called by an unauthorized sender.
function testFuzz_CannotAdd(address _sender) public {
Expand Down Expand Up @@ -249,7 +238,7 @@ contract PositionTest is Test, TokenUtils, DebtUtils {
// Test variables
address addr = positions[i].addr;

// Setup: open short position
// Setup: open position
uint256 cAmt = assets.maxCAmts(positions[i].cToken);
_fund(owner, positions[i].cToken, cAmt);
vm.startPrank(owner);
Expand Down
11 changes: 0 additions & 11 deletions test/PositionFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ contract PositionFactoryTest is Test, TokenUtils {
Assets public assets;

// Test Storage
uint256 public mainnetFork;
address public positionOwner = address(this);

// Errors
Expand All @@ -29,21 +28,11 @@ contract PositionFactoryTest is Test, TokenUtils {
event PositionCreated(address indexed owner, address indexed position);

function setUp() public {
// Setup: use mainnet fork
mainnetFork = vm.createFork(vm.envString("RPC_URL"));
vm.selectFork(mainnetFork);

vm.prank(CONTRACT_DEPLOYER);
positionFactory = new PositionFactory(CONTRACT_DEPLOYER);
assets = new Assets();
}

/// @dev
// - The active fork should be the forked network created in the setup
function test_ActiveFork() public {
assertEq(vm.activeFork(), mainnetFork, "vm.activeFork() != mainnetFork");
}

/// @dev
// - It should create a Position contract for each possible permutation of cToken, bToken, and bToken.
function test_CreatePosition() public {
Expand Down
10 changes: 6 additions & 4 deletions test/common/Constants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ uint256 constant WITHDRAW_BUFFER = 100_000;
uint256 constant REPAY_BUFFER = 2;
uint256 constant PROTOCOL_FEE_RATE = 3;
uint256 constant CLIENT_RATE = 30;
uint256 constant CLIENT_TAKE_RATE = 50;
uint256 constant SUCCESSIVE_ITERATIONS = 5;

contract Assets {
address[4] public supported = [USDC, DAI, WETH, WBTC];
Expand All @@ -43,10 +45,10 @@ contract Assets {
decimals[WBTC] = 8;

// Set max collateral amounts
maxCAmts[USDC] = 1_000 * 10 ** 6;
maxCAmts[DAI] = 100_000 * 10 ** 18;
maxCAmts[WETH] = 50 * 10 ** 18;
maxCAmts[WBTC] = 2 * 10 ** 8;
maxCAmts[USDC] = 10_000 * 10 ** 6;
maxCAmts[DAI] = 10_000 * 10 ** 18;
maxCAmts[WETH] = 25 * 10 ** 18;
maxCAmts[WBTC] = 1 * 10 ** 8;

// Set min collateral amounts
minCAmts[USDC] = 100 * 10 ** 6;
Expand Down
15 changes: 2 additions & 13 deletions test/integration/FeeCollector.add.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,9 @@ contract FeeCollectorAddTest is Test, TokenUtils, FeeUtils, DebtUtils {
TestPosition[] public positions;

// Test Storage
uint256 public mainnetFork;
address public positionOwner = address(this);

function setUp() public {
// Setup: use mainnet fork
mainnetFork = vm.createFork(vm.envString("RPC_URL"));
vm.selectFork(mainnetFork);

// Deploy assets
assets = new Assets();
address[4] memory supportedAssets = assets.getSupported();
Expand Down Expand Up @@ -102,12 +97,6 @@ contract FeeCollectorAddTest is Test, TokenUtils, FeeUtils, DebtUtils {
}
}

/// @dev
// - The active fork should be the forked network created in the setup
function test_ActiveFork() public {
assertEq(vm.activeFork(), mainnetFork, "vm.activeFork() != mainnetFork");
}

/// @dev
// - The FeeCollector's cToken balance should increase by (maxFee - userSavings).
// - The cToken amount supplied as collateral should be cAmt - (maxFee - userSavings).
Expand Down Expand Up @@ -150,7 +139,7 @@ contract FeeCollectorAddTest is Test, TokenUtils, FeeUtils, DebtUtils {
uint256 prePositionATokenBal = _getATokenBalance(positionAddr, cToken);
assertEq(prePositionATokenBal, 0);

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

// Post-act balances
Expand Down Expand Up @@ -204,7 +193,7 @@ contract FeeCollectorAddTest is Test, TokenUtils, FeeUtils, DebtUtils {
uint256 prePositionATokenBal = _getATokenBalance(positionAddr, cToken);
assertEq(prePositionATokenBal, 0);

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

// Post-act balances
Expand Down
11 changes: 0 additions & 11 deletions test/integration/FeeCollector.addLeverage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ contract FeeCollectorAddLeverageTest is Test, TokenUtils, DebtUtils, FeeUtils {

// Test Storage
address public positionAddr;
uint256 public mainnetFork;

function setUp() public {
// Setup: use mainnet fork
mainnetFork = vm.createFork(vm.envString("RPC_URL"));
vm.selectFork(mainnetFork);

// Deploy assets
assets = new Assets();
address[4] memory supportedAssets = assets.getSupported();
Expand Down Expand Up @@ -109,12 +104,6 @@ contract FeeCollectorAddLeverageTest is Test, TokenUtils, DebtUtils, FeeUtils {
}
}

/// @dev
// - The active fork should be the forked network created in the setup
function test_ActiveFork() public {
assertEq(vm.activeFork(), mainnetFork, "vm.activeFork() != mainnetFork");
}

/// @dev
// - The FeeCollector's feeToken balance should increase by (maxFee - userSavings).
// - The feeToken amount supplied as collateral should be _bAmt - (maxFee - userSavings).
Expand Down
11 changes: 0 additions & 11 deletions test/integration/FeeCollector.clientFees.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ contract FeeCollectorClientFeesTest is Test, TokenUtils {

// Test Storage
address[] public supportedAssets;
uint256 public mainnetFork;
address public owner = address(this);
address public feeCollectorAddr;

function setUp() public {
// Setup: use mainnet fork
mainnetFork = vm.createFork(vm.envString("RPC_URL"));
vm.selectFork(mainnetFork);

// Deploy assets
assets = new Assets();
supportedAssets = assets.getSupported();
Expand All @@ -38,12 +33,6 @@ contract FeeCollectorClientFeesTest is Test, TokenUtils {
feeCollectorAddr = address(feeCollector);
}

/// @dev
// - The active fork should be the forked network created in the setup
function test_ActiveFork() public {
assertEq(vm.activeFork(), mainnetFork, "vm.activeFork() != mainnetFork");
}

/// @dev
// - The sum of all client fees should equal totalClientBalances
// - The expectedClientFee should always be equal to or 1 less than clientFee, due to integer division
Expand Down
Loading

0 comments on commit 15cbc05

Please sign in to comment.