Skip to content

Commit

Permalink
Merge pull request #41 from web3-nomad/feature/erc4626
Browse files Browse the repository at this point in the history
refactor get rewards and add lastLockedTime
  • Loading branch information
maksimKrukovich authored Jun 12, 2024
2 parents cac9354 + f3f8762 commit 0fa2fa0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
51 changes: 26 additions & 25 deletions contracts/erc4626/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ contract HederaVault is IERC4626, FeeConfiguration, Ownable, ReentrancyGuard {
// User Info struct
struct UserInfo {
uint256 sharesAmount;
uint256 lastLockedTime;
mapping(address => uint256) lastClaimedAmountT;
bool exist;
}
Expand Down Expand Up @@ -259,10 +260,12 @@ contract HederaVault is IERC4626, FeeConfiguration, Ownable, ReentrancyGuard {
}
userContribution[msg.sender].sharesAmount = _amount;
userContribution[msg.sender].exist = true;
userContribution[msg.sender].lastLockedTime = block.timestamp;
assetTotalSupply += _amount;
} else {
claimAllReward(0);
userContribution[msg.sender].sharesAmount += _amount;
userContribution[msg.sender].lastLockedTime = block.timestamp;
assetTotalSupply += _amount;
}
}
Expand Down Expand Up @@ -449,37 +452,35 @@ contract HederaVault is IERC4626, FeeConfiguration, Ownable, ReentrancyGuard {
}

/**
* @dev Returns all rewards for a user with fee considering.
* @dev Returns rewards for a user with fee considering.
*
* @param _user The user address.
* @return rewards The calculated rewards.
* @param _rewardToken The reward address.
* @return unclaimedAmount The calculated rewards.
*/
function getUserRewards(address _user) public view returns (uint256[] memory rewards) {
uint256 currentReward;
uint256 currentFee;
uint256 rewardsSize = rewardTokens.length;
for (uint256 i = 0; i < rewardsSize; i++) {
currentReward = (tokensRewardInfo[rewardTokens[i]].amount -
userContribution[_user].lastClaimedAmountT[rewardTokens[i]]).mulDivDown(
1,
userContribution[_user].sharesAmount
);

if (feeConfig.feePercentage > 0) {
currentFee = _calculateFee(currentReward, feeConfig.feePercentage);
rewards[i] = currentReward - currentFee;
} else {
rewards[i] = currentReward;
}
function getUserReward(address _user, address _rewardToken) public view returns (uint256 unclaimedAmount) {
RewardsInfo storage _rewardInfo = tokensRewardInfo[_rewardToken];

uint256 perShareAmount = _rewardInfo.amount;
UserInfo storage cInfo = userContribution[_user];
uint256 userStakingTokenTotal = cInfo.sharesAmount;
uint256 perShareClaimedAmount = cInfo.lastClaimedAmountT[_rewardToken];
uint256 perShareUnclaimedAmount = perShareAmount - perShareClaimedAmount;
unclaimedAmount = perShareUnclaimedAmount.mulDivDown(1, userStakingTokenTotal);

if (feeConfig.feePercentage > 0) {
uint256 currentFee = _calculateFee(unclaimedAmount, feeConfig.feePercentage);
unclaimedAmount -= currentFee;
}
}

function calculateReward(uint256 _id) public view returns (uint256 reward) {
address token = rewardTokens[_id];
reward = (tokensRewardInfo[token].amount - userContribution[msg.sender].lastClaimedAmountT[token]).mulDivDown(
1,
userContribution[msg.sender].sharesAmount
);
function getAllRewards(address _user) public view returns (uint256[] memory) {
uint256[] memory _rewards;

for (uint256 i = 0; i < rewardTokens.length; i++) {
_rewards[i] = getUserReward(_user, rewardTokens[i]);
}
return _rewards;
}
}

Expand Down
2 changes: 1 addition & 1 deletion data/deployments/chain-296.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"vault": {
"HederaVault": "0xe95E635753a8A233cB736c5CB0dF181Bb865a90b",
"VaultFactory": "0x39127dc7C5Eacf239A25A3397770442CfcbFDeb9",
"VaultFactory": "0xa221354a965bC0d82da65DEee2f41bBCA5420b73",
"StakingToken": "0x0000000000000000000000000000000000423251",
"Share": "0x0000000000000000000000000000000000423255",
"RewardToken": "0x0000000000000000000000000000000000423252"
Expand Down

0 comments on commit 0fa2fa0

Please sign in to comment.