Let’s implement a modifier in our Marketplace
contract. We will define a onlySeller
modifier, which will verify whether the caller of a function is indeed the seller of an item.
Marketplace
contract updated with the onlySeller
modifier: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;
// Modifier that checks if the caller is the seller of an item
modifier onlySeller(uint _itemId) {
require(msg.sender == items[_itemId].seller, "Only the owner can execute this");
_;
}
// Function to remove an item from sale, updated with 'onlySeller' modifier
function removeItemFromSale(uint _itemId) public onlySeller(_itemId) {
items[_itemId].forSale = false;
}
// Function to update the price of an item, updated with 'onlySeller' modifier
function updateItemPrice(uint _itemId, uint _newPrice) public onlySeller(_itemId) {
items[_itemId].price = _newPrice;
}
}
With the onlySeller
modifier in place, we’ve made our removeItemFromSale
and updateItemPrice
functions more efficient and readable.
After enhancing the Marketplace
contract, follow the same steps as in the previous lessons to compile and deploy it.
Once the contract is deployed, you can interact with it just like before. With the improvements we’ve made in this lesson, our contract is more efficient and easier to read and maintain.
Congratulations! You’ve now learned how to create, enhance, and interact with a basic decentralized marketplace on the Ethereum blockchain. This marks the end of our beginner’s course on smart contract development with Solidity. Keep experimenting, learning, and building!
Congratulations! You’ve made it to the end of this beginner’s course on developing smart contracts using Solidity. Over the course of four lessons, we explored the creation, deployment, and interaction with smart contracts in a simulated Ethereum environment, using the Remix IDE.
Let’s recap what we’ve achieved:
Item.sol
, which defined a single item that could be bought or sold.Marketplace.sol
contract, which allowed for the creation, listing, and buying of multiple items.Moving forward, there are numerous paths for you to explore. You can dive deeper into Solidity, learning about more advanced features and security considerations. You could explore other blockchain platforms like Polkadot, Cardano, or Binance Smart Chain. Alternatively, you might want to learn about front-end development for dApps using Web3.js or Ethers.js, or about deploying your contracts on the actual Ethereum network.
Whatever path you choose, always remember: the most effective learning is by doing. So, don’t be afraid to experiment, build, break, and rebuild. Every challenge you face is an opportunity to learn and grow.
Thank you for participating in this course and joining the exciting world of blockchain development. The blockchain revolution is just getting started, and developers like you are at the forefront. So, keep learning, keep building, and most importantly, have fun!
Happy coding!
Let’s implement a modifier in our Marketplace
contract. We will define a onlySeller
modifier, which will verify whether the caller of a function is indeed the seller of an item.
Marketplace
contract updated with the onlySeller
modifier: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;
// Modifier that checks if the caller is the seller of an item
modifier onlySeller(uint _itemId) {
require(msg.sender == items[_itemId].seller, "Only the owner can execute this");
_;
}
// Function to remove an item from sale, updated with 'onlySeller' modifier
function removeItemFromSale(uint _itemId) public onlySeller(_itemId) {
items[_itemId].forSale = false;
}
// Function to update the price of an item, updated with 'onlySeller' modifier
function updateItemPrice(uint _itemId, uint _newPrice) public onlySeller(_itemId) {
items[_itemId].price = _newPrice;
}
}
With the onlySeller
modifier in place, we’ve made our removeItemFromSale
and updateItemPrice
functions more efficient and readable.
After enhancing the Marketplace
contract, follow the same steps as in the previous lessons to compile and deploy it.
Once the contract is deployed, you can interact with it just like before. With the improvements we’ve made in this lesson, our contract is more efficient and easier to read and maintain.
Congratulations! You’ve now learned how to create, enhance, and interact with a basic decentralized marketplace on the Ethereum blockchain. This marks the end of our beginner’s course on smart contract development with Solidity. Keep experimenting, learning, and building!
Congratulations! You’ve made it to the end of this beginner’s course on developing smart contracts using Solidity. Over the course of four lessons, we explored the creation, deployment, and interaction with smart contracts in a simulated Ethereum environment, using the Remix IDE.
Let’s recap what we’ve achieved:
Item.sol
, which defined a single item that could be bought or sold.Marketplace.sol
contract, which allowed for the creation, listing, and buying of multiple items.Moving forward, there are numerous paths for you to explore. You can dive deeper into Solidity, learning about more advanced features and security considerations. You could explore other blockchain platforms like Polkadot, Cardano, or Binance Smart Chain. Alternatively, you might want to learn about front-end development for dApps using Web3.js or Ethers.js, or about deploying your contracts on the actual Ethereum network.
Whatever path you choose, always remember: the most effective learning is by doing. So, don’t be afraid to experiment, build, break, and rebuild. Every challenge you face is an opportunity to learn and grow.
Thank you for participating in this course and joining the exciting world of blockchain development. The blockchain revolution is just getting started, and developers like you are at the forefront. So, keep learning, keep building, and most importantly, have fun!
Happy coding!