使用每第 n 个喜欢来铸造 ERC 令牌的功能

时间:2021-04-10 07:43:23

标签: solidity ipfs

我需要一个函数的帮助,一旦用户的内容(Ipfs-Hash,见下面的程序)点击 n 次喜欢(比如每 100 个喜欢),即,只要 ' netLike 计数器达到 100,它会自动向相应用户的地址铸造一个代币。

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

我为此添加了随机数,但无法真正弄清楚确切的逻辑!! (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!")
            }
            
    }
    

1 个答案:

答案 0 :(得分:1)

您可以将 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) 仅适用于第 1 个奖励(第 100 个 netLike)。

更新后的代码:

  • c.netLikes % 100 == 0 检查 netLikes 是否能被 100 整除
  • c.netLikes / 100 == c.rewards + 1 验证尚未为此 100 提供奖励。

值示例:

  1. netLikes 99, likes 99, dislikes 0, rewards 0, 一个新的喜欢:
    • 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 => false
    • 未调用“mint”片段
相关问题