From 6c3595b484f6752330a59fde0060c72b7663e3ba Mon Sep 17 00:00:00 2001 From: Sander Bosma Date: Tue, 18 Jul 2023 15:48:06 +0200 Subject: [PATCH] chore: add rpc to get free stakable funds --- crates/escrow/rpc/runtime-api/src/lib.rs | 3 +++ crates/escrow/rpc/src/lib.rs | 19 +++++++++++++++++++ crates/escrow/src/lib.rs | 8 ++++++++ crates/escrow/src/tests.rs | 19 +++++++++++++++++++ parachain/runtime/interlay/src/lib.rs | 4 ++++ parachain/runtime/kintsugi/src/lib.rs | 4 ++++ 6 files changed, 57 insertions(+) diff --git a/crates/escrow/rpc/runtime-api/src/lib.rs b/crates/escrow/rpc/runtime-api/src/lib.rs index 2da17b8bfe..3a3a1a8317 100644 --- a/crates/escrow/rpc/runtime-api/src/lib.rs +++ b/crates/escrow/rpc/runtime-api/src/lib.rs @@ -14,6 +14,9 @@ sp_api::decl_runtime_apis! { /// Get a given user's escrowed balance fn balance_at(account_id: AccountId, height: Option) -> BalanceWrapper; + /// amount of kint/intr that use can lock, taking into consideration the Limits. + fn free_stakable(account_id: AccountId) -> BalanceWrapper; + /// Get the total voting supply in the system fn total_supply(height: Option) -> BalanceWrapper; } diff --git a/crates/escrow/rpc/src/lib.rs b/crates/escrow/rpc/src/lib.rs index 8b4d34633f..301455fa03 100644 --- a/crates/escrow/rpc/src/lib.rs +++ b/crates/escrow/rpc/src/lib.rs @@ -21,6 +21,7 @@ where BlockNumber: Codec, AccountId: Codec, { + /// vkint/vintr balance #[method(name = "escrow_balanceAt")] fn balance_at( &self, @@ -29,6 +30,10 @@ where at: Option, ) -> RpcResult>; + /// amount of kint/intr that use can lock, taking into consideration the Limits. + #[method(name = "escrow_freeStakable")] + fn free_stakable(&self, account_id: AccountId, at: Option) -> RpcResult>; + #[method(name = "escrow_totalSupply")] fn total_supply(&self, height: Option, at: Option) -> RpcResult>; } @@ -87,6 +92,20 @@ where ) } + fn free_stakable( + &self, + account_id: AccountId, + at: Option<::Hash>, + ) -> RpcResult> { + let api = self.client.runtime_api(); + let at = at.unwrap_or_else(|| self.client.info().best_hash); + + handle_response( + api.free_stakable(at, account_id), + "Unable to obtain the escrow balance".into(), + ) + } + fn total_supply( &self, height: Option, diff --git a/crates/escrow/src/lib.rs b/crates/escrow/src/lib.rs index 4b9f9015dc..4351c69290 100644 --- a/crates/escrow/src/lib.rs +++ b/crates/escrow/src/lib.rs @@ -484,6 +484,7 @@ impl Pallet { >::insert(who, user_epoch, u_new); } + /// amount of kint/intr that use can lock, taking into consideration the Limits. fn get_free_balance(who: &T::AccountId) -> BalanceOf { let free_balance = T::Currency::free_balance(who); // prevent blocked accounts from minting @@ -501,6 +502,12 @@ impl Pallet { } } + pub fn free_stakable(who: &T::AccountId) -> BalanceOf { + let total_stakable = Self::get_free_balance(who); + let used = >::get(who).amount; + total_stakable.saturating_sub(used) + } + fn deposit_for(who: &T::AccountId, amount: BalanceOf, unlock_height: T::BlockNumber) -> DispatchResult { let old_locked = Self::locked_balance(who); let mut new_locked = old_locked.clone(); @@ -570,6 +577,7 @@ impl Pallet { Ok(()) } + /// vKINT/vINTR balance at given height pub fn balance_at(who: &T::AccountId, height: Option) -> BalanceOf { let height = height.unwrap_or(Self::current_height()); let last_point = >::get(who, >::get(who)); diff --git a/crates/escrow/src/tests.rs b/crates/escrow/src/tests.rs index b6709cb2b8..d909d5ff00 100644 --- a/crates/escrow/src/tests.rs +++ b/crates/escrow/src/tests.rs @@ -193,6 +193,8 @@ fn should_get_free_balance() { run_test(|| { limit_account(ALICE, 1000, 0, 100); assert_eq!(Escrow::get_free_balance(&ALICE), 0); + System::set_block_number(10); + assert_eq!(Escrow::get_free_balance(&ALICE), 100); System::set_block_number(100); assert_eq!(Escrow::get_free_balance(&ALICE), 1000); >::make_free_balance_be(&BOB, 2000); @@ -200,6 +202,23 @@ fn should_get_free_balance() { }) } +#[test] +fn test_free_stakable() { + run_test(|| { + limit_account(ALICE, 100_000, 0, 100); + System::set_block_number(10); + assert_eq!(Escrow::get_free_balance(&ALICE), 10_000); + + assert_ok!(Escrow::create_lock(RuntimeOrigin::signed(ALICE), 7_000, 100),); + let available = Escrow::free_stakable(&ALICE); + assert_err!( + Escrow::increase_amount(RuntimeOrigin::signed(ALICE), available + 1), + TestError::InsufficientFunds + ); + assert_ok!(Escrow::increase_amount(RuntimeOrigin::signed(ALICE), available)); + }) +} + #[test] fn should_not_allow_amount_smaller_than_max_period() { run_test(|| { diff --git a/parachain/runtime/interlay/src/lib.rs b/parachain/runtime/interlay/src/lib.rs index 28d68807e8..1702616773 100644 --- a/parachain/runtime/interlay/src/lib.rs +++ b/parachain/runtime/interlay/src/lib.rs @@ -1759,6 +1759,10 @@ impl_runtime_apis! { BalanceWrapper{amount: Escrow::balance_at(&account_id, height)} } + fn free_stakable(account_id: AccountId) -> BalanceWrapper { + BalanceWrapper{amount: Escrow::free_stakable(&account_id)} + } + fn total_supply(height: Option) -> BalanceWrapper { BalanceWrapper{amount: Escrow::total_supply(height)} } diff --git a/parachain/runtime/kintsugi/src/lib.rs b/parachain/runtime/kintsugi/src/lib.rs index a12ef73c30..3251487c2d 100644 --- a/parachain/runtime/kintsugi/src/lib.rs +++ b/parachain/runtime/kintsugi/src/lib.rs @@ -1623,6 +1623,10 @@ impl_runtime_apis! { BalanceWrapper{amount: Escrow::balance_at(&account_id, height)} } + fn free_stakable(account_id: AccountId) -> BalanceWrapper { + BalanceWrapper{amount: Escrow::free_stakable(&account_id)} + } + fn total_supply(height: Option) -> BalanceWrapper { BalanceWrapper{amount: Escrow::total_supply(height)} }