Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

erc721 with rewards #32

Open
JOHNFFFEE opened this issue Feb 5, 2022 · 0 comments
Open

erc721 with rewards #32

JOHNFFFEE opened this issue Feb 5, 2022 · 0 comments

Comments

@JOHNFFFEE
Copy link

JOHNFFFEE commented Feb 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant