2017-08-01 02:36:48 -07:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-08-03 15:48:12 -07:00
|
|
|
use tokio_core::reactor::{Handle};
|
2017-08-13 04:09:50 -07:00
|
|
|
use web3::Transport;
|
2017-08-01 02:36:48 -07:00
|
|
|
use web3::transports::ipc::Ipc;
|
2017-08-13 11:44:41 -07:00
|
|
|
use error::{Error, ResultExt};
|
2017-08-01 02:36:48 -07:00
|
|
|
use config::Config;
|
2017-08-12 11:03:48 -07:00
|
|
|
use contracts::{EthereumBridge, KovanBridge};
|
2017-08-04 08:57:09 -07:00
|
|
|
|
2017-08-01 02:36:48 -07:00
|
|
|
pub struct App<T> where T: Transport {
|
2017-08-12 07:57:07 -07:00
|
|
|
pub config: Config,
|
|
|
|
pub database_path: PathBuf,
|
|
|
|
pub connections: Connections<T>,
|
2017-08-01 02:36:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Connections<T> where T: Transport {
|
2017-08-12 07:57:07 -07:00
|
|
|
pub mainnet: T,
|
|
|
|
pub testnet: T,
|
2017-08-01 02:36:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Connections<Ipc> {
|
2017-08-02 03:45:15 -07:00
|
|
|
pub fn new_ipc<P: AsRef<Path>>(handle: &Handle, mainnet: P, testnet: P) -> Result<Self, Error> {
|
|
|
|
let mainnet = Ipc::with_event_loop(mainnet, handle).chain_err(|| "Cannot connect to mainnet node ipc")?;
|
|
|
|
let testnet = Ipc::with_event_loop(testnet, handle).chain_err(|| "Cannot connect to testnet node ipc")?;
|
|
|
|
|
2017-08-01 02:36:48 -07:00
|
|
|
let result = Connections {
|
2017-08-02 03:45:15 -07:00
|
|
|
mainnet,
|
|
|
|
testnet,
|
2017-08-01 02:36:48 -07:00
|
|
|
};
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 11:44:41 -07:00
|
|
|
impl<T: Transport> Connections<T> {
|
|
|
|
pub fn as_ref(&self) -> Connections<&T> {
|
|
|
|
Connections {
|
|
|
|
mainnet: &self.mainnet,
|
|
|
|
testnet: &self.testnet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 02:36:48 -07:00
|
|
|
impl App<Ipc> {
|
2017-08-03 15:48:12 -07:00
|
|
|
pub fn new_ipc<P: AsRef<Path>>(config: Config, database_path: P, handle: &Handle) -> Result<Self, Error> {
|
|
|
|
let connections = Connections::new_ipc(handle, &config.mainnet.ipc, &config.testnet.ipc)?;
|
2017-08-01 02:36:48 -07:00
|
|
|
let result = App {
|
|
|
|
config,
|
|
|
|
database_path: database_path.as_ref().to_path_buf(),
|
|
|
|
connections,
|
|
|
|
};
|
|
|
|
Ok(result)
|
|
|
|
}
|
2017-08-12 07:57:07 -07:00
|
|
|
}
|
2017-08-01 02:36:48 -07:00
|
|
|
|
2017-08-12 07:57:07 -07:00
|
|
|
impl<T: Transport> App<T> {
|
2017-08-13 11:44:41 -07:00
|
|
|
pub fn as_ref(&self) -> App<&T> {
|
|
|
|
App {
|
|
|
|
config: self.config.clone(),
|
|
|
|
connections: self.connections.as_ref(),
|
|
|
|
database_path: self.database_path.clone(),
|
2017-08-01 02:36:48 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-12 07:57:07 -07:00
|
|
|
|
|
|
|
pub fn mainnet_bridge(&self) -> EthereumBridge {
|
|
|
|
EthereumBridge(&self.config.mainnet.contract.abi)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn testnet_bridge(&self) -> KovanBridge {
|
|
|
|
KovanBridge(&self.config.mainnet.contract.abi)
|
|
|
|
}
|
2017-08-01 02:36:48 -07:00
|
|
|
}
|