In this section, we’ll introduce two new functions: removeItemFromSale
and updateItemPrice
. These functions will allow the seller to remove their item from sale and to update the price of their item, respectively.
Marketplace
contract: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;
}
}
In the removeItemFromSale
function, we first retrieve the item with the provided _itemId
. We then check that the person calling the function (msg.sender
) is the seller of the item. If this is the case, we set the forSale
property of the item to false
, effectively removing it from sale.
Similarly, in the updateItemPrice
function, we retrieve the item with the provided _itemId
, check that msg.sender
is the seller, and if so, we update the price of the item to the provided _newPrice
.
After enhancing the Marketplace
contract, compile and deploy it just like you did in previous lessons. Remember to select the correct contract from the dropdown menu in the Solidity compiler plugin before compiling and deploying.
Once the contract is deployed, it will appear under the Deployed Contracts section in the Deploy & Run Transactions plugin. Here, you can interact with the contract.
To remove an item from sale, enter the item ID into the removeItemFromSale
function and click the button. To update the price of an item, enter the item ID and the new price into the updateItemPrice
function and click the button.
With this, you’ve now built a basic but functional decentralized marketplace on the Ethereum blockchain. You can create, list, buy, remove, and update items with this smart contract. Great job!
In the next lesson, we will discuss how to handle potential security vulnerabilities in our contract and introduce modifiers to further streamline our code. Stay tuned!
In this section, we’ll introduce two new functions: removeItemFromSale
and updateItemPrice
. These functions will allow the seller to remove their item from sale and to update the price of their item, respectively.
Marketplace
contract: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;
}
}
In the removeItemFromSale
function, we first retrieve the item with the provided _itemId
. We then check that the person calling the function (msg.sender
) is the seller of the item. If this is the case, we set the forSale
property of the item to false
, effectively removing it from sale.
Similarly, in the updateItemPrice
function, we retrieve the item with the provided _itemId
, check that msg.sender
is the seller, and if so, we update the price of the item to the provided _newPrice
.
After enhancing the Marketplace
contract, compile and deploy it just like you did in previous lessons. Remember to select the correct contract from the dropdown menu in the Solidity compiler plugin before compiling and deploying.
Once the contract is deployed, it will appear under the Deployed Contracts section in the Deploy & Run Transactions plugin. Here, you can interact with the contract.
To remove an item from sale, enter the item ID into the removeItemFromSale
function and click the button. To update the price of an item, enter the item ID and the new price into the updateItemPrice
function and click the button.
With this, you’ve now built a basic but functional decentralized marketplace on the Ethereum blockchain. You can create, list, buy, remove, and update items with this smart contract. Great job!
In the next lesson, we will discuss how to handle potential security vulnerabilities in our contract and introduce modifiers to further streamline our code. Stay tuned!