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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{anyhow, Result};
use diem_api_types::mime_types;
use diem_sdk::client::AccountAddress;
use reqwest::{Client, Response, StatusCode};
use serde_json::Value;
use std::{
    io,
    io::Write,
    thread, time,
    time::{Duration, Instant},
};
use url::Url;

const DIEM_ACCOUNT_TYPE: &str = "0x1::DiemAccount::DiemAccount";

pub struct DevApiClient {
    client: Client,
    url: Url,
}

// Client that will make GET and POST requests based off of Dev API
impl DevApiClient {
    pub fn new(client: Client, url: Url) -> Result<Self> {
        Ok(Self { client, url })
    }

    pub async fn get_transactions_by_hash(&self, hash: &str) -> Result<Value> {
        let path = self.url.join(format!("transactions/{}", hash).as_str())?;
        for _ in 1..20 {
            let resp = self.client.get(path.as_str()).send().await?;
            let status = resp.status();
            let json: serde_json::Value = resp.json().await?;
            if status == StatusCode::from_u16(200)? {
                if json["type"] != "pending_transaction" {
                    return Ok(json);
                }
            } else if status != StatusCode::from_u16(404)? {
                return Err(anyhow!(DevApiClient::response_context(
                    "GET /transactions failed",
                    &json
                )?));
            }
            thread::sleep(time::Duration::from_secs(2));
        }
        Err(anyhow!(format!(
            "GET /transactions by hash {} execution timeout",
            hash
        )))
    }

    pub async fn post_transactions(&self, txn_bytes: Vec<u8>) -> Result<Value> {
        let path = self.url.join("transactions")?;

        DevApiClient::check_response(
            self.client
                .post(path.as_str())
                .header("Content-Type", mime_types::BCS_SIGNED_TRANSACTION)
                .body(txn_bytes)
                .send()
                .await?,
            "POST /transactions failed",
        )
        .await
    }

    pub async fn get_account_resources(&self, address: AccountAddress) -> Result<Value> {
        let path = self
            .url
            .join(format!("accounts/{}/resources", address.to_hex_literal()).as_str())?;

        DevApiClient::check_response(
            self.client.get(path.as_str()).send().await?,
            "Failed to get account resources with provided address",
        )
        .await
    }

    pub async fn get_account_transactions_response(
        &self,
        address: AccountAddress,
        start: u64,
        limit: u64,
    ) -> Result<Value> {
        let path = self
            .url
            .join(format!("accounts/{}/transactions", address).as_str())?;

        DevApiClient::check_response(
            self.client
                .get(path.as_str())
                .query(&[("start", start.to_string().as_str())])
                .query(&[("limit", limit.to_string().as_str())])
                .send()
                .await?,
            "Failed to get account transactions with provided address",
        )
        .await
    }

    async fn check_response(resp: Response, failure_message: &str) -> Result<Value> {
        let status = resp.status();
        let json = resp.json().await?;
        DevApiClient::check_response_status_code(
            &status,
            DevApiClient::response_context(failure_message, &json)?.as_str(),
        )?;
        Ok(json)
    }

    fn check_response_status_code(status: &StatusCode, context: &str) -> Result<()> {
        match status >= &StatusCode::from_u16(200)? && status < &StatusCode::from_u16(300)? {
            true => Ok(()),
            false => Err(anyhow!(context.to_string())),
        }
    }

    fn response_context(message: &str, json: &Value) -> Result<String> {
        Ok(format!(
            "{}. Here is the json block for the response that failed:\n{:?}",
            message, json
        ))
    }

    pub async fn get_account_sequence_number(&self, address: AccountAddress) -> Result<u64> {
        let account_resources_json = self.get_account_resources(address).await?;
        DevApiClient::parse_json_for_account_seq_num(account_resources_json)
    }

    fn parse_json_for_account_seq_num(json_objects: Value) -> Result<u64> {
        let json_arr = json_objects
            .as_array()
            .ok_or_else(|| anyhow!("Couldn't convert to array"))?
            .to_vec();
        let mut seq_number_string = "";
        for object in &json_arr {
            if object["type"] == DIEM_ACCOUNT_TYPE {
                seq_number_string = object["data"]["sequence_number"]
                    .as_str()
                    .ok_or_else(|| anyhow!("Invalid sequence number string"))?;
                break;
            };
        }
        let seq_number: u64 = seq_number_string.parse()?;
        Ok(seq_number)
    }

    pub async fn check_txn_executed_from_hash(&self, hash: &str) -> Result<()> {
        let mut json = self.get_transactions_by_hash(hash).await?;
        let start = Instant::now();
        while json["type"] == "pending_transaction" {
            thread::sleep(time::Duration::from_secs(1));
            json = self.get_transactions_by_hash(hash).await?;
            let duration = start.elapsed();
            if duration > Duration::from_secs(15) {
                break;
            }
        }
        DevApiClient::confirm_successful_execution(&mut io::stdout(), &json, hash)
    }

    fn confirm_successful_execution<W>(writer: &mut W, json: &Value, hash: &str) -> Result<()>
    where
        W: Write,
    {
        if DevApiClient::is_execution_successful(json)? {
            return Ok(());
        }
        writeln!(writer, "{:#?}", json)?;
        Err(anyhow!(format!(
            "Transaction with hash {} didn't execute successfully",
            hash
        )))
    }

    fn is_execution_successful(json: &Value) -> Result<bool> {
        json["success"]
            .as_bool()
            .ok_or_else(|| anyhow!("Unable to access success key"))
    }

    pub fn get_hash_from_post_txn(json: Value) -> Result<String> {
        Ok(json["hash"].as_str().unwrap().to_string())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use serde_json::json;

    fn post_txn_json_output() -> Value {
        json!({
        "type":"pending_transaction",
        "hash":"0xbca2738726dc456f23762372ab0dd2f450ec3ec20271e5318ae37e9d42ee2bb8",
        "sender":"0x24163afcc6e33b0a9473852e18327fa9",
        "sequence_number":"10",
        "max_gas_amount":"1000000",
        "gas_unit_price":"0",
        "gas_currency_code":"XUS",
        "expiration_timestamp_secs":"1635872777",
        "payload":{}
        })
    }

    fn get_transactions_by_hash_json_output_success() -> Value {
        json!({
            "type":"user_transaction",
            "version":"3997",
            "hash":"0x89e59bb50521334a69c06a315b6dd191a8da4c1c7a40ce27a8f96f12959496eb",
            "state_root_hash":"0x7a0b81379ab8786f34fcff804e5fb413255467c28f09672e8d22bfaa4e029102",
            "event_root_hash":"0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
            "gas_used":"8",
            "success":true,
            "vm_status":"Executed successfully",
            "sender":"0x24163afcc6e33b0a9473852e18327fa9",
            "sequence_number":"14",
            "max_gas_amount":"1000000",
            "gas_unit_price":"0",
            "gas_currency_code":"XUS",
            "expiration_timestamp_secs":"1635873470",
            "payload":{}
        })
    }

    fn get_transactions_by_hash_json_output_fail() -> Value {
        json!({
            "type":"user_transaction",
            "version":"3997",
            "hash":"0xbad59bb50521334a69c06a315b6dd191a8da4c1c7a40ce27a8f96f12959496eb",
            "state_root_hash":"0x7a0b81379ab8786f34fcff804e5fb413255467c28f09672e8d22bfaa4e029102",
            "event_root_hash":"0x414343554d554c41544f525f504c414345484f4c4445525f4841534800000000",
            "gas_used":"8",
            "success":false,
            "vm_status":"miscellaneous error",
            "sender":"0x24163afcc6e33b0a9473852e18327fa9",
            "sequence_number":"14",
            "max_gas_amount":"1000000",
            "gas_unit_price":"0",
            "gas_currency_code":"XUS",
            "expiration_timestamp_secs":"1635873470",
            "payload":{}
        })
    }

    #[test]
    fn test_confirm_is_execution_successful() {
        let successful_txn = get_transactions_by_hash_json_output_success();
        assert_eq!(
            DevApiClient::is_execution_successful(&successful_txn).unwrap(),
            true
        );

        let failed_txn = get_transactions_by_hash_json_output_fail();
        assert_eq!(
            DevApiClient::is_execution_successful(&failed_txn).unwrap(),
            false
        );
    }

    #[test]
    fn test_get_hash_from_post_txn() {
        let txn = post_txn_json_output();
        let hash = DevApiClient::get_hash_from_post_txn(txn).unwrap();
        assert_eq!(
            hash,
            "0xbca2738726dc456f23762372ab0dd2f450ec3ec20271e5318ae37e9d42ee2bb8"
        );
    }

    #[test]
    fn test_print_confirmation_with_success_value() {
        let successful_txn = get_transactions_by_hash_json_output_success();
        let mut stdout = Vec::new();
        let good_hash = "0xbca2738726dc456f23762372ab0dd2f450ec3ec20271e5318ae37e9d42ee2bb8";

        DevApiClient::confirm_successful_execution(&mut stdout, &successful_txn, good_hash)
            .unwrap();
        assert_eq!(String::from_utf8(stdout).unwrap().as_str(), "".to_string());

        let failed_txn = get_transactions_by_hash_json_output_fail();
        let mut stdout = Vec::new();
        let bad_hash = "0xbad59bb50521334a69c06a315b6dd191a8da4c1c7a40ce27a8f96f12959496eb";
        assert_eq!(
            DevApiClient::confirm_successful_execution(&mut stdout, &failed_txn, bad_hash).is_err(),
            true
        );

        let fail_string = format!("{:#?}\n", &failed_txn);
        assert_eq!(String::from_utf8(stdout).unwrap().as_str(), fail_string)
    }

    #[test]
    fn test_parse_json_for_seq_num() {
        let value_obj = json!([{
            "type":"0x1::DiemAccount::DiemAccount",
            "data": {
                "authentication_key": "0x88cae30f0fea7879708788df9e7c9b7524163afcc6e33b0a9473852e18327fa9",
                "key_rotation_capability":{
                    "vec":[{"account_address":"0x24163afcc6e33b0a9473852e18327fa9"}]
                },
                "received_events":{
                    "counter":"0",
                    "guid":{}
                },
                "sent_events":{},
                "sequence_number":"3",
                "withdraw_capability":{
                    "vec":[{"account_address":"0x24163afcc6e33b0a9473852e18327fa9"}]
                }
            }
        }]);

        let ret_seq_num = DevApiClient::parse_json_for_account_seq_num(value_obj).unwrap();
        assert_eq!(ret_seq_num, 3);
    }

    #[test]
    fn test_check_response_status_code() {
        assert_eq!(
            DevApiClient::check_response_status_code(
                &StatusCode::from_u16(200).unwrap(),
                "Success"
            )
            .is_err(),
            false
        );
        assert_eq!(
            DevApiClient::check_response_status_code(&StatusCode::from_u16(404).unwrap(), "Failed")
                .is_err(),
            true
        );
    }

    #[test]
    fn test_response_context() {
        let failed_obj = json!({
            "code": 404,
            "message": "account not found by address(0x132412341234124) and ledger version(81)",
            "diem_ledger_version": "81"
        });
        let context = DevApiClient::response_context(
            "Failed to get account resources with provided address",
            &failed_obj,
        )
        .unwrap();

        let correct_string = format!("Failed to get account resources with provided address. Here is the json block for the response that failed:\n{:?}", failed_obj);
        assert_eq!(context, correct_string);
    }
}