yield-utils-v2 icon indicating copy to clipboard operation
yield-utils-v2 copied to clipboard

erc721 with rewards

Open JOHNFFFEE opened this issue 4 years ago • 0 comments

Hi, Thank you for the library I am looking to implement the ERC20Rewards.sol to erc721 owners however the rewards per users is not accumulating. Can you give an example of implementation, or mb tell me what I am missing ?

image

I presume the mistake is

    /// @dev Update the rewards per token accumulator.
/// @notice Needs to be called on each liquidity event
function _updateRewardsPerToken() internal {
    RewardsPerToken memory rewardsPerToken_ = rewardsPerToken;
    RewardsPeriod memory rewardsPeriod_ = rewardsPeriod;
    uint256 totalSupply_ = rewardsToken.balanceOf(address(this)); //first change here

    // We skip the update if the program hasn't started
    if (uint32(block.timestamp) < rewardsPeriod_.start) return;

    // Find out the unaccounted time
    uint32 end = earliest(uint32(block.timestamp), rewardsPeriod_.end);
    uint256 unaccountedTime = end - rewardsPerToken_.lastUpdated; // Cast to uint256 to avoid overflows later on
    if (unaccountedTime == 0) return; // We skip the storage changes if already updated in the same block

    // Calculate and update the new value of the accumulator. unaccountedTime casts it into uint256, which is desired.
    // If the first mint happens mid-program, we don't update the accumulator, no one gets the rewards for that period.
    if (totalSupply_ != 0)
    rewardsPerToken_.accumulated = uint128(rewardsPerToken_.accumulated + 1e18 * unaccountedTime * rewardsPerToken_.rate / totalSupply_); // The rewards per token are scaled up for precision
    rewardsPerToken_.lastUpdated = end;
    rewardsPerToken = rewardsPerToken_;
    
}


   /// @dev Accumulate rewards for an user.
/// @notice Needs to be called on each liquidity event, or when user balances change.
function _updateUserRewards(address user) internal returns (uint128) {
    UserRewards memory userRewards_ = rewards[user];
    RewardsPerToken memory rewardsPerToken_ = rewardsPerToken;
    
    // Calculate and update the new value user reserves. _balanceOf[user] casts it into uint256, which is desired.
    userRewards_.accumulated = uint128(userRewards_.accumulated + rewardsToken.balanceOf(user)  //2nd change here

(rewardsPerToken_.accumulated - userRewards_.checkpoint) / 1e18); // We must scale down the rewards by the precision factor userRewards_.checkpoint = rewardsPerToken_.accumulated; rewards[user] = userRewards_;

    return userRewards_.accumulated;
}

what should I insert instead? I don't see the function _balanceOf[user] you mentioned on line 131

JOHNFFFEE avatar Feb 05 '22 10:02 JOHNFFFEE