Skip to content

Commit

Permalink
Merge pull request #6 from DeFiCh/canonbrother/json-rpc-vault
Browse files Browse the repository at this point in the history
Fix: `list_auctions` api
  • Loading branch information
canonbrother authored Jul 1, 2024
2 parents 4a42f2b + ec2b4cc commit 962b5a0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
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

0 comments on commit 962b5a0

Please sign in to comment.