Skip to content

Commit

Permalink
✨ escrow: mint to recipient onStreamCanceled hook
Browse files Browse the repository at this point in the history
  • Loading branch information
itofarina committed Sep 28, 2023
1 parent dd461e2 commit 1dfca20
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
52 changes: 27 additions & 25 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,35 @@ DebtPreviewerTest:testPreviewLeverageSameWETHAssetMaxRatioMultipleCollateralAndD
DebtPreviewerTest:testPreviewLeverageSameWETHAssetMultipleCollateralAndDebtWithMinHealthFactor() (gas: 1708472)
DebtPreviewerTest:testPreviewMaxRatioWithdrawWithSameAssetLeverage() (gas: 1020516)
DebtPreviewerTest:testPreviewSameAssetInvalidLeverageShouldCapRatio() (gas: 910754)
EscrowedEXATest:testCancelShouldDeleteReserves() (gas: 470835)
EscrowedEXATest:testCancelShouldGiveReservesBack() (gas: 676167)
EscrowedEXATest:testCancelTwiceShouldRevert() (gas: 470405)
EscrowedEXATest:testCancelWithInvalidAccount() (gas: 348063)
EscrowedEXATest:testGrantTransferrerRoleAsAdmin() (gas: 45857)
EscrowedEXATest:testMint() (gas: 165097)
EscrowedEXATest:testMintMoreThanBalance() (gas: 27542)
EscrowedEXATest:testMintToAnother() (gas: 167315)
EscrowedEXATest:testMintZero() (gas: 14141)
EscrowedEXATest:testRedeemAsNotRedeemer() (gas: 214597)
EscrowedEXATest:testRedeemAsRedeemer() (gas: 165462)
EscrowedEXATest:testRedeemAsRedeemerToAnother() (gas: 170346)
EscrowedEXATest:testCancelFromStreamAndGetReserveBack() (gas: 457085)
EscrowedEXATest:testCancelFromStreamJustCreated() (gas: 412309)
EscrowedEXATest:testCancelShouldDeleteReserves() (gas: 470857)
EscrowedEXATest:testCancelShouldGiveReservesBack() (gas: 676123)
EscrowedEXATest:testCancelTwiceShouldRevert() (gas: 470361)
EscrowedEXATest:testCancelWithInvalidAccount() (gas: 348019)
EscrowedEXATest:testGrantTransferrerRoleAsAdmin() (gas: 45901)
EscrowedEXATest:testMint() (gas: 165141)
EscrowedEXATest:testMintMoreThanBalance() (gas: 27564)
EscrowedEXATest:testMintToAnother() (gas: 167337)
EscrowedEXATest:testMintZero() (gas: 14163)
EscrowedEXATest:testRedeemAsNotRedeemer() (gas: 214641)
EscrowedEXATest:testRedeemAsRedeemer() (gas: 165497)
EscrowedEXATest:testRedeemAsRedeemerToAnother() (gas: 170381)
EscrowedEXATest:testSetReserveRatioAsAdmin() (gas: 24450)
EscrowedEXATest:testSetReserveRatioAsNotAdmin() (gas: 47296)
EscrowedEXATest:testSetVestingPeriodAsAdmin() (gas: 24707)
EscrowedEXATest:testSetVestingPeriodAsNotAdmin() (gas: 47520)
EscrowedEXATest:testTransferToNotTransferrer() (gas: 174731)
EscrowedEXATest:testTransferToTransferrer() (gas: 210331)
EscrowedEXATest:testVest() (gas: 347009)
EscrowedEXATest:testSetReserveRatioAsNotAdmin() (gas: 47318)
EscrowedEXATest:testSetVestingPeriodAsAdmin() (gas: 24598)
EscrowedEXATest:testSetVestingPeriodAsNotAdmin() (gas: 47411)
EscrowedEXATest:testTransferToNotTransferrer() (gas: 174775)
EscrowedEXATest:testTransferToTransferrer() (gas: 210353)
EscrowedEXATest:testVest() (gas: 346856)
EscrowedEXATest:testVestToAnother() (gas: 403671)
EscrowedEXATest:testVestToAnotherAndCancel() (gas: 489382)
EscrowedEXATest:testVestWithPermitReserve() (gas: 458530)
EscrowedEXATest:testVestZero() (gas: 14239)
EscrowedEXATest:testWithdrawFromStreamAndGetReserveBack() (gas: 329574)
EscrowedEXATest:testWithdrawMaxFromMultipleStreams() (gas: 1006869)
EscrowedEXATest:testWithdrawMaxShouldGiveReserveBackWhenDepleted() (gas: 327564)
EscrowedEXATest:testWithdrawMaxWithInvalidSender() (gas: 339458)
EscrowedEXATest:testVestToAnotherAndCancel() (gas: 489338)
EscrowedEXATest:testVestWithPermitReserve() (gas: 458552)
EscrowedEXATest:testVestZero() (gas: 14151)
EscrowedEXATest:testWithdrawFromStreamAndGetReserveBack() (gas: 329552)
EscrowedEXATest:testWithdrawMaxFromMultipleStreams() (gas: 1006649)
EscrowedEXATest:testWithdrawMaxShouldGiveReserveBackWhenDepleted() (gas: 327542)
EscrowedEXATest:testWithdrawMaxWithInvalidSender() (gas: 339414)
InterestRateModelTest:testFixedBorrowRate() (gas: 8089)
InterestRateModelTest:testFloatingBorrowRate() (gas: 6236)
InterestRateModelTest:testMinFixedRate() (gas: 7610)
Expand Down
9 changes: 9 additions & 0 deletions contracts/periphery/EscrowedEXA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ contract EscrowedEXA is ERC20VotesUpgradeable, AccessControlUpgradeable {
}
}

/// @notice Hook called when a recipient cancels a stream.
/// @notice Mints esEXA to the recipient with the remaining EXA received from the canceled stream.
/// @param recipient recipient of the cancelled stream.
/// @param senderAmount amount of EXA sent to the recipient.
function onStreamCanceled(uint256, address recipient, uint128 senderAmount, uint128) external {
assert(msg.sender == address(sablier));
_mint(recipient, senderAmount);
}

/// @notice Sets the vesting period.
/// @param vestingPeriod_ New vesting period.
/// @dev Caller must have DEFAULT_ADMIN_ROLE.
Expand Down
35 changes: 35 additions & 0 deletions test/EscrowedEXA.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,41 @@ contract EscrowedEXATest is ForkTest {
assertEq(exa.balanceOf(address(this)), exaBefore + amount + reserve, "got amount + reserve back");
}

function testCancelFromStreamAndGetReserveBack() external {
uint256 amount = 1_000 ether;
uint256 reserve = amount.mulWadDown(esEXA.reserveRatio());
esEXA.mint(amount, address(this));
uint256[] memory streams = new uint256[](1);
streams[0] = esEXA.vest(uint128(amount), address(this));

uint256 exaBefore = exa.balanceOf(address(this));
vm.warp(block.timestamp + esEXA.vestingPeriod() / 2);

esEXA.sablier().cancel(streams[0]);

assertEq(esEXA.balanceOf(address(this)), amount / 2, "half esEXA sholud be back");

esEXA.withdrawMax(streams);
assertEq(exa.balanceOf(address(this)), exaBefore + amount / 2 + reserve, "amount/2 + reserve should be back");
}

function testCancelFromStreamJustCreated() external {
uint256 amount = 1_000 ether;
uint256 reserve = amount.mulWadDown(esEXA.reserveRatio());
esEXA.mint(amount, address(this));
uint256[] memory streams = new uint256[](1);
streams[0] = esEXA.vest(uint128(amount), address(this));

uint256 exaBefore = exa.balanceOf(address(this));

esEXA.sablier().cancel(streams[0]);

assertEq(esEXA.balanceOf(address(this)), amount, "amount esEXA sholud be back");

esEXA.withdrawMax(streams);
assertEq(exa.balanceOf(address(this)), exaBefore + reserve, "reserve should be back");
}

event ReserveRatioSet(uint256 reserveRatio);
event VestingPeriodSet(uint256 vestingPeriod);
event Vest(address indexed caller, address indexed account, uint256 indexed streamId, uint256 amount);
Expand Down

0 comments on commit 1dfca20

Please sign in to comment.