บทเรียนที่ 3

Managing Marketplace Listings

After covering the creation, listing, and purchasing of items in our decentralized marketplace, we will now focus on enhancing our smart contract by adding functionality for removing items from sale and updating the price of an item.

Enhancing the Marketplace Contract

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.

Here’s the enhanced 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.

Deploying and Interacting with the Enhanced Marketplace Contract

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!

ข้อจำกัดความรับผิด
* การลงทุนคริปโตมีความเสี่ยงสูง โปรดดำเนินการด้วยความระมัดระวัง หลักสูตรนี้ไม่ได้มีไว้เพื่อเป็นคำแนะนำในการลงทุน
* หลักสูตรนี้สร้างขึ้นโดยผู้เขียนที่ได้เข้าร่วม Gate Learn ความคิดเห็นของผู้เขียนไม่ได้มาจาก Gate Learn
แคตตาล็อก
บทเรียนที่ 3

Managing Marketplace Listings

After covering the creation, listing, and purchasing of items in our decentralized marketplace, we will now focus on enhancing our smart contract by adding functionality for removing items from sale and updating the price of an item.

Enhancing the Marketplace Contract

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.

Here’s the enhanced 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.

Deploying and Interacting with the Enhanced Marketplace Contract

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!

ข้อจำกัดความรับผิด
* การลงทุนคริปโตมีความเสี่ยงสูง โปรดดำเนินการด้วยความระมัดระวัง หลักสูตรนี้ไม่ได้มีไว้เพื่อเป็นคำแนะนำในการลงทุน
* หลักสูตรนี้สร้างขึ้นโดยผู้เขียนที่ได้เข้าร่วม Gate Learn ความคิดเห็นของผู้เขียนไม่ได้มาจาก Gate Learn