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

Fix: list_auctions api #6

Merged
merged 20 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions client/src/traits/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Result, RpcApi};
use async_trait::async_trait;
use defichain_rpc_json::{common::UTXO, vault::*};

use crate::{into_json, Client, Result, RpcApi};
use crate::{into_json, obj_into_json, Client, Result, RpcApi};

#[async_trait]
pub trait VaultRPC: RpcApi {
Expand Down Expand Up @@ -31,7 +31,7 @@ pub trait VaultRPC: RpcApi {
owner: Option<String>,
pagination: Option<ListAuctionHistoryPagination>,
) -> Result<Vec<ListAuctionHistoryDetail>>;
async fn list_auctions(&self, pagination: AuctionPagination) -> Result<Vec<VaultLiquidation>>;
async fn list_auctions(&self, pagination: Option<AuctionPagination>) -> Result<Vec<VaultLiquidation>>;
async fn list_vaults(
&self,
options: ListVaultOptions,
Expand Down Expand Up @@ -103,8 +103,11 @@ impl VaultRPC for Client {
) -> Result<Vec<ListAuctionHistoryDetail>> {
self.call("listauctionhistory", &[into_json(owner)?, into_json(pagination)?]).await
}
async fn list_auctions(&self, pagination: AuctionPagination) -> Result<Vec<VaultLiquidation>> {
self.call("listauctions", &[into_json(pagination)?]).await
async fn list_auctions(
&self,
pagination: Option<AuctionPagination>
) -> Result<Vec<VaultLiquidation>> {
self.call("listauctions", &[obj_into_json(pagination)?]).await
}
async fn list_vaults(
&self,
Expand Down
3 changes: 1 addition & 2 deletions json/src/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ pub struct DestroyLoanScheme {
activate_after_block: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LoanSchemeResult {
pub id: String,
pub mincolratio: i64,
pub interestrate: f64,
default: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
93 changes: 69 additions & 24 deletions json/src/vault.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;

use crate::loan::LoanSchemeResult;
use std::{fmt, collections::HashMap};
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -16,8 +18,7 @@ pub struct UpdateVault {
loan_scheme_id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug)]
pub enum VaultState {
Unknown,
Active,
Expand All @@ -26,6 +27,49 @@ pub enum VaultState {
MayLiquidate,
}

impl Serialize for VaultState {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match *self {
VaultState::Unknown => serializer.serialize_str("unknown"),
VaultState::Active => serializer.serialize_str("active"),
VaultState::InLiquidation => serializer.serialize_str("inLiquidation"),
VaultState::Frozen => serializer.serialize_str("frozen"),
VaultState::MayLiquidate => serializer.serialize_str("mayLiquidate"),
}
}
}

impl<'a> Deserialize<'a> for VaultState {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<VaultState, D::Error> {
struct VaultStateVisitor;

impl<'de> Visitor<'de> for VaultStateVisitor {
type Value = VaultState;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`Unknown`, `Active`, `InLiquidation`, `Frozon`, `MayLiquidate`")
}

fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E>
{
match v {
"active" => Ok(VaultState::Active),
"inLiquidation" => Ok(VaultState::InLiquidation),
"frozen" => Ok(VaultState::Frozen),
"mayLiquidation" => Ok(VaultState::MayLiquidate),
_ => Ok(VaultState::Unknown),
}
}
}

deserializer.deserialize_identifier(VaultStateVisitor)
}
}

impl VaultState {
fn in_liquidation() -> Self { VaultState::InLiquidation }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Vault {
Expand Down Expand Up @@ -61,14 +105,15 @@ pub struct VaultActive {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VaultLiquidation {
vault_id: String,
loan_scheme_id: String,
owner_address: String,
state: VaultState,
liquidation_height: u64,
liquidation_penalty: f64,
batch_count: u64,
batches: Vec<VaultLiquidationBatch>,
pub vault_id: String,
pub loan_scheme_id: String,
pub owner_address: String,
#[serde(default = "VaultState::in_liquidation")]
pub state: VaultState,
pub liquidation_height: u64,
pub liquidation_penalty: f64,
pub batch_count: usize,
pub batches: Vec<VaultLiquidationBatch>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -123,32 +168,32 @@ pub struct PlaceAuctionBid {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuctionPagination {
start: Option<AuctionPaginationStart>,
including_start: Option<bool>,
limit: Option<u64>,
pub start: Option<AuctionPaginationStart>,
pub including_start: Option<bool>,
pub limit: Option<usize>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuctionPaginationStart {
vault_id: Option<String>,
height: Option<u64>,
pub vault_id: String,
pub height: u64,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VaultLiquidationBatch {
index: usize,
collaterals: Vec<String>,
loan: String,
highest_bid: Option<HighestBid>,
pub index: u32,
pub collaterals: Vec<String>,
pub loan: String,
pub highest_bid: Option<HighestBid>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HighestBid {
amount: String,
owner: String,
pub amount: String,
pub owner: String,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
Loading