第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讚同其觀點或證實其描述。