第4课

智能合约的交互与部署

在第四课中,我们将讨论智能合约的交互和部署。您将学习如何与外部合约交互、如何使用事件和日志,以及如何使用Remix和MetaMask部署智能合约。本节将为您提供可应用于实际项目开发的实用技能。

与外部合约交互

智能合约可以与以太坊区块链上的其他合约进行交互,允许它们调用函数、读取变量以及发送以太币或其他代币。促进这种交互的一种方法是使用Web3j(一个用于处理以太坊智能合约的轻量级Java库)。Web3j可以自动生成智能合约包装器代码,从而实现从JVM无缝部署智能合约并与之交互。

要使用Web3j与外部合约交互,首先需要编译智能合约并生成包装器代码。然后,您可以创建和部署智能合约或使用现有合约,从而轻松进行交易并直接调用智能合约。

事件和日志

事件对于跟踪和监控区块链上的智能合约活动至关重要。它们提供了一种发出日志的方法,这些日志可以存储起来,并在链下系统中检索。事件可以更轻松地跟踪特定的合约事件或状态变量的变化,这对于需要实时更新的dApp(去中心化应用)很有帮助。

日志是事件发出的记录,存储在区块链上,可以实现智能合约和链下系统之间的高效通信,是以太坊生态系统的重要组成部分。此外,针对日志建立索引,使应用程序可以轻松过滤和搜索特定的事件或数据点。

示例:使用Remix和MetaMask部署智能合约

第1步:打开Remix IDE
首先,在Web浏览器中打开Remix IDE(https://remix.ethereum.org/)。

第2步:创建新文件
单击IDE左上角的“+”按钮,创建一个新的空白工作区,然后单击“New File”创建一个新文件。

将文件命名为“Auction. sol”。

第3步:定义合约
将以下代码复制并粘贴到新的“Auction. sol”文件中:

TypeScript
// SPDX-License-Identifier: MIT
// Specify the Solidity version
pragma solidity ^0.8.0;

// Define the Auction contract
contract Auction {
    // Declare state variables
    address payable public owner; // The owner of the contract (can cancel the auction)
    uint public startBlock; // The block number at which the auction starts
    uint public endBlock; // The block number at which the auction ends
    string public ipfsHash; // IPFS hash for the item being auctioned
    bool public canceled; // Whether the auction has been canceled
    bool public ended; // Whether the auction has ended
    uint public highestBid; // The highest bid so far
    address payable public highestBidder; // The address of the highest bidder

    // Declare events
    event AuctionCanceled(); // Event emitted when the auction is canceled
    event HighestBidIncreased(address bidder, uint amount); // Event emitted when a new highest bid is set
    event AuctionEnded(address winner, uint amount); // Event emitted when the auction ends

      // Declare mapping
    mapping(address => uint256) public balances;

    // Constructor function
    constructor() {
        owner = payable(msg.sender); // Set the owner to the address that deploys the contract
        startBlock = block.number; // Set the start block to the current block number
        endBlock = startBlock + 40320; // Set the end block to 1 week (40320 blocks) after the start block
        ipfsHash = ""; // Initialize the IPFS hash to an empty string
    }

    // Function to place a bid
    function placeBid() public payable {
        require(block.number >= startBlock && block.number <= endBlock, "Auction is not active."); // Check that the auction is active
        require(msg.value > highestBid, "There is already a higher bid."); // Check that the new bid is higher than the current highest bid
        require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled

        // If there was a previous highest bidder, add their bid amount to their balance
        if (highestBidder != address(0)) {
        balances[highestBidder] += highestBid;
    }

        // Set the new highest bid and bidder
        highestBid = msg.value;
        highestBidder = payable(msg.sender);

        // Emit an event to signal a new highest bid has been set
        emit HighestBidIncreased(msg.sender, msg.value);
    }

    // Function to cancel the auction
    function cancelAuction() public {
        require(msg.sender == owner, "Only the owner can cancel the auction."); // Check that the sender is the owner
        require(!ended, "Auction has already ended."); // Check that the auction has not already ended

        // Set the canceled flag and emit an event to signal the auction has been canceled
        canceled = true;
        emit AuctionCanceled();
    }

    // Function to end the auction
    function endAuction() public {
        require(block.number > endBlock, "Auction is not over yet."); // Check that the auction is over
        require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled
        require(!ended, "Auction has already ended."); // Check that the auction has not already ended

        // Set the ended flag and emit an event to signal the auction has ended
        ended = true;
        emit AuctionEnded(highestBidder, highestBid);

        // Transfer the highest bid amount to the owner
        owner.transfer(highestBid);

        // Ifthere was a previous highest bidder, add their bid amount to their balance
        if (highestBidder != address(0)) {
        balances[highestBidder] += highestBid;
        }
    }

    // Function to set the IPFS hash for the item being auctioned
    function setIpfsHash(string memory hash) public {
        require(msg.sender == owner, "Only the owner can set the IPFS hash."); // Check that the sender is the owner
        ipfsHash = hash; // Set the IPFS hash to the provided value
    }
}

本段代码是一个拍卖合约,允许用户对物品进行竞标,并在指定期限后结束拍卖。该合约还有一个取消拍卖的函数和为正在拍卖的物品设置IPFS哈希值的函数。

第4步:编译合约

单击左侧菜单中的“Solidity Compiler”选项卡。在“Compile Auction.sol”下,单击“Compile”按钮。合约编译成功后,文件资源管理器(FILE EXPLORER)中可以看到“Auction.sol”旁会显示一个绿色的勾号。

第5步:部署合约

点击左侧菜单中的“Deploy & run transactions”选项卡。在“Environment”下,选择“Injected Web3”。在“Contract”下,选择“Auction”作为要部署的合约。单击“Deploy”按钮。

第6步:与合约进行交互
合约部署完成后,您可以使用合约中定义的各种函数与之交互。例如,您可以调用place eBid()函数对物品进行竞标。

通过使用Remix和MetaMask,您可以轻松地在以太坊网络上部署智能合约并与之交互,从而在用户友好的环境中实现去中心化应用的开发和测试。

要点

  • 智能合约可以与以太坊区块链上的其他合约交互,实现函数调用、变量读取以及以太币或其他代币的转移。
  • Web3j是一个用于与以太坊交互的轻量级Java库,可以自动生成智能合约包装器代码,方便从Java虚拟机(JVM)部署智能合约并与之交互。
  • 事件对于跟踪和监控区块链上的合约活动至关重要。它们发出可以由链下系统存储和检索的日志,从而实现dApp的实时更新。
  • 日志是事件发出的记录,在智能合约和链下系统之间的高效通信中发挥着至关重要的作用。日志经过索引,可以方便地过滤和搜索特定事件或数据点。
  • 本课中提供的示例演示了使用Remix IDE和MetaMask部署智能合约的过程,包括创建新文件、定义合约、编译、部署合约以及与合约函数进行交互等步骤。
免责声明
* 投资有风险,入市须谨慎。本课程不作为投资理财建议。
* 本课程由入驻Gate Learn的作者创作,观点仅代表作者本人,绝不代表Gate Learn赞同其观点或证实其描述。
目录
第4课

智能合约的交互与部署

在第四课中,我们将讨论智能合约的交互和部署。您将学习如何与外部合约交互、如何使用事件和日志,以及如何使用Remix和MetaMask部署智能合约。本节将为您提供可应用于实际项目开发的实用技能。

与外部合约交互

智能合约可以与以太坊区块链上的其他合约进行交互,允许它们调用函数、读取变量以及发送以太币或其他代币。促进这种交互的一种方法是使用Web3j(一个用于处理以太坊智能合约的轻量级Java库)。Web3j可以自动生成智能合约包装器代码,从而实现从JVM无缝部署智能合约并与之交互。

要使用Web3j与外部合约交互,首先需要编译智能合约并生成包装器代码。然后,您可以创建和部署智能合约或使用现有合约,从而轻松进行交易并直接调用智能合约。

事件和日志

事件对于跟踪和监控区块链上的智能合约活动至关重要。它们提供了一种发出日志的方法,这些日志可以存储起来,并在链下系统中检索。事件可以更轻松地跟踪特定的合约事件或状态变量的变化,这对于需要实时更新的dApp(去中心化应用)很有帮助。

日志是事件发出的记录,存储在区块链上,可以实现智能合约和链下系统之间的高效通信,是以太坊生态系统的重要组成部分。此外,针对日志建立索引,使应用程序可以轻松过滤和搜索特定的事件或数据点。

示例:使用Remix和MetaMask部署智能合约

第1步:打开Remix IDE
首先,在Web浏览器中打开Remix IDE(https://remix.ethereum.org/)。

第2步:创建新文件
单击IDE左上角的“+”按钮,创建一个新的空白工作区,然后单击“New File”创建一个新文件。

将文件命名为“Auction. sol”。

第3步:定义合约
将以下代码复制并粘贴到新的“Auction. sol”文件中:

TypeScript
// SPDX-License-Identifier: MIT
// Specify the Solidity version
pragma solidity ^0.8.0;

// Define the Auction contract
contract Auction {
    // Declare state variables
    address payable public owner; // The owner of the contract (can cancel the auction)
    uint public startBlock; // The block number at which the auction starts
    uint public endBlock; // The block number at which the auction ends
    string public ipfsHash; // IPFS hash for the item being auctioned
    bool public canceled; // Whether the auction has been canceled
    bool public ended; // Whether the auction has ended
    uint public highestBid; // The highest bid so far
    address payable public highestBidder; // The address of the highest bidder

    // Declare events
    event AuctionCanceled(); // Event emitted when the auction is canceled
    event HighestBidIncreased(address bidder, uint amount); // Event emitted when a new highest bid is set
    event AuctionEnded(address winner, uint amount); // Event emitted when the auction ends

      // Declare mapping
    mapping(address => uint256) public balances;

    // Constructor function
    constructor() {
        owner = payable(msg.sender); // Set the owner to the address that deploys the contract
        startBlock = block.number; // Set the start block to the current block number
        endBlock = startBlock + 40320; // Set the end block to 1 week (40320 blocks) after the start block
        ipfsHash = ""; // Initialize the IPFS hash to an empty string
    }

    // Function to place a bid
    function placeBid() public payable {
        require(block.number >= startBlock && block.number <= endBlock, "Auction is not active."); // Check that the auction is active
        require(msg.value > highestBid, "There is already a higher bid."); // Check that the new bid is higher than the current highest bid
        require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled

        // If there was a previous highest bidder, add their bid amount to their balance
        if (highestBidder != address(0)) {
        balances[highestBidder] += highestBid;
    }

        // Set the new highest bid and bidder
        highestBid = msg.value;
        highestBidder = payable(msg.sender);

        // Emit an event to signal a new highest bid has been set
        emit HighestBidIncreased(msg.sender, msg.value);
    }

    // Function to cancel the auction
    function cancelAuction() public {
        require(msg.sender == owner, "Only the owner can cancel the auction."); // Check that the sender is the owner
        require(!ended, "Auction has already ended."); // Check that the auction has not already ended

        // Set the canceled flag and emit an event to signal the auction has been canceled
        canceled = true;
        emit AuctionCanceled();
    }

    // Function to end the auction
    function endAuction() public {
        require(block.number > endBlock, "Auction is not over yet."); // Check that the auction is over
        require(!canceled, "Auction is canceled."); // Check that the auction has not been canceled
        require(!ended, "Auction has already ended."); // Check that the auction has not already ended

        // Set the ended flag and emit an event to signal the auction has ended
        ended = true;
        emit AuctionEnded(highestBidder, highestBid);

        // Transfer the highest bid amount to the owner
        owner.transfer(highestBid);

        // Ifthere was a previous highest bidder, add their bid amount to their balance
        if (highestBidder != address(0)) {
        balances[highestBidder] += highestBid;
        }
    }

    // Function to set the IPFS hash for the item being auctioned
    function setIpfsHash(string memory hash) public {
        require(msg.sender == owner, "Only the owner can set the IPFS hash."); // Check that the sender is the owner
        ipfsHash = hash; // Set the IPFS hash to the provided value
    }
}

本段代码是一个拍卖合约,允许用户对物品进行竞标,并在指定期限后结束拍卖。该合约还有一个取消拍卖的函数和为正在拍卖的物品设置IPFS哈希值的函数。

第4步:编译合约

单击左侧菜单中的“Solidity Compiler”选项卡。在“Compile Auction.sol”下,单击“Compile”按钮。合约编译成功后,文件资源管理器(FILE EXPLORER)中可以看到“Auction.sol”旁会显示一个绿色的勾号。

第5步:部署合约

点击左侧菜单中的“Deploy & run transactions”选项卡。在“Environment”下,选择“Injected Web3”。在“Contract”下,选择“Auction”作为要部署的合约。单击“Deploy”按钮。

第6步:与合约进行交互
合约部署完成后,您可以使用合约中定义的各种函数与之交互。例如,您可以调用place eBid()函数对物品进行竞标。

通过使用Remix和MetaMask,您可以轻松地在以太坊网络上部署智能合约并与之交互,从而在用户友好的环境中实现去中心化应用的开发和测试。

要点

  • 智能合约可以与以太坊区块链上的其他合约交互,实现函数调用、变量读取以及以太币或其他代币的转移。
  • Web3j是一个用于与以太坊交互的轻量级Java库,可以自动生成智能合约包装器代码,方便从Java虚拟机(JVM)部署智能合约并与之交互。
  • 事件对于跟踪和监控区块链上的合约活动至关重要。它们发出可以由链下系统存储和检索的日志,从而实现dApp的实时更新。
  • 日志是事件发出的记录,在智能合约和链下系统之间的高效通信中发挥着至关重要的作用。日志经过索引,可以方便地过滤和搜索特定事件或数据点。
  • 本课中提供的示例演示了使用Remix IDE和MetaMask部署智能合约的过程,包括创建新文件、定义合约、编译、部署合约以及与合约函数进行交互等步骤。
免责声明
* 投资有风险,入市须谨慎。本课程不作为投资理财建议。
* 本课程由入驻Gate Learn的作者创作,观点仅代表作者本人,绝不代表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, 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.