用每第 n 个类似的数字铸造 ERC 令牌的函数

Function to mint ERC-token with every nth-number of like

我需要一个功能来帮助用户在他的内容(Ipfs-Hash,见下面的程序)达到 n 个喜欢(假设每 100 个喜欢)时立即用 erc777-token 奖励用户,即每当'netLike' 计数器达到 100,它会自动向相应用户的地址铸造一个令牌。

并且因为还有一个不喜欢的功能,用户不应该在每次 likeCounter 达到 100 时都获得令牌奖励,(例如 - 如果用户第一次达到第 100 个喜欢,它会获得一个 erc 令牌奖励,一旦达到那个里程碑,为了获得第二个令牌,它必须达到 200 个赞的里程碑,同样,每 100 的倍数只能生成一个令牌 'one-time')

我为此添加了随机数,但无法真正弄清楚确切的逻辑!! (brainfog) 逻辑和 _mint() fn 在 like 函数中。

谢谢!!

  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;
            authorToContentIndices[msg.sender].push(contentIndex);
            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;
                    c.dislikes--;
                }
                c.likes++;
                c.netLikes++;
                //logic for rewarding ERC777 for every 100th netLike. 
                //todo
                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;
                    c.likes--;
                    c.netLikes--;
                }
                c.dislikes++;
                c.netLikes--;
            } else {
                revert("Already disliked!")
            }
            
    }
    

您可以将 rewards 的计数器添加到 struct Content

struct Content{
    // ... rest of your code
    uint8 rewards; // Max value of uint8 is 255, which represents 25.5k netLikes. If that's not sufficient, use uint16.
}
if(c.netLikes % 100 == 0 && c.netLikes / 100 == c.rewards + 1){
      //mint function to hit with every 100th netLike
     _mint(c.author, 1, "", "")
     c.rewards++;
}

您的原始代码 if(c.netLikes == 100) 仅适用于第一个奖励(第 100 个 netLike)。

更新后的代码:

  • c.netLikes % 100 == 0 检查 netLikes 是否可以被 100
  • 整除
  • c.netLikes / 100 == c.rewards + 1验证这100还没有打赏

值示例:

  1. netLikes99,likes99,dislikes0,rewards0,一个新的喜欢:
    • likes变成100,netLikes变成100
    • c.netLikes % 100 == 0 => 100 % 100 == 0 => 真
    • c.netLikes / 100 == c.rewards + 1 => 100 / 100 == 0 + 1 => 真
    • _mint() 被调用并且 rewards 变成 1
  2. netLikes 100,likes 100,dislikes 0,rewards 1,一个新的不喜欢:
    • dislikes变成1,netLikes变成99
  3. netLikes 99, likes 100, dislikes 1, rewards 1, 一个新的赞:
    • likes变成101,netLikes变成100
    • c.netLikes % 100 == 0 => 100 % 100 == 0 => 真
    • c.netLikes / 100 == c.rewards + 1 => 100 / 100 == 1 + 1 =>
    • 未调用“mint”片段