forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[r2r] get_current_mtp rpc impl (#1340)
* implemented get_current_mtp rpc && test * fmt fix * fix wasm * reitration * better error namings * review fixes * adjusted get mtp test * remove test_get_current_mtp from wasm target * Improved unit test with Mm2TestConf
- Loading branch information
1 parent
e48e0af
commit ac57077
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters