-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/repay wrapper #23
Open
foodaka
wants to merge
7
commits into
feat/susds-wrappers
Choose a base branch
from
feat/repay-wrapper
base: feat/susds-wrappers
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a5cd99
Merge branch 'feat/susds-wrappers' of github.com:aave/token-wrappers …
foodaka e287d95
init repay
foodaka 62360ae
setup
foodaka f713cdb
fixes on repay test
foodaka daf8f5a
repay tests
foodaka f0e3f34
feat: repay fuzz test
foodaka f0afea3
remove console log
foodaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -137,6 +137,35 @@ abstract contract BaseTokenWrapper is Ownable, IBaseTokenWrapper { | |||||
_borrowToken(amount, msg.sender, referralCode); | ||||||
} | ||||||
|
||||||
// @inheritdoc IBaseTokenWrapper | ||||||
function repayToken( | ||||||
uint256 amount, | ||||||
address onBehalfOf | ||||||
) external virtual returns (uint256) { | ||||||
return _repayToken(amount, onBehalfOf); | ||||||
} | ||||||
|
||||||
// @inheritdoc IBaseTokenWrapper | ||||||
function repayWithPermit( | ||||||
uint256 amount, | ||||||
address onBehalfOf, | ||||||
PermitSignature calldata signature | ||||||
) external virtual returns (uint256) { | ||||||
// explicitly left try-catch block blank to protect users from permit griefing | ||||||
try | ||||||
IERC20WithPermit(TOKEN_IN).permit( | ||||||
msg.sender, | ||||||
address(this), | ||||||
amount, | ||||||
signature.deadline, | ||||||
signature.v, | ||||||
signature.r, | ||||||
signature.s | ||||||
) | ||||||
{} catch {} | ||||||
return _repayToken(amount, onBehalfOf); | ||||||
} | ||||||
|
||||||
/// @inheritdoc IBaseTokenWrapper | ||||||
function rescueTokens( | ||||||
IERC20 token, | ||||||
|
@@ -240,6 +269,23 @@ abstract contract BaseTokenWrapper is Ownable, IBaseTokenWrapper { | |||||
IERC20(TOKEN_IN).transfer(onBehalfOf, amountIn); | ||||||
} | ||||||
|
||||||
function _repayToken( | ||||||
uint256 amount, | ||||||
address onBehalfOf | ||||||
) internal returns (uint256) { | ||||||
require(amount > 0, 'INSUFFICIENT_AMOUNT_TO_REPAY'); | ||||||
|
||||||
IERC20(TOKEN_IN).safeTransferFrom(msg.sender, address(this), amount); | ||||||
uint256 amountWrapped = _wrapTokenIn(amount); | ||||||
require(amountWrapped > 0, 'INSUFFICIENT_WRAPPED_TOKEN_RECEIVED'); | ||||||
|
||||||
SafeERC20.safeApprove(IERC20(TOKEN_OUT), address(POOL), amountWrapped); | ||||||
POOL.repay(TOKEN_OUT, amountWrapped, 2, onBehalfOf); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
SafeERC20.safeApprove(IERC20(TOKEN_OUT), address(POOL), 0); | ||||||
return amountWrapped; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @notice Helper to wrap an amount of tokenIn, receiving tokenOut | ||||||
* @param amount The amount of tokenIn to wrap | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -878,11 +878,16 @@ abstract contract BaseTokenWrapperTest is Test { | |
} | ||
} | ||
|
||
function testFuzzBorrowToken(uint256 borrowAmount) public { | ||
function testFuzzRepayToken( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are replacing |
||
uint256 borrowAmount, | ||
uint256 repayAmount | ||
) public { | ||
borrowAmount = bound(borrowAmount, 1, MAX_DEAL_AMOUNT); | ||
borrowAmount *= 10 ** tokenInDecimals; | ||
uint256 collateralAmount = borrowAmount * 10; | ||
|
||
repayAmount = bound(repayAmount, 1e6, borrowAmount); | ||
|
||
address debtToken = IPool(pool) | ||
.getReserveData(tokenWrapper.TOKEN_OUT()) | ||
.variableDebtTokenAddress; | ||
|
@@ -905,7 +910,38 @@ abstract contract BaseTokenWrapperTest is Test { | |
uint256 borrowedAmount = tokenWrapper.getTokenInForTokenOut(borrowAmount); | ||
assertEq( | ||
IERC20(tokenWrapper.TOKEN_IN()).balanceOf(address(alice)), | ||
borrowedAmount | ||
borrowedAmount, | ||
'Borrowed amount mismatch' | ||
); | ||
|
||
vm.warp(block.timestamp + 1 days); | ||
|
||
uint256 debtBefore = IERC20(debtToken).balanceOf(address(alice)); | ||
deal(tokenWrapper.TOKEN_IN(), alice, repayAmount); | ||
IERC20(tokenWrapper.TOKEN_IN()).approve( | ||
address(tokenWrapper), | ||
repayAmount | ||
); | ||
|
||
uint256 actualRepaidAmount = tokenWrapper.repayToken(repayAmount, alice); | ||
|
||
uint256 debtAfter = IERC20(debtToken).balanceOf(address(alice)); | ||
assertLt(debtAfter, debtBefore, 'Debt should decrease after repayment'); | ||
|
||
uint256 expectedRepaidAmount = tokenWrapper.getTokenOutForTokenIn( | ||
repayAmount | ||
); | ||
assertApproxEqRel( | ||
actualRepaidAmount, | ||
expectedRepaidAmount, | ||
0.01e18, // 1% tolerance | ||
'Repaid amount should match expected amount' | ||
); | ||
|
||
assertLe( | ||
IERC20(tokenWrapper.TOKEN_IN()).balanceOf(address(alice)), | ||
borrowedAmount - repayAmount, | ||
'Token balance after repayment is incorrect' | ||
); | ||
} else { | ||
vm.expectRevert(); | ||
|
@@ -914,6 +950,218 @@ abstract contract BaseTokenWrapperTest is Test { | |
vm.stopPrank(); | ||
} | ||
|
||
function testPartialRepayToken() public { | ||
uint256 collateralAmount = 1000e18; | ||
uint256 borrowAmount = 100e18; | ||
uint256 partialRepayAmount = 60e18; // Repay 60% of the borrowed amount | ||
deal(collateralAsset, ALICE, collateralAmount); | ||
|
||
address debtToken = IPool(pool) | ||
.getReserveData(tokenWrapper.TOKEN_OUT()) | ||
.variableDebtTokenAddress; | ||
vm.startPrank(ALICE); | ||
|
||
ICreditDelegationToken(debtToken).approveDelegation( | ||
address(tokenWrapper), | ||
borrowAmount | ||
); | ||
|
||
IERC20(collateralAsset).approve(address(pool), collateralAmount); | ||
IPool(pool).supply(collateralAsset, collateralAmount, ALICE, 0); | ||
|
||
if (borrowSupported) { | ||
tokenWrapper.borrowToken(borrowAmount, 0); | ||
uint256 borrowedAmount = tokenWrapper.getTokenInForTokenOut(borrowAmount); | ||
assertEq( | ||
IERC20(tokenWrapper.TOKEN_IN()).balanceOf(address(ALICE)), | ||
borrowedAmount | ||
); | ||
|
||
vm.warp(block.timestamp + 1 days); | ||
|
||
uint256 underlyingBalanceBeforeRepayment = IERC20(tokenWrapper.TOKEN_IN()) | ||
.balanceOf(address(ALICE)); | ||
|
||
uint256 debtBeforeRepayment = IERC20(debtToken).balanceOf(address(ALICE)); | ||
|
||
IERC20(tokenWrapper.TOKEN_IN()).approve( | ||
address(tokenWrapper), | ||
partialRepayAmount | ||
); | ||
|
||
uint256 amountRepaid = tokenWrapper.repayToken(partialRepayAmount, ALICE); | ||
|
||
uint256 underlyingBalanceAfterRepayment = IERC20(tokenWrapper.TOKEN_IN()) | ||
.balanceOf(address(ALICE)); | ||
uint256 debtAfterRepayment = IERC20(debtToken).balanceOf(address(ALICE)); | ||
|
||
assertApproxEqRel( | ||
underlyingBalanceBeforeRepayment - underlyingBalanceAfterRepayment, | ||
partialRepayAmount, | ||
0.001e18 // 0.1% tolerance | ||
); | ||
|
||
assertApproxEqRel( | ||
debtBeforeRepayment - debtAfterRepayment, | ||
amountRepaid, | ||
0.001e18 // 0.1% tolerance | ||
); | ||
|
||
assertTrue(debtAfterRepayment > 0, 'Debt should not be fully repaid'); | ||
|
||
assertApproxEqRel( | ||
amountRepaid, | ||
partialRepayAmount, | ||
0.001e18 // 0.1% tolerance | ||
); | ||
} | ||
vm.stopPrank(); | ||
} | ||
|
||
function testRepayTokenWithPermit() public { | ||
uint256 collateralAmount = 1000e18; | ||
uint256 borrowAmount = 100e18; | ||
deal(collateralAsset, ALICE, collateralAmount); | ||
|
||
address debtToken = IPool(pool) | ||
.getReserveData(tokenWrapper.TOKEN_OUT()) | ||
.variableDebtTokenAddress; | ||
vm.startPrank(ALICE); | ||
|
||
ICreditDelegationToken(debtToken).approveDelegation( | ||
address(tokenWrapper), | ||
borrowAmount | ||
); | ||
|
||
IERC20(collateralAsset).approve(address(pool), collateralAmount); | ||
IPool(pool).supply(collateralAsset, collateralAmount, ALICE, 0); | ||
|
||
if (borrowSupported) { | ||
tokenWrapper.borrowToken(borrowAmount, 0); | ||
uint256 borrowedAmount = tokenWrapper.getTokenInForTokenOut(borrowAmount); | ||
assertEq( | ||
IERC20(tokenWrapper.TOKEN_IN()).balanceOf(address(ALICE)), | ||
borrowedAmount | ||
); | ||
uint256 repayAmount = borrowedAmount; | ||
|
||
uint256 underlyingBalanceBeforeRepayment = IERC20(tokenWrapper.TOKEN_IN()) | ||
.balanceOf(address(ALICE)); | ||
|
||
SigUtils.Permit memory permit = SigUtils.Permit({ | ||
owner: address(ALICE), | ||
spender: address(tokenWrapper), | ||
value: repayAmount, | ||
nonce: IERC2612(tokenWrapper.TOKEN_IN()).nonces(ALICE), | ||
deadline: block.timestamp + 1 days | ||
}); | ||
|
||
bytes32 digest = SigUtils.getTypedDataHash( | ||
permit, | ||
IERC2612(tokenWrapper.TOKEN_IN()).DOMAIN_SEPARATOR() | ||
); | ||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(ALICE_KEY, digest); | ||
|
||
IBaseTokenWrapper.PermitSignature memory signature = IBaseTokenWrapper | ||
.PermitSignature({ | ||
deadline: block.timestamp + 1 days, | ||
v: v, | ||
r: r, | ||
s: s | ||
}); | ||
|
||
tokenWrapper.repayWithPermit(repayAmount, ALICE, signature); | ||
vm.stopPrank(); | ||
assertEq( | ||
IERC20(tokenWrapper.TOKEN_OUT()).balanceOf(ALICE), | ||
underlyingBalanceBeforeRepayment - repayAmount | ||
); | ||
} | ||
} | ||
|
||
function testPartialRepayTokenWithPermit() public { | ||
uint256 collateralAmount = 1000e18; | ||
uint256 borrowAmount = 100e18; | ||
uint256 partialRepayAmount = 60e18; // Repay 60% of the borrowed amount | ||
deal(collateralAsset, ALICE, collateralAmount); | ||
|
||
address debtToken = IPool(pool) | ||
.getReserveData(tokenWrapper.TOKEN_OUT()) | ||
.variableDebtTokenAddress; | ||
vm.startPrank(ALICE); | ||
|
||
ICreditDelegationToken(debtToken).approveDelegation( | ||
address(tokenWrapper), | ||
borrowAmount | ||
); | ||
|
||
IERC20(collateralAsset).approve(address(pool), collateralAmount); | ||
IPool(pool).supply(collateralAsset, collateralAmount, ALICE, 0); | ||
|
||
if (borrowSupported) { | ||
tokenWrapper.borrowToken(borrowAmount, 0); | ||
uint256 borrowedAmount = tokenWrapper.getTokenInForTokenOut(borrowAmount); | ||
assertEq( | ||
IERC20(tokenWrapper.TOKEN_IN()).balanceOf(address(ALICE)), | ||
borrowedAmount | ||
); | ||
|
||
vm.warp(block.timestamp + 1 days); | ||
|
||
uint256 underlyingBalanceBeforeRepayment = IERC20(tokenWrapper.TOKEN_IN()) | ||
.balanceOf(address(ALICE)); | ||
|
||
uint256 debtBeforeRepayment = IERC20(debtToken).balanceOf(address(ALICE)); | ||
|
||
SigUtils.Permit memory permit = SigUtils.Permit({ | ||
owner: address(ALICE), | ||
spender: address(tokenWrapper), | ||
value: partialRepayAmount, | ||
nonce: IERC2612(tokenWrapper.TOKEN_IN()).nonces(ALICE), | ||
deadline: block.timestamp + 1 days | ||
}); | ||
|
||
bytes32 digest = SigUtils.getTypedDataHash( | ||
permit, | ||
IERC2612(tokenWrapper.TOKEN_IN()).DOMAIN_SEPARATOR() | ||
); | ||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(ALICE_KEY, digest); | ||
|
||
IBaseTokenWrapper.PermitSignature memory signature = IBaseTokenWrapper | ||
.PermitSignature({ | ||
deadline: block.timestamp + 1 days, | ||
v: v, | ||
r: r, | ||
s: s | ||
}); | ||
|
||
tokenWrapper.repayWithPermit(partialRepayAmount, ALICE, signature); | ||
|
||
uint256 underlyingBalanceAfterRepayment = IERC20(tokenWrapper.TOKEN_IN()) | ||
.balanceOf(address(ALICE)); | ||
uint256 debtAfterRepayment = IERC20(debtToken).balanceOf(address(ALICE)); | ||
|
||
assertApproxEqRel( | ||
underlyingBalanceBeforeRepayment - underlyingBalanceAfterRepayment, | ||
partialRepayAmount, | ||
0.001e18 // 0.1% tolerance | ||
); | ||
|
||
assertApproxEqRel( | ||
debtBeforeRepayment - debtAfterRepayment, | ||
partialRepayAmount, | ||
0.001e18 // 0.1% tolerance | ||
); | ||
assertTrue(debtAfterRepayment > 0, 'Debt should not be fully repaid'); | ||
} | ||
vm.stopPrank(); | ||
} | ||
|
||
function testRepayTokenZeroAmount() public { | ||
vm.expectRevert('INSUFFICIENT_AMOUNT_TO_REPAY'); | ||
tokenWrapper.repayToken(0, address(this)); | ||
} | ||
|
||
function _signCreditDelegation( | ||
uint256 privateKey, | ||
address delegatee, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to handle max repayment here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so retrieve the outstanding debt of the user, and repay all if
amount
is higher.we must do the same thing for
withdraw