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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::{
    Base58BlockHash, Base58PublicKey, MessageHeader, PoseidonError, PoseidonResult, RpcClient,
    Transaction, TransactionError, UnixTimestamp,
};
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTransaction {
    pub jsonrpc: String,
    pub id: u8,
    pub result: Option<RpcResult>,
}

impl GetTransaction {
    pub async fn process(transaction: &str) -> PoseidonResult<GetTransaction> {
        use json::JsonValue;

        let body: json::JsonValue = json::object! {
            jsonrpc: "2.0",
            id: 1u8,
            method: "getTransaction",
            params: json::array![JsonValue::String(transaction.to_owned()), JsonValue::String("base58".to_owned())]
        };

        Ok(GetTransaction::request(body).await?)
    }

    pub fn transaction(&self) -> PoseidonResult<Transaction> {
        match &self.result {
            Some(rpc_result) => {
                let encoded = &rpc_result.transaction.0;
                let decoded = bs58::decode(encoded).into_vec()?;
                let data = bincode::deserialize::<Transaction>(&decoded)?;

                Ok(data)
            }
            None => Err(PoseidonError::TransactionNotFoundInCluster),
        }
    }

    async fn request(body: json::JsonValue) -> PoseidonResult<GetTransaction> {
        let mut rpc = RpcClient::new();
        rpc.add_body(body);
        let response = rpc.send().await?;
        let deser_response: GetTransaction = serde_json::from_str(response.as_str()?)?;

        Ok(deser_response)
    }
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcResult {
    pub block_time: UnixTimestamp,
    pub meta: RpcMeta,
    pub transaction: (String, String),
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcMeta {
    pub err: Option<TransactionError>,
    pub fee: u32,
    pub inner_instructions: Vec<RpcInnerInstructions>,
    pub log_messages: Vec<String>,
    pub pre_balances: Vec<u64>,
    pub post_balances: Vec<u64>,
    pub pre_token_balances: Vec<TokenBalances>,
    pub post_token_balances: Vec<TokenBalances>,
    pub rewards: Vec<Reward>,
    pub status: Result<(), TransactionError>,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct RpcInnerInstructions {
    pub index: u8,
    pub instructions: Vec<RpcCompiledInstruction>,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct TokenBalances {
    pub account_index: u8,
    pub mint: Base58PublicKey,
    pub owner: Base58PublicKey,
    pub ui_token_amount: TokenAmount,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct TokenAmount {
    pub amount: String,
    pub decimals: u8,
    pub ui_amount: f64,
    pub ui_amount_string: String,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct Reward {
    pub pubkey: String,
    pub lamports: i64,
    pub post_balance: u64,
    pub reward_type: RewardType,
    pub commission: u8,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub enum RewardType {
    Fee,
    Rent,
    Staking,
    Voting,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]

pub struct RpcCompiledInstruction {
    pub program_id_index: u8,
    pub accounts: Vec<u8>,
    pub data: String,
}

#[derive(
    Debug, PartialEq, PartialOrd, Clone, Deserialize, Serialize, BorshSerialize, BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct RpcMessage {
    pub header: MessageHeader,
    pub account_keys: Vec<Base58PublicKey>,
    pub recent_blockhash: Base58BlockHash,
    pub instructions: Vec<RpcCompiledInstruction>,
}

#[derive(
    Debug,
    PartialEq,
    Eq,
    Ord,
    PartialOrd,
    Clone,
    Deserialize,
    Serialize,
    BorshSerialize,
    BorshDeserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct Context {
    pub slot: u64,
}