-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_bridge.rs
More file actions
105 lines (86 loc) · 3.83 KB
/
Copy pathbasic_bridge.rs
File metadata and controls
105 lines (86 loc) · 3.83 KB
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
101
102
103
104
105
// SPDX-FileCopyrightText: 2025 Semiotic AI, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//! Basic example of bridging USDC using cctp-rs
//!
//! This example demonstrates the basic flow of bridging USDC from Ethereum to Arbitrum.
//!
//! Run with: `cargo run --example basic_bridge`
use alloy_chains::NamedChain;
use alloy_primitives::Address;
use alloy_provider::ProviderBuilder;
use cctp_rs::{Cctp, CctpError, CctpV1};
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), CctpError> {
// Initialize tracing for better debugging
tracing_subscriber::fmt::init();
println!("🌉 CCTP Bridge Example - Ethereum to Arbitrum");
println!("============================================\n");
// Step 1: Set up providers
println!("1️⃣ Setting up providers...");
// In a real application, use your actual RPC endpoints
let eth_provider = ProviderBuilder::new()
.connect_http("https://eth-mainnet.g.alchemy.com/v2/demo".parse().unwrap());
let arb_provider = ProviderBuilder::new()
.connect_http("https://arb-mainnet.g.alchemy.com/v2/demo".parse().unwrap());
// Step 2: Create the CCTP bridge
println!("2️⃣ Creating CCTP bridge...");
// Example recipient address (replace with actual address)
let recipient = Address::from_str("0x742d35Cc6634C0532925a3b844Bc9e7595f8fA0d")?;
let bridge = Cctp::builder()
.source_chain(NamedChain::Mainnet)
.destination_chain(NamedChain::Arbitrum)
.source_provider(eth_provider)
.destination_provider(arb_provider)
.recipient(recipient)
.build();
// Step 3: Display bridge configuration
println!("3️⃣ Bridge Configuration:");
println!(" Source Chain: {}", bridge.source_chain());
println!(" Destination Chain: {}", bridge.destination_chain());
println!(" Recipient: {}", bridge.recipient());
// Get important contract addresses
let token_messenger = bridge.token_messenger_contract()?;
let message_transmitter = bridge.message_transmitter_contract()?;
let destination_domain = bridge.destination_domain_id()?;
println!("\n📋 Contract Information:");
println!(" Token Messenger (source): {token_messenger}");
println!(" Message Transmitter (dest): {message_transmitter}");
println!(" Destination Domain ID: {destination_domain}");
// Step 4: Display chain information
println!("\n🔗 Chain Information:");
let source_chain = bridge.source_chain();
let dest_chain = bridge.destination_chain();
println!(
" {} confirmation time: {} seconds",
source_chain,
source_chain.confirmation_average_time_seconds()?
);
println!(
" {} confirmation time: {} seconds",
dest_chain,
dest_chain.confirmation_average_time_seconds()?
);
// Step 5: Show how to bridge USDC (without actually executing)
println!("\n💸 To bridge USDC:");
println!(" 1. Approve USDC spending to Token Messenger contract");
println!(" 2. Call depositForBurn() on Token Messenger");
println!(" 3. Wait for transaction confirmation");
println!(" 4. Get attestation from Circle's API");
println!(" 5. Call receiveMessage() on destination chain");
// Example of how to get attestation URL
let example_message_hash = [0u8; 32].into();
let attestation_url = bridge.create_url(example_message_hash)?;
println!(
"\n🔍 Attestation API endpoint: {}",
attestation_url.as_str()
);
println!("\n✅ Bridge setup complete!");
println!("\n⚠️ Note: This example shows the configuration only.");
println!("To perform an actual bridge, you need to:");
println!("- Have USDC tokens on the source chain");
println!("- Execute the approve and burn transactions");
println!("- Wait for attestation and complete the mint");
Ok(())
}