From 4f663551e98ec95161ea3ccb3e8388c82bcac204 Mon Sep 17 00:00:00 2001 From: David Asamonye Date: Wed, 27 Sep 2023 23:09:54 +0100 Subject: [PATCH] fixed mint flow --- contracts/Vault.sol | 46 +++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/contracts/Vault.sol b/contracts/Vault.sol index 2eb3e3c..7ae9d8b 100644 --- a/contracts/Vault.sol +++ b/contracts/Vault.sol @@ -199,6 +199,8 @@ contract Vault is uint256 netMintChange; + _mint(zUSD, msg.sender, _mintAmount); + if(globalDebt == 0 || netMintGlobal == 0) { netMintChange = _mintAmount; } else { @@ -210,8 +212,8 @@ contract Vault is grossMintUser[msg.sender] += _mintAmount; netMintUser[msg.sender] += netMintChange; - netMintGlobal += netMintChange; - + netMintGlobal += netMintChange; + /** * if this is user's first mint, add to minters list */ @@ -222,8 +224,6 @@ contract Vault is _testImpact(); - _mint(zUSD, msg.sender, _mintAmount); - collateral.safeTransferFrom( msg.sender, address(this), @@ -284,6 +284,10 @@ contract Vault is totalSwapVolume += swapAmountInUSD; + _burn(_zTokenFromAddress, msg.sender, _amount); + + _mint(_zTokenToAddress, msg.sender, mintAmount); + /** * Handle swap fees and rewards */ @@ -303,7 +307,7 @@ contract Vault is treasuryFeePerTransaction = treasuryFeePerTransaction / MULTIPLIER; - uint256 len = mintersAddresses.length; + uint256 len = mintersAddresses.length; for(uint256 i; i < len; i++) { mintersRewardPerTransaction[mintersAddresses[i]] = @@ -316,16 +320,12 @@ contract Vault is MULTIPLIER; } - _burn(_zTokenFromAddress, msg.sender, _amount); - - _mint(_zTokenToAddress, msg.sender, mintAmount); - /** * Send the treasury amount to a treasury wallet */ _mint(zUSD, treasuryWallet, treasuryFeePerTransaction); - /** + /** * Send the global minters fee from User to the global minters fee wallet */ _mint(zUSD, address(this), globalMintersFeePerTransaction); @@ -372,14 +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 @@ -435,7 +435,7 @@ contract Vault is emit Liquidate(_user, userDebt, totalRewards, msg.sender); } - /** + /** * Allow minters to claim rewards/fees on swap */ function claimFees() external nonReentrant { @@ -864,11 +864,25 @@ 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; + } + /** * Send the treasury amount to a treasury wallet */ _mint(zUSD, treasuryWallet, treasuryFeePerTransaction); - /** + + /** * Send the global minters fee from User to the global minters fee wallet */ _mint(zUSD, address(this), globalMintersFeePerTransaction); @@ -940,7 +954,7 @@ contract Vault is return globalDebt; } - function getUserDebt(address user) external view returns (uint256) { + function getUserDebt(address user) public view returns (uint256) { return _updateUserDebtOutstanding(netMintUser[user], netMintGlobal); } @@ -950,7 +964,7 @@ contract Vault is function _updateUserDebtOutstanding( uint256 _netMintUserzUSDValue, uint256 _netMintGlobalzUSDValue - ) public view returns (uint256) { + ) internal view returns (uint256) { if (_netMintGlobalzUSDValue != 0) { uint256 globalDebt = getGlobalDebt(); uint256 userDebtOutstanding;