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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use solana_extra_wasm::transaction_status::UiTransactionEncoding;
use solana_sdk::{commitment_config::CommitmentConfig, message::Message};

use super::Context;
use crate::{utils::rpc_config::serialize_and_encode, ClientRequest, ClientResponse};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetFeeForMessageRequest {
    pub message: Message,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config: Option<CommitmentConfig>,
}

impl GetFeeForMessageRequest {
    pub fn new(message: Message) -> Self {
        Self {
            message,
            config: None,
        }
    }
    pub fn new_with_config(message: Message, config: CommitmentConfig) -> Self {
        Self {
            message,
            config: Some(config),
        }
    }
}

impl Into<serde_json::Value> for GetFeeForMessageRequest {
    fn into(self) -> serde_json::Value {
        let message =
            serialize_and_encode::<Message>(&self.message, UiTransactionEncoding::Base64).unwrap();

        match self.config {
            Some(config) => serde_json::json!([message, config]),
            None => serde_json::json!([message]),
        }
    }
}

impl Into<ClientRequest> for GetFeeForMessageRequest {
    fn into(self) -> ClientRequest {
        let mut request = ClientRequest::new("getFeeForMessage");
        let params = self.into();

        request.params(params).clone()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeeForMessageValue(Option<u64>);

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetFeeForMessageResponse {
    pub context: Context,
    pub value: FeeForMessageValue,
}

impl From<ClientResponse> for GetFeeForMessageResponse {
    fn from(response: ClientResponse) -> Self {
        serde_json::from_value(response.result).unwrap()
    }
}

impl Into<u64> for GetFeeForMessageResponse {
    fn into(self) -> u64 {
        self.value.0.unwrap_or_default()
    }
}