Lesson 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中模擬整個投票過程。最後,我們探討了不衕的合約之間如何交互,以及如何記録事件以跟蹤區塊鏈上的活動。

您從本課程中穫得的技能和知識將不僅局限於用來創建投票繫統,還可以用於其他類型的去中心化應用的開髮。所有,不要停止,繼續您的練習、實驗和開髮之旅吧!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.
Catalog
Lesson 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中模擬整個投票過程。最後,我們探討了不衕的合約之間如何交互,以及如何記録事件以跟蹤區塊鏈上的活動。

您從本課程中穫得的技能和知識將不僅局限於用來創建投票繫統,還可以用於其他類型的去中心化應用的開髮。所有,不要停止,繼續您的練習、實驗和開髮之旅吧!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent 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.