I need help with a function to reward user with erc777-token as soon as his content (Ipfs-Hash, see the program below) hit n-number of likes (lets say every 100th like), i.e., whenever the 'netLike' counter hits 100, it automatically mints one token to the respective user's address.

And because there's a dislike function as well, the user should not be rewarded the token every time the likeCounter hits 100, (for example - if a user hits 100th like for the first time, its rewarded with one erc token, and once it hits that milestone, in order to earn 2nd token, it must hit the milestone of 200 likes, and likewise every multiple of 100 can only generate one token 'one-time')

I've added nonce for this purpose, but couldn't really figure out the exact logic!! (brainfog 😊) The logic and the _mint() fn is inside the like function.


     Content[] public contents;
    // A mapping of Content Hashes to their respective Owners
    mapping(bytes32 => address) public contentHashToAuthor;
    //Contains all the indices of content uploaded by the author
    mapping(address => uint256[]) public authorToContentIndices;
    //A mapping of contentHash to contentIndex
    mapping(bytes32 => uint256) contentIndex;
    //the struct that contains the content-Info
    struct Content{
        bytes32 hash;
        string[] tags;
        address author;
        uint256 likes;
        uint256 dislikes;
        int256 netLikes;
        uint256 nonce;
        uint64 timeStamp;
function addContent(bytes32[] memory _hash, string[][] memory _tags) public {
    for(uint256 i = 0; i < _hash.length; i++ ){
        if(contentHashToAuthor[_hash[i]] == 0) {
            Content memory _content = new Content({
                hash: _hash[i],
                tags: _tags[i][],
                author: msg.sender
                like: 0,
                dislikes: 0,
                netLikes: 0,
                nonce: 0,
                mapping(address => bool) usersLiked;
                mapping(address => bool) usersDisliked;
                timeStamp: uint64(now)
            uint256 contentIndex = contents.push(_content) - 1;
            contentHashToAuthor[_hash[i]] = msg.sender;
            contentIndices[_hash[i]] = contentIndex;
        } else {
            revert("Content already Exist!")


function like(bytes32 _hash) public {
            uint256 cId = contentIndex[_hash];
            Content storage c = contents[cId];
            if(c.usersLiked[msg.sender] != true){
                c.usersLiked[msg.sender] = true;
                if(c.usersDisliked[msg.sender] == true){
                    c.usersDisliked[msg.sender] == false;
                //logic for rewarding ERC777 for every 100th netLike. 
                if(c.netLikes == 100){
                      //mint function to hit with every 100th netLike
                     _mint(c.author, 1, "", "");
            } else {
                revert("Already liked!")

  function dislike(bytes32 _hash) public {
            uint256 cId = contentIndex[_hash];
            Content storage c = contents[cId];
            if(c.usersDisliked[msg.sender] != true){
                c.usersDisliked[msg.sender] = true;
                if(c.usersLiked == true){
                    c.usersLiked == false;
            } else {
                revert("Already disliked!")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.