Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r2r] get_current_mtp rpc impl #1340

Merged
merged 10 commits into from
Jul 27, 2022
78 changes: 78 additions & 0 deletions mm2src/coins/rpc_command/get_current_mtp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use common::{HttpStatusCode, StatusCode};
use derive_more::Display;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::MmError;

use crate::{lp_coinfind_or_err,
utxo::{rpc_clients::UtxoRpcError, UtxoCommonOps},
CoinFindError, MmCoinEnum};

pub type GetCurrentMtpRpcResult<T> = Result<T, MmError<GetCurrentMtpError>>;

#[derive(Deserialize)]
pub struct GetCurrentMtpRequest {
coin: String,
}

#[derive(Serialize)]
pub struct GetCurrentMtpResponse {
mtp: u32,
}

#[derive(Serialize, Display, SerializeErrorType)]
#[serde(tag = "error_type", content = "error_data")]
pub enum GetCurrentMtpError {
NoSuchCoin(String),
NotSupportedCoin(String),
UtxoRpcError(String),
shamardy marked this conversation as resolved.
Show resolved Hide resolved
}

impl HttpStatusCode for GetCurrentMtpError {
fn status_code(&self) -> StatusCode {
match self {
GetCurrentMtpError::NoSuchCoin(_)
| GetCurrentMtpError::UtxoRpcError(_)
| GetCurrentMtpError::NotSupportedCoin(_) => StatusCode::BAD_REQUEST,
shamardy marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl From<UtxoRpcError> for GetCurrentMtpError {
fn from(err: UtxoRpcError) -> Self { Self::UtxoRpcError(err.to_string()) }
}

impl From<CoinFindError> for GetCurrentMtpError {
fn from(err: CoinFindError) -> Self {
match err {
CoinFindError::NoSuchCoin { coin } => Self::NoSuchCoin(format!("No such coin: {}", coin)),
}
shamardy marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub async fn get_current_mtp_rpc(
ctx: MmArc,
req: GetCurrentMtpRequest,
) -> GetCurrentMtpRpcResult<GetCurrentMtpResponse> {
match lp_coinfind_or_err(&ctx, &req.coin).await? {
MmCoinEnum::UtxoCoin(utxo) => Ok(GetCurrentMtpResponse {
mtp: utxo.get_current_mtp().await?,
}),
MmCoinEnum::QtumCoin(qtum) => Ok(GetCurrentMtpResponse {
mtp: qtum.get_current_mtp().await?,
}),
MmCoinEnum::Qrc20Coin(qrc) => Ok(GetCurrentMtpResponse {
mtp: qrc.get_current_mtp().await?,
}),
#[cfg(not(target_arch = "wasm32"))]
MmCoinEnum::ZCoin(zcoin) => Ok(GetCurrentMtpResponse {
mtp: zcoin.get_current_mtp().await?,
}),
MmCoinEnum::Bch(bch) => Ok(GetCurrentMtpResponse {
mtp: bch.get_current_mtp().await?,
}),
_ => Err(MmError::new(GetCurrentMtpError::NotSupportedCoin(format!(
"Requested coin: {}; is not supported for this action.",
&req.coin
)))),
}
}
1 change: 1 addition & 0 deletions mm2src/coins/rpc_command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod account_balance;
pub mod get_current_mtp;
pub mod hd_account_balance_rpc_error;
pub mod init_create_account;
pub mod init_scan_for_new_addresses;
Expand Down
47 changes: 47 additions & 0 deletions mm2src/mm2_main/src/mm2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6789,6 +6789,53 @@ fn test_mm2_db_migration() {
.unwrap();
}

#[test]
fn test_get_current_mtp() {
let passphrase = "cMhHM3PMpMrChygR4bLF7QsTdenhWpFrrmf2UezBG3eeFsz41rtL";
// KMD coin config used for this test
let coins = json!([
{"coin":"KMD","txversion":4,"overwintered":1,"txfee":10000,"protocol":{"type":"UTXO"}},
]);
let mm = MarketMakerIt::start(
json! ({
"gui": "nogui",
"netid": 9000,
"dht": "on", // Enable DHT without delay.
"passphrase": passphrase,
"coins": coins,
"rpc_password": "password",
"i_am_seed": true,
}),
"password".to_string(),
None,
)
.unwrap();
sergeyboyko0791 marked this conversation as resolved.
Show resolved Hide resolved
let (_dump_log, _dump_dashboard) = mm2_test_helpers::for_tests::mm_dump(&mm.log_path);

let electrum = block_on(enable_electrum(&mm, "KMD", false, &[
"electrum1.cipig.net:10001",
"electrum2.cipig.net:10001",
"electrum3.cipig.net:10001",
]));
log!("{:?}", electrum);

let rc = block_on(mm.rpc(&json!({
"userpass": mm.userpass,
"mmrpc": "2.0",
"method": "get_current_mtp",
"params": {
"coin": "KMD",
},
})))
.unwrap();

// Test if request is successful before proceeding.
assert_eq!(true, rc.0.is_success());
let mtp_result = <Json as std::str::FromStr>::from_str(&rc.1).unwrap();
sergeyboyko0791 marked this conversation as resolved.
Show resolved Hide resolved
// Test if mtp returns a u32 Number.
assert_eq!(true, mtp_result["result"]["mtp"].is_number());
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_get_public_key() {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{mm2::lp_stats::{add_node_to_version_stat, remove_node_from_version_s
use coins::hd_wallet::get_new_address;
use coins::my_tx_history_v2::my_tx_history_v2_rpc;
use coins::rpc_command::account_balance::account_balance;
use coins::rpc_command::get_current_mtp::get_current_mtp_rpc;
use coins::rpc_command::init_create_account::{init_create_new_account, init_create_new_account_status,
init_create_new_account_user_action};
use coins::rpc_command::init_scan_for_new_addresses::{init_scan_for_new_addresses, init_scan_for_new_addresses_status};
Expand Down Expand Up @@ -125,6 +126,7 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult<Re
"best_orders" => handle_mmrpc(ctx, request, best_orders_rpc_v2).await,
"enable_bch_with_tokens" => handle_mmrpc(ctx, request, enable_platform_coin_with_tokens::<BchCoin>).await,
"enable_slp" => handle_mmrpc(ctx, request, enable_token::<SlpToken>).await,
"get_current_mtp" => handle_mmrpc(ctx, request, get_current_mtp_rpc).await,
"get_new_address" => handle_mmrpc(ctx, request, get_new_address).await,
"get_public_key" => handle_mmrpc(ctx, request, get_public_key).await,
"get_public_key_hash" => handle_mmrpc(ctx, request, get_public_key_hash).await,
Expand Down