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

Method generate_code

contracts/src/codegen.rs:268–346  ·  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

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

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