0x0::vault
The vault holds all of the assets for this pool. At the end of all transaction processing, the vault is used to settle the balances for the user.
Vault
FlashLoan
FlashLoanBorrowed
balances
empty
settle_balance_manager
withdraw_deep_to_burn
borrow_flashloan_base
borrow_flashloan_quote
return_flashloan_base
return_flashloan_quote
use 0x0::balance_manager;
use 0x0::balances;
use 0x0::deep;
use 0x1::type_name;
use 0x2::balance;
use 0x2::coin;
use 0x2::event;
use 0x2::object;
use 0x2::tx_context;
Vault
struct Vault<BaseAsset, QuoteAsset> has store
base_balance: balance::Balance<BaseAsset>
quote_balance: balance::Balance<QuoteAsset>
deep_balance: balance::Balance<deep::DEEP>
FlashLoan
struct FlashLoan
pool_id: object::ID
borrow_quantity: u64
type_name: type_name::TypeName
FlashLoanBorrowed
struct FlashLoanBorrowed has copy, drop
pool_id: object::ID
borrow_quantity: u64
type_name: type_name::TypeName
const EIncorrectLoanPool: u64 = 4;
const EIncorrectQuantityReturned: u64 = 6;
const EIncorrectTypeReturned: u64 = 5;
const ENotEnoughBaseForLoan: u64 = 1;
const ENotEnoughQuoteForLoan: u64 = 2;
const EinvalidLoanQuantity: u64 = 3;
balances
public(friend) fun balances<BaseAsset, QuoteAsset>(self: &vault::Vault<BaseAsset, QuoteAsset>): (u64, u64, u64)
public(package) fun balances<BaseAsset, QuoteAsset>(
self: &Vault<BaseAsset, QuoteAsset>,
): (u64, u64, u64) {
(
self.base_balance.value(),
self.quote_balance.value(),
self.deep_balance.value(),
)
}
empty
public(friend) fun empty<BaseAsset, QuoteAsset>(): vault::Vault<BaseAsset, QuoteAsset>
public(package) fun empty<BaseAsset, QuoteAsset>(): Vault<
BaseAsset,
QuoteAsset,
> {
Vault {
base_balance: balance::zero(),
quote_balance: balance::zero(),
deep_balance: balance::zero(),
}
}
settle_balance_manager
Transfer any settled amounts for the balance_manager
.
public(friend) fun settle_balance_manager<BaseAsset, QuoteAsset>(self: &mut vault::Vault<BaseAsset, QuoteAsset>, balances_out: balances::Balances, balances_in: balances::Balances, balance_manager: &mut balance_manager::BalanceManager, trade_proof: &balance_manager::TradeProof)
public(package) fun settle_balance_manager<BaseAsset, QuoteAsset>(
self: &mut Vault<BaseAsset, QuoteAsset>,
balances_out: Balances,
balances_in: Balances,
balance_manager: &mut BalanceManager,
trade_proof: &TradeProof,
) {
balance_manager.validate_proof(trade_proof);
if (balances_out.base() > balances_in.base()) {
let balance = self
.base_balance
.split(balances_out.base() - balances_in.base());
balance_manager.deposit_with_proof(trade_proof, balance);
};
if (balances_out.quote() > balances_in.quote()) {
let balance = self
.quote_balance
.split(balances_out.quote() - balances_in.quote());
balance_manager.deposit_with_proof(trade_proof, balance);
};
if (balances_out.deep() > balances_in.deep()) {
let balance = self
.deep_balance
.split(balances_out.deep() - balances_in.deep());
balance_manager.deposit_with_proof(trade_proof, balance);
};
if (balances_in.base() > balances_out.base()) {
let balance = balance_manager.withdraw_with_proof(
trade_proof,
balances_in.base() - balances_out.base(),
false,
);
self.base_balance.join(balance);
};
if (balances_in.quote() > balances_out.quote()) {
let balance = balance_manager.withdraw_with_proof(
trade_proof,
balances_in.quote() - balances_out.quote(),
false,
);
self.quote_balance.join(balance);
};
if (balances_in.deep() > balances_out.deep()) {
let balance = balance_manager.withdraw_with_proof(
trade_proof,
balances_in.deep() - balances_out.deep(),
false,
);
self.deep_balance.join(balance);
};
}
withdraw_deep_to_burn
public(friend) fun withdraw_deep_to_burn<BaseAsset, QuoteAsset>(self: &mut vault::Vault<BaseAsset, QuoteAsset>, amount_to_burn: u64): balance::Balance<deep::DEEP>
public(package) fun withdraw_deep_to_burn<BaseAsset, QuoteAsset>(
self: &mut Vault<BaseAsset, QuoteAsset>,
amount_to_burn: u64,
): Balance<DEEP> {
self.deep_balance.split(amount_to_burn)
}
borrow_flashloan_base
public(friend) fun borrow_flashloan_base<BaseAsset, QuoteAsset>(self: &mut vault::Vault<BaseAsset, QuoteAsset>, pool_id: object::ID, borrow_quantity: u64, ctx: &mut tx_context::TxContext): (coin::Coin<BaseAsset>, vault::FlashLoan)
public(package) fun borrow_flashloan_base<BaseAsset, QuoteAsset>(
self: &mut Vault<BaseAsset, QuoteAsset>,
pool_id: ID,
borrow_quantity: u64,
ctx: &mut TxContext,
): (Coin<BaseAsset>, FlashLoan) {
assert!(borrow_quantity > 0, EinvalidLoanQuantity);
assert!(
self.base_balance.value() >= borrow_quantity,
ENotEnoughBaseForLoan,
);
let borrow_type_name = type_name::get<BaseAsset>();
let borrow: Coin<BaseAsset> = self
.base_balance
.split(borrow_quantity)
.into_coin(ctx);
let flash_loan = FlashLoan {
pool_id,
borrow_quantity,
type_name: borrow_type_name,
};
event::emit(FlashLoanBorrowed {
pool_id,
borrow_quantity,
type_name: borrow_type_name,
});
(borrow, flash_loan)
}
borrow_flashloan_quote
public(friend) fun borrow_flashloan_quote<BaseAsset, QuoteAsset>(self: &mut vault::Vault<BaseAsset, QuoteAsset>, pool_id: object::ID, borrow_quantity: u64, ctx: &mut tx_context::TxContext): (coin::Coin<QuoteAsset>, vault::FlashLoan)
public(package) fun borrow_flashloan_quote<BaseAsset, QuoteAsset>(
self: &mut Vault<BaseAsset, QuoteAsset>,
pool_id: ID,
borrow_quantity: u64,
ctx: &mut TxContext,
): (Coin<QuoteAsset>, FlashLoan) {
assert!(borrow_quantity > 0, EinvalidLoanQuantity);
assert!(
self.quote_balance.value() >= borrow_quantity,
ENotEnoughQuoteForLoan,
);
let borrow_type_name = type_name::get<QuoteAsset>();
let borrow: Coin<QuoteAsset> = self
.quote_balance
.split(borrow_quantity)
.into_coin(ctx);
let flash_loan = FlashLoan {
pool_id,
borrow_quantity,
type_name: borrow_type_name,
};
event::emit(FlashLoanBorrowed {
pool_id,
borrow_quantity,
type_name: borrow_type_name,
});
(borrow, flash_loan)
}
return_flashloan_base
public(friend) fun return_flashloan_base<BaseAsset, QuoteAsset>(self: &mut vault::Vault<BaseAsset, QuoteAsset>, pool_id: object::ID, coin: coin::Coin<BaseAsset>, flash_loan: vault::FlashLoan)
public(package) fun return_flashloan_base<BaseAsset, QuoteAsset>(
self: &mut Vault<BaseAsset, QuoteAsset>,
pool_id: ID,
coin: Coin<BaseAsset>,
flash_loan: FlashLoan,
) {
assert!(pool_id == flash_loan.pool_id, EIncorrectLoanPool);
assert!(
type_name::get<BaseAsset>() == flash_loan.type_name,
EIncorrectTypeReturned,
);
assert!(
coin.value() == flash_loan.borrow_quantity,
EIncorrectQuantityReturned,
);
self.base_balance.join(coin.into_balance<BaseAsset>());
let FlashLoan {
pool_id: _,
borrow_quantity: _,
type_name: _,
} = flash_loan;
}
return_flashloan_quote
public(friend) fun return_flashloan_quote<BaseAsset, QuoteAsset>(self: &mut vault::Vault<BaseAsset, QuoteAsset>, pool_id: object::ID, coin: coin::Coin<QuoteAsset>, flash_loan: vault::FlashLoan)
public(package) fun return_flashloan_quote<BaseAsset, QuoteAsset>(
self: &mut Vault<BaseAsset, QuoteAsset>,
pool_id: ID,
coin: Coin<QuoteAsset>,
flash_loan: FlashLoan,
) {
assert!(pool_id == flash_loan.pool_id, EIncorrectLoanPool);
assert!(
type_name::get<QuoteAsset>() == flash_loan.type_name,
EIncorrectTypeReturned,
);
assert!(
coin.value() == flash_loan.borrow_quantity,
EIncorrectQuantityReturned,
);
self.quote_balance.join(coin.into_balance<QuoteAsset>());
let FlashLoan {
pool_id: _,
borrow_quantity: _,
type_name: _,
} = flash_loan;
}