Skip to content

Commit af860d3

Browse files
committed
refactor
1 parent 5b870fc commit af860d3

10 files changed

Lines changed: 322 additions & 179 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/main.rs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ pub mod helpers;
33

44
use crate::cli::{Cli, Commands, WalletAction};
55
use clap::Parser as _;
6+
use core::chain::SupportedChain;
67
use core::compiler::compile_contract;
7-
use core::layer_zero::deploy_on_chains;
8-
use core::provider::get_providers_with_endpoints;
8+
use core::layer_zero::{deploy_on_chains, setup_peer_connections};
9+
use core::provider::build_chain_clients;
910
use ethers::providers::Middleware;
1011
use ethers::signers::Signer;
1112
use ethers::types::Address;
1213
use std::process::Command;
14+
use std::str::FromStr;
1315

1416
#[tokio::main]
1517
async fn main() -> anyhow::Result<()> {
@@ -18,18 +20,6 @@ async fn main() -> anyhow::Result<()> {
1820
let cli = Cli::parse();
1921
let chain = cli.chain;
2022

21-
let providers = match get_providers_with_endpoints() {
22-
Ok(providers) => providers,
23-
Err(e) => return Err(e),
24-
};
25-
26-
let rpc_info = match providers.get(chain.as_str()) {
27-
Some(rpc_info) => rpc_info.clone(),
28-
None => return Err(anyhow::anyhow!("Chain '{}' not found", chain)),
29-
};
30-
31-
println!("{:?}", rpc_info);
32-
3323
match cli.command {
3424
Commands::Wallet { action } => match action {
3525
WalletAction::New => {
@@ -49,15 +39,14 @@ async fn main() -> anyhow::Result<()> {
4939
);
5040
}
5141
WalletAction::Balance { path, password } => {
52-
let wallet = helpers::get_wallet_from_keystore(
53-
path.as_str(),
54-
Some(rpc_info.clone().provider),
55-
password,
56-
)
57-
.await?;
42+
let wallet =
43+
helpers::get_wallet_from_keystore(path.as_str(), None, password).await?;
5844

5945
let addr: Address = wallet.address();
60-
let balance = rpc_info.provider.get_balance(addr, None).await?;
46+
let chain_object = SupportedChain::from_str(&chain).unwrap();
47+
let clients = build_chain_clients(&wallet).await?;
48+
let provider = clients.get(&chain_object).unwrap().provider();
49+
let balance = provider.get_balance(addr, None).await?;
6150

6251
println!(
6352
"Deployer: {:?}\nBalance: {} ETH",
@@ -71,15 +60,47 @@ async fn main() -> anyhow::Result<()> {
7160
password,
7261
chainlist,
7362
} => {
74-
let wallet = helpers::get_wallet_from_keystore(
75-
path.as_str(),
76-
Some(rpc_info.provider.clone()),
77-
password,
78-
)
79-
.await?;
80-
//let addr = deployer::deploy_oapp_contract(rpc_info, wallet).await?;
81-
deploy_on_chains(&chainlist, &wallet).await?;
82-
// println!("Deployed `{}` at {:?}\n", contract, addr);
63+
let wallet = helpers::get_wallet_from_keystore(path.as_str(), None, password).await?;
64+
65+
let supported_chains = chainlist
66+
.iter()
67+
.map(|chain_str| {
68+
chain_str
69+
.parse::<core::chain::SupportedChain>()
70+
.expect("Invalid chain")
71+
})
72+
.collect::<Vec<core::chain::SupportedChain>>();
73+
74+
let deployed = deploy_on_chains(&supported_chains, &wallet).await?;
75+
for (chain, addr) in supported_chains.iter().zip(deployed.iter()) {
76+
println!("Chain {}: Deployed contract at address: {:?}", chain, addr);
77+
}
78+
79+
// TODO make this optional
80+
setup_peer_connections(&wallet, &deployed).await?;
81+
// Send cross-chain message from each chain to every other chain
82+
println!("Sending cross-chain messages...");
83+
for (src_idx, src_chain) in supported_chains.iter().enumerate() {
84+
for (dst_idx, dst_chain) in supported_chains.iter().enumerate() {
85+
if src_idx != dst_idx {
86+
let message = format!("Hi {} from {}", dst_chain, src_chain);
87+
match core::layer_zero::send_cross_chain_message(
88+
&wallet, &deployed, src_chain, dst_chain, message,
89+
)
90+
.await
91+
{
92+
Ok(_) => println!(
93+
"Successfully sent message from {} to {}",
94+
src_chain, dst_chain
95+
),
96+
Err(e) => println!(
97+
"Failed to send message from {} to {}: {}",
98+
src_chain, dst_chain, e
99+
),
100+
}
101+
}
102+
}
103+
}
83104
}
84105
Commands::Compile => {
85106
println!("Compiling contract...");

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ tokio = { version = "1", features = ["full"] }
1212
foundry-compilers = "0.10.1"
1313
foundry-config = "0.2"
1414
dotenvy = "0.15"
15+
futures = "0.3.31"

core/src/chain.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use ethers::types::Address;
2+
use std::str::FromStr;
3+
4+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5+
pub enum SupportedChain {
6+
BaseSepolia,
7+
OptimismSepolia,
8+
ArbitrumSepolia,
9+
GnosisChiado,
10+
LineaSepolia,
11+
}
12+
13+
impl std::fmt::Display for SupportedChain {
14+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15+
match self {
16+
SupportedChain::BaseSepolia => write!(f, "base_sepolia"),
17+
SupportedChain::OptimismSepolia => write!(f, "optimism_sepolia"),
18+
SupportedChain::ArbitrumSepolia => write!(f, "arbitrum_sepolia"),
19+
SupportedChain::GnosisChiado => write!(f, "gnosis_chiado"),
20+
SupportedChain::LineaSepolia => write!(f, "linea_sepolia"),
21+
}
22+
}
23+
}
24+
25+
impl FromStr for SupportedChain {
26+
type Err = String;
27+
28+
fn from_str(s: &str) -> Result<Self, Self::Err> {
29+
match s.to_lowercase().as_str() {
30+
"base_sepolia" => Ok(SupportedChain::BaseSepolia),
31+
"optimism_sepolia" => Ok(SupportedChain::OptimismSepolia),
32+
"arbitrum_sepolia" => Ok(SupportedChain::ArbitrumSepolia),
33+
"gnosis_chiado" => Ok(SupportedChain::GnosisChiado),
34+
"linea_sepolia" => Ok(SupportedChain::LineaSepolia),
35+
_ => Err(format!("Unsupported chain alias: {}", s)),
36+
}
37+
}
38+
}
39+
40+
impl SupportedChain {
41+
pub fn lz_endpoint_id(&self) -> u32 {
42+
match self {
43+
SupportedChain::BaseSepolia => 40245,
44+
SupportedChain::OptimismSepolia => 40232,
45+
SupportedChain::ArbitrumSepolia => 40231,
46+
SupportedChain::GnosisChiado => 40145,
47+
SupportedChain::LineaSepolia => 40287,
48+
}
49+
}
50+
pub fn endpoint_address(&self) -> Address {
51+
match self {
52+
SupportedChain::BaseSepolia
53+
| SupportedChain::OptimismSepolia
54+
| SupportedChain::ArbitrumSepolia
55+
| SupportedChain::GnosisChiado
56+
| SupportedChain::LineaSepolia => "0x6EDCE65403992e310A62460808c4b910D972f10f"
57+
.parse::<Address>()
58+
.unwrap(),
59+
}
60+
}
61+
}

core/src/deployer.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use crate::provider::RpcInfo;
21
use anyhow::Result;
32
use ethers::prelude::*;
4-
use std::sync::Arc;
53

6-
pub async fn deploy_oapp_contract(rpc_info: RpcInfo, wallet: LocalWallet) -> Result<Address> {
7-
let endpoint_address = rpc_info.endpoint;
8-
let delegator_address = wallet.address();
4+
use crate::{chain::SupportedChain, provider::ChainClient};
95

10-
// FIXME Hardcoded string
11-
abigen!(MyOApp, "core/src/solidity/artifacts/MyOApp.sol/MyOApp.json");
6+
// FIXME Hardcoded string
7+
abigen!(MyOApp, "core/src/solidity/artifacts/MyOApp.sol/MyOApp.json");
128

13-
let client = Arc::new(SignerMiddleware::new(rpc_info.clone().provider, wallet));
9+
pub async fn deploy_oapp_contract(
10+
chain: &SupportedChain,
11+
chain_client: &ChainClient,
12+
) -> Result<Address> {
13+
let endpoint_address = chain.endpoint_address();
14+
let delegator_address = chain_client.address();
1415

15-
let deployed = MyOApp::deploy(client, (endpoint_address, delegator_address))
16+
let deployed = MyOApp::deploy(chain_client.clone(), (endpoint_address, delegator_address))
1617
.map_err(|e| {
1718
println!("Deployment preparation error: {:?}", e);
1819
anyhow::Error::from(e)

0 commit comments

Comments
 (0)