helpers::stake_pool_helpers

Module 0xc0ded0c0::stake_pool_helpers

module helpers::stake_pool_helpers

This module queries aptos_framework::stake and aptos_framework::staking_config to get information about a stake pool and current staking information.

use 0x1::stake;
use 0x1::staking_config;
use 0x1::timestamp;
use 0xc0ded0c0::math;

Constants

Validator is in state active

const VALIDATOR_STATUS_ACTIVE: u64 = 2;

Validator is in state inactive

const VALIDATOR_STATUS_INACTIVE: u64 = 4;

Validator is in state pending_active

const VALIDATOR_STATUS_PENDING_ACTIVE: u64 = 1;

Validator is in state pending_inactive

const VALIDATOR_STATUS_PENDING_INACTIVE: u64 = 3;

Function get_framework_min_stake

Returns the minimum stake a stake pool must have to become active.

public fun get_framework_min_stake(): u64
Implementation
public fun get_framework_min_stake(): u64 {
    let config = staking_config::get();
    let (min_stake, _max_stake) = staking_config::get_required_stake(
        &config
    );
    min_stake
}

Function get_stake_pool_inactive_balance

Returns the inactive stake of a stake pool.

public fun get_stake_pool_inactive_balance(stake_pool_address: address): u64
Implementation
public fun get_stake_pool_inactive_balance(
    stake_pool_address: address
): u64 {
    let (
        _active_stake,
        inactive_stake,
        _pending_active_stake,
        _pending_inactive_stake
    ) = stake::get_stake(stake_pool_address);
    inactive_stake
}

Function get_stake_pool_total_withdrawable_amount

Returns the total amount which can be withdrawn from a stake pool.

public fun get_stake_pool_total_withdrawable_amount(stake_pool_address: address): u64
Implementation
public fun get_stake_pool_total_withdrawable_amount(
    stake_pool_address: address,
): u64 {
    let (
        _active_stake,
        inactive_stake,
        _pending_active_stake,
        pending_inactive_stake
    ) = stake::get_stake(stake_pool_address);

    let validator_state = stake::get_validator_state(stake_pool_address);

    if (
        validator_state == VALIDATOR_STATUS_INACTIVE &&
            stake::get_remaining_lockup_secs(stake_pool_address) == 0
    ) {
        inactive_stake + pending_inactive_stake
    } else {
        inactive_stake
    }
}

Function get_stake_pool_total

Returns the total amount of stake in the stake pool.

public fun get_stake_pool_total(stake_pool_address: address): u64
Implementation
public fun get_stake_pool_total(stake_pool_address: address): u64 {
    let (
        active_stake,
        inactive_stake,
        pending_active_stake,
        pending_inactive_stake
    ) = stake::get_stake(stake_pool_address);

    active_stake +
        pending_active_stake +
        inactive_stake +
        pending_inactive_stake
}

Function get_stake_pool_maximum_possible_reward_this_epoch

Returns the maximum possible reward that the stake pool can earn in the current epoch.

public fun get_stake_pool_maximum_possible_reward_this_epoch(stake_pool_address: address): u64
Implementation
public fun get_stake_pool_maximum_possible_reward_this_epoch(
    stake_pool_address: address
): u64 {
    let validator_state = stake::get_validator_state(stake_pool_address);
    if (!(
        validator_state == VALIDATOR_STATUS_ACTIVE ||
            validator_state == VALIDATOR_STATUS_PENDING_INACTIVE
    )) {
        return 0
    };

    let config = staking_config::get();
    let (
        reward_rate,
        reward_rate_denominator
    ) = staking_config::get_reward_rate(&config);

    let voting_power = stake::get_current_epoch_voting_power(
        stake_pool_address
    );

    math::mul_div(voting_power, reward_rate, reward_rate_denominator)
}

Function get_stake_pool_non_withdrawing_balance

Return the balance which cannot be withdrawn (without unstaking) from the stake pool.

public fun get_stake_pool_non_withdrawing_balance(stake_pool_address: address): u64
Implementation
public fun get_stake_pool_non_withdrawing_balance(
    stake_pool_address: address
): u64 {
    let (
        active_stake,
        _inactive_stake,
        pending_active_stake,
        _pending_inactive_stake
    ) = stake::get_stake(stake_pool_address);

    active_stake + pending_active_stake
}

Function get_stake_pool_total_after_current_epoch_generation

This function is needed so to prevent last moment stake attack from a delegator to steal rewards. This assumes the validator has 100% proposal success in the current epoch, and thus is a disadvantage to new stakers. However, the discrepancy is small enough to be tolerated for now.

See get_num_new_shares_to_mint() in delegation_state module to see its usage.

public fun get_stake_pool_total_after_current_epoch_generation(stake_pool_address: address): u64
Implementation
public fun get_stake_pool_total_after_current_epoch_generation(
    stake_pool_address: address
): u64 {
    (
        get_stake_pool_maximum_possible_reward_this_epoch(
            stake_pool_address
        ) + get_stake_pool_total(stake_pool_address)
    )
}

Function has_lockup_duration_passed

Returns whether a lockup duration has passed since change_timestamp A change in lockup duration will affect this function. It is intended.

public fun has_lockup_duration_passed(change_timestamp: u64): bool
Implementation
public fun has_lockup_duration_passed(
    change_timestamp: u64,
): bool {
    let config = staking_config::get();
    let total = staking_config::get_recurring_lockup_duration(&config);
    let elapsed = math::safe_sub(timestamp::now_seconds(), change_timestamp);
    return (elapsed >= total)
}

Last updated