Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add rpc to get free stakable funds #1132

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/escrow/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ sp_api::decl_runtime_apis! {
/// Get a given user's escrowed balance
fn balance_at(account_id: AccountId, height: Option<BlockNumber>) -> BalanceWrapper<Balance>;

/// amount of kint/intr that use can lock, taking into consideration the Limits.
fn free_stakable(account_id: AccountId) -> BalanceWrapper<Balance>;

/// Get the total voting supply in the system
fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance>;
}
Expand Down
19 changes: 19 additions & 0 deletions crates/escrow/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ where
BlockNumber: Codec,
AccountId: Codec,
{
/// vkint/vintr balance
#[method(name = "escrow_balanceAt")]
fn balance_at(
&self,
Expand All @@ -29,6 +30,10 @@ where
at: Option<BlockHash>,
) -> RpcResult<BalanceWrapper<Balance>>;

/// 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<BlockHash>) -> RpcResult<BalanceWrapper<Balance>>;

#[method(name = "escrow_totalSupply")]
fn total_supply(&self, height: Option<BlockNumber>, at: Option<BlockHash>) -> RpcResult<BalanceWrapper<Balance>>;
}
Expand Down Expand Up @@ -87,6 +92,20 @@ where
)
}

fn free_stakable(
&self,
account_id: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<BalanceWrapper<Balance>> {
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<BlockNumber>,
Expand Down
8 changes: 8 additions & 0 deletions crates/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ impl<T: Config> Pallet<T> {
<UserPointHistory<T>>::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<T> {
let free_balance = T::Currency::free_balance(who);
// prevent blocked accounts from minting
Expand All @@ -501,6 +502,12 @@ impl<T: Config> Pallet<T> {
}
}

pub fn free_stakable(who: &T::AccountId) -> BalanceOf<T> {
let total_stakable = Self::get_free_balance(who);
let used = <Locked<T>>::get(who).amount;
total_stakable.saturating_sub(used)
}

fn deposit_for(who: &T::AccountId, amount: BalanceOf<T>, unlock_height: T::BlockNumber) -> DispatchResult {
let old_locked = Self::locked_balance(who);
let mut new_locked = old_locked.clone();
Expand Down Expand Up @@ -570,6 +577,7 @@ impl<T: Config> Pallet<T> {
Ok(())
}

/// vKINT/vINTR balance at given height
pub fn balance_at(who: &T::AccountId, height: Option<T::BlockNumber>) -> BalanceOf<T> {
let height = height.unwrap_or(Self::current_height());
let last_point = <UserPointHistory<T>>::get(who, <UserPointEpoch<T>>::get(who));
Expand Down
19 changes: 19 additions & 0 deletions crates/escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,32 @@ 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);
<Balances as Currency<AccountId>>::make_free_balance_be(&BOB, 2000);
assert_eq!(Escrow::get_free_balance(&BOB), 2000);
})
}

#[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(|| {
Expand Down
4 changes: 4 additions & 0 deletions parachain/runtime/interlay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,10 @@ impl_runtime_apis! {
BalanceWrapper{amount: Escrow::balance_at(&account_id, height)}
}

fn free_stakable(account_id: AccountId) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::free_stakable(&account_id)}
}

fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::total_supply(height)}
}
Expand Down
4 changes: 4 additions & 0 deletions parachain/runtime/kintsugi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,10 @@ impl_runtime_apis! {
BalanceWrapper{amount: Escrow::balance_at(&account_id, height)}
}

fn free_stakable(account_id: AccountId) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::free_stakable(&account_id)}
}

fn total_supply(height: Option<BlockNumber>) -> BalanceWrapper<Balance> {
BalanceWrapper{amount: Escrow::total_supply(height)}
}
Expand Down