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

Ocean API MN typing #2744

Merged
merged 13 commits into from
Dec 13, 2023
29 changes: 13 additions & 16 deletions lib/ain-ocean/src/api/block.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,29 +21,23 @@ struct BlockHash {
hash: String,
}

#[derive(Deserialize)]
pub struct ListBlocksQuery {
pub size: usize,
pub next: Option<String>,
}

#[debug_handler]
async fn list_blocks(Query(query): Query<ListBlocksQuery>) -> Json<ApiPagedResponse<Block>> {
// TODO(): query from db
// db::block::list(req).await...
async fn list_blocks(Query(query): Query<PaginationQuery>) -> OceanResult<Json<ApiPagedResponse<Block>>> {
// 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<BlockId>) -> String {
format!("Details of block with id {}", id)
async fn get_block(Path(BlockId { id }): Path<BlockId>) -> OceanResult<Json<Block>> {
Ok(Json(Block {
id,
}))
}

async fn get_transactions(Path(BlockHash { hash }): Path<BlockHash>) -> String {
Expand Down
22 changes: 17 additions & 5 deletions lib/ain-ocean/src/api/masternode.rs
Original file line number Diff line number Diff line change
@@ -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<PaginationQuery>) -> OceanResult<Json<ApiPagedResponse<MasternodeData>>> {
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>) -> String {
format!("Details of masternode with id {}", masternode_id)
async fn get_masternode(Path(masternode_id): Path<String>) -> OceanResult<Json<MasternodeData>> {
Ok(Json(MasternodeData::new(&masternode_id)))
}

pub fn router() -> Router {
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-ocean/src/api/poolpairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
8 changes: 8 additions & 0 deletions lib/ain-ocean/src/api_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::Deserialize;

#[derive(Deserialize)]
pub struct PaginationQuery {
pub size: usize,
pub next: Option<String>
}

3 changes: 1 addition & 2 deletions lib/ain-ocean/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use thiserror::Error;

pub type OceanResult<T> = Result<T, OceanError>;

#[derive(Error, Debug)]
#[derive(thiserror::Error, Debug)]
pub enum OceanError {}

impl IntoResponse for OceanError {
Expand Down
1 change: 1 addition & 0 deletions lib/ain-ocean/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod api_paged_response;
pub mod api_query;
pub mod error;
mod indexer;

Expand Down
82 changes: 82 additions & 0 deletions lib/ain-ocean/src/model/masternode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MasternodeResign>,
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 {
Expand Down