Skip to content

Commit

Permalink
✨ debt-manager: support EIP-2612 permit
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jun 6, 2023
1 parent 04225f6 commit c4dc9d7
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-jokes-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/protocol": patch
---

✨ debt-manager: support `EIP-2612` permit
83 changes: 42 additions & 41 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,48 @@ AuditorTest:testEnableMarketAuditorMismatch() (gas: 24805)
AuditorTest:testEnableMarketShouldRevertWithInvalidPriceFeed() (gas: 149281)
AuditorTest:testEnterExitMarket() (gas: 178761)
AuditorTest:testExitMarketOwning() (gas: 177445)
DebtManagerTest:testApproveMaliciousMarket() (gas: 29119)
DebtManagerTest:testApproveMarket() (gas: 61610)
DebtManagerTest:testAvailableLiquidity() (gas: 100410)
DebtManagerTest:testBalancerFlashloanCallFromDifferentOrigin() (gas: 65654)
DebtManagerTest:testCallReceiveFlashLoanFromAnyAddress() (gas: 33485)
DebtManagerTest:testDeleverage() (gas: 461852)
DebtManagerTest:testDeleverageHalfBorrowPosition() (gas: 500975)
DebtManagerTest:testFixedDeleverage() (gas: 451803)
DebtManagerTest:testFixedRoll() (gas: 525733)
DebtManagerTest:testFixedRollSameMaturityWithThreeLoops() (gas: 400447)
DebtManagerTest:testFixedRollWithAccurateBorrowSlippage() (gas: 796123)
DebtManagerTest:testFixedRollWithAccurateBorrowSlippageWithThreeLoops() (gas: 1130235)
DebtManagerTest:testFixedRollWithAccurateRepaySlippage() (gas: 796083)
DebtManagerTest:testFixedRollWithAccurateRepaySlippageWithThreeLoops() (gas: 1124995)
DebtManagerTest:testFixedToFloatingRoll() (gas: 480252)
DebtManagerTest:testFixedToFloatingRollHigherThanAvailableLiquidity() (gas: 542869)
DebtManagerTest:testFixedToFloatingRollHigherThanAvailableLiquidityWithSlippage() (gas: 833198)
DebtManagerTest:testFixedToFloatingRollHigherThanAvailableLiquidityWithSlippageWithThreeLoops() (gas: 1004798)
DebtManagerTest:testFixedToFloatingRollWithAccurateSlippage() (gas: 680795)
DebtManagerTest:testFlashloanFeeGreaterThanZero() (gas: 429735)
DebtManagerTest:testFloatingToFixedRoll() (gas: 516165)
DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidity() (gas: 601795)
DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidityWithSlippage() (gas: 999755)
DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidityWithSlippageWithThreePools() (gas: 1208171)
DebtManagerTest:testFloatingToFixedRollWithAccurateSlippage() (gas: 806652)
DebtManagerTest:testFloatingToFixedRollWithAccurateSlippageWithPreviousPosition() (gas: 757550)
DebtManagerTest:testLateFixedDeleverage() (gas: 484949)
DebtManagerTest:testLateFixedRoll() (gas: 536412)
DebtManagerTest:testLateFixedRollWithThreeLoops() (gas: 720006)
DebtManagerTest:testLateFixedToFloatingRoll() (gas: 487641)
DebtManagerTest:testLateFixedToFloatingRollWithThreeLoops() (gas: 649144)
DebtManagerTest:testLeverage() (gas: 375538)
DebtManagerTest:testLeverageShouldFailWhenHealthFactorNearOne() (gas: 751995)
DebtManagerTest:testLeverageWithAlreadyDepositedAmount() (gas: 403934)
DebtManagerTest:testLeverageWithInvalidBalancerVault() (gas: 2751564)
DebtManagerTest:testMockBalancerVault() (gas: 4104898)
DebtManagerTest:testPartialFixedDeleverage() (gas: 525811)
DebtManagerTest:testPartialFixedRoll() (gas: 589054)
DebtManagerTest:testPartialFixedToFloatingRoll() (gas: 553117)
DebtManagerTest:testPartialLateFixedRoll() (gas: 581433)
DebtManagerTest:testPartialLateFixedToFloatingRoll() (gas: 551859)
DebtManagerTest:testApproveMaliciousMarket() (gas: 29117)
DebtManagerTest:testApproveMarket() (gas: 61608)
DebtManagerTest:testAvailableLiquidity() (gas: 100500)
DebtManagerTest:testBalancerFlashloanCallFromDifferentOrigin() (gas: 65633)
DebtManagerTest:testCallReceiveFlashLoanFromAnyAddress() (gas: 33487)
DebtManagerTest:testDeleverage() (gas: 461764)
DebtManagerTest:testDeleverageHalfBorrowPosition() (gas: 500887)
DebtManagerTest:testFixedDeleverage() (gas: 451787)
DebtManagerTest:testFixedRoll() (gas: 525758)
DebtManagerTest:testFixedRollSameMaturityWithThreeLoops() (gas: 400516)
DebtManagerTest:testFixedRollWithAccurateBorrowSlippage() (gas: 796127)
DebtManagerTest:testFixedRollWithAccurateBorrowSlippageWithThreeLoops() (gas: 1130239)
DebtManagerTest:testFixedRollWithAccurateRepaySlippage() (gas: 796087)
DebtManagerTest:testFixedRollWithAccurateRepaySlippageWithThreeLoops() (gas: 1124999)
DebtManagerTest:testFixedToFloatingRoll() (gas: 480272)
DebtManagerTest:testFixedToFloatingRollHigherThanAvailableLiquidity() (gas: 542894)
DebtManagerTest:testFixedToFloatingRollHigherThanAvailableLiquidityWithSlippage() (gas: 833202)
DebtManagerTest:testFixedToFloatingRollHigherThanAvailableLiquidityWithSlippageWithThreeLoops() (gas: 1004802)
DebtManagerTest:testFixedToFloatingRollWithAccurateSlippage() (gas: 680799)
DebtManagerTest:testFlashloanFeeGreaterThanZero() (gas: 429690)
DebtManagerTest:testFloatingToFixedRoll() (gas: 516123)
DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidity() (gas: 601753)
DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidityWithSlippage() (gas: 999625)
DebtManagerTest:testFloatingToFixedRollHigherThanAvailableLiquidityWithSlippageWithThreePools() (gas: 1208041)
DebtManagerTest:testFloatingToFixedRollWithAccurateSlippage() (gas: 806522)
DebtManagerTest:testFloatingToFixedRollWithAccurateSlippageWithPreviousPosition() (gas: 757420)
DebtManagerTest:testLateFixedDeleverage() (gas: 484933)
DebtManagerTest:testLateFixedRoll() (gas: 536460)
DebtManagerTest:testLateFixedRollWithThreeLoops() (gas: 720031)
DebtManagerTest:testLateFixedToFloatingRoll() (gas: 487661)
DebtManagerTest:testLateFixedToFloatingRollWithThreeLoops() (gas: 649169)
DebtManagerTest:testLeverage() (gas: 375539)
DebtManagerTest:testLeverageShouldFailWhenHealthFactorNearOne() (gas: 751905)
DebtManagerTest:testLeverageWithAlreadyDepositedAmount() (gas: 403912)
DebtManagerTest:testLeverageWithInvalidBalancerVault() (gas: 2936191)
DebtManagerTest:testMockBalancerVault() (gas: 4289437)
DebtManagerTest:testPartialFixedDeleverage() (gas: 525791)
DebtManagerTest:testPartialFixedRoll() (gas: 589035)
DebtManagerTest:testPartialFixedToFloatingRoll() (gas: 553142)
DebtManagerTest:testPartialLateFixedRoll() (gas: 581414)
DebtManagerTest:testPartialLateFixedToFloatingRoll() (gas: 551840)
DebtManagerTest:testPermitAndRollFloatingToFixed() (gas: 597947)
InterestRateModelTest:testFixedBorrowRate() (gas: 8089)
InterestRateModelTest:testFloatingBorrowRate() (gas: 6236)
InterestRateModelTest:testMinFixedRate() (gas: 7610)
Expand Down
83 changes: 79 additions & 4 deletions contracts/periphery/DebtManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ contract DebtManager is Initializable {
uint256 borrowMaturity,
uint256 maxBorrowAssets,
uint256 percentage
) external {
) public {
uint256[] memory amounts = new uint256[](1);
ERC20[] memory tokens = new ERC20[](1);
bytes[] memory calls;
Expand Down Expand Up @@ -152,7 +152,7 @@ contract DebtManager is Initializable {
uint256 repayMaturity,
uint256 maxRepayAssets,
uint256 percentage
) external {
) public {
uint256[] memory amounts = new uint256[](1);
ERC20[] memory tokens = new ERC20[](1);
bytes[] memory calls;
Expand Down Expand Up @@ -196,14 +196,14 @@ contract DebtManager is Initializable {
/// @param maxRepayAssets Max amount of debt that the account is willing to accept to be repaid.
/// @param maxBorrowAssets Max amount of debt that the sender is willing to accept to be borrowed.
/// @param percentage The percentage of the position that will be rolled, represented with 18 decimals.
function fixedRoll(
function rollFixed(
Market market,
uint256 repayMaturity,
uint256 borrowMaturity,
uint256 maxRepayAssets,
uint256 maxBorrowAssets,
uint256 percentage
) external {
) public {
uint256[] memory amounts = new uint256[](1);
ERC20[] memory tokens = new ERC20[](1);
bytes[] memory calls;
Expand Down Expand Up @@ -316,6 +316,73 @@ contract DebtManager is Initializable {
}
}

/// @notice Calls `token.permit` on behalf of `permit.account`.
/// @param token The `ERC20` to call `permit`.
/// @param p Arguments for the permit call.
modifier permit(
ERC20 token,
uint256 assets,
Permit calldata p
) {
token.permit(p.account, address(this), assets, p.deadline, p.v, p.r, p.s);
_;
}

/// @notice Rolls a percentage of the floating position of `msg.sender` to a fixed position
/// after calling `market.permit`.
/// @param market The Market to roll the position in.
/// @param borrowMaturity The maturity of the fixed pool that the position is being rolled to.
/// @param maxBorrowAssets Max amount of debt that the sender is willing to accept to be borrowed.
/// @param percentage The percentage of the position that will be rolled, represented with 18 decimals.
/// @param p Arguments for the permit call to `market` on behalf of `permit.account`.
function rollFloatingToFixed(
Market market,
uint256 borrowMaturity,
uint256 maxBorrowAssets,
uint256 percentage,
Permit calldata p
) external permit(market, maxBorrowAssets, p) {
rollFloatingToFixed(market, borrowMaturity, maxBorrowAssets, percentage);

This comment has been minimized.

Copy link
@eugenioclrc

eugenioclrc Jul 4, 2023

Permit from p.account its not being use anywhere... its just an alias for rollFixedToFloating with extra validation.

}

/// @notice Rolls a percentage of the fixed position of `msg.sender` to a floating position
/// after calling `market.permit`.
/// @param market The Market to roll the position in.
/// @param repayMaturity The maturity of the fixed pool that the position is being rolled from.
/// @param maxRepayAssets Max amount of debt that the account is willing to accept to be repaid.
/// @param percentage The percentage of the position that will be rolled, represented with 18 decimals.
/// @param p Arguments for the permit call to `market` on behalf of `permit.account`.
function rollFixedToFloating(
Market market,
uint256 repayMaturity,
uint256 maxRepayAssets,
uint256 percentage,
Permit calldata p
) external permit(market, maxRepayAssets, p) {
rollFixedToFloating(market, repayMaturity, maxRepayAssets, percentage);
}

/// @notice Rolls a percentage of the fixed position of `msg.sender` to another fixed pool
/// after calling `market.permit`.
/// @param market The Market to roll the position in.
/// @param repayMaturity The maturity of the fixed pool that the position is being rolled from.
/// @param borrowMaturity The maturity of the fixed pool that the position is being rolled to.
/// @param maxRepayAssets Max amount of debt that the account is willing to accept to be repaid.
/// @param maxBorrowAssets Max amount of debt that the sender is willing to accept to be borrowed.
/// @param percentage The percentage of the position that will be rolled, represented with 18 decimals.
/// @param p Arguments for the permit call to `market` on behalf of `permit.account`.
function rollFixed(
Market market,
uint256 repayMaturity,
uint256 borrowMaturity,
uint256 maxRepayAssets,
uint256 maxBorrowAssets,
uint256 percentage,
Permit calldata p
) external permit(market, maxBorrowAssets, p) {
rollFixed(market, repayMaturity, borrowMaturity, maxRepayAssets, maxBorrowAssets, percentage);
}

/// @notice Returns Balancer Vault's available liquidity of each enabled underlying asset.
function availableLiquidity() external view returns (AvailableAsset[] memory availableAssets) {
uint256 marketsCount = auditor.allMarkets().length;
Expand Down Expand Up @@ -345,6 +412,14 @@ contract DebtManager is Initializable {

error InvalidOperation();

struct Permit {
address account;
uint256 deadline;
uint8 v;
bytes32 r;
bytes32 s;
}

struct RollVars {
uint256 positionAssets;
uint256 repayAssets;
Expand Down
Loading

0 comments on commit c4dc9d7

Please sign in to comment.