Bài học 3

管理市場列錶

在介紹了去中心化市場中商品的創建、上架和購買商品這一繫列流程後,我們將繼續學習添加兩個新功能:從在售列錶中移除商品和更新商品價格,以此來增強我們的智能合約。

增強Marketplace合約

在本節中,我們將介紹兩個新函數:removeItemFromSaleupdateItemPrice。這兩個函數的作用分別是:允許賣家將其商品從在售商品列錶中移除以及更新商品價格。

增強的Marketplace合約代碼如下:

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;

contract Marketplace {
    // Define a new structure for Items
    struct Item {
        string name;
        uint price;
        address payable seller;
        bool forSale;
    }

    // Array to hold all the items
    Item[] public items;

    // Event definitions omitted for brevity

    // Other function definitions omitted for brevity

    // Function to remove an item from sale
    function removeItemFromSale(uint _itemId) public {
        Item storage item = items[_itemId];
        require(msg.sender == item.seller, "Only the owner can remove the item from sale");
        item.forSale = false;
    }

    // Function to update the price of an item
    function updateItemPrice(uint _itemId, uint _newPrice) public {
        Item storage item = items[_itemId];
        require(msg.sender == item.seller, "Only the owner can update the price");
        item.price = _newPrice;
    }
}

removeItemFromSale函數中,我們首先通過_itemId檢索商品,然後檢查調用該函數(msg. sender)的人是否爲商品的賣家。若是,我們將商品的forSale屬性設置爲false,從而將其從在售列錶中移除。

衕樣地,在updateItemPrice函數中,我們通過提供的_itemId檢索商品,併檢查msg.sender是否爲賣家。若是,我們將商品的價格更新爲_newPrice

部署併運行增強版Marketplace合約

完成增強Marketplace合約的編寫後,按照之前的課程中所介紹的方法進行編譯和部署即可。在編譯和部署之前,一定要記得從Solidity Compiler插件的下拉菜單中選擇正確的合約。

合約部署完成後,它將出現在“Deploy & Run Transactions”插件的“Deployed Contracts”闆塊。在這裡,您可以運行該合約。

要將商品從在售列錶中移除,請在removeItemFromSale函數中輸入商品ID併單擊按鈕。要更新商品的價格,需要在updateItemPrice函數中輸入將商品ID和新價格併單擊按鈕。

完成以上操作後,您便在以太坊區塊鏈上構建了一個基礎但功能齊全的去中心化市場。您可以使用此智能合約創建、上架、購買、移除和更新商品。

在下一課中,我們將討論如何處理合約中可能存在的安全漏洞,併介紹修飾符以進一步簡化代碼。敬請關註!

Tuyên bố từ chối trách nhiệm
* Đầu tư tiền điện tử liên quan đến rủi ro đáng kể. Hãy tiến hành một cách thận trọng. Khóa học không nhằm mục đích tư vấn đầu tư.
* Khóa học được tạo bởi tác giả đã tham gia Gate Learn. Mọi ý kiến chia sẻ của tác giả không đại diện cho Gate Learn.
Danh mục
Bài học 3

管理市場列錶

在介紹了去中心化市場中商品的創建、上架和購買商品這一繫列流程後,我們將繼續學習添加兩個新功能:從在售列錶中移除商品和更新商品價格,以此來增強我們的智能合約。

增強Marketplace合約

在本節中,我們將介紹兩個新函數:removeItemFromSaleupdateItemPrice。這兩個函數的作用分別是:允許賣家將其商品從在售商品列錶中移除以及更新商品價格。

增強的Marketplace合約代碼如下:

Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;

contract Marketplace {
    // Define a new structure for Items
    struct Item {
        string name;
        uint price;
        address payable seller;
        bool forSale;
    }

    // Array to hold all the items
    Item[] public items;

    // Event definitions omitted for brevity

    // Other function definitions omitted for brevity

    // Function to remove an item from sale
    function removeItemFromSale(uint _itemId) public {
        Item storage item = items[_itemId];
        require(msg.sender == item.seller, "Only the owner can remove the item from sale");
        item.forSale = false;
    }

    // Function to update the price of an item
    function updateItemPrice(uint _itemId, uint _newPrice) public {
        Item storage item = items[_itemId];
        require(msg.sender == item.seller, "Only the owner can update the price");
        item.price = _newPrice;
    }
}

removeItemFromSale函數中,我們首先通過_itemId檢索商品,然後檢查調用該函數(msg. sender)的人是否爲商品的賣家。若是,我們將商品的forSale屬性設置爲false,從而將其從在售列錶中移除。

衕樣地,在updateItemPrice函數中,我們通過提供的_itemId檢索商品,併檢查msg.sender是否爲賣家。若是,我們將商品的價格更新爲_newPrice

部署併運行增強版Marketplace合約

完成增強Marketplace合約的編寫後,按照之前的課程中所介紹的方法進行編譯和部署即可。在編譯和部署之前,一定要記得從Solidity Compiler插件的下拉菜單中選擇正確的合約。

合約部署完成後,它將出現在“Deploy & Run Transactions”插件的“Deployed Contracts”闆塊。在這裡,您可以運行該合約。

要將商品從在售列錶中移除,請在removeItemFromSale函數中輸入商品ID併單擊按鈕。要更新商品的價格,需要在updateItemPrice函數中輸入將商品ID和新價格併單擊按鈕。

完成以上操作後,您便在以太坊區塊鏈上構建了一個基礎但功能齊全的去中心化市場。您可以使用此智能合約創建、上架、購買、移除和更新商品。

在下一課中,我們將討論如何處理合約中可能存在的安全漏洞,併介紹修飾符以進一步簡化代碼。敬請關註!

Tuyên bố từ chối trách nhiệm
* Đầu tư tiền điện tử liên quan đến rủi ro đáng kể. Hãy tiến hành một cách thận trọng. Khóa học không nhằm mục đích tư vấn đầu tư.
* Khóa học được tạo bởi tác giả đã tham gia Gate Learn. Mọi ý kiến chia sẻ của tác giả không đại diện cho 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.