Cryptozombies第1课第13章。无法理解Cryptozombies中的事件

发布于 2025-02-06 06:20:36 字数 908 浏览 1 评论 0原文

我正在学习Cryptozombies的坚固性。我刚刚完成了12章,并有了良好的理解。但是,我不了解事件概念。我想了解完整的代码。有人可以帮我吗?

pragma solidity >=0.5.0 <0.6.0;

contract ZombieFactory {

    event NewZombie(uint zombieId, string name, uint dna);

    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;

    struct Zombie {
        string name;
        uint dna;
    }

    Zombie[] public zombies;

    function _createZombie(string memory _name, uint _dna) private {
        uint id = zombies.push(Zombie(_name, _dna)) - 1;
        emit NewZombie(id, _name, _dna);
    }

    function _generateRandomDna(string memory _str) private view returns (uint) {
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    function createRandomZombie(string memory _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

I am learning solidity in cryptozombies. I just finished 12 chapters with the good understanding. but, I don't understand the events concept. I want to understand the full code. can somebody help me with that??

pragma solidity >=0.5.0 <0.6.0;

contract ZombieFactory {

    event NewZombie(uint zombieId, string name, uint dna);

    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;

    struct Zombie {
        string name;
        uint dna;
    }

    Zombie[] public zombies;

    function _createZombie(string memory _name, uint _dna) private {
        uint id = zombies.push(Zombie(_name, _dna)) - 1;
        emit NewZombie(id, _name, _dna);
    }

    function _generateRandomDna(string memory _str) private view returns (uint) {
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    function createRandomZombie(string memory _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝梦月影 2025-02-13 06:20:39

事件视为您在一个地方放置的代码作品,如果您想在那里发生某些事情,请立即被通知。

    contract test{
    function Alert() public returns (string memory) {
    string memory str = "Alert! Do Something.";
    return str;
    }
}

例如,在此代码中,您希望在最终用户屏幕上通知您,以便您希望您的前端能够获得此信息!正确的?您将如何做到这一点? 通过创建一个事件,您的前端可以与(特别是从中读取)。因此,您将修改上述代码以添加 event ,如下所示:

    contract test{
event readAlert(string str);
    function Alert() public returns (string memory) {
    string memory str = "Alert! Do Something.";
    return str;
    emit readAlert(str);
    }
}

完成此操作后,您可以使用刚刚创建的事件来完成应用程序前端传递的信息。

让我知道您是否需要进一步的解释。 :)

Think of Events as the code piece that you put in a place where you want to be notified instantly if something happens there.

    contract test{
    function Alert() public returns (string memory) {
    string memory str = "Alert! Do Something.";
    return str;
    }
}

for example in this code here you would want to be notified on your end-user screen so in order to that you would want your front-end to be able to get this info! right? and how would you go about doing that? by creating an event with which your front-end can interact with (particularly read from). Therefore you would modify the above code to add event to this as follows:

    contract test{
event readAlert(string str);
    function Alert() public returns (string memory) {
    string memory str = "Alert! Do Something.";
    return str;
    emit readAlert(str);
    }
}

After doing this you can now use the event you just created to do something with the info passed to it on you app front-end.

Let me know if you need further explanation. :)

∞梦里开花 2025-02-13 06:20:38

事件的功能是,当调用者调用此功能时,将添加附加日志。

​无法正确显示Hodler的数量,也不会显示ERC20令牌用户转移的详细交易历史记录。

让我们得出的结论是,添加事件功能将使区块链浏览器上的搜索器更容易理解或分析每个事务,但这也将使区块链上的搜索者更容易找到交易或过滤它们以进行一些套利:)

The function of the event is that when the caller invokes this function, an additional logs will be added to the transaction content.

enter image description here

enter image description here

For a common example of ERC20 tokens, if the contract creator didn't add the event function when writing the Transfer function, something interesting would happen, the blockchain browser would not display the number of Hodlers properly and it would not show the detailed transaction history of the user-user transfer of ERC20 tokens.

Let's conclude that adding the event function will make it easier for the searcher on the blockchain browser to understand or analyze each transaction, but it will also make it easier for the searcher on the blockchain to find transactions or filter them for some arbitrage :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文