Skip to content

Commit

Permalink
Merge pull request #14 from clober-dex/feat/deposit-controller
Browse files Browse the repository at this point in the history
Feat/deposit controller
  • Loading branch information
JhChoy authored Nov 30, 2023
2 parents 0e2f406 + 2aca1de commit 9c9ffb1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 71 deletions.
6 changes: 3 additions & 3 deletions contracts/BorrowController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ contract BorrowController is IBorrowController, Controller, IPositionLocker {
Epoch expiredWith,
SwapParams calldata swapParams,
ERC20PermitParams calldata collateralPermitParams
) external payable nonReentrant wrapETH {
) external payable nonReentrant wrapETH returns (uint256 positionId) {
collateralPermitParams.tryPermit(_getUnderlyingToken(collateralToken), msg.sender, address(this));

bytes memory lockData = abi.encode(collateralAmount, debtAmount, expiredWith, maxPayInterest);
lockData = abi.encode(0, msg.sender, swapParams, abi.encode(collateralToken, debtToken, lockData));
bytes memory result = _loanPositionManager.lock(lockData);
uint256 positionId = abi.decode(result, (uint256));
positionId = abi.decode(result, (uint256));

_burnAllSubstitute(collateralToken, msg.sender);
_burnAllSubstitute(debtToken, msg.sender);
_loanPositionManager.transferFrom(address(this), msg.sender, positionId);
}

function adjustPosition(
function adjust(
uint256 positionId,
uint256 collateralAmount,
uint256 debtAmount,
Expand Down
37 changes: 13 additions & 24 deletions contracts/DepositController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,46 +84,35 @@ contract DepositController is IDepositController, Controller, IPositionLocker {
function deposit(
address asset,
uint256 amount,
uint16 lockEpochs,
Epoch expiredWith,
int256 minEarnInterest,
ERC20PermitParams calldata tokenPermitParams
) external payable nonReentrant wrapETH {
) external payable nonReentrant wrapETH returns (uint256 positionId) {
tokenPermitParams.tryPermit(_getUnderlyingToken(asset), msg.sender, address(this));
bytes memory lockData = abi.encode(amount, EpochLibrary.current().add(lockEpochs - 1), -minEarnInterest);
bytes memory lockData = abi.encode(amount, expiredWith, -minEarnInterest);
bytes memory result = _bondPositionManager.lock(abi.encode(0, msg.sender, abi.encode(asset, lockData)));
uint256 id = abi.decode(result, (uint256));
positionId = abi.decode(result, (uint256));

_burnAllSubstitute(asset, msg.sender);

_bondPositionManager.transferFrom(address(this), msg.sender, id);
_bondPositionManager.transferFrom(address(this), msg.sender, positionId);
}

function withdraw(
function adjust(
uint256 positionId,
uint256 withdrawAmount,
int256 maxPayInterest,
uint256 amount,
Epoch expiredWith,
int256 interestThreshold,
ERC20PermitParams calldata tokenPermitParams,
PermitSignature calldata positionPermitParams
) external nonReentrant onlyPositionOwner(positionId) {
) external payable nonReentrant wrapETH onlyPositionOwner(positionId) {
positionPermitParams.tryPermit(_bondPositionManager, positionId, address(this));
BondPosition memory position = _bondPositionManager.getPosition(positionId);
tokenPermitParams.tryPermit(position.asset, msg.sender, address(this));

bytes memory lockData = abi.encode(position.amount - withdrawAmount, position.expiredWith, maxPayInterest);
bytes memory lockData = abi.encode(amount, expiredWith, interestThreshold);
_bondPositionManager.lock(abi.encode(positionId, msg.sender, lockData));

_burnAllSubstitute(position.asset, msg.sender);
}

function collect(uint256 positionId, PermitSignature calldata positionPermitParams)
external
nonReentrant
onlyPositionOwner(positionId)
{
positionPermitParams.tryPermit(_bondPositionManager, positionId, address(this));
BondPosition memory position = _bondPositionManager.getPosition(positionId);
if (position.expiredWith >= EpochLibrary.current()) revert NotExpired();

_bondPositionManager.lock(abi.encode(positionId, msg.sender, abi.encode(0, position.expiredWith, 0, 0)));

_burnAllSubstitute(position.asset, msg.sender);
}
}
5 changes: 2 additions & 3 deletions contracts/interfaces/IBorrowController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface IBorrowController is IController {
}

error CollateralSwapFailed(string reason);
error InvalidDebtAmount();

function borrow(
address collateralToken,
Expand All @@ -25,9 +24,9 @@ interface IBorrowController is IController {
Epoch expiredWith,
SwapParams calldata swapParams,
ERC20PermitParams calldata collateralPermitParams
) external payable;
) external payable returns (uint256 positionId);

function adjustPosition(
function adjust(
uint256 positionId,
uint256 collateralAmount,
uint256 debtAmount,
Expand Down
19 changes: 9 additions & 10 deletions contracts/interfaces/IDepositController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ pragma solidity ^0.8.0;

import {IController} from "./IController.sol";
import {ERC20PermitParams, PermitSignature} from "../libraries/PermitParams.sol";
import {Epoch} from "../libraries/Epoch.sol";

interface IDepositController is IController {
error NotExpired();

function deposit(
address token,
uint256 amount,
uint16 lockEpochs,
Epoch expiredWith,
int256 minEarnInterest,
ERC20PermitParams calldata tokenPermitParams
) external payable;
) external payable returns (uint256 positionId);

function withdraw(
function adjust(
uint256 positionId,
uint256 withdrawAmount,
int256 maxPayInterest,
uint256 amount,
Epoch expiredWith,
int256 interestThreshold,
ERC20PermitParams calldata tokenPermitParams,
PermitSignature calldata positionPermitParams
) external;

function collect(uint256 positionId, PermitSignature calldata positionPermitParams) external;
) external payable;
}
28 changes: 14 additions & 14 deletions test/foundry/integration/BorrowController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
vm.signPermit(1, loanPositionManager, address(borrowController), positionId);
IBorrowController.SwapParams memory swapParams;
vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount,
beforeLoanPosition.debtAmount + 0.5 ether,
Expand Down Expand Up @@ -275,7 +275,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
);
IBorrowController.SwapParams memory swapParams;
vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount + collateralAmount,
beforeLoanPosition.debtAmount,
Expand Down Expand Up @@ -313,7 +313,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv

IBorrowController.SwapParams memory swapParams;
vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount - collateralAmount,
beforeLoanPosition.debtAmount,
Expand Down Expand Up @@ -353,7 +353,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
IBorrowController.SwapParams memory swapParams;
vm.startPrank(user);
weth.approve(address(borrowController), maxPayInterest);
borrowController.adjustPosition{value: maxPayInterest}(
borrowController.adjust{value: maxPayInterest}(
positionId,
beforeLoanPosition.collateralAmount,
beforeLoanPosition.debtAmount,
Expand Down Expand Up @@ -390,7 +390,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv

IBorrowController.SwapParams memory swapParams;
vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount,
beforeLoanPosition.debtAmount,
Expand Down Expand Up @@ -428,7 +428,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv

IBorrowController.SwapParams memory swapParams;
vm.prank(user);
borrowController.adjustPosition{value: repayAmount}(
borrowController.adjust{value: repayAmount}(
positionId,
beforeLoanPosition.collateralAmount,
beforeLoanPosition.debtAmount - repayAmount,
Expand Down Expand Up @@ -466,7 +466,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv

IBorrowController.SwapParams memory swapParams;
vm.prank(user);
borrowController.adjustPosition{value: repayAmount}(
borrowController.adjust{value: repayAmount}(
positionId,
beforeLoanPosition.collateralAmount,
beforeLoanPosition.debtAmount - repayAmount,
Expand Down Expand Up @@ -571,7 +571,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
swapParams.inSubstitute = address(wausdc);

vm.prank(user);
borrowController.adjustPosition{value: 0.16 ether}(
borrowController.adjust{value: 0.16 ether}(
positionId,
loanPosition.collateralAmount + collateralAmount,
loanPosition.debtAmount + debtAmount,
Expand Down Expand Up @@ -624,7 +624,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
vm.signPermit(1, loanPositionManager, address(borrowController), positionId);

vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount - collateralAmount,
debtAmount,
Expand Down Expand Up @@ -675,7 +675,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
vm.signPermit(1, loanPositionManager, address(borrowController), positionId);

vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount - collateralAmount,
0,
Expand Down Expand Up @@ -726,7 +726,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
vm.signPermit(1, loanPositionManager, address(borrowController), positionId);

vm.prank(user);
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount - collateralAmount,
maxDebtAmount,
Expand Down Expand Up @@ -765,7 +765,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
IBorrowController.SwapParams memory swapParams;
vm.prank(user);
vm.expectRevert(abi.encodeWithSelector(ILoanPositionManagerTypes.FullRepaymentRequired.selector));
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount,
beforeLoanPosition.debtAmount + 0.5 ether,
Expand All @@ -790,7 +790,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv
IBorrowController.SwapParams memory swapParams;
vm.prank(user);
vm.expectRevert(abi.encodeWithSelector(ILoanPositionManagerTypes.FullRepaymentRequired.selector));
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount - collateralAmount,
beforeLoanPosition.debtAmount,
Expand All @@ -813,7 +813,7 @@ contract BorrowControllerIntegrationTest is Test, CloberMarketSwapCallbackReceiv

IBorrowController.SwapParams memory swapParams;
vm.expectRevert(abi.encodeWithSelector(IController.InvalidAccess.selector));
borrowController.adjustPosition(
borrowController.adjust(
positionId,
beforeLoanPosition.collateralAmount - collateralAmount,
beforeLoanPosition.debtAmount,
Expand Down
Loading

0 comments on commit 9c9ffb1

Please sign in to comment.