use super::error::RouterError;
use crate::cosmos_client::query_contract::QueryContractResponse;
use crate::cosmos_client::CosmosClient;
use crate::tx_builder::TxBuilder;
use cosmrs::proto::cosmos::auth::v1beta1::BaseAccount;
use cosmrs::tendermint::Hash;
use cosmrs::AccountId;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::sync::Arc;
use tendermint_rpc::endpoint::broadcast::tx_commit::Response;
use tendermint_rpc::endpoint::{status, tx};
pub struct RouterAdminClient {
pub public_router_client: RouterClient,
}
impl RouterAdminClient {
pub fn new(
cosmos_client: Arc<CosmosClient>,
router_contract_address: AccountId,
) -> Result<Self, RouterError> {
let public_router_client = RouterClient::new(cosmos_client, router_contract_address)?;
Ok(RouterAdminClient {
public_router_client,
})
}
#[cfg(any(test, feature = "test_scenario"))]
pub fn from_scenario(
scenario: &crate::test_utils::test_scenario::TestScenario,
router_contract: AccountId,
) -> Result<Self, RouterError> {
Self::new(scenario.cosmos_client.clone(), router_contract)
}
pub async fn execute_tx(&self, builder: TxBuilder) -> Result<Response, RouterError> {
self.public_router_client
.cosmos_client
.execute_tx(builder)
.await
.map_err(RouterError::CosmosClientError)
}
pub async fn broadcast_tx(&self, signed_bytes: Vec<u8>) -> Result<Response, RouterError> {
self.public_router_client
.cosmos_client
.broadcast_tx(signed_bytes)
.await
.map_err(RouterError::CosmosClientError)
}
pub async fn account(&self, account_address: String) -> Result<BaseAccount, RouterError> {
self.public_router_client.account(account_address).await
}
}
#[derive(Debug)]
pub struct RouterClient {
pub(crate) cosmos_client: Arc<CosmosClient>,
pub(crate) router_contract_address: AccountId,
}
impl RouterClient {
pub fn new(
cosmos_client: Arc<CosmosClient>,
router_contract_address: AccountId,
) -> Result<Self, RouterError> {
Ok(RouterClient {
cosmos_client,
router_contract_address,
})
}
pub fn get_contract_address(&self) -> AccountId {
self.router_contract_address.clone()
}
pub async fn status(&self) -> Result<status::Response, RouterError> {
self.cosmos_client
.status()
.await
.map_err(RouterError::CosmosClientError)
}
pub async fn account(&self, account_address: String) -> Result<BaseAccount, RouterError> {
self.cosmos_client
.account(account_address)
.await
.map_err(RouterError::CosmosClientError)
}
pub async fn get_tx(&self, hash: String) -> Result<tx::Response, RouterError> {
let hash = Hash::from_str(&hash)?;
self.cosmos_client
.get_tx(hash)
.await
.map_err(RouterError::CosmosClientError)
}
pub(crate) async fn query_contract<QUERY, RESPONSE>(
&self,
contract_address: String,
query: &QUERY,
height: Option<u64>,
) -> Result<QueryContractResponse<RESPONSE>, RouterError>
where
QUERY: Serialize,
for<'de> RESPONSE: Deserialize<'de>,
{
self.cosmos_client
.query_contract(contract_address, query, height)
.await
.map_err(RouterError::CosmosClientError)
}
}
#[cfg(test)]
mod tests {
use serial_test::serial;
use super::*;
use crate::test_utils::test_scenario::TestScenario;
#[tokio::test]
#[serial]
async fn test_router_admin_client() {
let test_scenario = TestScenario::new_from_config("config.json".to_string()).await;
let router_contract_address =
"archway1zeryxg95st30qm5elsxnr8np7fxvgeaknr9lw55hadlxejardymqw3sh2e".to_string();
let client = RouterAdminClient::new(
test_scenario.cosmos_client,
router_contract_address.parse().unwrap(),
)
.unwrap();
let _ = client
.public_router_client
.cosmos_client
.status()
.await
.unwrap();
}
#[tokio::test]
#[serial]
async fn test_router_client() {
let test_scenario = TestScenario::new_from_config("config.json".to_string()).await;
let router_contract_address =
"archway1zeryxg95st30qm5elsxnr8np7fxvgeaknr9lw55hadlxejardymqw3sh2e".to_string();
let client = RouterClient::new(
test_scenario.cosmos_client,
router_contract_address
.parse()
.expect("Failed to parse account id"),
)
.unwrap();
let _ = client.cosmos_client.status().await.unwrap();
}
}