Module deepbook::custodian
- Struct
Account
- Struct
AccountCap
- Struct
Custodian
- Function
mint_account_cap
- Function
account_balance
- Function
new
- Function
withdraw_asset
- Function
increase_user_available_balance
- Function
decrease_user_available_balance
- Function
increase_user_locked_balance
- Function
decrease_user_locked_balance
- Function
lock_balance
- Function
unlock_balance
- Function
account_available_balance
- Function
account_locked_balance
- Function
borrow_mut_account_balance
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 Account<phantom T> has store
Click to open
Fields
- available_balance: sui::balance::Balance<T>
- locked_balance: sui::balance::Balance<T>
Struct AccountCap
public struct AccountCap has key, store
Click to open
Fields
- id: sui::object::UID
Struct Custodian
public struct Custodian<phantom T> has key, store
Click to open
Fields
- id: sui::object::UID
- account_balances: sui::table::Table<sui::object::ID, deepbook::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 sui::tx_context::TxContext): deepbook::custodian::AccountCap
Click to open
Implementation
public fun mint_account_cap(ctx: &mut TxContext): AccountCap {
AccountCap { id: object::new(ctx) }
}
Function account_balance
public(package) fun account_balance<Asset>(custodian: &deepbook::custodian::Custodian<Asset>, user: sui::object::ID): (u64, u64)
Click to open
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(package) fun new<T>(ctx: &mut sui::tx_context::TxContext): deepbook::custodian::Custodian<T>
Click to open
Function withdraw_asset
public(package) fun withdraw_asset<Asset>(custodian: &mut deepbook::custodian::Custodian<Asset>, quantity: u64, account_cap: &deepbook::custodian::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_balance<T>(custodian: &mut deepbook::custodian::Custodian<T>, user: sui::object::ID, quantity: sui::balance::Balance<T>)
Click to open
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(package) fun decrease_user_available_balance<T>(custodian: &mut deepbook::custodian::Custodian<T>, account_cap: &deepbook::custodian::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, object::uid_to_inner(&account_cap.id));
balance::split(&mut account.available_balance, quantity)
}
Function increase_user_locked_balance
public(package) fun increase_user_locked_balance<T>(custodian: &mut deepbook::custodian::Custodian<T>, account_cap: &deepbook::custodian::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, object::uid_to_inner(&account_cap.id));
balance::join(&mut account.locked_balance, quantity);
}
Function decrease_user_locked_balance
public(package) fun decrease_user_locked_balance<T>(custodian: &mut deepbook::custodian::Custodian<T>, user: sui::object::ID, quantity: u64): sui::balance::Balance<T>
Click to open
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(package) fun lock_balance<T>(custodian: &mut deepbook::custodian::Custodian<T>, account_cap: &deepbook::custodian::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 balacne of user
public(package) fun unlock_balance<T>(custodian: &mut deepbook::custodian::Custodian<T>, user: sui::object::ID, quantity: u64)
Click to open
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(package) fun account_available_balance<T>(custodian: &deepbook::custodian::Custodian<T>, user: sui::object::ID): u64
Click to open
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(package) fun account_locked_balance<T>(custodian: &deepbook::custodian::Custodian<T>, user: sui::object::ID): u64
Click to open
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 deepbook::custodian::Custodian<T>, user: sui::object::ID): &mut deepbook::custodian::Account<T>
Click to open
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)
}