delegation::delegation_service
Module 0xc0ded0c0::delegation_service
0xc0ded0c0::delegation_service
This module provides a delegation service for staking on Aptos.
It:
builds on top of
delegation_state
module,consumes the
owner_capability
of aStakePool
owner and uses it to delegate to the pool,makes the liquid staking protocol just one of many delegators,
requires the owner to join with the minimum stake as specified by the
aptos_framework
, and cannot go below that amount until they self-destruct, and,lets the pool owner can retrieve the
owner_capability
of theirStakePool
after a successful self-destruct.
use 0x1::aptos_coin;
use 0x1::coin;
use 0x1::error;
use 0x1::signer;
use 0x1::stake;
use 0xc0ded0c0::delegation_state;
use 0xc0ded0c0::stake_pool_helpers;
Resource ManagedStakePool
ManagedStakePool
The struct which holds the OwnerCapability
of the StakePool
and stores config parameters.
struct ManagedStakePool has key
Struct ManageCapability
ManageCapability
Holders of ManageCapability
can call permissioned functions. It is returned during initialization.
struct ManageCapability has store
Constants
The minimum stake that can be delegated to a managed stake pool.
const DEFAULT_MINIMUM_DELEGATION_AMOUNT: u64 = 10000000000000;
When trying to remove the stake pool owner's delegation during self-destruct.
const ECANNOT_FORCE_OWNER_TO_WITHDRAW: u64 = 12;
When the commission being set is higher than the max_commission
.
const ECOMMISSION_EXCEEDS_MAX: u64 = 2;
When the delegation amount is below the threshold.
const EDELEGATION_AMOUNT_TOO_SMALL: u64 = 10;
When the managed stake pool already exists at the address during initialization.
const EMANAGED_POOL_ALREADY_EXISTS: u64 = 5;
When the stake of the owner is below the minimum stake, and the pool is not in self-destruct.
const EMINIMUM_VIOLATION: u64 = 7;
When the the pool is not in self-destruct state.
const ENOT_SELF_DESTRUCTING: u64 = 8;
When the managed stake pool does not exist.
const EPOOL_DOES_NOT_EXIST: u64 = 11;
When the commission being charged for the protocol delegator is higher than the commission being charged to outside delegators.
const EPROTOCOL_COMMISSION_EXCEEDS_DEFAULT: u64 = 3;
When the pool is in the self-destruct state.
const ESELF_DESTRUCTING: u64 = 1;
When the maximum number of delegators are already staked.
const ETOO_MANY_DELEGATIONS: u64 = 9;
The maximum number of delegators that can delegate to a managed pool.
const MAX_NUMBER_OF_DELEGATIONS: u64 = 100;
Function assert_is_self_destructing
assert_is_self_destructing
Assert the pool is in self-destruct mode
Abort conditions
If the managed stake pool is not in self-destruct state.
public fun assert_is_self_destructing(managed_pool_address: address)
Function begin_self_destruct
begin_self_destruct
Move the Managed Stake Pool into self-destruct mode. Cannot be reversed.
Restrictions
Can only be called using
ManageCapability
of the managed pool.
Abort conditions
If the pool does not exist.
public fun begin_self_destruct(manage_cap: &delegation_service::ManageCapability)
Function finish_self_destruct
finish_self_destruct
Destroys the ManageStakePool
and returns the StakePool's OwnerCapability
.
Restrictions
Can only be called using
ManageCapability
of the managed pool.
Abort conditions
If the pool is not in self-destruct state.
public fun finish_self_destruct(manage_cap: delegation_service::ManageCapability): stake::OwnerCapability
Function pay_commission_to_owner
pay_commission_to_owner
Compound ManagedStakePool's commission. Returns the amount of commission paid to the owner in APT.
Restrictions
Only available to the holder of
ManageCapability
.
public fun pay_commission_to_owner(manage_cap: &delegation_service::ManageCapability): u64
Function initialize
initialize
Allows a StakePool
owner managed_pool_owner
to start a ManagedStakePool
that can accept delegations from outside delegators. The maximum commission that can be charged is given by max_commission
. The address of the Tortuga protocol delegator is given by protocol_delegator_address
and the commission charged to the protocol delegator is given by protocol_commission
.
Returns the ManageCapability
of the newly created ManagedStakePool
.
Note: Commissions are specified in 6 decimal precision, following the value of COMMISSION_NORMALIZER
in delegation_state
module (i.e. commission of 1000000
= 100%
).
Restrictions
To be associated with Tortuga protocol, this function should be called from
validator_router
module.
Abort conditions
If the
ManagedStakePool
already exists on the given address.protocol_commission
must be lower thanmax_commission
.
public fun initialize(managed_pool_owner: &signer, max_commission: u64, commission_recipient_address: address, protocol_delegator_address: address, protocol_commission: u64): delegation_service::ManageCapability
Function set_min_delegation_amount
set_min_delegation_amount
Set minimum acceptable delegations from outside delegators. Does not affect pool owner and the protocol.
Restrictions
Can only be called by the
pool_owner
.
public fun set_min_delegation_amount(pool_owner: &signer, value: u64)
Function delegate
delegate
Lets a delegator
to delegate amount
APT with a ManagedStakePool
at address managed_pool_address
.
Abort conditions
If the
ManagedStakePool
is in self-destruct state.If the pool already has
MAX_NUMBER_OF_DELEGATIONS
delegations.If the delegation
amount
is less thanmin_delegation_amount
.
public fun delegate(delegator: &signer, managed_pool_address: address, amount: u64)
Function change_commission
change_commission
Change the default and protocol commissions.
Restrictions
Only the
pool_owner
can call this function.
Abort condition
If the
ManagedStakePool
does not exist.If the
new_default_commission
is greater thanmax_commission
.If the
new_protocol_commission
is greater thannew_default_commission
.
public fun change_commission(pool_owner: &signer, new_default_commission: u64, new_protocol_commission: u64)
Function change_commission_recipient
change_commission_recipient
Change the commission recipient of the ManagedStakePool
to new_commission_recipient_address
.
Restrictions
Only the
pool_owner
can call this function.
Abort conditions
If the
ManagedStakePool
does not exist.
public fun change_commission_recipient(pool_owner: &signer, new_commission_recipient_address: address)
Function reserve_shares_for_withdraw
reserve_shares_for_withdraw
delegator
can call this function to withdraw num_shares
from the ManagedStakePool
at address managed_pool_address
. This will move delegator
shares from unreserved_pool
to reserved_pool
and move corresponding StakePool
funds from 'active' to 'pending_inactive' state.. May have to call trigger_payout_dispersal
when the stake unlocks (i.e., changes state from 'pending_inactive' to 'inactive').
Restrictions
Only the
delegator
can call this function for themselves.
Abort conditions
If the withdrawal amount will leave less than
min_delegation_amount
in thedelegator
's account. However, it allows all the funds to be atomically withdrawn.
public fun reserve_shares_for_withdraw(delegator: &signer, managed_pool_address: address, num_shares: u64)
Function trigger_payout_dispersal
trigger_payout_dispersal
Disperses all APT if there is inactive
stake in the StakePool
. Can be called permissionlessly.
Abort conditions
If the
ManagedStakePool
does not exist.
public fun trigger_payout_dispersal(managed_pool_address: address)
Function remove_non_owner_delegator
remove_non_owner_delegator
Reserve all shares for withdrawal for delegator_address
. Can be called by anyone but only when pool is in self-destruct state. This ensures the owner can remove all delegators and retrieve their owner_capability
.
Abort conditions
If the
ManagedStakePool
does not exist.If the
ManagedStakePool
is not inself_destruct
state.If the
delegator_address
ismanaged_pool_address
itself.
public fun remove_non_owner_delegator(managed_pool_address: address, delegator_address: address)
Function set_operator
set_operator
Allow the stake_pool
owner to set new_operator
as the operator of the pool. See 0x1::stake::set_operator_with_cap
for details.
public fun set_operator(pool_owner: &signer, new_operator: address)
Function set_delegated_voter
set_delegated_voter
Allow the stake_pool
owner to set new_voter
as the delegated voter of the pool. See 0x1::stake::set_delegated_voter_with_cap
for details.
public fun set_delegated_voter(pool_owner: &signer, new_voter: address)
Function assert_not_self_destructing
assert_not_self_destructing
Assert the pool is not in self-destruct mode
Abort conditions
If the pool is in self-destruct mode.
fun assert_not_self_destructing(managed_pool_address: address)
Function assert_pool_exists
assert_pool_exists
Assert the ManagedStakePool
has been initialized at the provided address.
Abort conditions
If the
ManagedStakePool
does not exist.
fun assert_pool_exists(managed_pool_address: address)
Function certify_delegation
certify_delegation
Certifies that a delegation with amount
from delegator_address
can be made. Aborts if criteria are not met. The protocol_delegator
and the pool_owner
, and existing delegators are not subject to these conditions.
Abort conditions
If the pool already has
MAX_NUMBER_OF_DELEGATIONS
delegations.If the delegation
amount
is less thanmin_delegation_amount
.
fun certify_delegation(managed_pool_address: address, delegator_address: address, amount: u64)
Function ensure_minimum_delegation_remaining
ensure_minimum_delegation_remaining
Ensures withdrawal does not change the delegator's stake below the minimum, unless a non-stake-pool-owner delegator is withdrawing all of their stake.
Abort conditions
If the non-owner delegator's stake would be non-zero but below the minimum after the withdrawal.
If the owner delegator's stake would be below the
min_stake
required by theaptos_framework
after the withdrawal.
fun ensure_minimum_delegation_remaining(managed_pool_address: address, delegator_address: address, num_shares_to_withdraw: u64)
Last updated