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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use super::account::Account;
use super::rpc_config::*;
use super::rpc_request::RpcRequest;
use super::rpc_response::*;

use anyhow::bail;
use borsh::BorshDeserialize;
use log::debug;
use reqwest::header::CONTENT_TYPE;
use serde::de::DeserializeOwned;
use serde_json::json;
use solana_program::pubkey::Pubkey;
use solana_sdk::clock::{Slot, UnixTimestamp};
use solana_sdk::hash::Hash;
use solana_sdk::signature::Signature;
use solana_sdk::transaction::Transaction;

use std::str::FromStr;

/// Specifies which Solana cluster will be queried by the client.
#[derive(Clone, Copy, Debug)]
pub enum Net {
    Localhost,
    Testnet,
    Devnet,
    Mainnet,
}

impl Net {
    pub fn to_url(&self) -> &str {
        match self {
            Self::Localhost => "http://localhost:8899",
            Self::Testnet => "https://api.testnet.solana.com",
            Self::Devnet => "https://api.devnet.solana.com",
            Self::Mainnet => "https://api.mainnet-beta.solana.com",
        }
    }
}

pub type ClientResult<T> = Result<T, anyhow::Error>;

/// An async client to make rpc requests to the Solana blockchain.
pub struct RpcClient {
    client: reqwest::Client,
    config: RpcConfig,
    net: Net,
    request_id: u64,
}

impl RpcClient {
    pub fn new_with_config(net: Net, config: RpcConfig) -> Self {
        Self {
            client: reqwest::Client::new(),
            config,
            net,
            request_id: 0,
        }
    }

    pub fn new(net: Net) -> Self {
        let config = RpcConfig {
            encoding: Some(Encoding::JsonParsed),
            commitment: Some(CommitmentLevel::Confirmed),
        };
        Self::new_with_config(net, config)
    }

    pub fn set_commitment(&mut self, commitment: Option<CommitmentLevel>) {
        self.config.commitment = commitment;
    }

    async fn send<T: DeserializeOwned, R: Into<reqwest::Body>>(
        &mut self,
        request: R,
    ) -> reqwest::Result<T> {
        self.request_id = self.request_id.wrapping_add(1);
        let response = self
            .client
            .post(self.net.to_url())
            .header(CONTENT_TYPE, "application/json")
            .body(request)
            .send()
            .await?;

        response.json::<T>().await
    }

    /// Returns the decoded contents of a Solana account.
    pub async fn get_account(&mut self, account_pubkey: &Pubkey) -> ClientResult<Account> {
        let request = RpcRequest::GetAccountInfo
            .build_request_json(
                self.request_id,
                json!([account_pubkey.to_string(), self.config]),
            )
            .to_string();
        let response: RpcResponse<RpcResultWithContext<Account>> = self.send(request).await?;
        Ok(response.result.value)
        //let response: serde_json::Value = self.send(request).await?;
        //println!("{:#?}", response);
        //todo!();
    }

    /// Attempts to deserialize the contents of an account's data field into a
    /// given type using the Borsh deserialization framework.
    pub async fn get_and_deserialize_account_data<T: BorshDeserialize>(
        &mut self,
        account_pubkey: &Pubkey,
    ) -> ClientResult<T> {
        let account = self.get_account(account_pubkey).await?;
        account.data.parse_into_borsh::<T>()
    }

    /// Attempts to deserialize the contents of an account's data field into a
    /// given type using the Json deserialization framework.
    pub async fn get_and_deserialize_parsed_account_data<T: DeserializeOwned>(
        &mut self,
        account_pubkey: &Pubkey,
    ) -> ClientResult<T> {
        let account = self.get_account(account_pubkey).await?;
        account.data.parse_into_json::<T>()
    }

    /// Returns the owner of the account.
    pub async fn get_owner(&mut self, account_pubkey: &Pubkey) -> ClientResult<Pubkey> {
        let account = self.get_account(account_pubkey).await?;
        let pubkey_bytes = bs58::decode(account.owner).into_vec()?;
        Ok(Pubkey::new(&pubkey_bytes))
    }

    /// Returns the balance (in lamports) of the account.
    pub async fn get_balance(&mut self, account_pubkey: &Pubkey) -> ClientResult<u64> {
        let request = RpcRequest::GetBalance
            .build_request_json(
                self.request_id,
                json!([account_pubkey.to_string(), self.config,]),
            )
            .to_string();

        let response: RpcResponse<RpcResultWithContext<u64>> = self.send(request).await?;
        Ok(response.result.value)
    }

    /// Returns the minimum balance (in Lamports) required for an account to be rent exempt.
    pub async fn get_minimum_balance_for_rent_exemption(
        &mut self,
        data_len: usize,
    ) -> ClientResult<u64> {
        let request = RpcRequest::GetMinimumBalanceForRentExemption
            .build_request_json(self.request_id, json!([data_len]))
            .to_string();

        let response: RpcResponse<u64> = self.send(request).await?;
        Ok(response.result)
    }

    /// Requests an airdrop of lamports to a given account.
    pub async fn request_airdrop(
        &mut self,
        pubkey: &Pubkey,
        lamports: u64,
        recent_blockhash: &Hash,
    ) -> ClientResult<Signature> {
        let config = RpcRequestAirdropConfig {
            recent_blockhash: Some(recent_blockhash.to_string()),
            commitment: self.config.commitment.clone(),
        };
        let request = RpcRequest::RequestAirdrop
            .build_request_json(
                self.request_id,
                json!([pubkey.to_string(), lamports, config]),
            )
            .to_string();

        let response: RpcResponse<String> = self.send(request).await?;

        let signature = Signature::from_str(&response.result)?;
        Ok(signature)
    }

    /// Returns latest blockhash.
    pub async fn get_latest_blockhash(&mut self) -> ClientResult<Hash> {
        // TODO for some reason latest blockhash returns method not found
        // even though we are using 1.9.0 and the rpc servers are also updated
        let request = RpcRequest::GetRecentBlockhash
            .build_request_json(self.request_id, json!([self.config]))
            .to_string();

        let response: RpcResponse<RpcResultWithContext<Blockhash>> = self.send(request).await?;
        let blockhash = Hash::from_str(&response.result.value.blockhash)?;
        Ok(blockhash)
    }

    /// Attempts to send a signed transaction to the ledger without simulating
    /// it first.
    ///
    /// It is a bit faster, but no logs or confirmation is returned because the
    /// transaction is not simulated.
    pub async fn send_transaction_unchecked(
        &mut self,
        transaction: &Transaction,
    ) -> ClientResult<Signature> {
        let config = RpcTransactionConfig {
            skip_preflight: true,
            preflight_commitment: Some(CommitmentLevel::Processed),
            encoding: Some(Encoding::Base64),
        };
        self.send_transaction_with_config(transaction, &config)
            .await
    }

    pub async fn send_transaction(&mut self, transaction: &Transaction) -> ClientResult<Signature> {
        let config = RpcTransactionConfig {
            skip_preflight: false,
            preflight_commitment: self.config.commitment.clone(),
            encoding: Some(Encoding::Base64),
        };
        self.send_transaction_with_config(transaction, &config)
            .await
    }

    pub async fn send_transaction_with_config(
        &mut self,
        transaction: &Transaction,
        config: &RpcTransactionConfig,
    ) -> ClientResult<Signature> {
        let serialized = bincode::serialize(transaction)?;
        let encoded = base64::encode(serialized);
        let request = RpcRequest::SendTransaction
            .build_request_json(self.request_id, json!([encoded, config]))
            .to_string();

        match self.send::<serde_json::Value, String>(request).await {
            Ok(json_value) => {
                if let Ok(response) =
                    serde_json::from_value::<RpcResponse<String>>(json_value.clone())
                {
                    let signature = Signature::from_str(&response.result)?;
                    Ok(signature)
                } else if let Ok(tx_error) =
                    serde_json::from_value::<RpcResponse<RpcTransactionError>>(json_value)
                {
                    tx_error
                        .result
                        .data
                        .logs
                        .iter()
                        .enumerate()
                        .for_each(|(i, log)| debug!("{} {}", i, log));
                    bail!("{}", tx_error.result.message);
                } else {
                    bail!("failed to parse RPC response")
                }
            }
            Err(err) => bail!(err),
        }
    }

    pub async fn get_slot(&mut self) -> ClientResult<Slot> {
        let request = RpcRequest::GetSlot
            .build_request_json(self.request_id, json!([self.config]))
            .to_string();

        let response: RpcResponse<Slot> = self.send(request).await?;
        Ok(response.result)
    }

    pub async fn get_block_time(&mut self, slot: Slot) -> ClientResult<UnixTimestamp> {
        let request = RpcRequest::GetBlockTime
            .build_request_json(self.request_id, json!([slot]))
            .to_string();

        let response: RpcResponse<UnixTimestamp> = self.send(request).await?;
        Ok(response.result)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::account::{ProgramAccount, TokenAccount};
    use solana_sdk::signer::keypair::Keypair;
    use solana_sdk::signer::Signer;
    use solana_sdk::system_transaction::transfer;

    use std::time::{Duration, SystemTime, UNIX_EPOCH};

    #[rustfmt::skip]
    const ALICE: &[u8] = &[
        57,99,241,156,126,127,97,60,
        40,14,39,4,115,72,39,75,
        2,14,30,255,45,79,195,202,
        132,18,131,180,61,12,87,183,
        14,175,192,115,62,33,136,190,
        244,254,192,174,2,126,227,113,
        222,42,224,89,36,89,239,167,
        22,150,31,29,89,188,176,162
    ];

    #[rustfmt::skip]
    const BOB: &[u8] = &[
        176,252,96,172,240,61,215,84,
        138,250,147,178,208,59,227,60,
        190,204,80,88,55,137,236,252,
        231,118,253,64,65,106,39,5,
        14,212,250,187,124,127,43,205,
        30,117,63,227,13,218,202,68,
        160,161,52,12,59,211,152,183,
        119,140,213,205,174,210,108,128
    ];

    const AIRDROP_AMOUNT: u64 = 5500; // tx free of 5000 lamports included
    const TRANSFER_AMOUNT: u64 = 250;

    async fn wait_for_balance_change(
        client: &mut RpcClient,
        account: &Pubkey,
        balance_before: u64,
        expected_change: u64,
    ) {
        let mut i = 0;
        let max_loops = 60;
        loop {
            let balance_after = client.get_balance(account).await.unwrap();
            // NOTE might happen that alice is airdropped only after she
            // transferred the amount to BOB
            match balance_after.checked_sub(balance_before) {
                Some(0) => {
                    std::thread::sleep(std::time::Duration::from_secs(1));
                    i += 1;
                    dbg!(i);
                }
                Some(delta) => {
                    assert_eq!(delta, expected_change);
                    break;
                }
                None => {
                    assert_eq!(balance_before - balance_after, expected_change);
                    break;
                }
            }
            if i == max_loops {
                panic!("test was running for {} seconds", max_loops);
            }
        }
    }

    #[tokio::test]
    async fn airdrop_and_transfer() {
        let alice = Keypair::from_bytes(ALICE).unwrap();
        let bob = Keypair::from_bytes(BOB).unwrap();
        let mut client = RpcClient::new(Net::Devnet);

        let balance_before_airdrop_alice = client.get_balance(&alice.pubkey()).await.unwrap();
        let latest_blockhash = client.get_latest_blockhash().await.unwrap();

        client
            .request_airdrop(&alice.pubkey(), AIRDROP_AMOUNT, &latest_blockhash)
            .await
            .unwrap();

        wait_for_balance_change(
            &mut client,
            &alice.pubkey(),
            balance_before_airdrop_alice,
            AIRDROP_AMOUNT,
        )
        .await;

        let balance_before_bob = client.get_balance(&bob.pubkey()).await.unwrap();

        let recent_blockhash = client.get_latest_blockhash().await.unwrap();
        let transfer_tx = transfer(&alice, &bob.pubkey(), TRANSFER_AMOUNT, recent_blockhash);
        client.send_transaction(&transfer_tx).await.unwrap();

        wait_for_balance_change(
            &mut client,
            &bob.pubkey(),
            balance_before_bob,
            TRANSFER_AMOUNT,
        )
        .await;

        wait_for_balance_change(
            &mut client,
            &alice.pubkey(),
            balance_before_airdrop_alice,
            TRANSFER_AMOUNT, // also losing the 5000 lamport fee
        )
        .await;
    }

    #[tokio::test]
    async fn block_time() {
        // TODO compare results with solana_client's, once they use
        // spl_token 3.3.0
        let mut client = RpcClient::new(Net::Mainnet);
        for _ in 0..10 {
            let slot = client.get_slot().await.unwrap();
            let block_time = client.get_block_time(slot).await.unwrap();
            let time = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs() as i64;
            let delta_time = (time - block_time) as f32;
            assert!(delta_time.abs() < 60.0); // we are within one minute
            std::thread::sleep(Duration::from_secs(1));
        }
    }

    #[tokio::test]
    async fn get_spl_token_program() {
        let mut client = RpcClient::new(Net::Mainnet);
        client.set_commitment(Some(CommitmentLevel::Processed));
        let pubkey_bytes = bs58::decode("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
            .into_vec()
            .unwrap();
        let token_program_id = Pubkey::new(&pubkey_bytes);

        let account = client.get_account(&token_program_id).await.unwrap();
        assert_eq!(account.owner, "BPFLoader2111111111111111111111111111111111");
        assert!(account.executable);
    }

    #[test]
    fn commitment_change() {
        let config = RpcConfig {
            encoding: Some(Encoding::JsonParsed),
            commitment: None,
        };
        let mut client = RpcClient::new_with_config(Net::Mainnet, config);
        assert!(client.config.commitment.is_none());
        client.set_commitment(Some(CommitmentLevel::Processed));
        assert_eq!(client.config.commitment, Some(CommitmentLevel::Processed));
    }

    #[tokio::test]
    async fn mint_and_token_account() {
        let mut client = RpcClient::new(Net::Mainnet);
        // get NFT mint account from gold.xyz "teletubbies" auction
        let mint_pubkey = Pubkey::new(
            &bs58::decode("B2Kdr5MCJLxJZU1Ek91c6cAkxe1FgFTwEXG6y7cQ9gU7")
                .into_vec()
                .unwrap(),
        );
        let mint = client
            .get_and_deserialize_parsed_account_data::<TokenAccount>(&mint_pubkey)
            .await
            .unwrap();
        let mint_info = if let TokenAccount::Mint(mint_info) = mint {
            mint_info
        } else {
            panic!("should be mint account");
        };
        assert_eq!(mint_info.decimals, 0);
        assert_eq!(mint_info.supply.parse::<u8>().unwrap(), 1);
        // get NFT token account from gold.xyz "teletubbies" auction
        let token_account_pubkey = Pubkey::new(
            &bs58::decode("6xrSzvKGBux6FHZdRuKwrWwHxCcwdgfTVFVUaiPbsmSR")
                .into_vec()
                .unwrap(),
        );
        let token_account = client
            .get_and_deserialize_parsed_account_data::<TokenAccount>(&token_account_pubkey)
            .await
            .unwrap();

        let token_acc_info = if let TokenAccount::Account(account_info) = token_account {
            account_info
        } else {
            panic!("should be token account");
        };
        assert_eq!(token_acc_info.mint, mint_pubkey.to_string())
    }

    #[tokio::test]
    async fn deserialize_go1d_account() {
        let mut client = RpcClient::new(Net::Mainnet);
        let gold_pubkey = Pubkey::new(
            &bs58::decode("go1dcKcvafq8SDwmBKo6t2NVzyhvTEZJkMwnnfae99U")
                .into_vec()
                .unwrap(),
        );

        let gold_acc = client
            .get_and_deserialize_parsed_account_data::<ProgramAccount>(&gold_pubkey)
            .await
            .unwrap();

        if let ProgramAccount::Program(_program) = gold_acc {
        } else {
            panic!("should be a program account");
        }
    }

    #[derive(BorshDeserialize)]
    struct GoldContractBankState {
        admin: Pubkey,
        wd_auth: Pubkey,
    }

    #[tokio::test]
    async fn get_borsh_serialized_account_data() {
        let mut client = RpcClient::new(Net::Mainnet);
        let contract_pubkey = Pubkey::new(
            &bs58::decode("21d8ssndpeW5mw1EMqVZRNHnJhUfuWkKL7QomWF87LBK")
                .into_vec()
                .unwrap(),
        );
        let contract_state = client
            .get_and_deserialize_account_data::<GoldContractBankState>(&contract_pubkey)
            .await
            .unwrap();

        assert_eq!(
            contract_state.admin.to_string(),
            "gcadHFMc51A2fFzppTQ6DgmLNymatHjGwENZSkJpJNr"
        );
        assert_ne!(contract_state.admin, contract_state.wd_auth);
    }
}