Skip to content

Commit

Permalink
feat: add rpc calls to compute vault collateral with and without nomi…
Browse files Browse the repository at this point in the history
…nation

Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Jul 14, 2021
1 parent f77d5f2 commit 1f4c70f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/vault-registry/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BalanceWrapper<Collateral>, DispatchError>;

/// Get the vault's collateral (including nomination)
fn get_vault_total_collateral(vault_id: AccountId) -> Result<BalanceWrapper<Collateral>, DispatchError>;

/// Get the total collateralization of the system
fn get_total_collateralization() -> Result<UnsignedFixedPoint, DispatchError>;

Expand Down
42 changes: 42 additions & 0 deletions crates/vault-registry/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockHash>,
) -> JsonRpcResult<BalanceWrapper<Collateral>>;

#[rpc(name = "vaultRegistry_getVaultTotalCollateral")]
fn get_vault_total_collateral(
&self,
vault_id: AccountId,
at: Option<BlockHash>,
) -> JsonRpcResult<BalanceWrapper<Collateral>>;

#[rpc(name = "vaultRegistry_getTotalCollateralization")]
fn get_total_collateralization(&self, at: Option<BlockHash>) -> JsonRpcResult<UnsignedFixedPoint>;

Expand Down Expand Up @@ -158,6 +172,34 @@ where
Collateral: Codec + MaybeDisplay + MaybeFromStr,
UnsignedFixedPoint: Codec + MaybeDisplay + MaybeFromStr,
{
fn get_vault_collateral(
&self,
vault_id: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> JsonRpcResult<BalanceWrapper<Collateral>> {
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<<Block as BlockT>::Hash>,
) -> JsonRpcResult<BalanceWrapper<Collateral>> {
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<<Block as BlockT>::Hash>) -> JsonRpcResult<UnsignedFixedPoint> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
Expand Down
10 changes: 10 additions & 0 deletions parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,16 @@ impl_runtime_apis! {
Balance,
UnsignedFixedPoint
> for Runtime {
fn get_vault_collateral(vault_id: AccountId) -> Result<BalanceWrapper<Balance>, DispatchError> {
let result = VaultRegistry::compute_collateral(&vault_id)?;
Ok(BalanceWrapper{amount:result})
}

fn get_vault_total_collateral(vault_id: AccountId) -> Result<BalanceWrapper<Balance>, DispatchError> {
let result = VaultRegistry::get_backing_collateral(&vault_id)?;
Ok(BalanceWrapper{amount:result})
}

fn get_total_collateralization() -> Result<UnsignedFixedPoint, DispatchError> {
VaultRegistry::get_total_collateralization()
}
Expand Down
10 changes: 10 additions & 0 deletions standalone/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,16 @@ impl_runtime_apis! {
Balance,
UnsignedFixedPoint
> for Runtime {
fn get_vault_collateral(vault_id: AccountId) -> Result<BalanceWrapper<Balance>, DispatchError> {
let result = VaultRegistry::compute_collateral(&vault_id)?;
Ok(BalanceWrapper{amount:result})
}

fn get_vault_total_collateral(vault_id: AccountId) -> Result<BalanceWrapper<Balance>, DispatchError> {
let result = VaultRegistry::get_backing_collateral(&vault_id)?;
Ok(BalanceWrapper{amount:result})
}

fn get_total_collateralization() -> Result<UnsignedFixedPoint, DispatchError> {
VaultRegistry::get_total_collateralization()
}
Expand Down

0 comments on commit 1f4c70f

Please sign in to comment.