From 0facc2b501b93a7f1a9a81f4b1e763d2011029b8 Mon Sep 17 00:00:00 2001 From: Bobby Fiando Date: Fri, 22 Nov 2024 22:23:57 +0700 Subject: [PATCH] WIP: fixing yield --- src/Eduena.sol | 27 +++++++++++++-------------- test/Eduena.t.sol | 13 +++++++++++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Eduena.sol b/src/Eduena.sol index 5239bef..8a303a0 100644 --- a/src/Eduena.sol +++ b/src/Eduena.sol @@ -53,6 +53,7 @@ contract Eduena is ERC20, ReentrancyGuard { USDe.safeTransferFrom(msg.sender, address(this), amount); _stake(amount); + updateYield(); } function _stake(uint256 amount) internal { @@ -74,7 +75,7 @@ contract Eduena is ERC20, ReentrancyGuard { _burn(recipient, shares); sUSDe.transfer(recipient, amount); emit Withdraw(msg.sender, amount); - // updateYield(); + updateYield(); } function distribute( @@ -90,21 +91,19 @@ contract Eduena is ERC20, ReentrancyGuard { } //FIXME: Fix the logic of this function - function updateYield() public { - if (lastAssetValueInUSDe == 0) { - lastAssetValueInUSDe = sUSDe.previewRedeem( - sUSDe.balanceOf(address(this)) - ); - } - - if (lastAssetValueInUSDe > 0) { - uint256 yield = sUSDe.previewRedeem( - sUSDe.balanceOf(address(this)) - ) - lastAssetValueInUSDe; + function updateYield() private { + uint256 currentAssetValueInUSDe = sUSDe.previewRedeem( + sUSDe.balanceOf(address(this)) + ); + console.log(lastAssetValueInUSDe); + console.log(currentAssetValueInUSDe); + + if (currentAssetValueInUSDe < lastAssetValueInUSDe) { + totalUnclaimedYieldInUSDe = 0; + } else { + uint256 yield = currentAssetValueInUSDe - lastAssetValueInUSDe; totalUnclaimedYieldInUSDe += yield; - console.log(totalUnclaimedYieldInUSDe); - _mint(address(this), _calculateShares(yield)); emit YieldUpdated( sUSDe.previewRedeem(sUSDe.balanceOf(address(this))), diff --git a/test/Eduena.t.sol b/test/Eduena.t.sol index 2f85e10..ecc32be 100644 --- a/test/Eduena.t.sol +++ b/test/Eduena.t.sol @@ -50,6 +50,16 @@ contract EduenaTest is Test { eduena.deposit(amount); vm.stopPrank(); + //test yield + + address rewarder = address(0x456); + uint256 reward = 10000 ether; + + deal(address(usde), rewarder, reward); + vm.startPrank(rewarder); + usde.transfer(address(susde), reward); + vm.stopPrank(); + amount = 1000; deal(address(usde), user, amount); @@ -57,7 +67,6 @@ contract EduenaTest is Test { usde.approve(address(eduena), amount); eduena.deposit(amount); vm.stopPrank(); - } function testWithdraw() public { @@ -74,7 +83,7 @@ contract EduenaTest is Test { vm.startPrank(user); eduena.withdraw(withdrawAmount); vm.stopPrank(); - + assertEq(usde.balanceOf(user), 0); assertEq(eduena.totalSupply(), 0); assertEq(susde.balanceOf(address(eduena)), 0);