Lección 4

合约交互与事件记录

在最后一章中,我们将探讨不同合约的交互过程。我们还将学习如何通过事件记录来监控区块链上的活动。

合约交互

我们要学习的第一个概念是合约交互。合约交互是指合约如何通信和共享信息。在Solidity中,一个合约可以调用其他合约的函数,创建其他合约,甚至将以太币发送到其他合约或地址。

在我们构建的投票系统中,已经看到了合约的交互过程。每发出一次投票,投票合约都会与自己交互,以更新投票人的状态和该提案所获得的票数。

以下是一个简单的合约交互示例:

Solidity
pragma solidity >=0.7.0 <0.9.0;

contract CalledContract {
    uint public x;

    function setX(uint _x) public {
        x = _x;
    }
}

contract CallerContract {
    function callSetX(address _calledContractAddress, uint _x) public {
        CalledContract calledContract = CalledContract(_calledContractAddress);
        calledContract.setX(_x);
    }
}

在本例中,CallerContract通过调用setX函数与CalledContract进行交互。

事件记录

接下来,我们来了解一下什么是事件记录。事件是您的合约传达外部世界所发生事件的一种方式。例如,在我们的投票系统中,每次投票时,我们可能希望触发一个事件。实现方法如下:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Voter {
    // Event definition
    event VoteCast(address voter, uint proposal);

    // Rest of the contract...

    function vote(uint _proposal) public {
        Person storage sender = voters[msg.sender];
        require(!sender.voted, "Already voted.");
        sender.voted = true;
        sender.vote = _proposal;

        proposals[_proposal].voteCount += 1;

        // Emit event
        emit VoteCast(msg.sender, _proposal);
    }

    // Rest of the contract...
}

在更新后的vote函数中,每次进行投票时,我们都会触发一个VoteCast事件。该事件记录了投票人的地址和他们投票支持的提案。

在本章中,我们学习了合约交互和事件记录,这是Solidity中的两个基本概念。通过从本章中学到的知识,您将能够在以太坊上开发更复杂的去中心化应用。

结语

恭喜!您已经成功完成了《构建去中心化投票系统》课程的学习。

在本课程中,您学习了去中心化投票系统相关的核心概念,掌握了如何在Remix IDE上使用Solidity实现一个去中心化投票系统。我们从了解去中心化投票的定义和重要性开始,深入学习了在智能合约中添加投票人注册、投票和计票等核心功能。我们还学习了如何与我们的合约交互,并在Remix IDE中模拟整个投票过程。最后,我们探讨了不同的合约之间如何交互,以及如何记录事件以跟踪区块链上的活动。

您从本课程中获得的技能和知识将不仅局限于用来创建投票系统,还可以用于其他类型的去中心化应用的开发。所有,不要停止,继续您的练习、实验和开发之旅吧!

Descargo de responsabilidad
* La inversión en criptomonedas implica riesgos significativos. Proceda con precaución. El curso no pretende ser un asesoramiento de inversión.
* El curso ha sido creado por el autor que se ha unido a Gate Learn. Cualquier opinión compartida por el autor no representa a Gate Learn.
Catálogo
Lección 4

合约交互与事件记录

在最后一章中,我们将探讨不同合约的交互过程。我们还将学习如何通过事件记录来监控区块链上的活动。

合约交互

我们要学习的第一个概念是合约交互。合约交互是指合约如何通信和共享信息。在Solidity中,一个合约可以调用其他合约的函数,创建其他合约,甚至将以太币发送到其他合约或地址。

在我们构建的投票系统中,已经看到了合约的交互过程。每发出一次投票,投票合约都会与自己交互,以更新投票人的状态和该提案所获得的票数。

以下是一个简单的合约交互示例:

Solidity
pragma solidity >=0.7.0 <0.9.0;

contract CalledContract {
    uint public x;

    function setX(uint _x) public {
        x = _x;
    }
}

contract CallerContract {
    function callSetX(address _calledContractAddress, uint _x) public {
        CalledContract calledContract = CalledContract(_calledContractAddress);
        calledContract.setX(_x);
    }
}

在本例中,CallerContract通过调用setX函数与CalledContract进行交互。

事件记录

接下来,我们来了解一下什么是事件记录。事件是您的合约传达外部世界所发生事件的一种方式。例如,在我们的投票系统中,每次投票时,我们可能希望触发一个事件。实现方法如下:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Voter {
    // Event definition
    event VoteCast(address voter, uint proposal);

    // Rest of the contract...

    function vote(uint _proposal) public {
        Person storage sender = voters[msg.sender];
        require(!sender.voted, "Already voted.");
        sender.voted = true;
        sender.vote = _proposal;

        proposals[_proposal].voteCount += 1;

        // Emit event
        emit VoteCast(msg.sender, _proposal);
    }

    // Rest of the contract...
}

在更新后的vote函数中,每次进行投票时,我们都会触发一个VoteCast事件。该事件记录了投票人的地址和他们投票支持的提案。

在本章中,我们学习了合约交互和事件记录,这是Solidity中的两个基本概念。通过从本章中学到的知识,您将能够在以太坊上开发更复杂的去中心化应用。

结语

恭喜!您已经成功完成了《构建去中心化投票系统》课程的学习。

在本课程中,您学习了去中心化投票系统相关的核心概念,掌握了如何在Remix IDE上使用Solidity实现一个去中心化投票系统。我们从了解去中心化投票的定义和重要性开始,深入学习了在智能合约中添加投票人注册、投票和计票等核心功能。我们还学习了如何与我们的合约交互,并在Remix IDE中模拟整个投票过程。最后,我们探讨了不同的合约之间如何交互,以及如何记录事件以跟踪区块链上的活动。

您从本课程中获得的技能和知识将不仅局限于用来创建投票系统,还可以用于其他类型的去中心化应用的开发。所有,不要停止,继续您的练习、实验和开发之旅吧!

Descargo de responsabilidad
* La inversión en criptomonedas implica riesgos significativos. Proceda con precaución. El curso no pretende ser un asesoramiento de inversión.
* El curso ha sido creado por el autor que se ha unido a Gate Learn. Cualquier opinión compartida por el autor no representa a Gate Learn.
It seems that you are attempting to access our services from a Restricted Location where Gate.io is unable to provide services. We apologize for any inconvenience this may cause. Currently, the Restricted Locations include but not limited to: the United States of America, Canada, Cambodia, Thailand, Cuba, Iran, North Korea and so on. For more information regarding the Restricted Locations, please refer to the User Agreement. Should you have any other questions, please contact our Customer Support Team.