Skip to main content

Module 0xdee9::custodian

use 0x2::balance;
use 0x2::coin;
use 0x2::object;
use 0x2::table;
use 0x2::tx_context;

Struct Account

struct Account<T> has store
Fields
available_balance: balance::Balance<T>
locked_balance: balance::Balance<T>

Resource AccountCap

struct AccountCap has store, key
Fields

Resource Custodian

struct Custodian<T> has store, key
Fields
id: object::UID
account_balances: table::Table<object::ID, custodian::Account<T>>
Map from an AccountCap object ID to an Account object

Function mint_account_cap

Create an AccountCap that can be used across all DeepBook pool

public fun mint_account_cap(ctx: &mut tx_context::TxContext): custodian::AccountCap
Implementation
public fun mint_account_cap(ctx: &mut TxContext): AccountCap {
    AccountCap { id: object::new(ctx) }
}

Function account_balance

public(friend) fun account_balance<Asset>(custodian: &custodian::Custodian<Asset>, user: object::ID): (u64, u64)
Implementation
public(package) fun account_balance<Asset>(
    custodian: &Custodian<Asset>,
    user: ID
): (u64, u64) {
    // if custodian account is not created yet, directly return (0, 0) rather than abort
    if (!table::contains(&custodian.account_balances, user)) {
        return (0, 0)
    };
    let account_balances = table::borrow(&custodian.account_balances, user);
    let avail_balance = balance::value(&account_balances.available_balance);
    let locked_balance = balance::value(&account_balances.locked_balance);
    (avail_balance, locked_balance)
}

Function new

public(friend) fun new<T>(ctx: &mut tx_context::TxContext): custodian::Custodian<T>
Implementation
public(package) fun new<T>(ctx: &mut TxContext): Custodian<T> {
    Custodian<T> {
        id: object::new(ctx),
        account_balances: table::new(ctx),
    }
}

Function withdraw_asset

public(friend) fun withdraw_asset<Asset>(custodian: &mut custodian::Custodian<Asset>, quantity: u64, account_cap: &custodian::AccountCap, ctx: &mut tx_context::TxContext): coin::Coin<Asset>
Implementation
public(package) fun withdraw_asset<Asset>(
    custodian: &mut Custodian<Asset>,
    quantity: u64,
    account_cap: &AccountCap,
    ctx: &mut TxContext
): Coin<Asset> {
    coin::from_balance(decrease_user_available_balance<Asset>(custodian, account_cap, quantity), ctx)
}

Function increase_user_available_balance

public(friend) fun increase_user_available_balance<T>(custodian: &mut custodian::Custodian<T>, user: object::ID, quantity: balance::Balance<T>)
Implementation
public(package) fun increase_user_available_balance<T>(
    custodian: &mut Custodian<T>,
    user: ID,
    quantity: Balance<T>,
) {
    let account = borrow_mut_account_balance<T>(custodian, user);
    balance::join(&mut account.available_balance, quantity);
}

Function decrease_user_available_balance

public(friend) fun decrease_user_available_balance<T>(custodian: &mut custodian::Custodian<T>, account_cap: &custodian::AccountCap, quantity: u64): balance::Balance<T>
Implementation
public(package) fun decrease_user_available_balance<T>(
    custodian: &mut Custodian<T>,
    account_cap: &AccountCap,
    quantity: u64,
): Balance<T> {
    let account = borrow_mut_account_balance<T>(custodian, object::uid_to_inner(&account_cap.id));
    balance::split(&mut account.available_balance, quantity)
}

Function increase_user_locked_balance

public(friend) fun increase_user_locked_balance<T>(custodian: &mut custodian::Custodian<T>, account_cap: &custodian::AccountCap, quantity: balance::Balance<T>)
Implementation
public(package) fun increase_user_locked_balance<T>(
    custodian: &mut Custodian<T>,
    account_cap: &AccountCap,
    quantity: Balance<T>,
) {
    let account = borrow_mut_account_balance<T>(custodian, object::uid_to_inner(&account_cap.id));
    balance::join(&mut account.locked_balance, quantity);
}

Function decrease_user_locked_balance

public(friend) fun decrease_user_locked_balance<T>(custodian: &mut custodian::Custodian<T>, user: object::ID, quantity: u64): balance::Balance<T>
Implementation
public(package) fun decrease_user_locked_balance<T>(
    custodian: &mut Custodian<T>,
    user: ID,
    quantity: u64,
): Balance<T> {
    let account = borrow_mut_account_balance<T>(custodian, user);
    split(&mut account.locked_balance, quantity)
}

Function lock_balance

Move quantity from the unlocked balance of user to the locked balance of user

public(friend) fun lock_balance<T>(custodian: &mut custodian::Custodian<T>, account_cap: &custodian::AccountCap, quantity: u64)
Implementation
public(package) fun lock_balance<T>(
    custodian: &mut Custodian<T>,
    account_cap: &AccountCap,
    quantity: u64,
) {
    let to_lock = decrease_user_available_balance(custodian, account_cap, quantity);
    increase_user_locked_balance(custodian, account_cap, to_lock);
}

Function unlock_balance

Move quantity from the locked balance of user to the unlocked balacne of user

public(friend) fun unlock_balance<T>(custodian: &mut custodian::Custodian<T>, user: object::ID, quantity: u64)
Implementation
public(package) fun unlock_balance<T>(
    custodian: &mut Custodian<T>,
    user: ID,
    quantity: u64,
) {
    let locked_balance = decrease_user_locked_balance<T>(custodian, user, quantity);
    increase_user_available_balance<T>(custodian, user, locked_balance)
}

Function account_available_balance

public(friend) fun account_available_balance<T>(custodian: &custodian::Custodian<T>, user: object::ID): u64
Implementation
public(package) fun account_available_balance<T>(
    custodian: &Custodian<T>,
    user: ID,
): u64 {
    balance::value(&table::borrow(&custodian.account_balances, user).available_balance)
}

Function account_locked_balance

public(friend) fun account_locked_balance<T>(custodian: &custodian::Custodian<T>, user: object::ID): u64
Implementation
public(package) fun account_locked_balance<T>(
    custodian: &Custodian<T>,
    user: ID,
): u64 {
    balance::value(&table::borrow(&custodian.account_balances, user).locked_balance)
}

Function borrow_mut_account_balance

fun borrow_mut_account_balance<T>(custodian: &mut custodian::Custodian<T>, user: object::ID): &mut custodian::Account<T>
Implementation
fun borrow_mut_account_balance<T>(
    custodian: &mut Custodian<T>,
    user: ID,
): &mut Account<T> {
    if (!table::contains(&custodian.account_balances, user)) {
        table::add(
            &mut custodian.account_balances,
            user,
            Account { available_balance: balance::zero(), locked_balance: balance::zero() }
        );
    };
    table::borrow_mut(&mut custodian.account_balances, user)
}