From 1f4c70f7b59fd5767c0c0df1ed61bc01e7c40315 Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Wed, 14 Jul 2021 11:17:49 +0100 Subject: [PATCH] feat: add rpc calls to compute vault collateral with and without nomination Signed-off-by: Gregory Hill --- .../vault-registry/rpc/runtime-api/src/lib.rs | 6 +++ crates/vault-registry/rpc/src/lib.rs | 42 +++++++++++++++++++ parachain/runtime/src/lib.rs | 10 +++++ standalone/runtime/src/lib.rs | 10 +++++ 4 files changed, 68 insertions(+) diff --git a/crates/vault-registry/rpc/runtime-api/src/lib.rs b/crates/vault-registry/rpc/runtime-api/src/lib.rs index c06106c3fd..0d3dd07a16 100644 --- a/crates/vault-registry/rpc/runtime-api/src/lib.rs +++ b/crates/vault-registry/rpc/runtime-api/src/lib.rs @@ -14,6 +14,12 @@ sp_api::decl_runtime_apis! { Collateral: Codec, UnsignedFixedPoint: Codec { + /// Get the vault's collateral (excluding nomination) + fn get_vault_collateral(vault_id: AccountId) -> Result, DispatchError>; + + /// Get the vault's collateral (including nomination) + fn get_vault_total_collateral(vault_id: AccountId) -> Result, DispatchError>; + /// Get the total collateralization of the system fn get_total_collateralization() -> Result; diff --git a/crates/vault-registry/rpc/src/lib.rs b/crates/vault-registry/rpc/src/lib.rs index 87a1d53872..7df63bfac7 100644 --- a/crates/vault-registry/rpc/src/lib.rs +++ b/crates/vault-registry/rpc/src/lib.rs @@ -22,6 +22,20 @@ where Collateral: Codec + MaybeDisplay + MaybeFromStr, UnsignedFixedPoint: Codec + MaybeDisplay + MaybeFromStr, { + #[rpc(name = "vaultRegistry_getVaultCollateral")] + fn get_vault_collateral( + &self, + vault_id: AccountId, + at: Option, + ) -> JsonRpcResult>; + + #[rpc(name = "vaultRegistry_getVaultTotalCollateral")] + fn get_vault_total_collateral( + &self, + vault_id: AccountId, + at: Option, + ) -> JsonRpcResult>; + #[rpc(name = "vaultRegistry_getTotalCollateralization")] fn get_total_collateralization(&self, at: Option) -> JsonRpcResult; @@ -158,6 +172,34 @@ where Collateral: Codec + MaybeDisplay + MaybeFromStr, UnsignedFixedPoint: Codec + MaybeDisplay + MaybeFromStr, { + fn get_vault_collateral( + &self, + vault_id: AccountId, + at: Option<::Hash>, + ) -> JsonRpcResult> { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + + handle_response( + api.get_vault_collateral(&at, vault_id), + "Unable to get the vault's collateral.".into(), + ) + } + + fn get_vault_total_collateral( + &self, + vault_id: AccountId, + at: Option<::Hash>, + ) -> JsonRpcResult> { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + + handle_response( + api.get_vault_total_collateral(&at, vault_id), + "Unable to get the vault's collateral.".into(), + ) + } + fn get_total_collateralization(&self, at: Option<::Hash>) -> JsonRpcResult { let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); diff --git a/parachain/runtime/src/lib.rs b/parachain/runtime/src/lib.rs index c3b4ddd2b0..eae6ed11bd 100644 --- a/parachain/runtime/src/lib.rs +++ b/parachain/runtime/src/lib.rs @@ -1090,6 +1090,16 @@ impl_runtime_apis! { Balance, UnsignedFixedPoint > for Runtime { + fn get_vault_collateral(vault_id: AccountId) -> Result, DispatchError> { + let result = VaultRegistry::compute_collateral(&vault_id)?; + Ok(BalanceWrapper{amount:result}) + } + + fn get_vault_total_collateral(vault_id: AccountId) -> Result, DispatchError> { + let result = VaultRegistry::get_backing_collateral(&vault_id)?; + Ok(BalanceWrapper{amount:result}) + } + fn get_total_collateralization() -> Result { VaultRegistry::get_total_collateralization() } diff --git a/standalone/runtime/src/lib.rs b/standalone/runtime/src/lib.rs index d2c28bab65..5f2bf97338 100644 --- a/standalone/runtime/src/lib.rs +++ b/standalone/runtime/src/lib.rs @@ -664,6 +664,16 @@ impl_runtime_apis! { Balance, UnsignedFixedPoint > for Runtime { + fn get_vault_collateral(vault_id: AccountId) -> Result, DispatchError> { + let result = VaultRegistry::compute_collateral(&vault_id)?; + Ok(BalanceWrapper{amount:result}) + } + + fn get_vault_total_collateral(vault_id: AccountId) -> Result, DispatchError> { + let result = VaultRegistry::get_backing_collateral(&vault_id)?; + Ok(BalanceWrapper{amount:result}) + } + fn get_total_collateralization() -> Result { VaultRegistry::get_total_collateralization() }