use crate::http::{Credentials, Method, request::Request};
use serde_json::{Map, Value, json};
#[derive(Debug, Clone)]
pub struct SpotPriceTrigger {
pub price: String,
pub rule: String,
pub expiration: Option<i64>,
}
impl SpotPriceTrigger {
pub fn new(price: &str, rule: &str) -> Self {
Self {
price: price.to_owned(),
rule: rule.to_owned(),
expiration: None,
}
}
pub fn expiration(mut self, expiration: i64) -> Self {
self.expiration = Some(expiration);
self
}
}
#[derive(Debug, Clone)]
pub struct SpotPricePutOrder {
pub order_type: String,
pub side: String,
pub price: String,
pub amount: String,
pub account: Option<String>,
pub time_in_force: Option<String>,
}
impl SpotPricePutOrder {
pub fn new(order_type: &str, side: &str, price: &str, amount: &str) -> Self {
Self {
order_type: order_type.to_owned(),
side: side.to_owned(),
price: price.to_owned(),
amount: amount.to_owned(),
account: None,
time_in_force: None,
}
}
pub fn account(mut self, account: &str) -> Self {
self.account = Some(account.to_owned());
self
}
pub fn time_in_force(mut self, time_in_force: &str) -> Self {
self.time_in_force = Some(time_in_force.to_owned());
self
}
}
pub struct CreatePriceOrder {
pub trigger: SpotPriceTrigger,
pub put: SpotPricePutOrder,
pub market: String,
pub x_gate_exp_time: Option<u128>,
pub credentials: Option<Credentials>,
}
impl CreatePriceOrder {
pub fn new(
market: &str,
trigger_price: &str,
trigger_rule: &str,
order_side: &str,
order_price: &str,
order_amount: &str,
) -> Self {
Self {
trigger: SpotPriceTrigger::new(trigger_price, trigger_rule),
put: SpotPricePutOrder::new("limit", order_side, order_price, order_amount),
market: market.to_owned(),
x_gate_exp_time: None,
credentials: None,
}
}
pub fn trigger(mut self, trigger: SpotPriceTrigger) -> Self {
self.trigger = trigger;
self
}
pub fn put(mut self, put: SpotPricePutOrder) -> Self {
self.put = put;
self
}
pub fn trigger_expiration(mut self, expiration: i64) -> Self {
self.trigger.expiration = Some(expiration);
self
}
pub fn account(mut self, account: &str) -> Self {
self.put.account = Some(account.to_owned());
self
}
pub fn time_in_force(mut self, time_in_force: &str) -> Self {
self.put.time_in_force = Some(time_in_force.to_owned());
self
}
pub fn x_gate_exp_time(mut self, x_gate_exp_time: u128) -> Self {
self.x_gate_exp_time = Some(x_gate_exp_time);
self
}
pub fn credentials(mut self, creds: Credentials) -> Self {
self.credentials = Some(creds);
self
}
}
impl From<CreatePriceOrder> for Request {
fn from(request: CreatePriceOrder) -> Request {
let params = Vec::new();
let mut payload = Map::new();
payload.insert("market".to_string(), json!(request.market));
let mut trigger_obj = Map::new();
trigger_obj.insert("price".to_string(), json!(request.trigger.price));
trigger_obj.insert("rule".to_string(), json!(request.trigger.rule));
if let Some(expiration) = request.trigger.expiration {
trigger_obj.insert("expiration".to_string(), json!(expiration));
}
payload.insert("trigger".to_string(), Value::Object(trigger_obj));
let mut put_obj = Map::new();
put_obj.insert("type".to_string(), json!(request.put.order_type));
put_obj.insert("side".to_string(), json!(request.put.side));
put_obj.insert("price".to_string(), json!(request.put.price));
put_obj.insert("amount".to_string(), json!(request.put.amount));
if let Some(account) = request.put.account {
put_obj.insert("account".to_string(), json!(account));
}
if let Some(time_in_force) = request.put.time_in_force {
put_obj.insert("time_in_force".to_string(), json!(time_in_force));
}
payload.insert("put".to_string(), Value::Object(put_obj));
let payload_json = Value::Object(payload);
Request {
method: Method::Post,
path: "/api/v4/spot/price_orders".into(),
params,
payload: payload_json.to_string(),
x_gate_exp_time: request.x_gate_exp_time,
credentials: request.credentials,
sign: true,
}
}
}