Skip to content

Commit

Permalink
Merge pull request #48 from MystenLabs/at/init
Browse files Browse the repository at this point in the history
admin cap & init
  • Loading branch information
0xaslan authored Apr 28, 2024
2 parents 55d4118 + 74932eb commit b6ff72d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
23 changes: 22 additions & 1 deletion deepbook/sources/deepbook.move
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,6 +55,7 @@ module deepbook::deepbook {

/// Public facing function to set a pool as stable.
public fun set_pool_as_stable<BaseAsset, QuoteAsset>(
_cap: &DeepBookAdminCap,
state: &mut State,
pool: &Pool<BaseAsset, QuoteAsset>,
stable: bool,
Expand All @@ -46,6 +66,7 @@ module deepbook::deepbook {

/// Public facing function to add a reference pool.
public fun add_reference_pool<BaseAsset, QuoteAsset>(
_cap: &DeepBookAdminCap,
state: &mut State,
reference_pool: &Pool<BaseAsset, QuoteAsset>,
) {
Expand Down
2 changes: 1 addition & 1 deletion deepbook/sources/pool/deep_price.move
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion deepbook/sources/pool/pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,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 the total amount staked during this epoch.
public(package) fun remove_user_stake<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
user: address,
Expand Down
9 changes: 7 additions & 2 deletions deepbook/sources/state/deep_reference_price.move
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,11 +20,16 @@ module deepbook::deep_reference_price {
reference_pools: VecMap<String, String>,
}

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<BaseAsset, QuoteAsset>(
deep_reference_price: &mut DeepReferencePools,
pool: &Pool<BaseAsset, QuoteAsset>,
// cap: &DeepbookAdminCap TODO
) {
let (base, quote) = pool.get_base_quote_types();
let deep_type = type_name::get<DEEP>().into_string();
Expand Down
65 changes: 35 additions & 30 deletions deepbook/sources/state/state.move
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String, PoolMetadata>,
Expand All @@ -41,6 +39,17 @@ module deepbook::state { // Consider renaming this module
vault: Balance<DEEP>,
}

/// 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.
Expand All @@ -53,8 +62,8 @@ module deepbook::state { // Consider renaming this module
ctx: &mut TxContext,
) {
let pool = pool::create_pool<BaseAsset, QuoteAsset>(
VOLATILE_TAKER_FEE,
VOLATILE_MAKER_FEE,
DEFAULT_TAKER_FEE,
DEFAULT_MAKER_FEE,
tick_size,
lot_size,
min_size,
Expand All @@ -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<BaseAsset, QuoteAsset>(
// cap: DeepbookAdminCap, TODO
self: &mut State,
pool: &Pool<BaseAsset, QuoteAsset>,
stable: bool,
Expand Down Expand Up @@ -108,13 +116,10 @@ module deepbook::state { // Consider renaming this module
public(package) fun add_reference_pool<BaseAsset, QuoteAsset>(
self: &mut State,
reference_pool: &Pool<BaseAsset, QuoteAsset>,
// 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,
Expand All @@ -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());
}

Expand All @@ -145,18 +148,13 @@ module deepbook::state { // Consider renaming this module
ctx: &mut TxContext
): Coin<DEEP> {
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<BaseAsset, QuoteAsset>(
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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<BaseAsset, QuoteAsset>(
self: &mut State,
pool: &Pool<BaseAsset, QuoteAsset>,
Expand All @@ -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<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
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)
}
}

0 comments on commit b6ff72d

Please sign in to comment.