From f81748c9bb3591c694c95448fda4e20ec7a1afc5 Mon Sep 17 00:00:00 2001 From: Aslan Tashtanov Date: Fri, 26 Apr 2024 10:23:12 -0400 Subject: [PATCH 1/3] admin cap & init --- deepbook/sources/deepbook.move | 23 ++++++- deepbook/sources/pool/pool.move | 3 +- .../sources/state/deep_reference_price.move | 9 ++- deepbook/sources/state/state.move | 65 ++++++++++--------- 4 files changed, 66 insertions(+), 34 deletions(-) diff --git a/deepbook/sources/deepbook.move b/deepbook/sources/deepbook.move index 0fbaab00..ae32794c 100644 --- a/deepbook/sources/deepbook.move +++ b/deepbook/sources/deepbook.move @@ -13,11 +13,30 @@ module deepbook::deepbook { }; use deepbook::{ - state::State, + state::{Self, State}, pool::{Order, Pool, DEEP}, account::{Account, TradeProof}, }; + // INIT + + /// DeepBookAdminCap is used to call admin functions. + public struct DeepBookAdminCap has key, store { + id: UID, + } + + /// The one-time-witness used to claim Publisher object. + public struct DEEPBOOK has drop {} + + fun init(otw: DEEPBOOK, ctx: &mut TxContext) { + sui::package::claim_and_keep(otw, ctx); + state::create_and_share(ctx); + let cap = DeepBookAdminCap { + id: object::new(ctx), + }; + transfer::transfer(cap, ctx.sender()); + } + // POOL MANAGEMENT /// Public facing function to create a pool. @@ -36,6 +55,7 @@ module deepbook::deepbook { /// Public facing function to set a pool as stable. public fun set_pool_as_stable( + _cap: &DeepBookAdminCap, state: &mut State, pool: &Pool, stable: bool, @@ -46,6 +66,7 @@ module deepbook::deepbook { /// Public facing function to add a reference pool. public fun add_reference_pool( + _cap: &DeepBookAdminCap, state: &mut State, reference_pool: &Pool, ) { diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index de1473d8..654c55c2 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -437,7 +437,8 @@ module deepbook::pool { self.get_user_mut(user, ctx).increase_stake(amount) } - /// Removes a user's stake + /// Removes a user's stake. + /// Returns the total amount staked before this epoch and total amount staked during this epoch. public(package) fun remove_user_stake( self: &mut Pool, user: address, diff --git a/deepbook/sources/state/deep_reference_price.move b/deepbook/sources/state/deep_reference_price.move index 6edfd839..d6431794 100644 --- a/deepbook/sources/state/deep_reference_price.move +++ b/deepbook/sources/state/deep_reference_price.move @@ -7,7 +7,7 @@ module deepbook::deep_reference_price { ascii::String, }; - use sui::vec_map::VecMap; + use sui::vec_map::{Self, VecMap}; use deepbook::pool::{Pool, DEEP}; // TODO: DEEP token @@ -20,11 +20,16 @@ module deepbook::deep_reference_price { reference_pools: VecMap, } + public(package) fun new(): DeepReferencePools { + DeepReferencePools { + reference_pools: vec_map::empty(), + } + } + /// Add a reference pool. Can be performed by the DeepbookAdminCap owner. public(package) fun add_reference_pool( deep_reference_price: &mut DeepReferencePools, pool: &Pool, - // cap: &DeepbookAdminCap TODO ) { let (base, quote) = pool.get_base_quote_types(); let deep_type = type_name::get().into_string(); diff --git a/deepbook/sources/state/state.move b/deepbook/sources/state/state.move index fa8dba19..9b89c9ad 100644 --- a/deepbook/sources/state/state.move +++ b/deepbook/sources/state/state.move @@ -5,8 +5,8 @@ module deepbook::state { // Consider renaming this module use std::ascii::String; use sui::{ - balance::Balance, - table::Table, + balance::{Self, Balance}, + table::{Self, Table}, sui::SUI, coin::Coin, clock::Clock, @@ -16,20 +16,18 @@ module deepbook::state { // Consider renaming this module pool::{Pool, DEEP, Self}, pool_state, pool_metadata::{Self, PoolMetadata}, - deep_reference_price::DeepReferencePools, + deep_reference_price::{Self, DeepReferencePools}, }; const EPoolDoesNotExist: u64 = 1; const EPoolAlreadyExists: u64 = 2; const ENotEnoughStake: u64 = 3; - const STAKE_REQUIRED_TO_PARTICIPATE: u64 = 1000; // TODO - // const STABLE_TAKER_FEE: u64 = 100; - // const STABLE_MAKER_FEE: u64 = 50; - const VOLATILE_TAKER_FEE: u64 = 1000; - const VOLATILE_MAKER_FEE: u64 = 500; + const STAKE_REQUIRED_TO_PARTICIPATE: u64 = 100; + const DEFAULT_TAKER_FEE: u64 = 1000; + const DEFAULT_MAKER_FEE: u64 = 500; - public struct State has key, store { + public struct State has key { id: UID, // TODO: upgrade-ability plan? do we need? pools: Table, @@ -41,6 +39,17 @@ module deepbook::state { // Consider renaming this module vault: Balance, } + /// Create a new State and share it. Called once during init. + public(package) fun create_and_share(ctx: &mut TxContext) { + let state = State { + id: object::new(ctx), + pools: table::new(ctx), + deep_reference_pools: deep_reference_price::new(), + vault: balance::zero(), + }; + transfer::share_object(state); + } + /// Create a new pool. Calls create_pool inside Pool then registers it in /// the state. `pool_key` is a sorted, concatenated string of the two asset /// names. If SUI/USDC exists, you can't create USDC/SUI. @@ -53,8 +62,8 @@ module deepbook::state { // Consider renaming this module ctx: &mut TxContext, ) { let pool = pool::create_pool( - VOLATILE_TAKER_FEE, - VOLATILE_MAKER_FEE, + DEFAULT_TAKER_FEE, + DEFAULT_MAKER_FEE, tick_size, lot_size, min_size, @@ -72,7 +81,6 @@ module deepbook::state { // Consider renaming this module /// Set the as stable or volatile. This changes the fee structure of the pool. /// New proposals will be asserted against the new fee structure. public(package) fun set_pool_as_stable( - // cap: DeepbookAdminCap, TODO self: &mut State, pool: &Pool, stable: bool, @@ -108,13 +116,10 @@ module deepbook::state { // Consider renaming this module public(package) fun add_reference_pool( self: &mut State, reference_pool: &Pool, - // cap: &DeepbookAdminCap, TODO ) { self.deep_reference_pools.add_reference_pool(reference_pool); } - // STAKE - /// Stake DEEP in the pool. This will increase the user's voting power next epoch /// Individual user stakes are stored inside of the pool. /// A user's stake is tracked as stake_amount, staked before current epoch, their "active" amount, @@ -128,10 +133,8 @@ module deepbook::state { // Consider renaming this module ) { let user = ctx.sender(); let total_user_stake = pool.increase_user_stake(user, amount.value(), ctx); - self.get_pool_metadata_mut(pool, ctx) .add_voting_power(total_user_stake, amount.value()); - self.vault.join(amount.into_balance()); } @@ -145,18 +148,13 @@ module deepbook::state { // Consider renaming this module ctx: &mut TxContext ): Coin { let user = ctx.sender(); - - // total amount staked before this epoch, total amount staked during this epoch let (user_old_stake, user_new_stake) = pool.remove_user_stake(user, ctx); - self.get_pool_metadata_mut(pool, ctx) .remove_voting_power(user_old_stake, user_new_stake); self.vault.split(user_old_stake + user_new_stake).into_coin(ctx) } - // GOVERNANCE - /// Submit a proposal to change the fee structure of a pool. /// The user submitting this proposal must have vested stake in the pool. public(package) fun submit_proposal( @@ -167,9 +165,7 @@ module deepbook::state { // Consider renaming this module stake_required: u64, ctx: &TxContext, ) { - let user = ctx.sender(); - let (user_stake, _) = pool.get_user_stake(user, ctx); - assert!(user_stake >= STAKE_REQUIRED_TO_PARTICIPATE, ENotEnoughStake); + let (user, _) = assert_participant(pool, ctx); let pool_metadata = self.get_pool_metadata_mut(pool, ctx); pool_metadata.add_proposal(user, maker_fee, taker_fee, stake_required); @@ -184,9 +180,7 @@ module deepbook::state { // Consider renaming this module proposal_id: u64, ctx: &TxContext, ) { - let user = ctx.sender(); - let (user_stake, _) = pool.get_user_stake(user, ctx); - assert!(user_stake >= STAKE_REQUIRED_TO_PARTICIPATE, ENotEnoughStake); + let (user, user_stake) = assert_participant(pool, ctx); let pool_metadata = self.get_pool_metadata_mut(pool, ctx); let winning_proposal = pool_metadata.vote(proposal_id, user, user_stake); @@ -205,8 +199,7 @@ module deepbook::state { // Consider renaming this module pool.set_next_epoch(pool_state); } - // HELPERS - + /// Check whether pool exists, refresh and return its metadata. fun get_pool_metadata_mut( self: &mut State, pool: &Pool, @@ -219,4 +212,16 @@ module deepbook::state { // Consider renaming this module pool_metadata.refresh(ctx); pool_metadata } + + /// Check whether user can submit and vote on proposals. + fun assert_participant( + pool: &mut Pool, + ctx: &TxContext + ): (address, u64) { + let user = ctx.sender(); + let (user_stake, _) = pool.get_user_stake(user, ctx); + assert!(user_stake >= STAKE_REQUIRED_TO_PARTICIPATE, ENotEnoughStake); + + (user, user_stake) + } } From de0b31e19dc7269286dfae41782d463d87f776fb Mon Sep 17 00:00:00 2001 From: Aslan Tashtanov Date: Fri, 26 Apr 2024 10:25:31 -0400 Subject: [PATCH 2/3] comment --- deepbook/sources/pool/pool.move | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index 654c55c2..43549878 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -438,7 +438,7 @@ module deepbook::pool { } /// Removes a user's stake. - /// Returns the total amount staked before this epoch and total amount staked during this epoch. + /// Returns the total amount staked before this epoch and the total amount staked during this epoch. public(package) fun remove_user_stake( self: &mut Pool, user: address, From 74932ebe513bb9d673a73134d0ef31e02c3c065b Mon Sep 17 00:00:00 2001 From: Aslan Tashtanov Date: Fri, 26 Apr 2024 13:02:41 -0400 Subject: [PATCH 3/3] comment --- deepbook/sources/pool/deep_price.move | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepbook/sources/pool/deep_price.move b/deepbook/sources/pool/deep_price.move index 8a6169e0..d4f99c89 100644 --- a/deepbook/sources/pool/deep_price.move +++ b/deepbook/sources/pool/deep_price.move @@ -30,7 +30,7 @@ module deepbook::deep_price { _base_conversion_rate: u64, _quote_conversion_rate: u64, ) { - // TODO + // TODO } public(package) fun deep_per_base(deep_price: &DeepPrice): u64 {