Skip to content

Commit a5e813f

Browse files
committed
refactor(interface): FunctionCallArgs constructors
FunctionCallArgs::new and ::no_deposit mirror ViewArgs::new/no_args; every handle method builds its call through them and a shared private call(), replacing the struct literals and the call_without_deposit helper. No wire change — the snapshot catalog is untouched.
1 parent c3627be commit a5e813f

2 files changed

Lines changed: 115 additions & 115 deletions

File tree

crates/near-contract-transport/src/types.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ pub struct FunctionCallArgs {
1212
pub deposit: NearToken,
1313
}
1414

15+
impl FunctionCallArgs {
16+
pub fn new(
17+
method_name: impl Into<String>,
18+
args: Vec<u8>,
19+
gas: NearGas,
20+
deposit: NearToken,
21+
) -> Self {
22+
Self {
23+
method_name: method_name.into(),
24+
args,
25+
gas,
26+
deposit,
27+
}
28+
}
29+
30+
pub fn no_deposit(method_name: impl Into<String>, args: Vec<u8>, gas: NearGas) -> Self {
31+
Self::new(method_name, args, gas, NearToken::from_yoctonear(0))
32+
}
33+
}
34+
1535
#[derive(Debug, Clone)]
1636
pub struct ViewArgs {
1737
pub method_name: String,

crates/near-mpc-contract-interface/src/client.rs

Lines changed: 95 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,13 @@ impl<C: CallContract> MpcContractHandle<C> {
6666
request: SignRequestArgs,
6767
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
6868
let args = serde_json::to_vec(&SignArgs::new(request))?;
69-
self.caller
70-
.call_contract(
71-
&self.contract_id,
72-
FunctionCallArgs {
73-
method_name: SIGN.to_string(),
74-
args,
75-
gas: SIGN_GAS,
76-
deposit: NearToken::from_yoctonear(SIGN_DEPOSIT_YOCTONEAR),
77-
},
78-
)
79-
.await
80-
.map_err(MpcContractHandleError::Call)
69+
self.call(FunctionCallArgs::new(
70+
SIGN,
71+
args,
72+
SIGN_GAS,
73+
NearToken::from_yoctonear(SIGN_DEPOSIT_YOCTONEAR),
74+
))
75+
.await
8176
}
8277

8378
pub async fn request_app_private_key(
@@ -89,37 +84,27 @@ impl<C: CallContract> MpcContractHandle<C> {
8984
CKDAppPublicKey::AppPublicKeyPV(_) => CKD_PV_GAS,
9085
};
9186
let args = serde_json::to_vec(&RequestAppPrivateKeyArgs::new(request))?;
92-
self.caller
93-
.call_contract(
94-
&self.contract_id,
95-
FunctionCallArgs {
96-
method_name: REQUEST_APP_PRIVATE_KEY.to_string(),
97-
args,
98-
gas,
99-
deposit: NearToken::from_yoctonear(SIGN_DEPOSIT_YOCTONEAR),
100-
},
101-
)
102-
.await
103-
.map_err(MpcContractHandleError::Call)
87+
self.call(FunctionCallArgs::new(
88+
REQUEST_APP_PRIVATE_KEY,
89+
args,
90+
gas,
91+
NearToken::from_yoctonear(SIGN_DEPOSIT_YOCTONEAR),
92+
))
93+
.await
10494
}
10595

10696
pub async fn verify_foreign_transaction(
10797
&self,
10898
request: VerifyForeignTransactionRequestArgs,
10999
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
110100
let args = serde_json::to_vec(&VerifyForeignTransactionArgs::new(request))?;
111-
self.caller
112-
.call_contract(
113-
&self.contract_id,
114-
FunctionCallArgs {
115-
method_name: VERIFY_FOREIGN_TRANSACTION.to_string(),
116-
args,
117-
gas: SIGN_GAS,
118-
deposit: NearToken::from_yoctonear(SIGN_DEPOSIT_YOCTONEAR),
119-
},
120-
)
121-
.await
122-
.map_err(MpcContractHandleError::Call)
101+
self.call(FunctionCallArgs::new(
102+
VERIFY_FOREIGN_TRANSACTION,
103+
args,
104+
SIGN_GAS,
105+
NearToken::from_yoctonear(SIGN_DEPOSIT_YOCTONEAR),
106+
))
107+
.await
123108
}
124109

125110
pub async fn propose_update(
@@ -135,46 +120,35 @@ impl<C: CallContract> MpcContractHandle<C> {
135120
STORAGE_BYTE_COST_YOCTONEAR,
136121
)?);
137122
let args = borsh::to_vec(&args)?;
138-
self.caller
139-
.call_contract(
140-
&self.contract_id,
141-
FunctionCallArgs {
142-
method_name: PROPOSE_UPDATE.to_string(),
143-
args,
144-
gas: MAX_GAS,
145-
deposit,
146-
},
147-
)
148-
.await
149-
.map_err(MpcContractHandleError::Call)
123+
self.call(FunctionCallArgs::new(
124+
PROPOSE_UPDATE,
125+
args,
126+
MAX_GAS,
127+
deposit,
128+
))
129+
.await
150130
}
151131

152132
pub async fn vote_update(
153133
&self,
154134
id: u64,
155135
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
156136
let args = serde_json::to_vec(&VoteUpdateArgs::new(id))?;
157-
self.caller
158-
.call_contract(
159-
&self.contract_id,
160-
FunctionCallArgs {
161-
method_name: VOTE_UPDATE.to_string(),
162-
args,
163-
gas: MAX_GAS,
164-
deposit: NearToken::from_yoctonear(0),
165-
},
166-
)
137+
self.call(FunctionCallArgs::no_deposit(VOTE_UPDATE, args, MAX_GAS))
167138
.await
168-
.map_err(MpcContractHandleError::Call)
169139
}
170140

171141
pub async fn vote_add_domains(
172142
&self,
173143
domains: Vec<DomainConfig>,
174144
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
175145
let args = serde_json::to_vec(&VoteAddDomainsArgs::new(domains))?;
176-
self.call_without_deposit(VOTE_ADD_DOMAINS, args, MAX_GAS)
177-
.await
146+
self.call(FunctionCallArgs::no_deposit(
147+
VOTE_ADD_DOMAINS,
148+
args,
149+
MAX_GAS,
150+
))
151+
.await
178152
}
179153

180154
pub async fn vote_new_parameters(
@@ -183,51 +157,75 @@ impl<C: CallContract> MpcContractHandle<C> {
183157
proposal: ProposedGovernanceThresholdParameters,
184158
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
185159
let args = serde_json::to_vec(&VoteNewParametersArgs::new(prospective_epoch_id, proposal))?;
186-
self.call_without_deposit(VOTE_NEW_PARAMETERS, args, MAX_GAS)
187-
.await
160+
self.call(FunctionCallArgs::no_deposit(
161+
VOTE_NEW_PARAMETERS,
162+
args,
163+
MAX_GAS,
164+
))
165+
.await
188166
}
189167

190168
pub async fn vote_cancel_keygen(
191169
&self,
192170
next_domain_id: u64,
193171
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
194172
let args = serde_json::to_vec(&VoteCancelKeygenArgs::new(next_domain_id))?;
195-
self.call_without_deposit(VOTE_CANCEL_KEYGEN, args, MAX_GAS)
196-
.await
173+
self.call(FunctionCallArgs::no_deposit(
174+
VOTE_CANCEL_KEYGEN,
175+
args,
176+
MAX_GAS,
177+
))
178+
.await
197179
}
198180

199181
pub async fn vote_cancel_resharing(
200182
&self,
201183
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
202-
self.call_without_deposit(VOTE_CANCEL_RESHARING, b"{}".to_vec(), MAX_GAS)
203-
.await
184+
self.call(FunctionCallArgs::no_deposit(
185+
VOTE_CANCEL_RESHARING,
186+
b"{}".to_vec(),
187+
MAX_GAS,
188+
))
189+
.await
204190
}
205191

206192
pub async fn update_participant_url(
207193
&self,
208194
url: String,
209195
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
210196
let args = serde_json::to_vec(&UpdateParticipantUrlArgs::new(url))?;
211-
self.call_without_deposit(UPDATE_PARTICIPANT_URL, args, MAX_GAS)
212-
.await
197+
self.call(FunctionCallArgs::no_deposit(
198+
UPDATE_PARTICIPANT_URL,
199+
args,
200+
MAX_GAS,
201+
))
202+
.await
213203
}
214204

215205
pub async fn register_backup_service(
216206
&self,
217207
backup_service_info: BackupServiceInfo,
218208
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
219209
let args = serde_json::to_vec(&RegisterBackupServiceArgs::new(backup_service_info))?;
220-
self.call_without_deposit(REGISTER_BACKUP_SERVICE, args, MAX_GAS)
221-
.await
210+
self.call(FunctionCallArgs::no_deposit(
211+
REGISTER_BACKUP_SERVICE,
212+
args,
213+
MAX_GAS,
214+
))
215+
.await
222216
}
223217

224218
pub async fn start_node_migration(
225219
&self,
226220
destination_node_info: DestinationNodeInfo,
227221
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
228222
let args = serde_json::to_vec(&StartNodeMigrationArgs::new(destination_node_info))?;
229-
self.call_without_deposit(START_NODE_MIGRATION, args, MAX_GAS)
230-
.await
223+
self.call(FunctionCallArgs::no_deposit(
224+
START_NODE_MIGRATION,
225+
args,
226+
MAX_GAS,
227+
))
228+
.await
231229
}
232230

233231
pub async fn register_foreign_chain_support(
@@ -236,40 +234,33 @@ impl<C: CallContract> MpcContractHandle<C> {
236234
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
237235
let args =
238236
serde_json::to_vec(&RegisterForeignChainSupportArgs::new(foreign_chain_support))?;
239-
self.call_without_deposit(REGISTER_FOREIGN_CHAIN_SUPPORT, args, MAX_GAS)
240-
.await
237+
self.call(FunctionCallArgs::no_deposit(
238+
REGISTER_FOREIGN_CHAIN_SUPPORT,
239+
args,
240+
MAX_GAS,
241+
))
242+
.await
241243
}
242244

243245
pub async fn vote_update_foreign_chain_providers(
244246
&self,
245247
batch: NonEmptyBTreeMap<ForeignChain, ChainEntry>,
246248
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
247249
let args = borsh::to_vec(&batch)?;
248-
self.call_without_deposit(
250+
self.call(FunctionCallArgs::no_deposit(
249251
VOTE_UPDATE_FOREIGN_CHAIN_PROVIDERS,
250252
args,
251253
VOTE_FOREIGN_CHAIN_GAS,
252-
)
254+
))
253255
.await
254256
}
255257

256-
/// Zero-deposit call with pre-encoded args.
257-
async fn call_without_deposit(
258+
async fn call(
258259
&self,
259-
method_name: &str,
260-
args: Vec<u8>,
261-
gas: NearGas,
260+
call_args: FunctionCallArgs,
262261
) -> Result<C::Output, MpcContractHandleError<C::Error>> {
263262
self.caller
264-
.call_contract(
265-
&self.contract_id,
266-
FunctionCallArgs {
267-
method_name: method_name.to_string(),
268-
args,
269-
gas,
270-
deposit: NearToken::from_yoctonear(0),
271-
},
272-
)
263+
.call_contract(&self.contract_id, call_args)
273264
.await
274265
.map_err(MpcContractHandleError::Call)
275266
}
@@ -283,33 +274,22 @@ impl<C: CallContract> MpcContractHandle<C> {
283274
proposed_participant_attestation,
284275
tls_public_key,
285276
))?;
286-
self.caller
287-
.call_contract(
288-
&self.contract_id,
289-
FunctionCallArgs {
290-
method_name: SUBMIT_PARTICIPANT_INFO.to_string(),
291-
args,
292-
gas: MAX_GAS,
293-
deposit: NearToken::from_millinear(SUBMIT_PARTICIPANT_INFO_DEPOSIT_MILLINEAR),
294-
},
295-
)
296-
.await
297-
.map_err(MpcContractHandleError::Call)
277+
self.call(FunctionCallArgs::new(
278+
SUBMIT_PARTICIPANT_INFO,
279+
args,
280+
MAX_GAS,
281+
NearToken::from_millinear(SUBMIT_PARTICIPANT_INFO_DEPOSIT_MILLINEAR),
282+
))
283+
.await
298284
}
299285

300286
pub async fn verify_tee(&self) -> Result<C::Output, MpcContractHandleError<C::Error>> {
301-
self.caller
302-
.call_contract(
303-
&self.contract_id,
304-
FunctionCallArgs {
305-
method_name: VERIFY_TEE.to_string(),
306-
args: b"{}".to_vec(),
307-
gas: MAX_GAS,
308-
deposit: NearToken::from_yoctonear(0),
309-
},
310-
)
311-
.await
312-
.map_err(MpcContractHandleError::Call)
287+
self.call(FunctionCallArgs::no_deposit(
288+
VERIFY_TEE,
289+
b"{}".to_vec(),
290+
MAX_GAS,
291+
))
292+
.await
313293
}
314294
}
315295

0 commit comments

Comments
 (0)