-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBasic Solidity Factory Pattern.sol
78 lines (58 loc) · 1.29 KB
/
Basic Solidity Factory Pattern.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
pragma solidity 0.4.26;
contract Bakery {
// index of created contracts
mapping (address => bool) public validContracts;
address[] public contracts;
// useful to know the row count in contracts index
function getContractCount()
public
view
returns(uint contractCount)
{
return contracts.length;
}
// get all contracts
function getDeployedContracts() public view returns (address[])
{
return contracts;
}
// deploy a new contract
function newCookie(string flavor)
public
returns(address)
{
Cookie c = new Cookie(msg.sender, flavor);
validContracts[c] = true;
contracts.push(c);
return c;
}
// access child functions
function getFlavor(address cookie)
public
view
returns(string)
{
//ensure valid address
require(validContracts[cookie],"Contract Not Found!");
return (Cookie(cookie).getFlavor());
}
}
contract Cookie {
address public owner;
address public factory;
string public flavor;
// standard constructor
constructor (address _owner, string _flavor) public {
owner = _owner;
factory = msg.sender;
flavor = _flavor;
}
// suppose the deployed contract has a purpose
function getFlavor()
public
view
returns (string)
{
return flavor;
}
}