Factory-Based Smart Contracts
In order to:
  • Save Smart Contract space;
  • Have the assurance to interact with 100% genuine SmartContracts;
  • Easily Smart Contract code update;
  • Easy search system of similar Smart Contract through events
We usually use a Model - Factory pattern to fuse flexibility and security together.
The Model always contains all the Business Logic of the Smart Contract. It does not have a constructor, but an optional initialization method.
The Factory has the logic to reference the original model and to clone it keeping track of all the instances through events. The initialization method is general purpose in order to make the model updateable.
1
pragma solidity ^0.7.0;
2
pragma abicoder v2;
3
4
contract Model {
5
6
address public factory;
7
8
function init() {
9
require(factory == address(0), "Init already called!");
10
//This links the Model directly to the Factory
11
factory = msg.sender;
12
13
/*
14
* Other init stuff
15
*/
16
}
17
}
18
19
pragma solidity ^0.7.0;
20
pragma abicoder v2;
21
22
contract Factory {
23
24
address public model;
25
address public doubleProxy;
26
27
event ModelDeployed(address indexed instanceAddress, address indexed sender, bytes initResultData);
28
29
constructor(address _doubleProxy, address _model) {
30
doubleProxy = _doubleProxy;
31
model = _model;
32
}
33
34
function setDoubleProxy(address _doubleProxy) public byDFO {
35
doubleProxy = _doubleProxy;
36
}
37
38
//Model can be changed only by authorization (Proposal)
39
function setModel(address _model) public byDFO {
40
model = _model;
41
}
42
43
//Clones the bytecode of the Model and calls the model initialization method.
44
//In order to maintain the Factory totally model independant, initialization data must be passed as ABI-Encoded parameters
45
//E.G. in this case, data is equal to bytes4(keccak256("init()")), which corresponds to the init() method of the actual Model.
46
//This method will work even if the Model and its initialization method changes
47
function deploy(bytes memory data) public returns (address contractAddress, bytes memory initResultData) {
48
initResultData = _call(contractAddress = _clone(model), data);
49
emit ModelDeployed(contractAddress, msg.sender, initResultData);
50
}
51
52
function _clone(address original) private returns (address copy) {
53
assembly {
54
mstore(
55
0,
56
or(
57
0x5880730000000000000000000000000000000000000000803b80938091923cF3,
58
mul(original, 0x1000000000000000000)
59
)
60
)
61
copy := create(0, 0, 32)
62
switch extcodesize(copy)
63
case 0 {
64
invalid()
65
}
66
}
67
}
68
69
function _call(address location, bytes memory payload) private returns(bytes memory returnData) {
70
assembly {
71
let result := call(gas(), location, 0, add(payload, 0x20), mload(payload), 0, 0)
72
let size := returndatasize()
73
returnData := mload(0x40)
74
mstore(returnData, size)
75
let returnDataPayloadStart := add(returnData, 0x20)
76
returndatacopy(returnDataPayloadStart, 0, size)
77
mstore(0x40, add(returnDataPayloadStart, size))
78
switch result case 0 {revert(returnDataPayloadStart, size)}
79
}
80
}
81
82
modifier byDFO() {
83
require(IMVDFunctionalitiesManager(IMVDProxy(IDoubleProxy(doubleProxy).proxy()).getMVDFunctionalitiesManagerAddress()).isAuthorizedFunctionality(msg.sender), "Unauthorized.");
84
_;
85
}
86
}
87
Copied!
Below are some use cases of Factory contracts developed and used by Ethos:
Copy link