feature: adds name function to Common trait

buddies-main-deployment
James Prestwich 4 years ago committed by James Prestwich
parent 3382a591cc
commit 7f91fbdbea
No known key found for this signature in database
GPG Key ID: 7CC174C250AD83AD
  1. 16
      rust/optics-base/src/abis/mod.rs
  2. 4
      rust/optics-base/src/agent.rs
  3. 51
      rust/optics-base/src/settings/ethereum.rs
  4. 9
      rust/optics-base/src/settings/mod.rs
  5. 4
      rust/optics-core/src/traits/mod.rs

@ -26,6 +26,7 @@ where
{
contract: contracts::ReplicaContractInternal<M>,
slip44: u32,
name: String,
}
impl<M> ReplicaContract<M>
@ -34,10 +35,11 @@ where
{
/// Create a reference to a Replica at a specific Ethereum address on some
/// chain
pub fn at(slip44: u32, address: Address, provider: Arc<M>) -> Self {
pub fn new(name: &str, slip44: u32, address: Address, provider: Arc<M>) -> Self {
Self {
contract: contracts::ReplicaContractInternal::new(address, provider),
slip44,
name: name.to_owned(),
}
}
}
@ -47,6 +49,10 @@ impl<M> Common for ReplicaContract<M>
where
M: ethers_providers::Middleware + 'static,
{
fn name(&self) -> &str {
&self.name
}
async fn status(&self, txid: H256) -> Result<Option<TxOutcome>, ChainCommunicationError> {
let receipt_opt = self
.contract
@ -182,6 +188,7 @@ where
{
contract: contracts::HomeContractInternal<M>,
slip44: u32,
name: String,
}
impl<M> HomeContract<M>
@ -190,10 +197,11 @@ where
{
/// Create a reference to a Home at a specific Ethereum address on some
/// chain
pub fn at(slip44: u32, address: Address, provider: Arc<M>) -> Self {
pub fn new(name: &str, slip44: u32, address: Address, provider: Arc<M>) -> Self {
Self {
contract: contracts::HomeContractInternal::new(address, provider),
slip44,
name: name.to_owned(),
}
}
}
@ -203,6 +211,10 @@ impl<M> Common for HomeContract<M>
where
M: ethers_providers::Middleware + 'static,
{
fn name(&self) -> &str {
&self.name
}
async fn status(&self, txid: H256) -> Result<Option<TxOutcome>, ChainCommunicationError> {
let receipt_opt = self
.contract

@ -57,12 +57,12 @@ pub trait OpticsAgent: Send + Sync + std::fmt::Debug {
async fn run_from_settings(&self, settings: &Settings) -> Result<()> {
let home = settings
.home
.try_into_home()
.try_into_home("home")
.await
.wrap_err("failed to instantiate Home")?;
let replicas = join_all(settings.replicas.iter().map(|(k, v)| async move {
v.try_into_replica()
v.try_into_replica(k)
.await
.wrap_err_with(|| format!("Failed to instantiate replica named {}", k))
}))

@ -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)

@ -32,19 +32,20 @@ pub struct ChainSetup {
impl ChainSetup {
/// Try to convert the chain setting into a Home contract
pub async fn try_into_home(&self) -> Result<Box<dyn Home>, Report> {
pub async fn try_into_home(&self, name: &str) -> Result<Box<dyn Home>, Report> {
match &self.chain {
ChainConf::Ethereum(conf) => {
conf.try_into_home(self.slip44, self.address.parse()?).await
conf.try_into_home(name, self.slip44, self.address.parse()?)
.await
}
}
}
/// Try to convert the chain setting into a replica contract
pub async fn try_into_replica(&self) -> Result<Box<dyn Replica>, Report> {
pub async fn try_into_replica(&self, name: &str) -> Result<Box<dyn Replica>, Report> {
match &self.chain {
ChainConf::Ethereum(conf) => {
conf.try_into_replica(self.slip44, self.address.parse()?)
conf.try_into_replica(name, self.slip44, self.address.parse()?)
.await
}
}

@ -46,6 +46,10 @@ pub type ChainCommunicationError = Box<dyn std::error::Error + Send + Sync>;
/// Interface for attributes shared by Home and Replica
#[async_trait]
pub trait Common: Sync + Send + std::fmt::Debug {
/// Return an identifier (not necessarily unique) for the chain this
/// contract is running on.
fn name(&self) -> &str;
/// Get the status of a transaction
async fn status(&self, txid: H256) -> Result<Option<TxOutcome>, ChainCommunicationError>;

Loading…
Cancel
Save