Skip to content

Commit

Permalink
WIP: ibc refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed May 7, 2024
1 parent 9802160 commit b431563
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 425 deletions.
2 changes: 2 additions & 0 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ fn test_withdraw_impl_manual_fee() {
gas_price: 1.into(),
}),
memo: None,
ibc_source_channel: None,
};
coin.my_balance().wait().unwrap();

Expand Down Expand Up @@ -362,6 +363,7 @@ fn test_withdraw_impl_fee_details() {
gas_price: 1.into(),
}),
memo: None,
ibc_source_channel: None,
};
coin.my_balance().wait().unwrap();

Expand Down
16 changes: 16 additions & 0 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,10 @@ pub enum WithdrawFrom {
HDWalletAddress(StandardHDCoinAddress),
}

/// TODO: Avoid using a single request structure on every platform.
/// Instead, accept a generic type from withdraw implementations.
/// This way we won't have to update the payload for every platform when
/// one of them requires specific addition.
#[derive(Clone, Deserialize)]
pub struct WithdrawRequest {
coin: String,
Expand All @@ -1981,6 +1985,7 @@ pub struct WithdrawRequest {
max: bool,
fee: Option<WithdrawFee>,
memo: Option<String>,
ibc_source_channel: Option<String>,
/// Currently, this flag is used by ETH/ERC20 coins activated with MetaMask **only**.
#[cfg(target_arch = "wasm32")]
#[serde(default)]
Expand Down Expand Up @@ -2035,6 +2040,7 @@ impl WithdrawRequest {
max: true,
fee: None,
memo: None,
ibc_source_channel: None,
#[cfg(target_arch = "wasm32")]
broadcast: false,
}
Expand Down Expand Up @@ -2177,6 +2183,7 @@ pub enum TransactionType {
token_id: Option<BytesJson>,
},
NftTransfer,
TendermintIBCTransfer,
}

/// Transaction details
Expand Down Expand Up @@ -2809,6 +2816,13 @@ pub enum WithdrawError {
},
#[display(fmt = "Nft Protocol is not supported yet!")]
NftProtocolNotSupported,
#[display(fmt = "'chain_registry_name' was not found in coins configuration for '{}'", _0)]
RegistryNameIsMissing(String),
#[display(
fmt = "IBC channel could not found for '{}' address. Consider providing it manually with 'ibc_source_channel' in the request.",
_0
)]
IBCChannelCouldNotFound(String),
}

impl HttpStatusCode for WithdrawError {
Expand All @@ -2834,6 +2848,8 @@ impl HttpStatusCode for WithdrawError {
| WithdrawError::ContractTypeDoesntSupportNftWithdrawing(_)
| WithdrawError::CoinDoesntSupportNftWithdraw { .. }
| WithdrawError::NotEnoughNftsAmount { .. }
| WithdrawError::RegistryNameIsMissing(_)
| WithdrawError::IBCChannelCouldNotFound(_)
| WithdrawError::MyAddressNotNftOwner { .. } => StatusCode::BAD_REQUEST,
WithdrawError::HwError(_) => StatusCode::GONE,
#[cfg(target_arch = "wasm32")]
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/my_tx_history_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ impl<'a, Addr: Clone + DisplayAddress + Eq + std::hash::Hash, Tx: Transaction> T
| TransactionType::RemoveDelegation
| TransactionType::FeeForTokenTx
| TransactionType::StandardTransfer
| TransactionType::NftTransfer => tx_hash.clone(),
| TransactionType::NftTransfer
| TransactionType::TendermintIBCTransfer => tx_hash.clone(),
};

TransactionDetails {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/qrc20/qrc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn test_withdraw_to_p2sh_address_should_fail() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
};
let err = coin.withdraw(req).wait().unwrap_err().into_inner();
let expect = WithdrawError::InvalidAddress("QRC20 can be sent to P2PKH addresses only".to_owned());
Expand Down Expand Up @@ -131,6 +132,7 @@ fn test_withdraw_impl_fee_details() {
gas_price: 40,
}),
memo: None,
ibc_source_channel: None,
};
let tx_details = coin.withdraw(withdraw_req).wait().unwrap();

Expand Down
51 changes: 39 additions & 12 deletions mm2src/coins/rpc_command/tendermint/ibc_transfer_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use common::HttpStatusCode;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::MmError;

use crate::{lp_coinfind_or_err, MmCoinEnum};
use crate::{coin_conf, tendermint::get_ibc_transfer_channels};

pub type IBCTransferChannelsResult = Result<IBCTransferChannelsResponse, MmError<IBCTransferChannelsRequestError>>;

#[derive(Clone, Deserialize)]
pub struct IBCTransferChannelsRequest {
pub(crate) coin: String,
pub(crate) destination_chain_registry_name: String,
pub(crate) source_coin: String,
pub(crate) destination_coin: String,
}

#[derive(Clone, Serialize)]
Expand Down Expand Up @@ -42,10 +42,17 @@ pub enum IBCTransferChannelsRequestError {
_0
)]
UnsupportedCoin(String),
#[display(
fmt = "'chain_registry_name' was not found in coins configuration for '{}' prefix. Either update the coins configuration or use 'ibc_source_channel' in the request.",
_0
)]
RegistryNameIsMissing(String),
#[display(fmt = "Could not find '{}' registry source.", _0)]
RegistrySourceCouldNotFound(String),
#[display(fmt = "Transport error: {}", _0)]
Transport(String),
#[display(fmt = "Could not found channel for '{}'.", _0)]
CouldNotFoundChannel(String),
#[display(fmt = "Internal error: {}", _0)]
InternalError(String),
}
Expand All @@ -56,21 +63,41 @@ impl HttpStatusCode for IBCTransferChannelsRequestError {
IBCTransferChannelsRequestError::UnsupportedCoin(_) | IBCTransferChannelsRequestError::NoSuchCoin(_) => {
common::StatusCode::BAD_REQUEST
},
IBCTransferChannelsRequestError::RegistrySourceCouldNotFound(_) => common::StatusCode::NOT_FOUND,
IBCTransferChannelsRequestError::CouldNotFoundChannel(_)
| IBCTransferChannelsRequestError::RegistryNameIsMissing(_)
| IBCTransferChannelsRequestError::RegistrySourceCouldNotFound(_) => common::StatusCode::NOT_FOUND,
IBCTransferChannelsRequestError::Transport(_) => common::StatusCode::SERVICE_UNAVAILABLE,
IBCTransferChannelsRequestError::InternalError(_) => common::StatusCode::INTERNAL_SERVER_ERROR,
}
}
}

pub async fn ibc_transfer_channels(ctx: MmArc, req: IBCTransferChannelsRequest) -> IBCTransferChannelsResult {
let coin = lp_coinfind_or_err(&ctx, &req.coin)
.await
.map_err(|_| IBCTransferChannelsRequestError::NoSuchCoin(req.coin.clone()))?;
let source_coin_conf = coin_conf(&ctx, &req.source_coin);
let source_registry_name = source_coin_conf
.get("protocol")
.unwrap_or_else(|| &serde_json::Value::Null)
.get("protocol_data")
.unwrap_or_else(|| &serde_json::Value::Null)
.get("chain_registry_name")
.map(|t| t.as_str().unwrap_or_default().to_owned());

match coin {
MmCoinEnum::Tendermint(coin) => coin.get_ibc_transfer_channels(req).await,
MmCoinEnum::TendermintToken(token) => token.platform_coin.get_ibc_transfer_channels(req).await,
_ => MmError::err(IBCTransferChannelsRequestError::UnsupportedCoin(req.coin)),
}
let Some(source_registry_name) = source_registry_name else {
return MmError::err(IBCTransferChannelsRequestError::RegistryNameIsMissing(req.source_coin));
};

let destination_coin_conf = coin_conf(&ctx, &req.destination_coin);
let destination_registry_name = destination_coin_conf
.get("protocol")
.unwrap_or_else(|| &serde_json::Value::Null)
.get("protocol_data")
.unwrap_or_else(|| &serde_json::Value::Null)
.get("chain_registry_name")
.map(|t| t.as_str().unwrap_or_default().to_owned());

let Some(destination_registry_name) = destination_registry_name else {
return MmError::err(IBCTransferChannelsRequestError::RegistryNameIsMissing(req.destination_coin));
};

get_ibc_transfer_channels(source_registry_name, destination_registry_name).await
}
29 changes: 0 additions & 29 deletions mm2src/coins/rpc_command/tendermint/ibc_withdraw.rs

This file was deleted.

4 changes: 1 addition & 3 deletions mm2src/coins/rpc_command/tendermint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
mod ibc_chains;
mod ibc_transfer_channels;
mod ibc_withdraw;

pub use ibc_chains::*;
pub use ibc_transfer_channels::*;
pub use ibc_withdraw::*;

// Global constants for interacting with https://github.com/KomodoPlatform/chain-registry repository
// using `mm2_git` crate.
pub(crate) const CHAIN_REGISTRY_REPO_OWNER: &str = "KomodoPlatform";
pub(crate) const CHAIN_REGISTRY_REPO_NAME: &str = "chain-registry";
pub(crate) const CHAIN_REGISTRY_BRANCH: &str = "master";
pub(crate) const CHAIN_REGISTRY_BRANCH: &str = "nucl";
pub(crate) const CHAIN_REGISTRY_IBC_DIR_NAME: &str = "_IBC";
5 changes: 5 additions & 0 deletions mm2src/coins/solana/solana_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn solana_transaction_simulations() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
})
.compat(),
)
Expand Down Expand Up @@ -192,6 +193,7 @@ fn solana_transaction_zero_balance() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
})
.compat(),
);
Expand Down Expand Up @@ -221,6 +223,7 @@ fn solana_transaction_simulations_not_enough_for_fees() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
})
.compat(),
);
Expand Down Expand Up @@ -255,6 +258,7 @@ fn solana_transaction_simulations_max() {
max: true,
fee: None,
memo: None,
ibc_source_channel: None,
})
.compat(),
)
Expand Down Expand Up @@ -284,6 +288,7 @@ fn solana_test_transactions() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
})
.compat(),
)
Expand Down
1 change: 1 addition & 0 deletions mm2src/coins/solana/spl_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn test_spl_transactions() {
max: false,
fee: None,
memo: None,
ibc_source_channel: None,
})
.compat(),
)
Expand Down
Loading

0 comments on commit b431563

Please sign in to comment.