Skip to content

Commit

Permalink
Merge branch 'alpo/cleanup-refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Feb 17, 2024
2 parents 63ac430 + f3bf6f0 commit d2fc8de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
46 changes: 41 additions & 5 deletions contracts/orderbook/src/orderbook.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::ContractError;
use crate::types::Orderbook;
use crate::state::new_orderbook_id;
use crate::state::{new_orderbook_id, ORDERBOOKS, MIN_TICK, MAX_TICK};
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};

pub fn create_orderbook(
Expand All @@ -10,15 +10,51 @@ pub fn create_orderbook(
quote_denom: String,
base_denom: String,
) -> Result<Response, ContractError> {
let book_id = new_orderbook_id(deps.storage).unwrap();
// TODO: add necessary validation logic
// https://github.com/osmosis-labs/orderbook/issues/26

let book_id = new_orderbook_id(deps.storage)?;
let _book = Orderbook {
book_id,
quote_denom,
base_denom,
current_tick: 0,
next_bid_tick: -1,
next_ask_tick: 1,
next_bid_tick: MIN_TICK,
next_ask_tick: MAX_TICK,
};

Ok(Response::new().add_attribute("method", "createOrderbook"))
ORDERBOOKS.save(deps.storage, &book_id, &_book)?;

Ok(Response::new().add_attribute("method", "createOrderbook").add_attribute("book_id", &book_id.to_string()))
}

#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};

#[test]
fn test_create_orderbook() {
let mut deps = mock_dependencies();
let env = mock_env();
let info = mock_info("creator", &[]);

// Attempt to create an orderbook
let quote_denom = "quote".to_string();
let base_denom = "base".to_string();
let create_response = create_orderbook(deps.as_mut(), env, info, quote_denom.clone(), base_denom.clone()).unwrap();

// Verify response
let expected_book_id: u64 = 0;
assert_eq!(create_response.attributes[0], ("method", "createOrderbook"));
assert_eq!(create_response.attributes[1], ("book_id", &expected_book_id.to_string()));

// Verify orderbook is saved correctly
let orderbook = ORDERBOOKS.load(deps.as_ref().storage, &expected_book_id).unwrap();
assert_eq!(orderbook.quote_denom, quote_denom);
assert_eq!(orderbook.base_denom, base_denom);
assert_eq!(orderbook.current_tick, 0);
assert_eq!(orderbook.next_bid_tick, MIN_TICK);
assert_eq!(orderbook.next_ask_tick, MAX_TICK);
}
}
4 changes: 4 additions & 0 deletions contracts/orderbook/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use crate::ContractError;
use cosmwasm_std::{Storage, Uint128};
use cw_storage_plus::{Item, Map};

pub const MIN_TICK: i64 = -108000000;
pub const MAX_TICK: i64 = 342000000;


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");
Expand Down

0 comments on commit d2fc8de

Please sign in to comment.