diff --git a/lib/ain-ocean/src/api/block.rs b/lib/ain-ocean/src/api/block.rs index 80c8191b90..8da1ba7cca 100644 --- a/lib/ain-ocean/src/api/block.rs +++ b/lib/ain-ocean/src/api/block.rs @@ -1,12 +1,15 @@ use axum::{ - debug_handler, extract::{Path, Query}, routing::get, Json, Router, }; use serde::{Deserialize, Serialize}; -use crate::api_paged_response::ApiPagedResponse; +use crate::{ + api_paged_response::ApiPagedResponse, + api_query::PaginationQuery, + error::OceanResult, +}; #[derive(Deserialize)] struct BlockId { @@ -18,29 +21,23 @@ struct BlockHash { hash: String, } -#[derive(Deserialize)] -pub struct ListBlocksQuery { - pub size: usize, - pub next: Option, -} - -#[debug_handler] -async fn list_blocks(Query(query): Query) -> Json> { - // TODO(): query from db - // db::block::list(req).await... +async fn list_blocks(Query(query): Query) -> OceanResult>> { + // TODO(): query from lvldb.. or maybe pull from index let blocks = vec![ Block { id: "0".into() }, Block { id: "1".into() }, Block { id: "2".into() }, ]; - Json(ApiPagedResponse::of(blocks, query.size, |block| { + Ok(Json(ApiPagedResponse::of(blocks, query.size, |block| { block.clone().id - })) + }))) } -async fn get_block(Path(BlockId { id }): Path) -> String { - format!("Details of block with id {}", id) +async fn get_block(Path(BlockId { id }): Path) -> OceanResult> { + Ok(Json(Block { + id, + })) } async fn get_transactions(Path(BlockHash { hash }): Path) -> String { diff --git a/lib/ain-ocean/src/api/masternode.rs b/lib/ain-ocean/src/api/masternode.rs index a15e184fd2..6b7011fab9 100644 --- a/lib/ain-ocean/src/api/masternode.rs +++ b/lib/ain-ocean/src/api/masternode.rs @@ -1,11 +1,23 @@ -use axum::{extract::Path, routing::get, Router}; +use axum::{extract::{Path, Query}, routing::get, Router, Json}; -async fn list_masternodes() -> String { - "List of masternodes".to_string() +use crate::{ + api_paged_response::ApiPagedResponse, + api_query::PaginationQuery, + error::OceanResult, + model::masternode::MasternodeData, +}; + +async fn list_masternodes(Query(query): Query) -> OceanResult>> { + let masternodes = vec![ + MasternodeData::new("0"), + ]; + Ok(Json(ApiPagedResponse::of(masternodes, query.size, |masternode| { + masternode.clone().id + }))) } -async fn get_masternode(Path(masternode_id): Path) -> String { - format!("Details of masternode with id {}", masternode_id) +async fn get_masternode(Path(masternode_id): Path) -> OceanResult> { + Ok(Json(MasternodeData::new(&masternode_id))) } pub fn router() -> Router { diff --git a/lib/ain-ocean/src/api/poolpairs.rs b/lib/ain-ocean/src/api/poolpairs.rs index 5950094938..f20fd790e6 100644 --- a/lib/ain-ocean/src/api/poolpairs.rs +++ b/lib/ain-ocean/src/api/poolpairs.rs @@ -100,7 +100,7 @@ pub fn router() -> Router { get(list_pool_swap_aggregates), ) .route("/paths/swappable/:tokenId", get(get_swappable_tokens)) - .route("/paths/best", get(get_best_path)) - .route("/paths", get(get_all_paths)) + .route("/paths/best/from/:fromTokenId/to/:toTokenId", get(get_best_path)) + .route("/paths/from/:fromTokenId/to/:toTokenId", get(get_all_paths)) .route("/dexprices", get(list_dex_prices)) } diff --git a/lib/ain-ocean/src/api_query.rs b/lib/ain-ocean/src/api_query.rs new file mode 100644 index 0000000000..fcd1dadea8 --- /dev/null +++ b/lib/ain-ocean/src/api_query.rs @@ -0,0 +1,8 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct PaginationQuery { + pub size: usize, + pub next: Option +} + diff --git a/lib/ain-ocean/src/error.rs b/lib/ain-ocean/src/error.rs index 1a037199cc..e0be55ee79 100644 --- a/lib/ain-ocean/src/error.rs +++ b/lib/ain-ocean/src/error.rs @@ -2,11 +2,10 @@ use axum::{ http::StatusCode, response::{IntoResponse, Response}, }; -use thiserror::Error; pub type OceanResult = Result; -#[derive(Error, Debug)] +#[derive(thiserror::Error, Debug)] pub enum OceanError {} impl IntoResponse for OceanError { diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 547943eaaa..15af10216d 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -1,4 +1,5 @@ pub mod api_paged_response; +pub mod api_query; pub mod error; mod indexer; diff --git a/lib/ain-ocean/src/model/masternode.rs b/lib/ain-ocean/src/model/masternode.rs index 27f1608dca..168bfbad77 100644 --- a/lib/ain-ocean/src/model/masternode.rs +++ b/lib/ain-ocean/src/model/masternode.rs @@ -25,6 +25,88 @@ pub struct MasternodeBlock { pub median_time: u64, } +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub enum MasternodeState { + PreEnabled, + Enabled, + PreResigned, + Resigned, + PreBanned, + Banned, + #[default] + Unknown, +} + +impl std::fmt::Display for MasternodeState { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + MasternodeState::PreEnabled => write!(f, "PRE_ENABLED"), + MasternodeState::Enabled => write!(f, "ENABLED"), + MasternodeState::PreResigned => write!(f, "PRE_RESIGNED"), + MasternodeState::Resigned => write!(f, "RESIGNED"), + MasternodeState::PreBanned => write!(f, "PRE_BANNED"), + MasternodeState::Banned => write!(f, "BANNED"), + MasternodeState::Unknown => write!(f, "UNKNOWN"), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct MasternodeOwner { + pub address: String, +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct MasternodeOperator { + pub address: String, +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct MasternodeCreation { + pub height: i32, +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct MasternodeResign { + pub tx: String, + pub height: i32, +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +#[serde(rename_all = "camelCase")] +pub struct MasternodeData { + pub id: String, + pub sort: String, + pub state: MasternodeState, + pub minted_blocks: i32, + pub owner: MasternodeOwner, + pub operator: MasternodeOperator, + pub creation: MasternodeCreation, + pub resign: Option, + pub timelock: i32, +} +impl MasternodeData { + pub fn new(id: &str) -> Self { + Self { + id: id.repeat(64), + sort: id.repeat(72), + state: MasternodeState::Enabled, + minted_blocks: 2, + owner: MasternodeOwner { + address: id.repeat(34), + }, + operator: MasternodeOperator { + address: id.repeat(34), + }, + creation: MasternodeCreation { + height: 0 + }, + resign: None, + timelock: 0 + } + } +} + #[derive(Serialize, Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] pub struct HistoryItem {