|
|
|
@ -24,17 +24,19 @@ pub enum EthereumConnection { |
|
|
|
|
// Construct boxed contracts in a big "if-else" chain to handle multiple
|
|
|
|
|
// combinations of middleware.
|
|
|
|
|
macro_rules! construct_box_contract { |
|
|
|
|
($contract:ident, $origin_slip44:expr, $address:expr, $provider:expr, $signer:expr) => {{ |
|
|
|
|
($contract:ident, $name:expr, $slip44:expr, $address:expr, $provider:expr, $signer:expr) => {{ |
|
|
|
|
if let Some(signer) = $signer { |
|
|
|
|
let provider = ethers_middleware::SignerMiddleware::new($provider, signer); |
|
|
|
|
Box::new(crate::abis::$contract::at( |
|
|
|
|
$origin_slip44, |
|
|
|
|
Box::new(crate::abis::$contract::new( |
|
|
|
|
$name, |
|
|
|
|
$slip44, |
|
|
|
|
$address, |
|
|
|
|
provider.into(), |
|
|
|
|
)) |
|
|
|
|
} else { |
|
|
|
|
Box::new(crate::abis::$contract::at( |
|
|
|
|
$origin_slip44, |
|
|
|
|
Box::new(crate::abis::$contract::new( |
|
|
|
|
$name, |
|
|
|
|
$slip44, |
|
|
|
|
$address, |
|
|
|
|
$provider.into(), |
|
|
|
|
)) |
|
|
|
@ -43,19 +45,19 @@ macro_rules! construct_box_contract { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
macro_rules! construct_ws_box_contract { |
|
|
|
|
($contract:ident, $slip44:expr, $address:expr, $url:expr, $signer:expr) => {{ |
|
|
|
|
($contract:ident, $name:expr, $slip44:expr, $address:expr, $url:expr, $signer:expr) => {{ |
|
|
|
|
let ws = ethers_providers::Ws::connect($url).await?; |
|
|
|
|
let provider = ethers_providers::Provider::new(ws); |
|
|
|
|
construct_box_contract!($contract, $slip44, $address, provider, $signer) |
|
|
|
|
construct_box_contract!($contract, $name, $slip44, $address, provider, $signer) |
|
|
|
|
}}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
macro_rules! construct_http_box_contract { |
|
|
|
|
($contract:ident, $slip44:expr, $address:expr, $url:expr, $signer:expr) => {{ |
|
|
|
|
($contract:ident, $name:expr, $slip44:expr, $address:expr, $url:expr, $signer:expr) => {{ |
|
|
|
|
let provider = |
|
|
|
|
ethers_providers::Provider::<ethers_providers::Http>::try_from($url.as_ref())?; |
|
|
|
|
|
|
|
|
|
construct_box_contract!($contract, $slip44, $address, provider, $signer) |
|
|
|
|
construct_box_contract!($contract, $name, $slip44, $address, provider, $signer) |
|
|
|
|
}}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -74,15 +76,23 @@ impl EthereumConf { |
|
|
|
|
/// Try to convert this into a home contract
|
|
|
|
|
pub async fn try_into_home( |
|
|
|
|
&self, |
|
|
|
|
name: &str, |
|
|
|
|
slip44: u32, |
|
|
|
|
address: Address, |
|
|
|
|
) -> Result<Box<dyn Home>, Report> { |
|
|
|
|
let b: Box<dyn Home> = match &self.connection { |
|
|
|
|
EthereumConnection::Http { url } => { |
|
|
|
|
construct_http_box_contract!(HomeContract, slip44, address, url, self.signer()) |
|
|
|
|
construct_http_box_contract!( |
|
|
|
|
HomeContract, |
|
|
|
|
name, |
|
|
|
|
slip44, |
|
|
|
|
address, |
|
|
|
|
url, |
|
|
|
|
self.signer() |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
EthereumConnection::Ws { url } => { |
|
|
|
|
construct_ws_box_contract!(HomeContract, slip44, address, url, self.signer()) |
|
|
|
|
construct_ws_box_contract!(HomeContract, name, slip44, address, url, self.signer()) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
Ok(b) |
|
|
|
@ -91,15 +101,30 @@ impl EthereumConf { |
|
|
|
|
/// Try to convert this into a replica contract
|
|
|
|
|
pub async fn try_into_replica( |
|
|
|
|
&self, |
|
|
|
|
name: &str, |
|
|
|
|
slip44: u32, |
|
|
|
|
address: Address, |
|
|
|
|
) -> Result<Box<dyn Replica>, Report> { |
|
|
|
|
let b: Box<dyn Replica> = match &self.connection { |
|
|
|
|
EthereumConnection::Http { url } => { |
|
|
|
|
construct_http_box_contract!(ReplicaContract, slip44, address, url, self.signer()) |
|
|
|
|
construct_http_box_contract!( |
|
|
|
|
ReplicaContract, |
|
|
|
|
name, |
|
|
|
|
slip44, |
|
|
|
|
address, |
|
|
|
|
url, |
|
|
|
|
self.signer() |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
EthereumConnection::Ws { url } => { |
|
|
|
|
construct_ws_box_contract!(ReplicaContract, slip44, address, url, self.signer()) |
|
|
|
|
construct_ws_box_contract!( |
|
|
|
|
ReplicaContract, |
|
|
|
|
name, |
|
|
|
|
slip44, |
|
|
|
|
address, |
|
|
|
|
url, |
|
|
|
|
self.signer() |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
Ok(b) |
|
|
|
|