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
72 changes: 72 additions & 0 deletions mm2src/coins/rpc_command/get_current_mtp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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),
#[display(fmt = "Requested coin: {}; is not supported for this action.", _0)]
NotSupportedCoin(String),
RpcError(String),
}

impl HttpStatusCode for GetCurrentMtpError {
fn status_code(&self) -> StatusCode {
match self {
GetCurrentMtpError::NoSuchCoin(_) => StatusCode::PRECONDITION_REQUIRED,
GetCurrentMtpError::NotSupportedCoin(_) => StatusCode::BAD_REQUEST,
GetCurrentMtpError::RpcError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}

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

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

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(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
39 changes: 39 additions & 0 deletions mm2src/mm2_main/src/mm2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6790,6 +6790,45 @@ fn test_mm2_db_migration() {
.unwrap();
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_get_current_mtp() {
use mm2_test_helpers::for_tests::Mm2TestConf;

// KMD coin config used for this test
let coins = json!([
{"coin":"KMD","txversion":4,"overwintered":1,"txfee":10000,"protocol":{"type":"UTXO"}},
]);
let passphrase = "cMhHM3PMpMrChygR4bLF7QsTdenhWpFrrmf2UezBG3eeFsz41rtL";

let conf = Mm2TestConf::seednode(&passphrase, &coins);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's overhead to run a seednode in this test.
Seednode opens a server socket and listens for incoming P2P connections.
But anyway it's not critical now

let mm = MarketMakerIt::start(conf.conf, conf.rpc_password, conf.local).unwrap();
let (_dump_log, _dump_dashboard) = mm.mm_dump();

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 = json::from_str(&rc.1).unwrap();
// 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