Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored and canonbrother committed Jul 1, 2024
1 parent bd540ae commit 4a42f2b
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 28 deletions.
126 changes: 113 additions & 13 deletions client/src/traits/vault.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,139 @@
use crate::{Result, RpcApi};
use async_trait::async_trait;
use defichain_rpc_json::{common::UTXO, vault::*};

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

#[async_trait]
pub trait VaultRPC: RpcApi {
fn close_vault(&self, close_vault: CloseVault, utxos: Option<UTXO>) -> Result<String>;
fn create_vault(&self, vault: CreateVault, utxos: Option<UTXO>) -> Result<String>;
fn deposit_to_vault(&self, deposit_vault: DepositVault, utxos: Option<UTXO>) -> Result<String>;
fn estimate_collateral(&self, target_ratio: u64) -> Result<()>;
fn estimate_loan(
async fn close_vault(&self, close_vault: CloseVault, utxos: Option<UTXO>) -> Result<String>;
async fn create_vault(&self, vault: CreateVault, utxos: Option<UTXO>) -> Result<String>;
async fn deposit_to_vault(
&self,
deposit_vault: DepositVault,
utxos: Option<UTXO>,
) -> Result<String>;
async fn estimate_collateral(
&self,
target_ratio: u64,
token_split: TokenPercentageSplit,
) -> Result<()>;
async fn estimate_loan(
&self,
vault_id: String,
token_split: TokenPercentageSplit,
target_ratio: Option<u64>,
) -> Result<()>;
fn estimate_vault(&self) -> Result<VaultEstimation>;
fn get_vault(&self, vault_id: String) -> Result<()>;
fn list_auction_history(
async fn estimate_vault(&self) -> Result<VaultEstimation>;
async fn get_vault(&self, vault_id: String, verbose: Option<bool>) -> Result<VaultResult>;
async fn list_auction_history(
&self,
owner: Option<String>,
pagination: Option<ListAuctionHistoryPagination>,
) -> Result<Vec<ListAuctionHistoryDetail>>;
fn list_auctions(&self) -> Result<Vec<VaultLiquidation>>;
fn list_vaults(&self) -> Result<VaultActive>;
fn place_auction_bid(
async fn list_auctions(&self, pagination: AuctionPagination) -> Result<Vec<VaultLiquidation>>;
async fn list_vaults(
&self,
options: ListVaultOptions,
pagination: VaultPagination,
) -> Result<Vec<VaultResult>>;
async fn place_auction_bid(
&self,
place_auction_bid: PlaceAuctionBid,
utxos: Option<UTXO>,
) -> Result<String>;
fn update_vault(
async fn update_vault(
&self,
vault_id: String,
vault: UpdateVault,
utxos: Option<UTXO>,
) -> Result<String>;
fn withdraw_from_vault(
async fn withdraw_from_vault(
&self,
withdraw_vault: WithdrawVault,
utxos: Option<UTXO>,
) -> Result<String>;
}

#[async_trait]
impl VaultRPC for Client {
async fn close_vault(&self, close_vault: CloseVault, utxos: Option<UTXO>) -> Result<String> {
self.call("closevault", &[into_json(close_vault)?, into_json(utxos)?]).await
}
async fn create_vault(&self, vault: CreateVault, utxos: Option<UTXO>) -> Result<String> {
self.call("createvault", &[into_json(vault)?, into_json(utxos)?]).await
}
async fn deposit_to_vault(
&self,
deposit_vault: DepositVault,
utxos: Option<UTXO>,
) -> Result<String> {
self.call("deposittovault", &[into_json(deposit_vault)?, into_json(utxos)?]).await
}
async fn estimate_collateral(
&self,
target_ratio: u64,
token_split: TokenPercentageSplit,
) -> Result<()> {
self.call("estimatecollateral", &[into_json(target_ratio)?, into_json(token_split)?]).await
}
async fn estimate_loan(
&self,
vault_id: String,
token_split: TokenPercentageSplit,
target_ratio: Option<u64>,
) -> Result<()> {
self.call(
"estimateloan",
&[into_json(vault_id)?, into_json(token_split)?, into_json(target_ratio)?],
)
.await
}
async fn estimate_vault(&self) -> Result<VaultEstimation> {
self.call("estimatevault", &[]).await
}
async fn get_vault(&self, vault_id: String, verbose: Option<bool>) -> Result<VaultResult> {
self.call("getvault", &[into_json(vault_id)?, into_json(verbose.unwrap_or_default())?])
.await
}
async fn list_auction_history(
&self,
owner: Option<String>,
pagination: Option<ListAuctionHistoryPagination>,
) -> 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_vaults(
&self,
options: ListVaultOptions,
pagination: VaultPagination,
) -> Result<Vec<VaultResult>> {
self.call("listvaults", &[into_json(options)?, into_json(pagination)?]).await
}
async fn place_auction_bid(
&self,
place_auction_bid: PlaceAuctionBid,
utxos: Option<UTXO>,
) -> Result<String> {
self.call("placeauctionbid", &[into_json(place_auction_bid)?, into_json(utxos)?]).await
}
async fn update_vault(
&self,
vault_id: String,
vault: UpdateVault,
utxos: Option<UTXO>,
) -> Result<String> {
self.call("updatevault", &[into_json(vault_id)?, into_json(vault)?, into_json(utxos)?])
.await
}
async fn withdraw_from_vault(
&self,
withdraw_vault: WithdrawVault,
utxos: Option<UTXO>,
) -> Result<String> {
self.call("withdrawfromvault", &[into_json(withdraw_vault)?, into_json(utxos)?]).await
}
}
2 changes: 1 addition & 1 deletion json/src/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct LoanSchemeResult {
pub id: String,
pub mincolratio: i64,
pub interestrate: f64,
default: bool,
default: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
50 changes: 36 additions & 14 deletions json/src/vault.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use crate::loan::LoanSchemeResult;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateVault {
Expand All @@ -15,14 +17,15 @@ pub struct UpdateVault {
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
#[serde(rename_all = "camelCase")]
pub enum VaultState {
Unknown,
Active,
InLiquidation,
Frozen,
MayLiquidate,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Vault {
Expand All @@ -35,24 +38,35 @@ pub struct Vault {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VaultActive {
vault_id: String,
loan_scheme_id: String,
owner_address: String,
state: VaultState,
collateral_amounts: Vec<String>,
loan_amounts: Vec<String>,
interest_amounts: Vec<String>,
collateral_value: i64,
loan_value: i64,
interest_value: i64,
collateral_ratio: u64,
informative_ratio: i64,
collateral_value: f64,
loan_value: f64,
interest_value: f64,
collateral_ratio: i64,
informative_ratio: f64,
#[serde(skip_serializing_if = "Option::is_none")]
next_collateral_ratio: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
interest_per_block_value: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
interests_per_block: Option<Vec<String>>,
}

#[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: u64,
liquidation_penalty: f64,
batch_count: u64,
batches: Vec<VaultLiquidationBatch>,
}
Expand All @@ -73,21 +87,21 @@ pub struct WithdrawVault {
amount: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct VaultPagination {
start: Option<String>,
including_start: Option<bool>,
limit: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ListVaultOptions {
owner_address: Option<String>,
loan_scheme_id: Option<String>,
state: Option<VaultState>,
verbose: Option<bool>,
pub owner_address: Option<String>,
pub loan_scheme_id: Option<String>,
pub state: Option<VaultState>,
pub verbose: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -124,7 +138,7 @@ pub struct AuctionPaginationStart {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VaultLiquidationBatch {
index: u64,
index: usize,
collaterals: Vec<String>,
loan: String,
highest_bid: Option<HighestBid>,
Expand Down Expand Up @@ -170,3 +184,11 @@ pub struct VaultEstimation {

#[derive(Debug, Serialize, Deserialize)]
pub struct TokenPercentageSplit(HashMap<String, u64>);

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum VaultResult {
VaultActive(VaultActive), // Verbose active
VaultLiquidation(VaultLiquidation), // Verbose in liquidation
// Vault(Vault), // Any state non-verbose
}

0 comments on commit 4a42f2b

Please sign in to comment.