智能合约-工厂合约模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pragma solidity ^0.6.12;

contract AutoShop {
address[] autoAssets;
function createChildContract(string memory brand, string memory model) public payable {
// 增加检查:ether是否足够支付 car
address newAutoAsset = address(new AutoAsset(brand, model, msg.sender));
autoAssets.push(newAutoAsset);
}
function getDeployChildContracts() public view returns (address[] memory) {
return autoAssets;
}
}

contract AutoAsset {
string brand;
string model;
address owner;
constructor(string memory _brand, string memory _model, address _owner) public {
brand = _brand;
model = _model;
owner = _owner;
}
}

address newAutoAsset = address(new AutoAsset(...) 出发了一个交易,将子合约部署到区块链并返回合约地址。同时,将合约地址存储到数组 address[] autoAssets 中。

关联文档:

  1. 智能合约-CURD的详细分析
  2. 智能合约-自毁模式
  3. 智能合约-工厂合约模式
  4. 智能合约-名字登录模式
  5. 智能合约-退款方式