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.