Skip to content

Commit

Permalink
reverted reward logic
Browse files Browse the repository at this point in the history
  • Loading branch information
David405 committed Sep 20, 2023
1 parent a3e0105 commit 0d4e6d1
Showing 1 changed file with 36 additions and 43 deletions.
79 changes: 36 additions & 43 deletions contracts/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ contract Vault is
/**
* Initializers
*/
constructor() {
_disableInitializers();
}

function vault_init(
address _controller,
Expand Down Expand Up @@ -196,8 +199,6 @@ contract Vault is

uint256 netMintChange;

_mint(zUSD, msg.sender, _mintAmount);

if(globalDebt == 0 || netMintGlobal == 0) {
netMintChange = _mintAmount;
} else {
Expand All @@ -221,6 +222,8 @@ contract Vault is

_testImpact();

_mint(zUSD, msg.sender, _mintAmount);

collateral.safeTransferFrom(
msg.sender,
address(this),
Expand Down Expand Up @@ -281,10 +284,6 @@ contract Vault is

totalSwapVolume += swapAmountInUSD;

_burn(_zTokenFromAddress, msg.sender, _amount);

_mint(_zTokenToAddress, msg.sender, mintAmount);

/**
* Handle swap fees and rewards
*/
Expand All @@ -304,6 +303,23 @@ contract Vault is

treasuryFeePerTransaction = treasuryFeePerTransaction / MULTIPLIER;

uint256 len = mintersAddresses.length;

for(uint256 i; i < len; i++) {
mintersRewardPerTransaction[mintersAddresses[i]] =
((netMintUser[mintersAddresses[i]] * MULTIPLIER) /
netMintGlobal) *
globalMintersFeePerTransaction;

userAccruedFeeBalance[mintersAddresses[i]] +=
mintersRewardPerTransaction[mintersAddresses[i]] /
MULTIPLIER;
}

_burn(_zTokenFromAddress, msg.sender, _amount);

_mint(_zTokenToAddress, msg.sender, mintAmount);

/**
* Send the treasury amount to a treasury wallet
*/
Expand Down Expand Up @@ -356,12 +372,14 @@ contract Vault is
netMintGlobal -= amountToSubtract;
}

_burn(zUSD, msg.sender, amountToRepayinUSD);

userCollateralBalance[msg.sender] -= _amountToWithdraw;

totalCollateral -= _amountToWithdraw;

_testImpact();

_burn(zUSD, msg.sender, amountToRepayinUSD);

collateral.safeTransfer(
msg.sender,
_amountToWithdraw
Expand All @@ -376,7 +394,7 @@ contract Vault is

uint256 userDebt;

uint totalRewards = getPotentialTotalReward(_user);
uint256 totalRewards = getPotentialTotalReward(_user);

userDebt = _updateUserDebtOutstanding(
netMintUser[_user],
Expand All @@ -396,6 +414,8 @@ contract Vault is
if (userCollateralBalance[_user] <= totalRewards) {
userCollateralBalance[_user] = 0;

totalCollateral -= totalRewards;

collateral.safeTransfer(
msg.sender,
totalRewards
Expand All @@ -404,6 +424,8 @@ contract Vault is
} else {
userCollateralBalance[_user] -= totalRewards;

totalCollateral -= totalRewards;

collateral.safeTransfer(
msg.sender,
totalRewards
Expand All @@ -417,15 +439,12 @@ contract Vault is
* Allow minters to claim rewards/fees on swap
*/
function claimFees() external nonReentrant {
uint256 amount = getSwapReward(msg.sender);
require(userAccruedFeeBalance[msg.sender] > 0,
"User has no accumulated rewards");
uint256 amount;

require(
amount > 0,
"No reward"
);

userAccruedFeeBalance[msg.sender] += amount;
GlobalMintersFeeAtClaim[msg.sender] = globalMintersFee;
amount = userAccruedFeeBalance[msg.sender];
userAccruedFeeBalance[msg.sender] = 0;

bool transferSuccess = IERC20Upgradeable(zUSD).transfer(msg.sender, amount);
if (!transferSuccess) revert();
Expand Down Expand Up @@ -892,32 +911,6 @@ contract Vault is
return zUSD = BakiOracleInterface(Oracle).getZToken("zusd");
}

/**
* View minter's reward Helper
*/
function getSwapReward(address _user) public view returns(uint256) {

if (netMintUser[msg.sender] == 0) {
return 0;
}
uint256 amount;
uint256 y = globalMintersFee - GlobalMintersFeeAtClaim[_user];

if (y == 0) {
return 0;
}
uint256 x = netMintUser[msg.sender] * y;

uint256 mintRatio = WadRayMath.wadDiv(
x,
netMintGlobal
);

amount = mintRatio / MULTIPLIER;

return amount;
}

/**
* Helper function for global debt
*/
Expand Down

0 comments on commit 0d4e6d1

Please sign in to comment.