Skip to content

Commit 833206c

Browse files
authored
Merge pull request #24 from yajamon/trade_api/cancel_order
Trade api/cancel orderを実装
2 parents ec5a18b + 6bed0ab commit 833206c

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/main.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
extern crate zaif_api;
2+
extern crate serde_json;
3+
4+
use std::{thread, time};
5+
use serde_json::Value;
26

37
use zaif_api::AccessKey;
48
use zaif_api::public_api::*;
@@ -27,7 +31,21 @@ fn main() {
2731
.price(1.0)
2832
.amount(0.1)
2933
.finalize();
30-
println!("{}", api.exec().unwrap());
34+
match api.exec() {
35+
Ok(res) => {
36+
println!("{}", res);
37+
let json:Value = serde_json::from_str(res.as_str()).unwrap();
38+
let order_id = json["return"]["order_id"].as_u64().unwrap();
39+
let api = CancelOrderBuilder::new(access_key.clone())
40+
.order_id(order_id)
41+
.currency_pair("zaif_jpy")
42+
.finalize();
43+
let wait_time = time::Duration::from_secs(5);
44+
thread::sleep(wait_time);
45+
println!("{}", api.exec().unwrap());
46+
},
47+
_ => return,
48+
}
3149

3250
let api = ActiveOrdersBuilder::new(access_key.clone()).currency_pair("zaif_jpy").finalize();
3351
println!("{}", api.exec().unwrap());

src/trade_api/cancel_order.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
extern crate reqwest;
2+
3+
use std::collections::HashMap;
4+
5+
use core::*;
6+
7+
pub struct CancelOrder {
8+
access_key: AccessKey,
9+
order_id: u64,
10+
currency_pair: Option<String>,
11+
}
12+
13+
impl CancelOrder {
14+
pub fn exec(&self) -> reqwest::Result<String> {
15+
let param: &mut HashMap<String, String> = &mut HashMap::new();
16+
param.insert("method".to_string(), "cancel_order".to_string());
17+
param.insert("order_id".to_string(), format!("{}", self.order_id));
18+
if let Some(ref currency_pair) = self.currency_pair {
19+
param.insert("currency_pair".to_string(), format!("{}", currency_pair.clone()));
20+
}
21+
22+
let api = ApiBuilder::new()
23+
.access_key(self.access_key.clone())
24+
.uri("https://api.zaif.jp/tapi")
25+
.method(Method::Post)
26+
.param(param.clone())
27+
.finalize();
28+
29+
api.exec()
30+
}
31+
}
32+
33+
pub struct CancelOrderBuilder {
34+
access_key: AccessKey,
35+
order_id: Option<u64>,
36+
currency_pair: Option<String>,
37+
}
38+
39+
impl CancelOrderBuilder{
40+
pub fn new(access_key: AccessKey) -> CancelOrderBuilder {
41+
CancelOrderBuilder {
42+
access_key: access_key,
43+
order_id: None,
44+
currency_pair: None,
45+
}
46+
}
47+
pub fn order_id(&mut self, order_id: u64) -> &mut CancelOrderBuilder {
48+
self.order_id = Some(order_id);
49+
self
50+
}
51+
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut CancelOrderBuilder {
52+
self.currency_pair = Some(currency_pair.to_string());
53+
self
54+
}
55+
pub fn finalize(&self) -> CancelOrder {
56+
CancelOrder {
57+
access_key: self.access_key.clone(),
58+
order_id: self.order_id.unwrap(),
59+
currency_pair: self.currency_pair.clone(),
60+
}
61+
}
62+
}

src/trade_api/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
pub use self::get_info2::*;
22
pub use self::trade::*;
33
pub use self::active_orders::*;
4+
pub use self::cancel_order::*;
45

56
mod get_info2;
67
mod trade;
78
mod active_orders;
9+
mod cancel_order;

0 commit comments

Comments
 (0)