From b741816bcf77fbf3fa649613603a40a8f1f85724 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Sun, 19 Nov 2023 22:39:24 +0800 Subject: [PATCH 1/3] Fix redeem totalSupply --- contracts/StableAsset.sol | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/contracts/StableAsset.sol b/contracts/StableAsset.sol index c0fda7e..fea8f98 100644 --- a/contracts/StableAsset.sol +++ b/contracts/StableAsset.sol @@ -798,9 +798,15 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { ); } - totalSupply = D - _amount; + totalSupply = D - redeemAmount; // After reducing the redeem fee, the remaining pool tokens are burned! poolToken.burnSharesFrom(msg.sender, _amount); + + // Add fee to totalSupply after burning _amount + if (feeAmount > 0) { + poolToken.setTotalSupply(feeAmount); + } + collectFeeOrYield(true); emit Redeemed(msg.sender, _amount, amounts, feeAmount); return amounts; @@ -900,8 +906,14 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { } amounts[_i] = transferAmount; IERC20Upgradeable(tokens[_i]).safeTransfer(msg.sender, transferAmount); - totalSupply = D - _amount; + totalSupply = D - redeemAmount; poolToken.burnSharesFrom(msg.sender, _amount); + + // Add fee to totalSupply after burning _amount + if (feeAmount > 0) { + poolToken.setTotalSupply(feeAmount); + } + collectFeeOrYield(true); emit Redeemed(msg.sender, _amount, amounts, feeAmount); return transferAmount; @@ -996,8 +1008,14 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { // Updates token balances in storage. balances = _balances; - totalSupply = oldD - redeemAmount; + totalSupply = oldD - (redeemAmount - feeAmount); poolToken.burnSharesFrom(msg.sender, redeemAmount); + + // Add fee to totalSupply after burning _amount + if (feeAmount > 0) { + poolToken.setTotalSupply(feeAmount); + } + uint256[] memory amounts = _amounts; for (i = 0; i < _balances.length; i++) { if (_amounts[i] == 0) continue; From 77bdc1a0a25a5a9b2b3b19eec794dc8406350c00 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Mon, 20 Nov 2023 07:42:06 +0800 Subject: [PATCH 2/3] Fix yield amount --- contracts/StableAsset.sol | 41 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/contracts/StableAsset.sol b/contracts/StableAsset.sol index fea8f98..007c261 100644 --- a/contracts/StableAsset.sol +++ b/contracts/StableAsset.sol @@ -539,13 +539,9 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { _amounts[i] ); } - totalSupply = newD; + totalSupply = oldD + mintAmount; poolToken.mintShares(msg.sender, mintAmount); - if (feeAmount > 0) { - poolToken.setTotalSupply(feeAmount); - } - - collectFeeOrYield(true); + feeAmount = collectFeeOrYield(true); emit Minted(msg.sender, mintAmount, _amounts, feeAmount); return mintAmount; } @@ -801,13 +797,7 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { totalSupply = D - redeemAmount; // After reducing the redeem fee, the remaining pool tokens are burned! poolToken.burnSharesFrom(msg.sender, _amount); - - // Add fee to totalSupply after burning _amount - if (feeAmount > 0) { - poolToken.setTotalSupply(feeAmount); - } - - collectFeeOrYield(true); + feeAmount = collectFeeOrYield(true); emit Redeemed(msg.sender, _amount, amounts, feeAmount); return amounts; } @@ -908,13 +898,7 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { IERC20Upgradeable(tokens[_i]).safeTransfer(msg.sender, transferAmount); totalSupply = D - redeemAmount; poolToken.burnSharesFrom(msg.sender, _amount); - - // Add fee to totalSupply after burning _amount - if (feeAmount > 0) { - poolToken.setTotalSupply(feeAmount); - } - - collectFeeOrYield(true); + feeAmount = collectFeeOrYield(true); emit Redeemed(msg.sender, _amount, amounts, feeAmount); return transferAmount; } @@ -1010,18 +994,12 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { balances = _balances; totalSupply = oldD - (redeemAmount - feeAmount); poolToken.burnSharesFrom(msg.sender, redeemAmount); - - // Add fee to totalSupply after burning _amount - if (feeAmount > 0) { - poolToken.setTotalSupply(feeAmount); - } - uint256[] memory amounts = _amounts; for (i = 0; i < _balances.length; i++) { if (_amounts[i] == 0) continue; IERC20Upgradeable(tokens[i]).safeTransfer(msg.sender, _amounts[i]); } - collectFeeOrYield(true); + feeAmount = collectFeeOrYield(true); emit Redeemed(msg.sender, redeemAmount, amounts, feeAmount); return amounts; } @@ -1106,7 +1084,14 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { } else { uint256[] memory amounts = new uint256[](_balances.length); for (uint256 i = 0; i < _balances.length; i++) { - amounts[i] = _balances[i] - oldBalances[i]; + uint256 amount = _balances[i] - oldBalances[i]; + if (i == exchangeRateTokenIndex) { + amount = + (amount * + (10 ** exchangeRateProvider.exchangeRateDecimals())) / + exchangeRateProvider.exchangeRate(); + } + amounts[i] = amount; } emit YieldCollected(amounts, feeAmount, totalSupply); } From 3bb0d9de344a34705234ba303457b5a2e34d9c67 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Mon, 20 Nov 2023 07:44:43 +0800 Subject: [PATCH 3/3] Fix --- contracts/StableAsset.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/StableAsset.sol b/contracts/StableAsset.sol index 007c261..f85cdc8 100644 --- a/contracts/StableAsset.sol +++ b/contracts/StableAsset.sol @@ -794,7 +794,7 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { ); } - totalSupply = D - redeemAmount; + totalSupply = D - _amount; // After reducing the redeem fee, the remaining pool tokens are burned! poolToken.burnSharesFrom(msg.sender, _amount); feeAmount = collectFeeOrYield(true); @@ -896,7 +896,7 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { } amounts[_i] = transferAmount; IERC20Upgradeable(tokens[_i]).safeTransfer(msg.sender, transferAmount); - totalSupply = D - redeemAmount; + totalSupply = D - _amount; poolToken.burnSharesFrom(msg.sender, _amount); feeAmount = collectFeeOrYield(true); emit Redeemed(msg.sender, _amount, amounts, feeAmount); @@ -992,7 +992,7 @@ contract StableAsset is Initializable, ReentrancyGuardUpgradeable { // Updates token balances in storage. balances = _balances; - totalSupply = oldD - (redeemAmount - feeAmount); + totalSupply = oldD - redeemAmount; poolToken.burnSharesFrom(msg.sender, redeemAmount); uint256[] memory amounts = _amounts; for (i = 0; i < _balances.length; i++) {