在本节中,我们将介绍两个新函数:removeItemFromSale
和updateItemPrice
。这两个函数的作用分别是:允许卖家将其商品从在售商品列表中移除以及更新商品价格。
增强的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
合约的编写后,按照之前的课程中所介绍的方法进行编译和部署即可。在编译和部署之前,一定要记得从Solidity Compiler插件的下拉菜单中选择正确的合约。
合约部署完成后,它将出现在“Deploy & Run Transactions”插件的“Deployed Contracts”板块。在这里,您可以运行该合约。
要将商品从在售列表中移除,请在removeItemFromSale
函数中输入商品ID并单击按钮。要更新商品的价格,需要在updateItemPrice
函数中输入将商品ID和新价格并单击按钮。
完成以上操作后,您便在以太坊区块链上构建了一个基础但功能齐全的去中心化市场。您可以使用此智能合约创建、上架、购买、移除和更新商品。
在下一课中,我们将讨论如何处理合约中可能存在的安全漏洞,并介绍修饰符以进一步简化代码。敬请关注!
在本节中,我们将介绍两个新函数:removeItemFromSale
和updateItemPrice
。这两个函数的作用分别是:允许卖家将其商品从在售商品列表中移除以及更新商品价格。
增强的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
合约的编写后,按照之前的课程中所介绍的方法进行编译和部署即可。在编译和部署之前,一定要记得从Solidity Compiler插件的下拉菜单中选择正确的合约。
合约部署完成后,它将出现在“Deploy & Run Transactions”插件的“Deployed Contracts”板块。在这里,您可以运行该合约。
要将商品从在售列表中移除,请在removeItemFromSale
函数中输入商品ID并单击按钮。要更新商品的价格,需要在updateItemPrice
函数中输入将商品ID和新价格并单击按钮。
完成以上操作后,您便在以太坊区块链上构建了一个基础但功能齐全的去中心化市场。您可以使用此智能合约创建、上架、购买、移除和更新商品。
在下一课中,我们将讨论如何处理合约中可能存在的安全漏洞,并介绍修饰符以进一步简化代码。敬请关注!