レッスン2

Listing and Trading Items

In this lesson, we will expand on our Item contract to handle listing and trading of items in our marketplace. We will learn about how to add, list, and remove items, as well as the basics of buying and selling items. We will also learn how to deploy our contract and interact with it in the Remix IDE.

Building the Marketplace Contract

We will now transform our simple Item contract into a more complex Marketplace contract. The Marketplace contract will maintain a list of items for sale, provide a way for users to list new items for sale, and provide a way for users to buy items.

To do this, we will add a few new features to our contract:

  1. Each item will now have a seller address, which represents the owner of the item.

  2. We will introduce a new function listItemForSale that allows users to list new items for sale.

  3. We will introduce a new function buyItem that allows users to buy an item.

Here’s what the Marketplace contract looks like:

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 that will be emitted when a new item is created
    event NewItem(uint itemId, string itemName, uint itemPrice, address seller);

    // Event that will be emitted when an item is listed for sale
    event ItemListed(uint itemId, string itemName, uint itemPrice, address seller);

    // Event that will be emitted when an item is bought
    event ItemBought(uint itemId, string itemName, uint itemPrice, address seller, address buyer);

    // Function to create a new item but not list it for sale immediately
    function createItem(string memory _name, uint _price) public {
    items.push(Item(_name, _price, payable(msg.sender), false)); // We need to explicitly convert msg.sender to 'address payable'
    emit NewItem(items.length - 1, _name, _price, msg.sender);
    }


    // Function to list an item for sale
    function listItemForSale(uint _itemId) public {
        Item storage item = items[_itemId];
        require(msg.sender == item.seller, "Only the owner can list the item for sale");
        item.forSale = true;
        emit ItemListed(_itemId, item.name, item.price, item.seller);
    }

    // Function to buy an item
    function buyItem(uint _itemId) public payable {
        Item storage item = items[_itemId];
        require(msg.sender != item.seller, "Seller cannot buy their own item");
        require(item.forSale, "Item is not for sale");
        require(msg.value == item.price, "Incorrect price sent");
        item.seller.transfer(msg.value);
        item.forSale = false;
        emit ItemBought(_itemId, item.name, item.price, item.seller, msg.sender);
    }
}

In this contract, we have:

  1. An Item struct that now includes a seller address and a forSale boolean. The seller is the owner of the item, and forSale indicates whether the item is currently listed for sale.

  2. A createItem function that creates a new item and assigns the msg.sender as the seller. The msg.sender is a global variable in Solidity that represents the address of the person (or smart contract) who is called the current function. However, the item is not listed for sale immediately.

  3. A listItemForSale function that allows the seller of an item to list it for sale. We use the require function to make sure that only the seller can list the item for sale.

  4. A buyItem function that allows someone to buy an item. The function checks that the item is for sale, the buyer is not the seller, and the correct price was sent. If these conditions are met, the function sends the money to the seller and marks the item as not for sale.

Deploying and Interacting with the Marketplace Contract

After writing the Marketplace contract, the next step is to compile and deploy it. You can use the Solidity Compiler plugin in Remix to compile the contract, just like we did in Lesson 1.

To deploy the contract, go to the Deploy & Run Transactions plugin (the one with the cube icon) on the right panel. Choose the appropriate environment (JavaScript VM for simulation), select the Marketplace contract from the Contract dropdown, and click the Deploy button.

Once the contract is deployed, it will appear under the Deployed Contracts section. You can expand it to see its public state variables and functions. You can create, list, and buy items by calling these functions.

To create an item, enter a name and a price, and click the createItem button. To list an item for sale, enter the item ID and click the listItemForSale button. To buy an item, enter the item ID, send the correct amount of Ether, and click the buyItem button.

Congratulations! You now know how to create a basic decentralized marketplace on the Ethereum blockchain.

In the next lesson, we will improve our marketplace by adding functionality for removing items from sale and updating the price of an item. Stay tuned!

免責事項
* 暗号資産投資には重倧なリスクが䌎いたす。泚意しお進めおください。このコヌスは投資アドバむスを目的ずしたものではありたせん。
※ このコヌスはGate Learnに参加しおいるメンバヌが䜜成したものです。䜜成者が共有した意芋はGate Learnを代衚するものではありたせん。
カタログ
レッスン2

Listing and Trading Items

In this lesson, we will expand on our Item contract to handle listing and trading of items in our marketplace. We will learn about how to add, list, and remove items, as well as the basics of buying and selling items. We will also learn how to deploy our contract and interact with it in the Remix IDE.

Building the Marketplace Contract

We will now transform our simple Item contract into a more complex Marketplace contract. The Marketplace contract will maintain a list of items for sale, provide a way for users to list new items for sale, and provide a way for users to buy items.

To do this, we will add a few new features to our contract:

  1. Each item will now have a seller address, which represents the owner of the item.

  2. We will introduce a new function listItemForSale that allows users to list new items for sale.

  3. We will introduce a new function buyItem that allows users to buy an item.

Here’s what the Marketplace contract looks like:

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 that will be emitted when a new item is created
    event NewItem(uint itemId, string itemName, uint itemPrice, address seller);

    // Event that will be emitted when an item is listed for sale
    event ItemListed(uint itemId, string itemName, uint itemPrice, address seller);

    // Event that will be emitted when an item is bought
    event ItemBought(uint itemId, string itemName, uint itemPrice, address seller, address buyer);

    // Function to create a new item but not list it for sale immediately
    function createItem(string memory _name, uint _price) public {
    items.push(Item(_name, _price, payable(msg.sender), false)); // We need to explicitly convert msg.sender to 'address payable'
    emit NewItem(items.length - 1, _name, _price, msg.sender);
    }


    // Function to list an item for sale
    function listItemForSale(uint _itemId) public {
        Item storage item = items[_itemId];
        require(msg.sender == item.seller, "Only the owner can list the item for sale");
        item.forSale = true;
        emit ItemListed(_itemId, item.name, item.price, item.seller);
    }

    // Function to buy an item
    function buyItem(uint _itemId) public payable {
        Item storage item = items[_itemId];
        require(msg.sender != item.seller, "Seller cannot buy their own item");
        require(item.forSale, "Item is not for sale");
        require(msg.value == item.price, "Incorrect price sent");
        item.seller.transfer(msg.value);
        item.forSale = false;
        emit ItemBought(_itemId, item.name, item.price, item.seller, msg.sender);
    }
}

In this contract, we have:

  1. An Item struct that now includes a seller address and a forSale boolean. The seller is the owner of the item, and forSale indicates whether the item is currently listed for sale.

  2. A createItem function that creates a new item and assigns the msg.sender as the seller. The msg.sender is a global variable in Solidity that represents the address of the person (or smart contract) who is called the current function. However, the item is not listed for sale immediately.

  3. A listItemForSale function that allows the seller of an item to list it for sale. We use the require function to make sure that only the seller can list the item for sale.

  4. A buyItem function that allows someone to buy an item. The function checks that the item is for sale, the buyer is not the seller, and the correct price was sent. If these conditions are met, the function sends the money to the seller and marks the item as not for sale.

Deploying and Interacting with the Marketplace Contract

After writing the Marketplace contract, the next step is to compile and deploy it. You can use the Solidity Compiler plugin in Remix to compile the contract, just like we did in Lesson 1.

To deploy the contract, go to the Deploy & Run Transactions plugin (the one with the cube icon) on the right panel. Choose the appropriate environment (JavaScript VM for simulation), select the Marketplace contract from the Contract dropdown, and click the Deploy button.

Once the contract is deployed, it will appear under the Deployed Contracts section. You can expand it to see its public state variables and functions. You can create, list, and buy items by calling these functions.

To create an item, enter a name and a price, and click the createItem button. To list an item for sale, enter the item ID and click the listItemForSale button. To buy an item, enter the item ID, send the correct amount of Ether, and click the buyItem button.

Congratulations! You now know how to create a basic decentralized marketplace on the Ethereum blockchain.

In the next lesson, we will improve our marketplace by adding functionality for removing items from sale and updating the price of an item. Stay tuned!

免責事項
* 暗号資産投資には重倧なリスクが䌎いたす。泚意しお進めおください。このコヌスは投資アドバむスを目的ずしたものではありたせん。
※ このコヌスはGate Learnに参加しおいるメンバヌが䜜成したものです。䜜成者が共有した意芋はGate Learnを代衚するものではありたせん。