Lección 4

使用修饰符提升代码效率

在Solidity中,修饰符的功能非常强大。通过修饰符,我们可以在函数中嵌入预检查,从而生成更易读和高效的代码。

引入修饰符

在本例中,我们将在Marketplace合约中实现一个修饰符。我们会定义一个onlySeller修饰符,它将验证函数的调用者是否确实是商品的卖家。

以下是已更新并添加onlySeller修饰符的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;

    // 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;
    }
}

在引入了onlySeller修饰符后,removeItemFromSaleupdateItemPrice函数变得更加高效和易于理解。

部署并运行增强版合约

在增强了Marketplace合约后,请按照我们前面所介绍的步骤进行编译和部署。

合约部署完成后,您可以运行该合约。在经过我们以上改进措施后,我们的合约变得更加高效,更易于阅读和维护。

祝贺!现在,您已经学会了如何在以太坊区块链上创建、增强和使用基本的去中心化市场合约。至此,我们的《Solidity和智能合约开发》入门课程便结束了。在未来的探索中,希望您能继续实践、学习并构建智能合约!

结语

恭喜大家完成了使用Solidity开发智能合约的入门课程的学习。在整个课程中,我们探索了使用Remix IDE在模拟的以太坊环境中创建、部署和使用智能合约的过程。

我们来回顾一下本课程的重点内容:

  • 第1课:介绍了以太坊、区块链和智能合约的基础知识,创建了我们的第一个简单的智能合约Item.sol,该合约定义了一个可以购买或出售的单个商品。
  • 第2课:在最初的合约基础上进行了扩展,创建了Marketplace.sol合约,实现了创建、上架和购买多个商品的功能。
  • 第3课:引入了从在售列表中移除商品和更新商品价格的方法,为市场添加了新功能。
  • 第4课:通过实现Solidity中的“修饰符”功能,提升了智能合约的可读性和效率,并学习了简化代码并提升其安全性的技巧。
    通过本次课程,您不仅建立了坚实的Solidity和智能合约开发基础,还迈出了成为区块链开发者的第一步。您学会了如何评估去中心化应用,并对区块链技术的创新潜力有了初步了解。

未来,您可以选择不同的路径继续探索。您可以深入学习Solidity,了解更高级的功能和安全因素;也可以探索其他区块链平台,如Polkadot、Cardano或币安智能链;或学习使用Web3.js或Ether.js进行dApp的前端开发;亦或是在真实的以太坊网络上部署自己的合约。

无论您选择哪条道路,都请记住:实践是最有效的学习方式。不要害怕尝试、构建、打破并重建。您面临的每一个挑战都是学习和成长的机会。

最后,再次感谢大家参与本次课程的学习,加入到这令人激动的区块链开发之旅中。区块链革命才刚刚开始,像您这样的开发者正处于这场革命的最前沿。所以,请继续学习,继续构建,最重要的是,享受其中的乐趣!

祝编程愉快!

Descargo de responsabilidad
* La inversión en criptomonedas implica riesgos significativos. Proceda con precaución. El curso no pretende ser un asesoramiento de inversión.
* El curso ha sido creado por el autor que se ha unido a Gate Learn. Cualquier opinión compartida por el autor no representa a Gate Learn.
Catálogo
Lección 4

使用修饰符提升代码效率

在Solidity中,修饰符的功能非常强大。通过修饰符,我们可以在函数中嵌入预检查,从而生成更易读和高效的代码。

引入修饰符

在本例中,我们将在Marketplace合约中实现一个修饰符。我们会定义一个onlySeller修饰符,它将验证函数的调用者是否确实是商品的卖家。

以下是已更新并添加onlySeller修饰符的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;

    // 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;
    }
}

在引入了onlySeller修饰符后,removeItemFromSaleupdateItemPrice函数变得更加高效和易于理解。

部署并运行增强版合约

在增强了Marketplace合约后,请按照我们前面所介绍的步骤进行编译和部署。

合约部署完成后,您可以运行该合约。在经过我们以上改进措施后,我们的合约变得更加高效,更易于阅读和维护。

祝贺!现在,您已经学会了如何在以太坊区块链上创建、增强和使用基本的去中心化市场合约。至此,我们的《Solidity和智能合约开发》入门课程便结束了。在未来的探索中,希望您能继续实践、学习并构建智能合约!

结语

恭喜大家完成了使用Solidity开发智能合约的入门课程的学习。在整个课程中,我们探索了使用Remix IDE在模拟的以太坊环境中创建、部署和使用智能合约的过程。

我们来回顾一下本课程的重点内容:

  • 第1课:介绍了以太坊、区块链和智能合约的基础知识,创建了我们的第一个简单的智能合约Item.sol,该合约定义了一个可以购买或出售的单个商品。
  • 第2课:在最初的合约基础上进行了扩展,创建了Marketplace.sol合约,实现了创建、上架和购买多个商品的功能。
  • 第3课:引入了从在售列表中移除商品和更新商品价格的方法,为市场添加了新功能。
  • 第4课:通过实现Solidity中的“修饰符”功能,提升了智能合约的可读性和效率,并学习了简化代码并提升其安全性的技巧。
    通过本次课程,您不仅建立了坚实的Solidity和智能合约开发基础,还迈出了成为区块链开发者的第一步。您学会了如何评估去中心化应用,并对区块链技术的创新潜力有了初步了解。

未来,您可以选择不同的路径继续探索。您可以深入学习Solidity,了解更高级的功能和安全因素;也可以探索其他区块链平台,如Polkadot、Cardano或币安智能链;或学习使用Web3.js或Ether.js进行dApp的前端开发;亦或是在真实的以太坊网络上部署自己的合约。

无论您选择哪条道路,都请记住:实践是最有效的学习方式。不要害怕尝试、构建、打破并重建。您面临的每一个挑战都是学习和成长的机会。

最后,再次感谢大家参与本次课程的学习,加入到这令人激动的区块链开发之旅中。区块链革命才刚刚开始,像您这样的开发者正处于这场革命的最前沿。所以,请继续学习,继续构建,最重要的是,享受其中的乐趣!

祝编程愉快!

Descargo de responsabilidad
* La inversión en criptomonedas implica riesgos significativos. Proceda con precaución. El curso no pretende ser un asesoramiento de inversión.
* El curso ha sido creado por el autor que se ha unido a Gate Learn. Cualquier opinión compartida por el autor no representa a Gate Learn.
It seems that you are attempting to access our services from a Restricted Location where Gate.io is unable to provide services. We apologize for any inconvenience this may cause. Currently, the Restricted Locations include but not limited to: the United States of America, Canada, Cambodia, Thailand, Cuba, Iran, North Korea and so on. For more information regarding the Restricted Locations, please refer to the User Agreement. Should you have any other questions, please contact our Customer Support Team.