MCPcopy Create free account
hub / github.com/cowprotocol/services / generate_code

Method generate_code

contracts/src/codegen.rs:269–348  ·  view source on GitHub ↗

Generate the Rust code for this contract's bindings.

(&self, artifacts_dir: &Path)

Source from the content-addressed store, hash-verified

267
268 /// Generate the Rust code for this contract's bindings.
269 fn generate_code(&self, artifacts_dir: &Path) -> Result<TokenStream> {
270 let bindings_path = self.artifacts_path(artifacts_dir);
271 let mut macrogen = SolMacroGen::new(bindings_path, self.name.clone());
272 generate_binding(&mut macrogen, true)?;
273 let mut expansion = macrogen
274 .expansion
275 .expect("if the expansion failed, it should have errored earlier");
276
277 let module_name_ident = format_ident!("{}", self.name);
278 let instance_name_ident = format_ident!("{}Instance", self.name);
279 let instance = quote::quote! {
280 pub type Instance = #module_name_ident :: #instance_name_ident<::alloy_provider::DynProvider>;
281 };
282 expansion.extend(instance);
283
284 if !self.networks.is_empty() {
285 let mut networks: Vec<_> = self
286 .networks
287 .iter()
288 .map(|(&chain_id, info)| NetworkArm(chain_id, (info.0.clone(), info.1)))
289 .collect();
290 // Sorting is required to keep generation stable and changes to a
291 // minimum
292 networks.sort_by_key(|arm| arm.0);
293 let deployment_info = quote::quote! {
294 use {
295 std::{
296 sync::LazyLock,
297 collections::HashMap
298 },
299 anyhow::{Result, Context},
300 alloy_primitives::{address, Address},
301 alloy_provider::{Provider, DynProvider},
302 };
303
304 pub const fn deployment_info(chain_id: u64) -> Option<(Address, Option<u64>)> {
305 match chain_id {
306 #( #networks ,)*
307 _ => None
308 }
309 }
310
311 pub const fn deployment_address(chain_id: &u64) -> Option<::alloy_primitives::Address> {
312 match deployment_info(*chain_id) {
313 Some((address, _)) => Some(address),
314 None => None,
315 }
316 }
317
318 pub const fn deployment_block(chain_id: &u64) -> Option<u64> {
319 match deployment_info(*chain_id) {
320 Some((_, block)) => block,
321 None => None,
322 }
323 }
324
325 impl Instance {
326 pub fn deployed(provider: &DynProvider) -> impl Future<Output = Result<Self>> + Send {

Callers 1

write_crateMethod · 0.80

Calls 9

generate_bindingFunction · 0.85
NetworkArmClass · 0.85
artifacts_pathMethod · 0.80
extendMethod · 0.80
collectMethod · 0.80
newFunction · 0.50
cloneMethod · 0.45
is_emptyMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected