在本節中,我們將介紹兩個新函數: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和新價格併單擊按鈕。
完成以上操作後,您便在以太坊區塊鏈上構建了一個基礎但功能齊全的去中心化市場。您可以使用此智能合約創建、上架、購買、移除和更新商品。
在下一課中,我們將討論如何處理合約中可能存在的安全漏洞,併介紹修飾符以進一步簡化代碼。敬請關註!