跟我学 Solidity :工厂模式

如何在智能合约中使用clone工厂模式减少合约部署 gas。

> * 原文 https://medium.com/better-programming/learn-solidity-the-factory-pattern-75d11c3e7d29 作者 [Wissal haji](https://wissal-haji.medium.com/) > * 译文出自:[登链翻译计划](https://github.com/lbc-team/Pioneer) > * 译者:[翻译小组](https://learnblockchain.cn/people/412) > * 校对:[Tiny 熊](https://learnblockchain.cn/people/15) > * 本文永久链接:[learnblockchain.cn/article…](https://learnblockchain.cn/article/1) > 欢迎来到学习 Solidity 系列的另一部分。在[上一篇文章](https://learnblockchain.cn/article/1944),我们讨论了如何从智能合约中创建另一个智能合约。今天,我们将研究这种情况下的典型用例。 ## 什么是工厂模式? 工厂模式的想法是拥有一个合约(工厂),该合约将承担创建其他合约的任务。在基于类的编程中,此模式的主要动机来自单一职责原则(一个类不需要知道如何创建其他类的实例),并且该模式为构造函数提供了一种抽象。 ![UML diagram for factory method](https://img.learnblockchain.cn/2020/12/30//zog5MPg.png) > 图片来自[Wikipedia](https://en.wikipedia.org/wiki/Factory_method_pattern). ## 为什么要在 Solidity 中使用工厂模式? 在Solidity中,出于以下原因之一,你可能要使用工厂模式: - 如果要创建同一合约的多个实例,并且正在寻找一种跟踪它们并简化管理的方法。 ```javascript contract Factory { Child[] children; function createChild(uint data){ Child child = new Child(data); children.push(child); } } contract Child{ uint data; constructor(uint _data){ data = _data; } } ``` - 节省部署成本:你可以先部署工厂,之后在使用时再来部署其他合约。 - 提高合约安全性(请[参阅](https://learnblockchain.cn/article/2297)). ## 如何与已部署的智能合约进行交互 在深入探讨如何实现工厂模式的细节之前,我想澄清一下我们与已部署的智能合约进行交互的方式。工厂模式是用来创建子合约的,并且我们可能希望调用它们的某些函数以更好地管理这些合约。 调用部署的智能合约,需要做两件事: 1. 合约的ABI(提供有关函数签名的信息)。如果合约在同一个项目中。你可以使用import关键字将其导入。 2. 部署合约的地址。 举个例子: ```javascript contract A { address bAddress; constructor(address b){ bAddress = b; } function callHello() external view returns(string memory){ B b = B(bAddress); // 转换地址为合约类型 return b.sayHello(); } } contract B { string greeting = "hello world"; function sayHello() external view returns(string memory){ return greeting; } } ``` 在Remix中,首先部署合约B,然后复制其地址,并在部署时将其提供给A的构造函数。现在你可以调用`callHello()`函数,你将获得合约B的`sayHello()`函数的结果。 ## 普通工厂模式 在此模式下,我们创建具有创建子合约函数的工厂合约,并且可能还会添加其他函数来有效管理这些合约(例如,查找特定合约或禁用合约)。在create函数中,我们使用`new`关键字来部署子合约。 ```javascript contract Factory{ Child[] public children; uint disabledCount; event ChildCreated(address childAddress, uint data); function createChild(uint data) external{ Child child = new Child(data, children.length); children.push(child); emit ChildCreated(address(child), data); } function getChildren() external view returns(Child[] memory _children){ _children = new Child[](children.length- disabledCount); uint count; for(uint i=0;i<children.length; i++){ if(children[i].isEnabled()){ _children[count] = children[i]; count++; } } } function disable(Child child) external { children[child.index()].disable(); disabledCount++; } } contract Child{ uint data; bool public isEnabled; uint public index; constructor(uint _data,uint _index){ data = _data; isEnabled = true; i...

  • 原文 https://medium.com/better-programming/learn-solidity-the-factory-pattern-75d11c3e7d29 作者 Wissal haji
  • 译文出自:登链翻译计划
  • 译者:翻译小组
  • 校对:Tiny 熊
  • 本文永久链接:learnblockchain.cn/article…

欢迎来到学习 Solidity 系列的另一部分。在上一篇文章,我们讨论了如何从智能合约中创建另一个智能合约。今天,我们将研究这种情况下的典型用例。

什么是工厂模式?

工厂模式的想法是拥有一个合约(工厂),该合约将承担创建其他合约的任务。在基于类的编程中,此模式的主要动机来自单一职责原则(一个类不需要知道如何创建其他类的实例),并且该模式为构造函数提供了一种抽象。

图片来自Wikipedia.

为什么要在 Solidity 中使用工厂模式?

在Solidity中,出于以下原因之一,你可能要使用工厂模式:

  • 如果要创建同一合约的多个实例,并且正在寻找一种跟踪它们并简化管理的方法。

    contract Factory {
        Child[] children;
        function createChild(uint data){
           Child child = new Child(data);
           children.push(child);
        }
    }
    contract Child{
       uint data;
       constructor(uint _data){
          data = _data;
       }
    }
  • 节省部署成本:你可以先部署工厂,之后在使用时再来部署其他合约。

  • 提高合约安全性(请参阅).

如何与已部署的智能合约进行交互

在深入探讨如何实现工厂模式的细节之前,我想澄清一下我们与已部署的智能合约进行交互的方式。工厂模式是用来创建子合约的,并且我们可能希望调用它们的某些函数以更好地管理这些合约。

调用部署的智能合约,需要做两件事:

  1. 合约的ABI(提供有关函数签名的信息)。如果合约在同一个项目中。你可以使用import关键字将其导入。
  2. 部署合约的地址。

举个例子:

contract A {
    address bAddress;
    constructor(address b){
       bAddress = b;
    }

    function callHello() external view returns(string memory){
       B b = B(bAddress); // 转换地址为合约类型
       return b.sayHello();
    }
}

contract B {
     string greeting = "hello world";
     function sayHello() external view returns(string memory){
         return greeting;
     }
}

在Remix中,首先部署合约B,然后复制其地址,并在部署时将其提供给A的构造函数。现在你可以调用callHello()函数,你将获得合约B的sayHello()函数的结果。

普通工厂模式

在此模式下,我们创建具有创建子合约函数的工厂合约,并且可能还会添加其他函数来有效管理这些合约(例如,查找特定合约或禁用合约)。在create函数中,我们使用new关键字来部署子合约。


contract Factory{
     Child[] public children;
     uint disabledCount;

    event ChildCreated(address childAddress, uint data);

     function createChild(uint data) external{
       Child child = new Child(data, children.length);
       children.push(child);
       emit ChildCreated(address(child), data);
     }

     function getChildren() external view returns(Child[] memory _children){
       _children = new Child[](children.length- disabledCount);
       uint count;
       for(uint i=0;i&lt;children.length; i++){
          if(children[i].isEnabled()){
             _children[count] = children[i];
             count++;
          }
        }
     }  

     function disable(Child child) external {
        children[child.index()].disable();
        disabledCount++;
     }

}
contract Child{
    uint data;
    bool public isEnabled;
    uint public index;
    constructor(uint _data,uint _index){
       data = _data;
       isEnabled = true;
       i...

剩余50%的内容订阅专栏后可查看

  • 单篇购买 5学分
  • 永久订阅专栏 (90学分)
  • 发表于 2020-12-30 18:04
  • 阅读 ( 1966 )
  • 学分 ( 105 )
  • 分类:智能合约
  • 专栏:全面掌握Solidity智能合约开发

评论