Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
Signed-off-by: ozkanonur <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Feb 7, 2023
1 parent 0fe3c5e commit 71b5cd2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
17 changes: 12 additions & 5 deletions mm2src/coins/tendermint/rpc/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use mm2_number::BigDecimal;
pub type IBCChainRegistriesResult = Result<IBCChainRegistriesResponse, MmError<IBCChainsRequestError>>;
pub type IBCTransferChannelsResult = Result<IBCTransferChannelsResponse, MmError<IBCTransferChannelsRequestError>>;

// Global constants for interacting with https://github.com/cosmos/chain-registry repository
// using `mm2_git` crate.
pub(crate) const CHAIN_REGISTRY_REPO_OWNER: &str = "cosmos";
pub(crate) const CHAIN_REGISTRY_REPO_NAME: &str = "chain-registry";
pub(crate) const CHAIN_REGISTRY_BRANCH: &str = "master";
pub(crate) const CHAIN_REGISTRY_IBC_DIR_NAME: &str = "_IBC";

#[derive(Clone, Deserialize)]
pub struct IBCWithdrawRequest {
pub(crate) ibc_source_channel: String,
Expand All @@ -29,15 +36,15 @@ pub struct IBCTransferChannelsResponse {
}

#[derive(Clone, Serialize, Deserialize)]
pub struct IBCTransferChannel {
pub(crate) struct IBCTransferChannel {
pub(crate) channel_id: String,
pub(crate) ordering: String,
pub(crate) version: String,
pub(crate) tags: Option<IBCTransferChannelTag>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IBCTransferChannelTag {
pub(crate) struct IBCTransferChannelTag {
pub(crate) status: String,
pub(crate) preferred: bool,
pub(crate) dex: Option<String>,
Expand All @@ -58,8 +65,8 @@ pub enum IBCTransferChannelsRequestError {
_0
)]
UnsupportedCoin(String),
#[display(fmt = "Could not find '{}' chain in channel registry source.", _0)]
ChainCouldNotFound(String),
#[display(fmt = "Could not find '{}' registry source.", _0)]
RegistrySourceCouldNotFound(String),
#[display(fmt = "Transport error: {}", _0)]
Transport(String),
#[display(fmt = "Internal error: {}", _0)]
Expand Down Expand Up @@ -90,7 +97,7 @@ impl HttpStatusCode for IBCTransferChannelsRequestError {
IBCTransferChannelsRequestError::UnsupportedCoin(_) | IBCTransferChannelsRequestError::NoSuchCoin(_) => {
common::StatusCode::BAD_REQUEST
},
IBCTransferChannelsRequestError::ChainCouldNotFound(_) => common::StatusCode::NOT_FOUND,
IBCTransferChannelsRequestError::RegistrySourceCouldNotFound(_) => common::StatusCode::NOT_FOUND,
IBCTransferChannelsRequestError::Transport(_) => common::StatusCode::SERVICE_UNAVAILABLE,
IBCTransferChannelsRequestError::InternalError(_) => common::StatusCode::INTERNAL_SERVER_ERROR,
}
Expand Down
72 changes: 35 additions & 37 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use super::rpc::ibc::{IBCChainRegistriesResult, IBCChainsRequestError, IBCTransf
IBCTransferChannelsResult, IBCWithdrawRequest};
use super::rpc::*;
use crate::coin_errors::{MyAddressError, ValidatePaymentError};
use crate::tendermint::ibc::IBC_OUT_SOURCE_PORT;
use crate::tendermint::rpc::ibc::{IBCChainRegistriesResponse, IBCTransferChannel, IBCTransferChannelTag,
IBCTransferChannelsRequestError, IBCTransferChannelsResponse};
IBCTransferChannelsRequestError, IBCTransferChannelsResponse, CHAIN_REGISTRY_BRANCH,
CHAIN_REGISTRY_IBC_DIR_NAME, CHAIN_REGISTRY_REPO_NAME, CHAIN_REGISTRY_REPO_OWNER};
use crate::utxo::sat_from_big_decimal;
use crate::utxo::utxo_common::big_decimal_from_sat;
use crate::{big_decimal_from_sat_unsigned, BalanceError, BalanceFut, BigDecimal, CheckIfMyPaymentSentArgs,
Expand Down Expand Up @@ -656,48 +658,35 @@ impl TendermintCoin {
}

pub async fn get_ibc_transfer_channels(&self, req: IBCTransferChannelsRequest) -> IBCTransferChannelsResult {
const REPO_OWNER: &str = "cosmos";
const REPO_NAME: &str = "chain-registry";
const BRANCH: &str = "master";
const DIR_NAME: &str = "_IBC";

#[derive(Debug, Deserialize)]
#[derive(Deserialize)]
struct ChainRegistry {
chain_1: ChainInfo,
chain_2: ChainInfo,
channels: Vec<IbcChannel>,
}

#[derive(Debug, Deserialize)]
#[derive(Deserialize)]
struct ChannelInfo {
channel_id: String,
port_id: String,
}

#[derive(Deserialize)]
struct IbcChannel {
chain_1: ChannelInfo,
#[allow(dead_code)]
chain_2: ChannelInfo,
ordering: String,
version: String,
tags: Option<IBCTransferChannelTag>,
}

#[derive(Debug, Deserialize)]
struct ChainInfo {
chain_name: String,
client_id: String,
connection_id: String,
}

#[derive(Debug, Deserialize)]
struct ChannelInfo {
channel_id: String,
port_id: String,
}

let src_chain_registry_name = self.chain_registry_name.as_ref().or_mm_err(|| {
IBCTransferChannelsRequestError::InternalError(format!(
"`chain_registry_name` is not set for '{}'",
self.platform_ticker()
))
})?;

let source_channel_filename = format!(
let source_filename = format!(
"{}-{}.json",
src_chain_registry_name, req.destination_chain_registry_name
);
Expand All @@ -706,20 +695,29 @@ impl TendermintCoin {

let metadata_list = git_controller
.client
.get_file_metadata_list(REPO_OWNER, REPO_NAME, BRANCH, DIR_NAME)
.get_file_metadata_list(
CHAIN_REGISTRY_REPO_OWNER,
CHAIN_REGISTRY_REPO_NAME,
CHAIN_REGISTRY_BRANCH,
CHAIN_REGISTRY_IBC_DIR_NAME,
)
.await
.unwrap();
.map_err(|e| IBCTransferChannelsRequestError::Transport(format!("{:?}", e)))?;

let source_channel_file = metadata_list
.iter()
.find(|metadata| metadata.name == source_channel_filename)
.or_mm_err(|| IBCTransferChannelsRequestError::ChainCouldNotFound(self.platform_ticker().to_owned()))?;
.find(|metadata| metadata.name == source_filename)
.or_mm_err(|| IBCTransferChannelsRequestError::RegistrySourceCouldNotFound(source_filename))?;

let registry_object = git_controller
let mut registry_object = git_controller
.client
.deserialize_json_source::<ChainRegistry>(source_channel_file.to_owned())
.await
.unwrap();
.map_err(|e| IBCTransferChannelsRequestError::Transport(format!("{:?}", e)))?;

registry_object
.channels
.retain(|ch| ch.chain_1.port_id == *IBC_OUT_SOURCE_PORT);

let result: Vec<IBCTransferChannel> = registry_object
.channels
Expand Down Expand Up @@ -1658,11 +1656,6 @@ fn clients_from_urls(rpc_urls: &[String]) -> MmResult<Vec<HttpClient>, Tendermin
}

pub async fn get_ibc_chain_list() -> IBCChainRegistriesResult {
const REPO_OWNER: &str = "cosmos";
const REPO_NAME: &str = "chain-registry";
const BRANCH: &str = "master";
const DIR_NAME: &str = "_IBC";

fn map_metadata_to_chain_registry_name(metadata: &FileMetadata) -> Result<String, MmError<IBCChainsRequestError>> {
let split_filename_by_dash: Vec<&str> = metadata.name.split('-').collect();
let chain_registry_name = split_filename_by_dash
Expand All @@ -1682,9 +1675,14 @@ pub async fn get_ibc_chain_list() -> IBCChainRegistriesResult {

let metadata_list = git_controller
.client
.get_file_metadata_list(REPO_OWNER, REPO_NAME, BRANCH, DIR_NAME)
.get_file_metadata_list(
CHAIN_REGISTRY_REPO_OWNER,
CHAIN_REGISTRY_REPO_NAME,
CHAIN_REGISTRY_BRANCH,
CHAIN_REGISTRY_IBC_DIR_NAME,
)
.await
.unwrap();
.map_err(|e| IBCChainsRequestError::Transport(format!("{:?}", e)))?;

let chain_list: Result<Vec<String>, MmError<IBCChainsRequestError>> =
metadata_list.iter().map(map_metadata_to_chain_registry_name).collect();
Expand Down
3 changes: 3 additions & 0 deletions mm2src/mm2_git/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "mm2_git"
version = "0.1.0"
edition = "2021"

[lib]
doctest = false

[dependencies]
async-trait = "0.1"
common = { path = "../common" }
Expand Down

0 comments on commit 71b5cd2

Please sign in to comment.