1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use serde::{Deserialize, Serialize};

use crate::account::Account;
use crate::client::Client;
use crate::error::Error;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FundBuyRequest {
    pub orderbook_id: String,
    pub account_id: String,
    pub amount: f64,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FundBuyResponse {
    pub order_request_status: String,
    pub order_id: i64,
    pub message: String,
    pub account_id: String,
}

impl Client {
    pub async fn buy_fund(
        &self,
        account: &Account,
        orderbook_id: String,
        amount: f64,
    ) -> Result<FundBuyResponse, Error> {
        let order = FundBuyRequest {
            orderbook_id,
            account_id: account.id.clone(),
            amount: (amount * 100.0).round() / 100.0,
        };
        let res = self
            .http_client
            .post(&self.config.urls.fund_buy)
            .body_json(&order)?
            .recv_json::<FundBuyResponse>()
            .await?;
        return Ok(res);
    }
}