gpt4 book ai didi

solidity - 用每第 n 个类似的数字类型转换 ERC token 的函数

转载 作者:行者123 更新时间:2023-12-04 03:35:16 25 4
gpt4 key购买 nike

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

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

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

}

最佳答案

您可以将奖励的计数器添加到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. netLikes 99,likes 99,dislikes 0,rewards 0,一个新的喜欢:
    • likes 变为 100,netLikes 变为 100
    • c.netLikes % 100 == 0 => 100 % 100 == 0 => true
    • c.netLikes/100 == c.rewards + 1 => 100/100 == 0 + 1 => true
    • _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 => true
    • c.netLikes/100 == c.rewards + 1 => 100/100 == 1 + 1 => false
    • 没有调用“mint”片段

关于solidity - 用每第 n 个类似的数字类型转换 ERC token 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67031948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com