Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stake / unstake as account, remove all ctx.sender() #50

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions deepbook/sources/deepbook.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
module deepbook::deepbook {
use sui::{
balance::Balance,
coin::Coin,
sui::SUI,
clock::Clock,
vec_set::VecSet,
};

use deepbook::{
state::{Self, State},
pool::{Order, Pool, DEEP},
pool::{Order, Pool},
account::{Account, TradeProof},
};

Expand Down Expand Up @@ -89,9 +88,11 @@ module deepbook::deepbook {
/// Public facing function to remove a deep price point from a specific pool.
public fun claim_rebates<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &mut Account,
proof: &TradeProof,
ctx: &mut TxContext
): Coin<DEEP> {
pool.claim_rebates(ctx)
) {
pool.claim_rebates(account, proof, ctx)
}

// GOVERNANCE
Expand All @@ -100,43 +101,52 @@ module deepbook::deepbook {
public fun stake<BaseAsset, QuoteAsset>(
state: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
amount: Coin<DEEP>,
account: &mut Account,
proof: &TradeProof,
amount: u64,
ctx: &mut TxContext,
) {
state.stake(pool, amount, ctx);
state.stake(pool, account, proof, amount, ctx)
}

/// Public facing function to unstake DEEP tokens from a specific pool.
public fun unstake<BaseAsset, QuoteAsset>(
state: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &mut Account,
proof: &TradeProof,
ctx: &mut TxContext
): Coin<DEEP> {
state.unstake(pool, ctx)
) {
state.unstake(pool, account, proof, ctx)
}

/// Public facing function to submit a proposal.
public fun submit_proposal<BaseAsset, QuoteAsset>(
state: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &Account,
proof: &TradeProof,
maker_fee: u64,
taker_fee: u64,
stake_required: u64,
ctx: &mut TxContext,
) {
state.submit_proposal(
pool, maker_fee, taker_fee, stake_required, ctx
pool, account, proof, maker_fee, taker_fee, stake_required, ctx
);
}

/// Public facing function to vote on a proposal.
public fun vote<BaseAsset, QuoteAsset>(
state: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &Account,
proof: &TradeProof,
proposal_id: u64,
ctx: &mut TxContext,
) {
state.vote(pool, proposal_id, ctx);
account.validate_proof(proof);
state.vote(pool, account, proof, proposal_id, ctx);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is proof needed if only owner can vote, or do we plan on allowing delegation of voting power as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since account is a shared object, anyone can pass any other user's account. I am just validating that the sender is the account owner via a proof. I did notice I am doing the same validation in State level as well, which I will remove

}

// ORDERS
Expand Down
6 changes: 3 additions & 3 deletions deepbook/sources/pool/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module deepbook::account {
proof: &TradeProof,
coin: Coin<T>,
) {
proof.validate_proof(account);
account.validate_proof(proof);

let key = BalanceKey<T> {};
let to_deposit = coin.into_balance();
Expand Down Expand Up @@ -151,7 +151,7 @@ module deepbook::account {
amount: u64,
ctx: &mut TxContext,
): Coin<T> {
proof.validate_proof(account);
account.validate_proof(proof);

let key = BalanceKey<T> {};
assert!(account.balances.contains(key), ENoBalance);
Expand All @@ -174,7 +174,7 @@ module deepbook::account {
assert!(account.allow_listed.contains(object::borrow_id(trade_cap)), EInvalidTrader);
}

fun validate_proof(proof: &TradeProof, account: &Account) {
public fun validate_proof(account: &Account, proof: &TradeProof) {
assert!(object::id(account) == proof.account_id, EInvalidProof);
}
}
31 changes: 16 additions & 15 deletions deepbook/sources/pool/pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module deepbook::pool {
fee_quantity,
is_bid,
expire_timestamp,
ctx
account.owner(),
);

event::emit(OrderPlaced<BaseAsset, QuoteAsset> {
Expand Down Expand Up @@ -656,13 +656,14 @@ module deepbook::pool {
/// Claim the rebates for the user
public(package) fun claim_rebates<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
account: &mut Account,
proof: &TradeProof,
ctx: &mut TxContext
): Coin<DEEP> {
let user = self.get_user_mut(ctx.sender(), ctx);
) {
let user = self.get_user_mut(account.owner(), ctx);
let amount = user.reset_rebates();
self.deepbook_balance
.split(amount)
.into_coin(ctx)
let coin = self.deepbook_balance.split(amount).into_coin(ctx);
account.deposit_with_proof<DEEP>(proof, coin);
}

/// Cancel all orders for an account. Withdraw settled funds back into user account.
Expand Down Expand Up @@ -879,7 +880,7 @@ module deepbook::pool {
amount: u64,
ctx: &mut TxContext,
) {
let base = user_account.withdraw_with_proof(proof, amount, ctx);
let base = user_account.withdraw_with_proof<BaseAsset>(proof, amount, ctx);
self.base_balances.join(base.into_balance());
}

Expand All @@ -890,7 +891,7 @@ module deepbook::pool {
amount: u64,
ctx: &mut TxContext,
) {
let quote = user_account.withdraw_with_proof(proof, amount, ctx);
let quote = user_account.withdraw_with_proof<QuoteAsset>(proof, amount, ctx);
self.quote_balances.join(quote.into_balance());
}

Expand All @@ -901,7 +902,7 @@ module deepbook::pool {
amount: u64,
ctx: &mut TxContext,
) {
let coin = user_account.withdraw_with_proof(proof, amount, ctx);
let coin = user_account.withdraw_with_proof<DEEP>(proof, amount, ctx);
self.deepbook_balance.join(coin.into_balance());
}

Expand All @@ -913,7 +914,7 @@ module deepbook::pool {
ctx: &mut TxContext,
) {
let coin = self.base_balances.split(amount).into_coin(ctx);
user_account.deposit_with_proof(proof, coin);
user_account.deposit_with_proof<BaseAsset>(proof, coin);
}

fun withdraw_quote<BaseAsset, QuoteAsset>(
Expand All @@ -924,7 +925,7 @@ module deepbook::pool {
ctx: &mut TxContext,
) {
let coin = self.quote_balances.split(amount).into_coin(ctx);
user_account.deposit_with_proof(proof, coin);
user_account.deposit_with_proof<QuoteAsset>(proof, coin);
}

fun withdraw_deep<BaseAsset, QuoteAsset>(
Expand All @@ -935,7 +936,7 @@ module deepbook::pool {
ctx: &mut TxContext,
) {
let coin = self.deepbook_balance.split(amount).into_coin(ctx);
user_account.deposit_with_proof(proof, coin);
user_account.deposit_with_proof<DEEP>(proof, coin);
}

#[allow(unused_function)]
Expand All @@ -954,7 +955,7 @@ module deepbook::pool {
fee_quantity: u64,
is_bid: bool, // true for bid, false for ask
expire_timestamp: u64, // Expiration timestamp in ms
ctx: &TxContext,
owner: address,
) {

// Create Order
Expand All @@ -968,7 +969,7 @@ module deepbook::pool {
fee_quantity,
fee_is_deep: self.fee_is_deep(),
is_bid,
owner: ctx.sender(),
owner,
expire_timestamp,
self_matching_prevention: 0, // TODO
};
Expand All @@ -981,7 +982,7 @@ module deepbook::pool {
};

// Add order to user's open orders
let user_data = &mut self.users[ctx.sender()];
let user_data = &mut self.users[owner];
user_data.add_open_order(order_id);
}

Expand Down
41 changes: 27 additions & 14 deletions deepbook/sources/state/state.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module deepbook::state { // Consider renaming this module
balance::{Self, Balance},
table::{Self, Table},
sui::SUI,
coin::Coin,
clock::Clock,
};

use deepbook::{
account::{Account, TradeProof},
pool::{Pool, DEEP, Self},
pool_state,
pool_metadata::{Self, PoolMetadata},
Expand Down Expand Up @@ -128,14 +128,17 @@ module deepbook::state { // Consider renaming this module
public(package) fun stake<BaseAsset, QuoteAsset>(
self: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
amount: Coin<DEEP>,
ctx: &TxContext,
account: &mut Account,
proof: &TradeProof,
amount: u64,
ctx: &mut TxContext,
) {
let user = ctx.sender();
let total_user_stake = pool.increase_user_stake(user, amount.value(), ctx);
let user = account.owner();
let total_user_stake = pool.increase_user_stake(user, amount, ctx);
self.get_pool_metadata_mut(pool, ctx)
.add_voting_power(total_user_stake, amount.value());
self.vault.join(amount.into_balance());
.add_voting_power(total_user_stake, amount);
let balance = account.withdraw_with_proof<DEEP>(proof, amount, ctx).into_balance();
self.vault.join(balance);
}

/// Unstake DEEP in the pool. This will decrease the user's voting power.
Expand All @@ -145,27 +148,31 @@ module deepbook::state { // Consider renaming this module
public(package) fun unstake<BaseAsset, QuoteAsset>(
self: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &mut Account,
proof: &TradeProof,
ctx: &mut TxContext
): Coin<DEEP> {
let user = ctx.sender();
) {
let user = account.owner();
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)
let balance = self.vault.split(user_old_stake + user_new_stake).into_coin(ctx);
account.deposit_with_proof<DEEP>(proof, balance);
}

/// 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>(
self: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &Account,
proof: &TradeProof,
maker_fee: u64,
taker_fee: u64,
stake_required: u64,
ctx: &TxContext,
) {
let (user, _) = assert_participant(pool, ctx);
let (user, _) = assert_participant(pool, account, proof, ctx);

let pool_metadata = self.get_pool_metadata_mut(pool, ctx);
pool_metadata.add_proposal(user, maker_fee, taker_fee, stake_required);
Expand All @@ -177,10 +184,12 @@ module deepbook::state { // Consider renaming this module
public(package) fun vote<BaseAsset, QuoteAsset>(
self: &mut State,
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &Account,
proof: &TradeProof,
proposal_id: u64,
ctx: &TxContext,
) {
let (user, user_stake) = assert_participant(pool, ctx);
let (user, user_stake) = assert_participant(pool, account, proof, ctx);

let pool_metadata = self.get_pool_metadata_mut(pool, ctx);
let winning_proposal = pool_metadata.vote(proposal_id, user, user_stake);
Expand Down Expand Up @@ -216,9 +225,13 @@ module deepbook::state { // Consider renaming this module
/// Check whether user can submit and vote on proposals.
fun assert_participant<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
account: &Account,
proof: &TradeProof,
ctx: &TxContext
): (address, u64) {
let user = ctx.sender();
account.validate_proof(proof);

let user = account.owner();
let (user_stake, _) = pool.get_user_stake(user, ctx);
assert!(user_stake >= STAKE_REQUIRED_TO_PARTICIPATE, ENotEnoughStake);

Expand Down
Loading