Skip to main content

Module deepbook::custodian_v2

use std::address;
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::type_name;
use std::vector;
use sui::address;
use sui::bag;
use sui::balance;
use sui::coin;
use sui::config;
use sui::deny_list;
use sui::dynamic_field;
use sui::dynamic_object_field;
use sui::event;
use sui::hex;
use sui::object;
use sui::table;
use sui::transfer;
use sui::tx_context;
use sui::types;
use sui::url;
use sui::vec_set;

Struct Account

public struct AccountT has store
Click to open
Fields
available_balance: sui::balance::Balance<T>
locked_balance: sui::balance::Balance<T>

Struct AccountCap

Capability granting permission to access an entry in Custodian.account_balances. Calling mint_account_cap creates an "admin account cap" such that id == owner with the permission to both access funds and create new AccountCaps. Calling create_child_account_cap creates a "child account cap" such that id != owner that can access funds, but cannot create new AccountCaps.

public struct AccountCap has key, store
Click to open
Fields
id: sui::object::UID
owner: address
The owner of this AccountCap. Note: this is derived from an object ID, not a user address

Struct Custodian

public struct CustodianT has key, store
Click to open
Fields
id: sui::object::UID
account_balances: sui::table::Table<address, deepbook::custodian_v2::Account<T>>
Map from the owner address of AccountCap object to an Account object

Constants

const EAdminAccountCapRequired: u64 = 2;

Function mint_account_cap

Create an admin AccountCap that can be used across all DeepBook pools, and has the permission to create new AccountCaps that can access the same source of funds

public(package) fun mint_account_cap(ctx: &mut sui::tx_context::TxContext): deepbook::custodian_v2::AccountCap
Click to open
Implementation
public(package) fun mint_account_cap(ctx: &mut TxContext): AccountCap {
    let id = object::new(ctx);
    let owner = object::uid_to_address(&id);
    AccountCap { id, owner }
}

Function create_child_account_cap

Create a "child account cap" such that id != owner that can access funds, but cannot create new AccountCaps.

public fun create_child_account_cap(admin_account_cap: &deepbook::custodian_v2::AccountCap, ctx: &mut sui::tx_context::TxContext): deepbook::custodian_v2::AccountCap
Click to open
Implementation
public fun create_child_account_cap(admin_account_cap: &AccountCap, ctx: &mut TxContext): AccountCap {
    // Only the admin account cap can create new account caps
    assert!(object::uid_to_address(&admin_account_cap.id) == admin_account_cap.owner, EAdminAccountCapRequired);
    AccountCap {
        id: object::new(ctx),
        owner: admin_account_cap.owner
    }
}

Function delete_account_cap

Destroy the given account_cap object

public fun delete_account_cap(account_cap: deepbook::custodian_v2::AccountCap)
Click to open
Implementation
public fun delete_account_cap(account_cap: AccountCap) {
    let AccountCap { id, owner: _ } = account_cap;
    object::delete(id)
}

Function account_owner

Return the owner of an AccountCap

public fun account_owner(account_cap: &deepbook::custodian_v2::AccountCap): address
Click to open
Implementation
public fun account_owner(account_cap: &AccountCap): address {
    account_cap.owner
}

Function account_balance

public(package) fun account_balanceAsset(custodian: &deepbook::custodian_v2::Custodian<Asset>, owner: address): (u64, u64)
Click to open
Implementation
public(package) fun account_balance<Asset>(
    custodian: &Custodian<Asset>,
    owner: address
): (u64, u64) {
    // if custodian account is not created yet, directly return (0, 0) rather than abort
    if (!table::contains(&custodian.account_balances, owner)) {
        return (0, 0)
    };
    let account_balances = table::borrow(&custodian.account_balances, owner);
    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(package) fun newT(ctx: &mut sui::tx_context::TxContext): deepbook::custodian_v2::Custodian<T>
Click to open
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(package) fun withdraw_assetAsset(custodian: &mut deepbook::custodian_v2::Custodian<Asset>, quantity: u64, account_cap: &deepbook::custodian_v2::AccountCap, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<Asset>
Click to open
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(package) fun increase_user_available_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, owner: address, quantity: sui::balance::Balance<T>)
Click to open
Implementation
public(package) fun increase_user_available_balance<T>(
    custodian: &mut Custodian<T>,
    owner: address,
    quantity: Balance<T>,
) {
    let account = borrow_mut_account_balance<T>(custodian, owner);
    balance::join(&mut account.available_balance, quantity);
}

Function decrease_user_available_balance

public(package) fun decrease_user_available_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, account_cap: &deepbook::custodian_v2::AccountCap, quantity: u64): sui::balance::Balance<T>
Click to open
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, account_cap.owner);
    balance::split(&mut account.available_balance, quantity)
}

Function increase_user_locked_balance

public(package) fun increase_user_locked_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, account_cap: &deepbook::custodian_v2::AccountCap, quantity: sui::balance::Balance<T>)
Click to open
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, account_cap.owner);
    balance::join(&mut account.locked_balance, quantity);
}

Function decrease_user_locked_balance

public(package) fun decrease_user_locked_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, owner: address, quantity: u64): sui::balance::Balance<T>
Click to open
Implementation
public(package) fun decrease_user_locked_balance<T>(
    custodian: &mut Custodian<T>,
    owner: address,
    quantity: u64,
): Balance<T> {
    let account = borrow_mut_account_balance<T>(custodian, owner);
    split(&mut account.locked_balance, quantity)
}

Function lock_balance

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

public(package) fun lock_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, account_cap: &deepbook::custodian_v2::AccountCap, quantity: u64)
Click to open
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 balance of user

public(package) fun unlock_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, owner: address, quantity: u64)
Click to open
Implementation
public(package) fun unlock_balance<T>(
    custodian: &mut Custodian<T>,
    owner: address,
    quantity: u64,
) {
    let locked_balance = decrease_user_locked_balance<T>(custodian, owner, quantity);
    increase_user_available_balance<T>(custodian, owner, locked_balance)
}

Function account_available_balance

public(package) fun account_available_balanceT(custodian: &deepbook::custodian_v2::Custodian<T>, owner: address): u64
Click to open
Implementation
public(package) fun account_available_balance<T>(
    custodian: &Custodian<T>,
    owner: address,
): u64 {
    balance::value(&table::borrow(&custodian.account_balances, owner).available_balance)
}

Function account_locked_balance

public(package) fun account_locked_balanceT(custodian: &deepbook::custodian_v2::Custodian<T>, owner: address): u64
Click to open
Implementation
public(package) fun account_locked_balance<T>(
    custodian: &Custodian<T>,
    owner: address,
): u64 {
    balance::value(&table::borrow(&custodian.account_balances, owner).locked_balance)
}

Function borrow_mut_account_balance

fun borrow_mut_account_balanceT(custodian: &mut deepbook::custodian_v2::Custodian<T>, owner: address): &mut deepbook::custodian_v2::Account<T>
Click to open
Implementation
fun borrow_mut_account_balance<T>(
    custodian: &mut Custodian<T>,
    owner: address,
): &mut Account<T> {
    if (!table::contains(&custodian.account_balances, owner)) {
        table::add(
            &mut custodian.account_balances,
            owner,
            Account { available_balance: balance::zero(), locked_balance: balance::zero() }
        );
    };
    table::borrow_mut(&mut custodian.account_balances, owner)
}