-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path11_ContractDeployer.sol
100 lines (86 loc) · 2.81 KB
/
11_ContractDeployer.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
pragma tvm-solidity >= 0.72.0;
pragma AbiHeader expire;
import "11_SimpleContract.sol";
import "11_Waller_no_constructor.sol";
contract ContractDeployer {
// addresses of deployed contracts
address[] public contracts;
constructor() {
// check that contract's public key is set
require(tvm.pubkey() != 0, 101);
// Check that message has signature (msg.pubkey() is not zero) and message is signed with the owner's private key
require(msg.pubkey() == tvm.pubkey(), 102);
tvm.accept();
}
// Modifier that allows public function to accept external calls only from the contract owner.
modifier checkOwnerAndAccept {
require(msg.pubkey() == tvm.pubkey(), 102);
tvm.accept();
_;
}
// The first option of contract deployment.
function deployFromCodeAndData(
TvmCell code,
TvmCell data,
coins initialBalance,
uint paramA,
uint32 paramB
)
public
checkOwnerAndAccept
{
// Runtime function to generate StateInit from code and data cells.
TvmCell stateInit = abi.encodeStateInit(code, data);
address addr = new SimpleContract{stateInit: stateInit, value: initialBalance}(paramA, paramB);
// save address
contracts.push(addr);
}
// The second option of contract deployment
function deployWithMsgBody(
TvmCell stateInit,
int8 wid,
coins initialBalance,
TvmCell payload
)
public
checkOwnerAndAccept
{
// Runtime function to deploy contract with prepared msg body for constructor call.
address addr = address.makeAddrStd(wid, tvm.hash(stateInit));
addr.transfer({stateInit: stateInit, body: payload, value: initialBalance});
// save address
contracts.push(addr);
}
// The third option of contract deployment
function deployNoConstructor(TvmCell code) external checkOwnerAndAccept {
TvmCell data = abi.encodeData({
contr: Wallet,
varInit: {
m_creator: address(this)
},
pubkey: 0x3f82435f2bd40915c28f56d3c2f07af4108931ae8bf1ca9403dcf77d96250827
});
TvmCell stateInit = abi.encodeStateInit(code, data);
// Get address of new contracts
address addr = address.makeAddrStd(0, tvm.hash(stateInit));
// Deploy the contract (that has no constructor) by calling `sendCoins` function
Wallet(addr).sendCoins{stateInit: stateInit, value: 2 ever}(address(0x12345), 1 ever, false);
// save address
contracts.push(addr);
}
function deployNoConstructor2(TvmCell code) public checkOwnerAndAccept {
TvmCell data = abi.encodeData({
contr: Wallet,
varInit: {
m_creator: address(this)
},
pubkey: 0x3f82435f2bd40915c28f56d3c2f07af4108931ae8bf1ca9403dcf77d96250828
});
TvmCell stateInit = abi.encodeStateInit(code, data);
// Deploy the contract (that has no constructor) by calling `receive` function
address addr = address.makeAddrStd(0, tvm.hash(stateInit));
addr.transfer({stateInit: stateInit, value: 1 ever});
// save address
contracts.push(addr);
}
}