Skip to content

Commit

Permalink
Merge pull request #38 from web3-nomad/feature/erc4626
Browse files Browse the repository at this point in the history
add calculateRewards func
  • Loading branch information
maksimKrukovich authored Jun 6, 2024
2 parents b0d4b9d + 6419643 commit c8d4606
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
12 changes: 3 additions & 9 deletions contracts/common/FeeConfiguration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@ abstract contract FeeConfiguration is AccessControl {
address _token = feeConfig.token;
uint256 fee = _calculateFee(_amount, feeConfig.feePercentage);

if (_token == address(0)) {
require(msg.value == fee, "FC: Not enough HBAR to pay fee");
Address.sendValue(payable(feeConfig.receiver), fee);
} else {
require(IERC20(_token).balanceOf(msg.sender) >= fee, "FC: Insufficient token balance");
SafeHTS.safeTransferToken(_token, msg.sender, feeConfig.receiver, int64(uint64(fee)));
}
require(IERC20(_token).balanceOf(msg.sender) >= fee, "FC: Insufficient token balance");
SafeHTS.safeTransferToken(_token, msg.sender, feeConfig.receiver, int64(uint64(fee)));
}

/**
Expand All @@ -91,7 +86,6 @@ abstract contract FeeConfiguration is AccessControl {
* @param _feePercentage The fee percentage.
*/
function _updateFeeConfigInternally(address _receiver, address _token, uint256 _feePercentage) private {
require(_feePercentage > 0 && _receiver != address(0), "FC: Invalid fee config data");
require(_feePercentage < BASIS_POINTS, "FC: Invalid fee");
feeConfig.receiver = _receiver;
feeConfig.token = _token;
Expand All @@ -105,7 +99,7 @@ abstract contract FeeConfiguration is AccessControl {
* @param _amount The amount of the transfer.
* @param _feePercentage The fee percentage.
*/
function _calculateFee(uint256 _amount, uint256 _feePercentage) private pure returns (uint256) {
function _calculateFee(uint256 _amount, uint256 _feePercentage) internal pure returns (uint256) {
require(_amount * _feePercentage >= BASIS_POINTS, "FC: Too small amount to consider fee");
return (_amount * _feePercentage) / BASIS_POINTS;
}
Expand Down
28 changes: 26 additions & 2 deletions contracts/erc4626/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {SafeTransferLib} from "./SafeTransferLib.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

import {IPyth} from "@pythnetwork/pyth-sdk-solidity/IPyth.sol";

import "../common/safe-HTS/SafeHTS.sol";
import "../common/safe-HTS/IHederaTokenService.sol";

Expand Down Expand Up @@ -448,6 +446,32 @@ contract HederaVault is IERC4626, FeeConfiguration, Ownable, ReentrancyGuard {
return (_startPosition, rewardTokensSize);
}

/**
* @dev Returns all rewards for a user with fee considering.
*
* @param _user The user address.
* @return rewards 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 calculateReward(uint256 _id) public view returns (uint256 reward) {
address token = rewardTokens[_id];
reward = (tokensRewardInfo[token].amount - userContribution[msg.sender].lastClaimedAmountT[token]).mulDivDown(
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": "0xd58b64b0CEa4eB82D0427524Bc56927410E3120a",
"VaultFactory": "0x4097b8cDe36d20E442B59F78f468eBcA16B9482E",
"StakingToken": "0x0000000000000000000000000000000000423251",
"Share": "0x0000000000000000000000000000000000423255",
"RewardToken": "0x0000000000000000000000000000000000423252"
Expand Down

0 comments on commit c8d4606

Please sign in to comment.