axelar-cgp-sui

Module 0x0::account

Account module manages the account data for each user.

use 0x0::balances;
use 0x0::fill;
use 0x1::option;
use 0x2::object;
use 0x2::tx_context;
use 0x2::vec_set;

Struct Account

Account data that is updated every epoch. One Account struct per BalanceManager object.

struct Account has copy, drop, store
Fields
epoch: u64
open_orders: vec_set::VecSet<u128>
taker_volume: u128
maker_volume: u128
active_stake: u64
inactive_stake: u64
created_proposal: bool
voted_proposal: option::Option<object::ID>
unclaimed_rebates: balances::Balances
settled_balances: balances::Balances
owed_balances: balances::Balances

Function open_orders

public fun open_orders(self: &account::Account): vec_set::VecSet<u128>
Implementation
public fun open_orders(self: &Account): VecSet<u128> {
    self.open_orders
}

Function taker_volume

public fun taker_volume(self: &account::Account): u128
Implementation
public fun taker_volume(self: &Account): u128 {
    self.taker_volume
}

Function maker_volume

public fun maker_volume(self: &account::Account): u128
Implementation
public fun maker_volume(self: &Account): u128 {
    self.maker_volume
}

Function total_volume

public fun total_volume(self: &account::Account): u128
Implementation
public fun total_volume(self: &Account): u128 {
    self.taker_volume + self.maker_volume
}

Function active_stake

public fun active_stake(self: &account::Account): u64
Implementation
public fun active_stake(self: &Account): u64 {
    self.active_stake
}

Function inactive_stake

public fun inactive_stake(self: &account::Account): u64
Implementation
public fun inactive_stake(self: &Account): u64 {
    self.inactive_stake
}

Function created_proposal

public fun created_proposal(self: &account::Account): bool
Implementation
public fun created_proposal(self: &Account): bool {
    self.created_proposal
}

Function voted_proposal

public fun voted_proposal(self: &account::Account): option::Option<object::ID>
Implementation
public fun voted_proposal(self: &Account): Option<ID> {
    self.voted_proposal
}

Function empty

public(friend) fun empty(ctx: &tx_context::TxContext): account::Account
Implementation
public(package) fun empty(ctx: &TxContext): Account {
    Account {
        epoch: ctx.epoch(),
        open_orders: vec_set::empty(),
        taker_volume: 0,
        maker_volume: 0,
        active_stake: 0,
        inactive_stake: 0,
        created_proposal: false,
        voted_proposal: option::none(),
        unclaimed_rebates: balances::empty(),
        settled_balances: balances::empty(),
        owed_balances: balances::empty(),
    }
}

Function update

Update the account data for the new epoch. Returns the previous epoch, maker volume, and active stake.

public(friend) fun update(self: &mut account::Account, ctx: &tx_context::TxContext): (u64, u128, u64)
Implementation
public(package) fun update(
    self: &mut Account,
    ctx: &TxContext,
): (u64, u128, u64) {
    if (self.epoch == ctx.epoch()) return (0, 0, 0);

    let prev_epoch = self.epoch;
    let prev_maker_volume = self.maker_volume;
    let prev_active_stake = self.active_stake;

    self.epoch = ctx.epoch();
    self.maker_volume = 0;
    self.taker_volume = 0;
    self.active_stake = self.active_stake + self.inactive_stake;
    self.inactive_stake = 0;
    self.created_proposal = false;
    self.voted_proposal = option::none();

    (prev_epoch, prev_maker_volume, prev_active_stake)
}

Function process_maker_fill

Given a fill, update the account balances and volumes as the maker.

public(friend) fun process_maker_fill(self: &mut account::Account, fill: &fill::Fill)
Implementation
public(package) fun process_maker_fill(self: &mut Account, fill: &Fill) {
    let settled_balances = fill.get_settled_maker_quantities();
    self.settled_balances.add_balances(settled_balances);
    if (!fill.expired()) {
        self.maker_volume = self.maker_volume + (fill.base_quantity() as u128);
    };
    if (fill.expired() || fill.completed()) {
        self.open_orders.remove(&fill.maker_order_id());
    }
}

Function add_taker_volume

public(friend) fun add_taker_volume(self: &mut account::Account, volume: u64)
Implementation
public(package) fun add_taker_volume(self: &mut Account, volume: u64) {
    self.taker_volume = self.taker_volume + (volume as u128);
}

Function set_voted_proposal

Set the voted proposal for the account and return the previous proposal.

public(friend) fun set_voted_proposal(self: &mut account::Account, proposal: option::Option<object::ID>): option::Option<object::ID>
Implementation
public(package) fun set_voted_proposal(
    self: &mut Account,
    proposal: Option<ID>,
): Option<ID> {
    let prev_proposal = self.voted_proposal;
    self.voted_proposal = proposal;

    prev_proposal
}

Function set_created_proposal

public(friend) fun set_created_proposal(self: &mut account::Account, created: bool)
Implementation
public(package) fun set_created_proposal(self: &mut Account, created: bool) {
    self.created_proposal = created;
}

Function add_settled_balances

public(friend) fun add_settled_balances(self: &mut account::Account, balances: balances::Balances)
Implementation
public(package) fun add_settled_balances(
    self: &mut Account,
    balances: Balances,
) {
    self.settled_balances.add_balances(balances);
}

Function add_owed_balances

public(friend) fun add_owed_balances(self: &mut account::Account, balances: balances::Balances)
Implementation
public(package) fun add_owed_balances(self: &mut Account, balances: Balances) {
    self.owed_balances.add_balances(balances);
}

Function settle

Settle the account balances. Returns the settled and owed balances by this account. Vault uses these values to perform any necessary transfers.

public(friend) fun settle(self: &mut account::Account): (balances::Balances, balances::Balances)
Implementation
public(package) fun settle(self: &mut Account): (Balances, Balances) {
    let settled = self.settled_balances.reset();
    let owed = self.owed_balances.reset();

    (settled, owed)
}

Function add_rebates

public(friend) fun add_rebates(self: &mut account::Account, rebates: balances::Balances)
Implementation
public(package) fun add_rebates(self: &mut Account, rebates: Balances) {
    self.unclaimed_rebates.add_balances(rebates);
}

Function claim_rebates

public(friend) fun claim_rebates(self: &mut account::Account): u64
Implementation
public(package) fun claim_rebates(self: &mut Account): u64 {
    let rebate_amount = self.unclaimed_rebates.deep();
    self.settled_balances.add_balances(self.unclaimed_rebates);
    self.unclaimed_rebates.reset();

    rebate_amount
}

Function add_order

public(friend) fun add_order(self: &mut account::Account, order_id: u128)
Implementation
public(package) fun add_order(self: &mut Account, order_id: u128) {
    self.open_orders.insert(order_id);
}

Function remove_order

public(friend) fun remove_order(self: &mut account::Account, order_id: u128)
Implementation
public(package) fun remove_order(self: &mut Account, order_id: u128) {
    self.open_orders.remove(&order_id)
}

Function add_stake

public(friend) fun add_stake(self: &mut account::Account, stake: u64): (u64, u64)
Implementation
public(package) fun add_stake(self: &mut Account, stake: u64): (u64, u64) {
    let stake_before = self.active_stake + self.inactive_stake;
    self.inactive_stake = self.inactive_stake + stake;
    self.owed_balances.add_deep(stake);

    (stake_before, self.active_stake + self.inactive_stake)
}

Function remove_stake

public(friend) fun remove_stake(self: &mut account::Account)
Implementation
public(package) fun remove_stake(self: &mut Account) {
    let stake_before = self.active_stake + self.inactive_stake;
    self.active_stake = 0;
    self.inactive_stake = 0;
    self.voted_proposal = option::none();
    self.settled_balances.add_deep(stake_before);
}