helpers::pool
Module 0xc0ded0c0::pool
0xc0ded0c0::pool
module helpers::pool
helpers::pool
A data structure to keep track of ownership in a shared pool of assets.
It handles:
buy_in
andbuy_out
of shares for a specific address.maintaining the cost basis (or
commission_exempt_amount
) of the shares for commission calculations.minting extra shares to represent commission.
use 0x1::error;
use 0x1::option;
use 0xc0ded0c0::iterable_table_custom;
use 0xc0ded0c0::math;
Struct Pool
Pool
struct Pool has store
Constants
When the pool is not empty and a destroy/reset is attempted.
const EPOOL_IS_NOT_EMPTY: u64 = 1;
When a shareholder more shares than they own.
const ENOT_ENOUGH_SHARES: u64 = 0;
Function create_pool
create_pool
Create a new pool.
public fun create_pool(): pool::Pool
Function destroy_empty
destroy_empty
Destroy an empty pool, or destroy a pool with a single shareholder, the owner, remaining.
Abort conditions
If the pool is not empty.
public fun destroy_empty(pool: pool::Pool, owner_address: address, remove_owner_first: bool)
Function is_shareholder
is_shareholder
Returns whether the shareholder_in_question
is in the pool
. Because zero values are removed from the map, this means they must have non zero shares.
public fun is_shareholder(pool: &pool::Pool, shareholder_in_question: address): bool
Function get_commission_exempt_amount
get_commission_exempt_amount
Returns get_commission_exempt_amount
of the pool
.
public fun get_commission_exempt_amount(pool: &pool::Pool): u64
Function get_num_shares
get_num_shares
Returns number of shares for shareholder
in the pool
. Returns 0 if the shareholder does not exist.
public fun get_num_shares(pool: &pool::Pool, shareholder: address): u64
Function share_supply
share_supply
Returns the total number of shares in the pool
.
public fun share_supply(pool: &pool::Pool): u64
Function num_share_holders
num_share_holders
Returns the total number of shareholders in the pool
.
public fun num_share_holders(pool: &pool::Pool): u64
Function buy_in
buy_in
Add num_shares
to shareholder
in the pool
. Increase commission_exempt_amount
by amount
, which the new shares are worth.
public fun buy_in(pool: &mut pool::Pool, shareholder: address, num_shares: u64, amount: u64)
Function buy_out
buy_out
Remove num_shares
from shareholder
in the pool
. Decrease commission_exempt_amount
by amount
which removed shares are worth.
public fun buy_out(pool: &mut pool::Pool, shareholder: address, num_shares: u64, amount: u64)
Function remove_next_share_holder
remove_next_share_holder
Removes the shareholder at the head of the iterable table in the pool
. Returns the address of the removed shareholder and the number of shares removed.
Abort conditions
If the pool is empty, i.e., there are no shareholders.
Usage notes
This function is not safe. Only call it after checking there are shares to remove. This should only be used in a loop which removes all shareholders, together with reset_pool
to set defaults at the end.
public fun remove_next_share_holder(pool: &mut pool::Pool): (address, u64)
Function reset_pool
reset_pool
Reset share_supply
and commission_exempt_amount
to 0
.
Abort conditions
If the
pool
has noshareholders
.
public fun reset_pool(pool: &mut pool::Pool)
Function pay_commission
pay_commission
This dilutes the shares of all shareholders of the pool
according to commission
amount due, by minting new shares for the commission_recipient
. It allows a special (lower) commission rate protocol_commission
for the protocol_delegator_address
, which gets some of the newly minted shares as a rebate. Returns the total new number of shares minted.
public fun pay_commission(pool: &mut pool::Pool, commission_recipient: address, new_total_balance: u64, commission: u64, protocol_commission: u64, commission_normalizer: u64, protocol_delegator_address: address): u64
Function calculate_owner_commission_shares_helper
calculate_owner_commission_shares_helper
Returns the news shares for commission_recipient
, for protocol_delegator
, and the total value of the new shares minted.
fun calculate_owner_commission_shares_helper(share_supply: u64, total_balance: u64, commission_exempt: u64, current_commission: u64, protocol_commission: u64, commission_normalizer: u64, protocol_delegator_existing_shares: u64): (u64, u64, u64)
Function calculate_commission_rebate_for_protocol
calculate_commission_rebate_for_protocol
Returns the rebate shares for the protocol delegator. This is calculated separately because the protocol might have a lower commission rate than the default commission rate charged to other delegators.
fun calculate_commission_rebate_for_protocol(protocol_delegator_existing_shares: u64, new_shares_for_owner: u64, share_supply: u64, current_commission: u64, protocol_commission: u64): u64
Function add_shares
add_shares
Add num_shares
for shareholder
in the pool
and increase the share supply
fun add_shares(pool: &mut pool::Pool, shareholder: address, num_shares: u64)
Function remove_shares
remove_shares
Remove num_shares
from the pool
and decrease the share supply. If a shareholders ends up with 0 shares, remove their entry from the table.
fun remove_shares(pool: &mut pool::Pool, shareholder: address, num_shares: u64)
Last updated