第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和新价格并单击按钮。

完成以上操作后,您便在以太坊区块链上构建了一个基础但功能齐全的去中心化市场。您可以使用此智能合约创建、上架、购买、移除和更新商品。

在下一课中,我们将讨论如何处理合约中可能存在的安全漏洞,并介绍修饰符以进一步简化代码。敬请关注!

免责声明
* 投资有风险,入市须谨慎。本课程不作为投资理财建议。
* 本课程由入驻Gate Learn的作者创作,观点仅代表作者本人,绝不代表Gate Learn赞同其观点或证实其描述。
目录
第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和新价格并单击按钮。

完成以上操作后,您便在以太坊区块链上构建了一个基础但功能齐全的去中心化市场。您可以使用此智能合约创建、上架、购买、移除和更新商品。

在下一课中,我们将讨论如何处理合约中可能存在的安全漏洞,并介绍修饰符以进一步简化代码。敬请关注!

免责声明
* 投资有风险,入市须谨慎。本课程不作为投资理财建议。
* 本课程由入驻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.