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

[Orderbook]: Orderbook rename and module refactor #24

Merged
merged 3 commits into from
Feb 17, 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
2 changes: 1 addition & 1 deletion Beaker.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
name = "perpetuals"
name = "orderbook"
2 changes: 1 addition & 1 deletion contracts/orderbook/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["alpo <[email protected]>"]
edition = "2021"
name = "perpetuals"
name = "orderbook"
version = "0.1.0"

exclude = [
Expand Down
2 changes: 1 addition & 1 deletion contracts/orderbook/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# perpetuals
# orderbook
2 changes: 1 addition & 1 deletion contracts/orderbook/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_schema::write_api;

use perpetuals::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use orderbook::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

fn main() {
write_api! {
Expand Down
51 changes: 5 additions & 46 deletions contracts/orderbook/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

use crate::orderbook;
use crate::order;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:perpetuals";
const CONTRACT_NAME: &str = "crates.io:orderbook";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Handling contract instantiation
Expand Down Expand Up @@ -68,55 +69,13 @@ pub fn execute(
),

// Places limit order on given market
ExecuteMsg::PlaceLimit => orderbook::place_limit(_deps, _env, _info),
ExecuteMsg::PlaceLimit => order::place_limit(_deps, _env, _info),

// Cancels limit order with given ID
ExecuteMsg::CancelLimit => orderbook::cancel_limit(_deps, _env, _info),
ExecuteMsg::CancelLimit => order::cancel_limit(_deps, _env, _info),

// Places a market order on the passed in market
ExecuteMsg::PlaceMarket => orderbook::place_market(_deps, _env, _info),


// === Margin ===

// depositCollateral

// withdrawCollateral: withdraw collateral from account if initial margin satisfied

// collateralizationCheckForOrder: check if the passed in order pushes the account passed initial margin requirements

// syntheticBorrow: borrow synthetic assets for accounting purposes (add to assets & liabilities)
// TODO: is this necessary?

// isLiquidatable: check if an account is liquidatable

// liquidate: liquidate an account


// === Funding ===

// settleFundingForAccount: settle funding accumulators for all markets


// === Risk ===

// updateRiskParamsForMarket


// === Emergency ===

// depositToInsuranceFund: deposit collateral to insurance fund

// pauseMarket: freeze trading on a specific market

// deleverMarket: settle highest leverage positions against profitable positions to lower market leverage

// initiateInsuranceFund
// TODO: this should likely be governance-gated


// === Oracle ===
// TODO: mock oracle until slinky interface is known/ready
ExecuteMsg::PlaceMarket => order::place_market(_deps, _env, _info),
}
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/orderbook/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod contract;
mod error;
pub mod msg;
pub mod types;
pub mod state;
mod orderbook;
mod order;

pub use crate::error::ContractError;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::ContractError;
use crate::orderbook::types::Orderbook;
use crate::state::new_order_book_id;
use crate::types::Orderbook;
use crate::state::new_orderbook_id;
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};

pub fn create_orderbook(
Expand All @@ -10,7 +10,7 @@ pub fn create_orderbook(
quote_denom: String,
base_denom: String,
) -> Result<Response, ContractError> {
let book_id = new_order_book_id(deps.storage).unwrap();
let book_id = new_orderbook_id(deps.storage).unwrap();
let _book = Orderbook {
book_id,
quote_denom,
Expand Down
28 changes: 14 additions & 14 deletions contracts/orderbook/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::orderbook::types::{LimitOrder, Orderbook};
use crate::types::{LimitOrder, Orderbook};
use crate::ContractError;
use cosmwasm_std::{Storage, Uint128};
use cw_storage_plus::{Item, Map};

pub const ORDER_BOOKS: Map<&u64, Orderbook> = Map::new("order_books");
/// Key: (order_book_id, tick)
pub const ORDERBOOKS: Map<&u64, Orderbook> = Map::new("orderbooks");
/// Key: (orderbook_id, tick)
pub const TICK_LIQUIDITY: Map<&(u64, i64), Uint128> = Map::new("tick_liquidity");
/// Key: (order_book_id, tick, order_id)
/// Key: (orderbook_id, tick, order_id)
pub const ORDERS: Map<&(u64, i64, u64), LimitOrder> = Map::new("tick_orders");

// Counters for ID tracking
pub const ORDER_ID: Item<u64> = Item::new("order_id");
pub const ORDER_BOOK_ID: Item<u64> = Item::new("order_book_id");
pub const ORDERBOOK_ID: Item<u64> = Item::new("orderbook_id");

pub fn new_order_book_id(storage: &mut dyn Storage) -> Result<u64, ContractError> {
let id = ORDER_BOOK_ID.load(storage).unwrap_or_default();
ORDER_BOOK_ID.save(storage, &(id + 1))?;
pub fn new_orderbook_id(storage: &mut dyn Storage) -> Result<u64, ContractError> {
let id = ORDERBOOK_ID.load(storage).unwrap_or_default();
ORDERBOOK_ID.save(storage, &(id + 1))?;
Ok(id)
}

Expand All @@ -28,16 +28,16 @@ pub fn new_order_id(storage: &mut dyn Storage) -> Result<u64, ContractError> {
#[cfg(test)]
mod test {
use super::*;
use crate::orderbook::types::OrderDirection;
use crate::types::OrderDirection;
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{Addr, Order};

#[test]
fn test_new_order_book_id() {
fn test_new_orderbook_id() {
let mut storage = MockStorage::new();
let id = new_order_book_id(&mut storage).unwrap();
let id = new_orderbook_id(&mut storage).unwrap();
assert_eq!(id, 0);
let id = new_order_book_id(&mut storage).unwrap();
let id = new_orderbook_id(&mut storage).unwrap();
assert_eq!(id, 1);
}

Expand All @@ -53,7 +53,7 @@ mod test {
#[test]
fn test_tick_iteration() {
let mut storage = MockStorage::new();
let book_id = new_order_book_id(&mut storage).unwrap();
let book_id = new_orderbook_id(&mut storage).unwrap();
let tick_amount = 50;
for i in -tick_amount..tick_amount {
TICK_LIQUIDITY
Expand All @@ -79,7 +79,7 @@ mod test {
fn test_order_iteration() {
let mut storage = MockStorage::new();
let order_amount = 50;
let book_id = new_order_book_id(&mut storage).unwrap();
let book_id = new_orderbook_id(&mut storage).unwrap();
let tick = 0;
for i in 0..order_amount {
let order_id = new_order_id(&mut storage).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion ts/sdk/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# perpetuals-sdk
# orderbook-sdk
2 changes: 1 addition & 1 deletion ts/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "perpetuals-sdk",
"name": "orderbook-sdk",
"version": " 0.0.1",
"description": "",
"cdn": "dist/index.umd.js",
Expand Down
2 changes: 1 addition & 1 deletion ts/sdk/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"out": "./docs",
"name": "perpetuals",
"name": "orderbook",
"theme": "markdown",
"readme": "none",
"tsconfig": "./tsconfig.bundle.json"
Expand Down
Loading