1
1
use color_eyre:: eyre:: Context ;
2
+ use strum:: { EnumDiscriminants , EnumIter , EnumMessage } ;
2
3
3
4
pub mod initialize_mode;
4
5
@@ -9,9 +10,34 @@ pub struct Contract {
9
10
#[ interactive_clap( skip_default_input_arg) ]
10
11
/// What is the contract account ID?
11
12
account_id : crate :: types:: account_id:: AccountId ,
12
- #[ interactive_clap( named_arg) ]
13
- /// Specify a path to wasm file
14
- use_file : ContractFile ,
13
+ #[ interactive_clap( subcommand) ]
14
+ /// Specify a deploy mode
15
+ deploy_mode : DeployModes ,
16
+ }
17
+
18
+ #[ derive( Debug , EnumDiscriminants , Clone , interactive_clap:: InteractiveClap ) ]
19
+ #[ interactive_clap( context = ContractContext ) ]
20
+ #[ strum_discriminants( derive( EnumMessage , EnumIter ) ) ]
21
+ #[ non_exhaustive]
22
+ /// Choose a contract action:
23
+ pub enum DeployModes {
24
+ #[ strum_discriminants( strum(
25
+ message = "use-file - Deploy a contract using a wasm file"
26
+ ) ) ]
27
+ /// Deploy a contract using a wasm file
28
+ UseFile ( ContractFile ) ,
29
+
30
+ #[ strum_discriminants( strum(
31
+ message = "use-global-hash - Deploy a contract using a global contract code hash"
32
+ ) ) ]
33
+ /// Deploy a contract using a global contract code hash
34
+ UseGlobalHash ( ContractHash ) ,
35
+
36
+ #[ strum_discriminants( strum(
37
+ message = "use-global-account-id - Deploy a contract using an account ID"
38
+ ) ) ]
39
+ /// Deploy a contract using an global contract account ID
40
+ UseGlobalAccountId ( ContractAccountId ) ,
15
41
}
16
42
17
43
#[ derive( Debug , Clone ) ]
@@ -45,6 +71,14 @@ impl Contract {
45
71
}
46
72
}
47
73
74
+ #[ derive( Debug , Clone ) ]
75
+ pub struct GenericDeployContext {
76
+ pub global_context : crate :: GlobalContext ,
77
+ pub receiver_account_id : near_primitives:: types:: AccountId ,
78
+ pub signer_account_id : near_primitives:: types:: AccountId ,
79
+ pub deploy_action : near_primitives:: transaction:: Action ,
80
+ }
81
+
48
82
#[ derive( Debug , Clone , interactive_clap_derive:: InteractiveClap ) ]
49
83
#[ interactive_clap( input_context = ContractContext ) ]
50
84
#[ interactive_clap( output_context = ContractFileContext ) ]
@@ -55,13 +89,7 @@ pub struct ContractFile {
55
89
initialize : self :: initialize_mode:: InitializeMode ,
56
90
}
57
91
58
- #[ derive( Debug , Clone ) ]
59
- pub struct ContractFileContext {
60
- pub global_context : crate :: GlobalContext ,
61
- pub receiver_account_id : near_primitives:: types:: AccountId ,
62
- pub signer_account_id : near_primitives:: types:: AccountId ,
63
- pub code : Vec < u8 > ,
64
- }
92
+ pub struct ContractFileContext ( GenericDeployContext ) ;
65
93
66
94
impl ContractFileContext {
67
95
pub fn from_previous_context (
@@ -71,11 +99,109 @@ impl ContractFileContext {
71
99
let code = std:: fs:: read ( & scope. file_path ) . wrap_err_with ( || {
72
100
format ! ( "Failed to open or read the file: {:?}." , & scope. file_path. 0 , )
73
101
} ) ?;
74
- Ok ( Self {
102
+ Ok ( Self ( GenericDeployContext {
75
103
global_context : previous_context. global_context ,
76
104
receiver_account_id : previous_context. receiver_account_id ,
77
105
signer_account_id : previous_context. signer_account_id ,
78
- code,
106
+ deploy_action : near_primitives:: transaction:: Action :: DeployContract (
107
+ near_primitives:: action:: DeployContractAction { code } ,
108
+ ) ,
109
+ } ) )
110
+ }
111
+ }
112
+
113
+ impl From < ContractFileContext > for GenericDeployContext {
114
+ fn from ( item : ContractFileContext ) -> Self {
115
+ item. 0
116
+ }
117
+ }
118
+
119
+ #[ derive( Debug , Clone , interactive_clap_derive:: InteractiveClap ) ]
120
+ #[ interactive_clap( input_context = ContractContext ) ]
121
+ #[ interactive_clap( output_context = ContractHashContext ) ]
122
+ pub struct ContractHash {
123
+ /// What is a global contract code hash?
124
+ pub hash : crate :: types:: crypto_hash:: CryptoHash ,
125
+ #[ interactive_clap( subcommand) ]
126
+ initialize : self :: initialize_mode:: InitializeMode ,
127
+ }
128
+
129
+ pub struct ContractHashContext ( GenericDeployContext ) ;
130
+
131
+ impl ContractHashContext {
132
+ pub fn from_previous_context (
133
+ previous_context : ContractContext ,
134
+ scope : & <ContractHash as interactive_clap:: ToInteractiveClapContextScope >:: InteractiveClapContextScope ,
135
+ ) -> color_eyre:: eyre:: Result < Self > {
136
+ Ok ( Self ( GenericDeployContext {
137
+ global_context : previous_context. global_context ,
138
+ receiver_account_id : previous_context. receiver_account_id ,
139
+ signer_account_id : previous_context. signer_account_id ,
140
+ deploy_action : near_primitives:: transaction:: Action :: UseGlobalContract ( Box :: new (
141
+ near_primitives:: action:: UseGlobalContractAction {
142
+ contract_identifier :
143
+ near_primitives:: action:: GlobalContractIdentifier :: CodeHash (
144
+ scope. hash . into ( ) ,
145
+ ) ,
146
+ } ,
147
+ ) ) ,
148
+ } ) )
149
+ }
150
+ }
151
+
152
+ impl From < ContractHashContext > for GenericDeployContext {
153
+ fn from ( item : ContractHashContext ) -> Self {
154
+ item. 0
155
+ }
156
+ }
157
+
158
+ #[ derive( Debug , Clone , interactive_clap_derive:: InteractiveClap ) ]
159
+ #[ interactive_clap( input_context = ContractContext ) ]
160
+ #[ interactive_clap( output_context = ContractAccountIdContext ) ]
161
+ pub struct ContractAccountId {
162
+ #[ interactive_clap( skip_default_input_arg) ]
163
+ /// What is a global contract account ID?
164
+ pub global_contract_account_id : crate :: types:: account_id:: AccountId ,
165
+ #[ interactive_clap( subcommand) ]
166
+ initialize : self :: initialize_mode:: InitializeMode ,
167
+ }
168
+
169
+ impl ContractAccountId {
170
+ pub fn input_global_contract_account_id (
171
+ context : & ContractContext ,
172
+ ) -> color_eyre:: eyre:: Result < Option < crate :: types:: account_id:: AccountId > > {
173
+ crate :: common:: input_non_signer_account_id_from_used_account_list (
174
+ & context. global_context . config . credentials_home_dir ,
175
+ "What is a global contract account ID?" ,
176
+ )
177
+ }
178
+ }
179
+
180
+ pub struct ContractAccountIdContext ( GenericDeployContext ) ;
181
+
182
+ impl ContractAccountIdContext {
183
+ pub fn from_previous_context (
184
+ previous_context : ContractContext ,
185
+ scope : & <ContractAccountId as interactive_clap:: ToInteractiveClapContextScope >:: InteractiveClapContextScope ,
186
+ ) -> color_eyre:: eyre:: Result < GenericDeployContext > {
187
+ Ok ( GenericDeployContext {
188
+ global_context : previous_context. global_context ,
189
+ receiver_account_id : previous_context. receiver_account_id ,
190
+ signer_account_id : previous_context. signer_account_id ,
191
+ deploy_action : near_primitives:: transaction:: Action :: UseGlobalContract ( Box :: new (
192
+ near_primitives:: action:: UseGlobalContractAction {
193
+ contract_identifier :
194
+ near_primitives:: action:: GlobalContractIdentifier :: AccountId (
195
+ scope. global_contract_account_id . clone ( ) . into ( ) ,
196
+ ) ,
197
+ } ,
198
+ ) ) ,
79
199
} )
80
200
}
81
201
}
202
+
203
+ impl From < ContractAccountIdContext > for GenericDeployContext {
204
+ fn from ( item : ContractAccountIdContext ) -> Self {
205
+ item. 0
206
+ }
207
+ }
0 commit comments