|
| 1 | +// Copyright (c) Mysten Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +/// Account app-data for the DeepBook core wrapper. This module owns the embedded |
| 5 | +/// balance manager slot and exposes only package-scoped helpers for the public |
| 6 | +/// facade to fund, trade, and sweep through that manager. |
| 7 | +module deepbook_core_account::account_data; |
| 8 | + |
| 9 | +use account::{ |
| 10 | + account::{Account, Auth}, |
| 11 | + account_registry::{Self as account_registry, AccountRegistry} |
| 12 | +}; |
| 13 | +use deepbook::{ |
| 14 | + balance_manager::{Self, BalanceManager, DepositCap, TradeCap, TradeProof, WithdrawCap}, |
| 15 | + registry::Registry |
| 16 | +}; |
| 17 | +use std::internal::permit; |
| 18 | +use sui::{accumulator::AccumulatorRoot, clock::Clock, coin::{Self, Coin}, event}; |
| 19 | +use token::deep::DEEP; |
| 20 | + |
| 21 | +/// App witness that namespaces DeepBook core account data on an `Account`. |
| 22 | +public struct DeepbookCoreAccountApp has drop {} |
| 23 | + |
| 24 | +/// Per-account DeepBook core state. The embedded manager ID is stable across all |
| 25 | +/// wrapped calls, so resting orders remain addressable by DeepBook core state. |
| 26 | +public struct DeepbookCoreAccountData has store { |
| 27 | + balance_manager: BalanceManager, |
| 28 | + deposit_cap: DepositCap, |
| 29 | + withdraw_cap: WithdrawCap, |
| 30 | + trade_cap: TradeCap, |
| 31 | +} |
| 32 | + |
| 33 | +/// Emitted once when a canonical account first gets DeepBook core account data. |
| 34 | +public struct DeepbookCoreAccountInitialized has copy, drop { |
| 35 | + account_id: ID, |
| 36 | + account_owner: address, |
| 37 | + wrapper_id: ID, |
| 38 | + balance_manager_id: ID, |
| 39 | +} |
| 40 | + |
| 41 | +/// Return whether this account already has a DeepBook core account slot. |
| 42 | +public fun is_initialized(account: &Account): bool { |
| 43 | + account.has_data<DeepbookCoreAccountApp>() |
| 44 | +} |
| 45 | + |
| 46 | +/// Return this account's embedded balance-manager ID, if the slot exists. |
| 47 | +public fun balance_manager_id(account: &Account): Option<ID> { |
| 48 | + if (!is_initialized(account)) { |
| 49 | + option::none() |
| 50 | + } else { |
| 51 | + option::some(borrow(account).balance_manager.id()) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Return the free balance of `T` currently sitting in the embedded manager. |
| 56 | +public fun balance_manager_balance<T>(account: &Account): u64 { |
| 57 | + if (!is_initialized(account)) { |
| 58 | + 0 |
| 59 | + } else { |
| 60 | + borrow(account).balance_manager.balance<T>() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public(package) fun generate_auth_as_app(account_registry: &AccountRegistry): Auth { |
| 65 | + account_registry::generate_auth_as_app<DeepbookCoreAccountApp>( |
| 66 | + account_registry, |
| 67 | + permit<DeepbookCoreAccountApp>(), |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +public(package) fun borrow(account: &Account): &DeepbookCoreAccountData { |
| 72 | + account.borrow_data<DeepbookCoreAccountApp, DeepbookCoreAccountData>() |
| 73 | +} |
| 74 | + |
| 75 | +public(package) fun ensure( |
| 76 | + account: &mut Account, |
| 77 | + deepbook_registry: &Registry, |
| 78 | + ctx: &mut TxContext, |
| 79 | +) { |
| 80 | + if (!is_initialized(account)) { |
| 81 | + let ( |
| 82 | + balance_manager, |
| 83 | + deposit_cap, |
| 84 | + withdraw_cap, |
| 85 | + trade_cap, |
| 86 | + ) = balance_manager::new_with_custom_owner_caps_v2<DeepbookCoreAccountApp>( |
| 87 | + DeepbookCoreAccountApp {}, |
| 88 | + deepbook_registry, |
| 89 | + account.account_id().to_address(), |
| 90 | + ctx, |
| 91 | + ); |
| 92 | + let account_id = account.account_id(); |
| 93 | + let account_owner = account.owner(); |
| 94 | + let wrapper_id = account.receive_address().to_id(); |
| 95 | + let balance_manager_id = balance_manager.id(); |
| 96 | + account.attach( |
| 97 | + permit<DeepbookCoreAccountApp>(), |
| 98 | + DeepbookCoreAccountData { balance_manager, deposit_cap, withdraw_cap, trade_cap }, |
| 99 | + ); |
| 100 | + event::emit(DeepbookCoreAccountInitialized { |
| 101 | + account_id, |
| 102 | + account_owner, |
| 103 | + wrapper_id, |
| 104 | + balance_manager_id, |
| 105 | + }); |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +public(package) fun borrow_mut(account: &mut Account): &mut DeepbookCoreAccountData { |
| 110 | + account.borrow_data_mut<DeepbookCoreAccountApp, DeepbookCoreAccountData>( |
| 111 | + permit<DeepbookCoreAccountApp>(), |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +public(package) fun balance_manager(d: &DeepbookCoreAccountData): &BalanceManager { |
| 116 | + &d.balance_manager |
| 117 | +} |
| 118 | + |
| 119 | +public(package) fun balance_manager_mut(d: &mut DeepbookCoreAccountData): &mut BalanceManager { |
| 120 | + &mut d.balance_manager |
| 121 | +} |
| 122 | + |
| 123 | +public(package) fun generate_trader_proof( |
| 124 | + d: &mut DeepbookCoreAccountData, |
| 125 | + ctx: &TxContext, |
| 126 | +): TradeProof { |
| 127 | + d.balance_manager.generate_proof_as_trader(&d.trade_cap, ctx) |
| 128 | +} |
| 129 | + |
| 130 | +public(package) fun sweep_all<BaseAsset, QuoteAsset>( |
| 131 | + d: &mut DeepbookCoreAccountData, |
| 132 | + ctx: &mut TxContext, |
| 133 | +): (Coin<BaseAsset>, Coin<QuoteAsset>, Coin<DEEP>) { |
| 134 | + ( |
| 135 | + sweep_from_manager<BaseAsset>(d, ctx), |
| 136 | + sweep_from_manager<QuoteAsset>(d, ctx), |
| 137 | + sweep_from_manager<DEEP>(d, ctx), |
| 138 | + ) |
| 139 | +} |
| 140 | + |
| 141 | +public(package) fun deposit_all<BaseAsset, QuoteAsset>( |
| 142 | + account: &mut Account, |
| 143 | + base: Coin<BaseAsset>, |
| 144 | + quote: Coin<QuoteAsset>, |
| 145 | + deep: Coin<DEEP>, |
| 146 | +) { |
| 147 | + deposit_to_account_if_nonzero<BaseAsset>(account, base); |
| 148 | + deposit_to_account_if_nonzero<QuoteAsset>(account, quote); |
| 149 | + deposit_to_account_if_nonzero<DEEP>(account, deep); |
| 150 | +} |
| 151 | + |
| 152 | +public(package) fun withdraw_all<BaseAsset, QuoteAsset>( |
| 153 | + account: &mut Account, |
| 154 | + root: &AccumulatorRoot, |
| 155 | + clock: &Clock, |
| 156 | + ctx: &mut TxContext, |
| 157 | +): (Coin<BaseAsset>, Coin<QuoteAsset>, Coin<DEEP>) { |
| 158 | + ( |
| 159 | + withdraw_all_of_type<BaseAsset>(account, root, clock, ctx), |
| 160 | + withdraw_all_of_type<QuoteAsset>(account, root, clock, ctx), |
| 161 | + withdraw_all_of_type<DEEP>(account, root, clock, ctx), |
| 162 | + ) |
| 163 | +} |
| 164 | + |
| 165 | +fun withdraw_all_of_type<T>( |
| 166 | + account: &mut Account, |
| 167 | + root: &AccumulatorRoot, |
| 168 | + clock: &Clock, |
| 169 | + ctx: &mut TxContext, |
| 170 | +): Coin<T> { |
| 171 | + let amount = account.balance<T>(root, clock); |
| 172 | + if (amount == 0) { |
| 173 | + coin::zero<T>(ctx) |
| 174 | + } else { |
| 175 | + account.withdraw<T>(amount, ctx) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +public(package) fun deposit_to_manager_if_nonzero<T>( |
| 180 | + d: &mut DeepbookCoreAccountData, |
| 181 | + coin: Coin<T>, |
| 182 | + ctx: &TxContext, |
| 183 | +) { |
| 184 | + if (coin.value() == 0) { |
| 185 | + coin.destroy_zero(); |
| 186 | + } else { |
| 187 | + d.balance_manager.deposit_with_cap<T>(&d.deposit_cap, coin, ctx); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +fun sweep_from_manager<T>(d: &mut DeepbookCoreAccountData, ctx: &mut TxContext): Coin<T> { |
| 192 | + let amount = d.balance_manager.balance<T>(); |
| 193 | + if (amount == 0) { |
| 194 | + coin::zero<T>(ctx) |
| 195 | + } else { |
| 196 | + d.balance_manager.withdraw_with_cap<T>(&d.withdraw_cap, amount, ctx) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +fun deposit_to_account_if_nonzero<T>(account: &mut Account, coin: Coin<T>) { |
| 201 | + if (coin.value() == 0) { |
| 202 | + coin.destroy_zero(); |
| 203 | + } else { |
| 204 | + account.deposit<T>(coin); |
| 205 | + } |
| 206 | +} |
0 commit comments