From 4a42f2b069b61efb08a336f066e337ee5ccbe9a9 Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 27 May 2024 12:56:39 +0100 Subject: [PATCH] WIP --- client/src/traits/vault.rs | 126 +++++++++++++++++++++++++++++++++---- json/src/loan.rs | 2 +- json/src/vault.rs | 50 ++++++++++----- 3 files changed, 150 insertions(+), 28 deletions(-) diff --git a/client/src/traits/vault.rs b/client/src/traits/vault.rs index c5759fb..aeb06ff 100644 --- a/client/src/traits/vault.rs +++ b/client/src/traits/vault.rs @@ -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) -> Result; - fn create_vault(&self, vault: CreateVault, utxos: Option) -> Result; - fn deposit_to_vault(&self, deposit_vault: DepositVault, utxos: Option) -> Result; - fn estimate_collateral(&self, target_ratio: u64) -> Result<()>; - fn estimate_loan( + async fn close_vault(&self, close_vault: CloseVault, utxos: Option) -> Result; + async fn create_vault(&self, vault: CreateVault, utxos: Option) -> Result; + async fn deposit_to_vault( + &self, + deposit_vault: DepositVault, + utxos: Option, + ) -> Result; + 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, ) -> Result<()>; - fn estimate_vault(&self) -> Result; - fn get_vault(&self, vault_id: String) -> Result<()>; - fn list_auction_history( + async fn estimate_vault(&self) -> Result; + async fn get_vault(&self, vault_id: String, verbose: Option) -> Result; + async fn list_auction_history( &self, + owner: Option, pagination: Option, ) -> Result>; - fn list_auctions(&self) -> Result>; - fn list_vaults(&self) -> Result; - fn place_auction_bid( + async fn list_auctions(&self, pagination: AuctionPagination) -> Result>; + async fn list_vaults( + &self, + options: ListVaultOptions, + pagination: VaultPagination, + ) -> Result>; + async fn place_auction_bid( &self, place_auction_bid: PlaceAuctionBid, utxos: Option, ) -> Result; - fn update_vault( + async fn update_vault( &self, vault_id: String, vault: UpdateVault, utxos: Option, ) -> Result; - fn withdraw_from_vault( + async fn withdraw_from_vault( &self, withdraw_vault: WithdrawVault, utxos: Option, ) -> Result; } + +#[async_trait] +impl VaultRPC for Client { + async fn close_vault(&self, close_vault: CloseVault, utxos: Option) -> Result { + self.call("closevault", &[into_json(close_vault)?, into_json(utxos)?]).await + } + async fn create_vault(&self, vault: CreateVault, utxos: Option) -> Result { + self.call("createvault", &[into_json(vault)?, into_json(utxos)?]).await + } + async fn deposit_to_vault( + &self, + deposit_vault: DepositVault, + utxos: Option, + ) -> Result { + 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, + ) -> Result<()> { + self.call( + "estimateloan", + &[into_json(vault_id)?, into_json(token_split)?, into_json(target_ratio)?], + ) + .await + } + async fn estimate_vault(&self) -> Result { + self.call("estimatevault", &[]).await + } + async fn get_vault(&self, vault_id: String, verbose: Option) -> Result { + self.call("getvault", &[into_json(vault_id)?, into_json(verbose.unwrap_or_default())?]) + .await + } + async fn list_auction_history( + &self, + owner: Option, + pagination: Option, + ) -> Result> { + self.call("listauctionhistory", &[into_json(owner)?, into_json(pagination)?]).await + } + async fn list_auctions(&self, pagination: AuctionPagination) -> Result> { + self.call("listauctions", &[into_json(pagination)?]).await + } + async fn list_vaults( + &self, + options: ListVaultOptions, + pagination: VaultPagination, + ) -> Result> { + self.call("listvaults", &[into_json(options)?, into_json(pagination)?]).await + } + async fn place_auction_bid( + &self, + place_auction_bid: PlaceAuctionBid, + utxos: Option, + ) -> Result { + self.call("placeauctionbid", &[into_json(place_auction_bid)?, into_json(utxos)?]).await + } + async fn update_vault( + &self, + vault_id: String, + vault: UpdateVault, + utxos: Option, + ) -> Result { + 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, + ) -> Result { + self.call("withdrawfromvault", &[into_json(withdraw_vault)?, into_json(utxos)?]).await + } +} diff --git a/json/src/loan.rs b/json/src/loan.rs index fd1f171..da353de 100644 --- a/json/src/loan.rs +++ b/json/src/loan.rs @@ -31,7 +31,7 @@ pub struct LoanSchemeResult { pub id: String, pub mincolratio: i64, pub interestrate: f64, - default: bool, + default: Option, } #[derive(Debug, Serialize, Deserialize)] diff --git a/json/src/vault.rs b/json/src/vault.rs index dceca02..93a441f 100644 --- a/json/src/vault.rs +++ b/json/src/vault.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use crate::loan::LoanSchemeResult; + #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct CreateVault { @@ -15,7 +17,7 @@ pub struct UpdateVault { } #[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "UPPERCASE")] +#[serde(rename_all = "camelCase")] pub enum VaultState { Unknown, Active, @@ -23,6 +25,7 @@ pub enum VaultState { Frozen, MayLiquidate, } + #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Vault { @@ -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, loan_amounts: Vec, interest_amounts: Vec, - 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, + #[serde(skip_serializing_if = "Option::is_none")] interest_per_block_value: Option, + #[serde(skip_serializing_if = "Option::is_none")] interests_per_block: Option>, } #[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, } @@ -73,7 +87,7 @@ pub struct WithdrawVault { amount: String, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Default)] #[serde(rename_all = "camelCase")] pub struct VaultPagination { start: Option, @@ -81,13 +95,13 @@ pub struct VaultPagination { limit: Option, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Default)] #[serde(rename_all = "camelCase")] pub struct ListVaultOptions { - owner_address: Option, - loan_scheme_id: Option, - state: Option, - verbose: Option, + pub owner_address: Option, + pub loan_scheme_id: Option, + pub state: Option, + pub verbose: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -124,7 +138,7 @@ pub struct AuctionPaginationStart { #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct VaultLiquidationBatch { - index: u64, + index: usize, collaterals: Vec, loan: String, highest_bid: Option, @@ -170,3 +184,11 @@ pub struct VaultEstimation { #[derive(Debug, Serialize, Deserialize)] pub struct TokenPercentageSplit(HashMap); + +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum VaultResult { + VaultActive(VaultActive), // Verbose active + VaultLiquidation(VaultLiquidation), // Verbose in liquidation + // Vault(Vault), // Any state non-verbose +}