dexter-swap/contracts/OrderBook/OrderBook.deploy.rs
2024-06-22 21:57:39 +03:00

155 lines
4.0 KiB
Rust

// SPDX-License-Identifier: MIT
#![cfg_attr(not(feature = "std"), no_std)]
use cosmwasm_std::{
to_binary, Api, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Order, Response,
StdError, StdResult, Storage, Uint128, WasmMsg,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
// Define your order struct
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Order {
pub id: String,
pub trader: String,
pub token: String,
pub amount: Uint128,
pub price: Uint128,
pub order_type: OrderType,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum OrderType {
Buy,
Sell,
}
impl Order {
pub fn new(
id: String,
trader: String,
token: String,
amount: Uint128,
price: Uint128,
order_type: OrderType,
) -> Self {
Order {
id,
trader,
token,
amount,
price,
order_type,
}
}
}
// Main struct for the order book contract
pub struct OrderBook {}
impl OrderBook {
// Init function to initialize the order book
pub fn init(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
) -> Result<Response, StdError> {
// Initialize storage for your order book
Ok(Response::default())
}
// Function to add an order to the order book
pub fn add_order(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
id: String,
trader: String,
token: String,
amount: Uint128,
price: Uint128,
order_type: OrderType,
) -> Result<Response, StdError> {
// Implement logic to add the order to the order book
Ok(Response::default())
}
// Function to match buy and sell orders
pub fn match_orders(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
token: String,
) -> Result<Response, StdError> {
// Implement logic to match buy and sell orders
Ok(Response::default())
}
}
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::{coin, testing::mock_dependencies, MessageInfo};
// Test initialization of order book
#[test]
fn test_init_order_book() {
let mut deps = mock_dependencies(&[]);
let env = Env {
block: cosmwasm_std::BlockInfo {
height: 1,
time: 0,
chain_id: "test".to_string(),
},
message_id: "0".to_string(),
contract: "orderbook".to_string(),
contract_address: "addr0000".to_string(),
sender: "addr0000".to_string(),
sent_funds: vec![],
};
let info = MessageInfo {
sender: "addr0000".to_string(),
funds: vec![coin(100, "token".to_string())],
};
let res = OrderBook::init(deps.as_mut(), env.clone(), info.clone());
assert_eq!(res.is_ok(), true);
}
// Test adding an order to the order book
#[test]
fn test_add_order() {
let mut deps = mock_dependencies(&[]);
let env = Env {
block: cosmwasm_std::BlockInfo {
height: 1,
time: 0,
chain_id: "test".to_string(),
},
message_id: "0".to_string(),
contract: "orderbook".to_string(),
contract_address: "addr0000".to_string(),
sender: "addr0000".to_string(),
sent_funds: vec![],
};
let info = MessageInfo {
sender: "addr0000".to_string(),
funds: vec![coin(100, "token".to_string())],
};
let res = OrderBook::add_order(
deps.as_mut(),
env.clone(),
info.clone(),
"order1".to_string(),
"trader1".to_string(),
"token".to_string(),
Uint128::new(100),
Uint128::new(10),
OrderType::Buy,
);
assert_eq!(res.is_ok(), true);
}
}