arch_sdk 0.6.4

A Rust SDK for building applications on the Arch Network blockchain platform. Provides tools and interfaces for developing, testing, and deploying programs with native Bitcoin integration.
Documentation
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use crate::arch_program::pubkey::Pubkey;
use crate::client::error::{ArchError, Result};
use crate::client::transport::http::HttpClient;
use crate::client::transport::{RpcTransport, TcpClient};
use crate::{
    sign_message_bip322, AccountInfoWithPubkey, BlockTransactionFilter, Config, FullBlock,
    MAX_TX_BATCH_SIZE,
};
use arch_program::hash::Hash;
use bitcoin::key::Keypair;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{from_str, json, Value};
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant};

// Import the appropriate result types
use crate::types::{
    AccountFilter, AccountInfo, Block, ProcessedTransaction, ProgramAccount, RuntimeTransaction,
    Status,
};

pub const ACCOUNT_FUNDING_AMOUNT: u64 = 1_000_000;

// RPC method constants
pub const READ_ACCOUNT_INFO: &str = "read_account_info";
pub const GET_MULTIPLE_ACCOUNTS: &str = "get_multiple_accounts";
pub const SEND_TRANSACTION: &str = "send_transaction";
pub const SEND_TRANSACTIONS: &str = "send_transactions";
pub const GET_BLOCK: &str = "get_block";
pub const GET_FULL_BLOCK_WITH_TXIDS: &str = "get_full_block_with_txids";
pub const GET_BLOCK_BY_HEIGHT: &str = "get_block_by_height";
pub const GET_BLOCK_COUNT: &str = "get_block_count";
pub const GET_BLOCK_HASH: &str = "get_block_hash";
pub const GET_BEST_BLOCK_HASH: &str = "get_best_block_hash";
pub const GET_BEST_FINALIZED_BLOCK_HASH: &str = "get_best_finalized_block_hash";
pub const GET_PROCESSED_TRANSACTION: &str = "get_processed_transaction";
pub const GET_ACCOUNT_ADDRESS: &str = "get_account_address";
pub const GET_PROGRAM_ACCOUNTS: &str = "get_program_accounts";
pub const CHECK_PRE_ANCHOR_CONFLICT: &str = "check_pre_anchor_conflict";

/// Error code returned by the RPC server for "not found" responses
const RPC_NOT_FOUND_CODE: i64 = 404;

/// ArchRpcClient provides a simple interface for making RPC calls to the Arch blockchain
#[derive(Clone)]
pub struct ArchRpcClient {
    pub config: Config,
    transport: Arc<dyn RpcTransport>,
}

impl ArchRpcClient {
    /// Create a new ArchRpcClient with the specified URL
    pub fn new(config: &Config) -> Self {
        let http = HttpClient::new(config.arch_node_url.clone());
        Self {
            config: config.clone(),
            transport: Arc::new(http),
        }
    }

    /// Create a new ArchRpcClient with the specified TCP server address.
    pub fn new_tcp(config: &Config, addr: String) -> Result<Self> {
        let tcp = TcpClient::new(addr)?;
        Ok(Self {
            config: config.clone(),
            transport: Arc::new(tcp),
        })
    }

    /// Make a raw RPC call with no parameters and parse the result
    /// Returns None if the item was not found (404)
    pub async fn call_method<R: DeserializeOwned>(&self, method: &str) -> Result<Option<R>> {
        match self.process_result(self.post(method).await?)? {
            Some(value) => {
                let result = serde_json::from_value(value).map_err(|e| {
                    ArchError::ParseError(format!("Failed to deserialize response: {}", e))
                })?;
                Ok(Some(result))
            }
            None => Ok(None),
        }
    }

    /// Make a raw RPC call with parameters and parse the result
    /// Returns None if the item was not found (404)
    pub async fn call_method_with_params<T: Serialize + std::fmt::Debug, R: DeserializeOwned>(
        &self,
        method: &str,
        params: T,
    ) -> Result<Option<R>> {
        match self.process_result(self.post_data(method, params).await?)? {
            Some(value) => {
                let result = serde_json::from_value(value).map_err(|e| {
                    ArchError::ParseError(format!("Failed to deserialize response: {}", e))
                })?;
                Ok(Some(result))
            }
            None => Ok(None),
        }
    }

    /// Get raw value from a method call
    /// Returns None if the item was not found (404)
    pub async fn call_method_raw(&self, method: &str) -> Result<Option<Value>> {
        self.process_result(self.post(method).await?)
    }

    /// Get raw value from a method call with parameters
    /// Returns None if the item was not found (404)
    pub async fn call_method_with_params_raw<T: Serialize + std::fmt::Debug>(
        &self,
        method: &str,
        params: T,
    ) -> Result<Option<Value>> {
        self.process_result(self.post_data(method, params).await?)
    }

    /// Read account information for the specified public key
    pub async fn read_account_info(&self, pubkey: Pubkey) -> Result<AccountInfo> {
        match self
            .call_method_with_params(READ_ACCOUNT_INFO, pubkey)
            .await?
        {
            Some(info) => Ok(info),
            None => Err(ArchError::NotFound(format!(
                "Account not found for pubkey: {}",
                pubkey
            ))),
        }
    }

    /// Read account information for multiple public keys
    pub async fn get_multiple_accounts(
        &self,
        pubkeys: Vec<Pubkey>,
    ) -> Result<Vec<Option<AccountInfoWithPubkey>>> {
        match self
            .call_method_with_params(GET_MULTIPLE_ACCOUNTS, pubkeys.clone())
            .await?
        {
            Some(info) => Ok(info),
            None => Err(ArchError::NotFound(format!(
                "Accounts not found for pubkeys: {:?}",
                pubkeys
            ))),
        }
    }

    /// Request an airdrop for a given public key
    pub async fn request_airdrop(&self, pubkey: Pubkey) -> Result<ProcessedTransaction> {
        let result = self
            .process_result(self.post_data("request_airdrop", pubkey).await?)?
            .ok_or(ArchError::RpcRequestFailed(
                "request_airdrop failed".to_string(),
            ))?;

        // Handle the result parsing with proper error handling
        let txid_str = result.as_str().ok_or_else(|| {
            ArchError::ParseError("Failed to get transaction ID as string".to_string())
        })?;

        let txid = Hash::from_str(txid_str)?;
        let processed_tx = self.wait_for_processed_transaction(&txid).await?;
        Ok(processed_tx)
    }

    /// Create an account with lamports
    pub async fn create_and_fund_account_with_faucet(&self, keypair: &Keypair) -> Result<()> {
        let pubkey = Pubkey::from_slice(&keypair.x_only_public_key().0.serialize());

        if self.read_account_info(pubkey).await.is_ok() {
            let _processed_tx = self.request_airdrop(pubkey).await?;
        } else {
            let result = self
                .process_result(self.post_data("create_account_with_faucet", pubkey).await?)?
                .ok_or(ArchError::RpcRequestFailed(
                    "create_account_with_faucet failed".to_string(),
                ))?;
            let mut runtime_tx: RuntimeTransaction = serde_json::from_value(result)?;

            let message_hash = runtime_tx.message.hash();
            let signature = crate::Signature::from(sign_message_bip322(
                keypair,
                &message_hash,
                self.config.network,
            )?);

            runtime_tx.signatures.push(signature);

            let result = self.send_transaction(runtime_tx).await?;

            let _processed_tx = self.wait_for_processed_transaction(&result).await?;
        }
        let account_info = self.read_account_info(pubkey).await?;

        // assert_eq!(account_info.owner, Pubkey::system_program());
        assert!(account_info.lamports >= ACCOUNT_FUNDING_AMOUNT);

        Ok(())
    }

    /// Get a processed transaction by ID
    pub async fn get_processed_transaction(
        &self,
        tx_id: &Hash,
    ) -> Result<Option<ProcessedTransaction>> {
        self.call_method_with_params(GET_PROCESSED_TRANSACTION, tx_id.to_string())
            .await
    }

    /// Get a block with its transactions by ID
    pub async fn get_full_block_with_txids(
        &self,
        block_id: &Hash,
    ) -> Result<(Block, Vec<ProcessedTransaction>)> {
        match self
            .call_method_with_params(GET_FULL_BLOCK_WITH_TXIDS, block_id.to_string())
            .await?
        {
            Some(info) => Ok(info),
            None => Err(ArchError::NotFound(format!(
                "Block with txids not found for block id: {}",
                block_id
            ))),
        }
    }

    /// Waits for a transaction to be processed, polling until it reaches "Processed" or "Failed" status.
    /// Will timeout after 60 seconds of wall-clock time.
    pub async fn wait_for_processed_transaction(
        &self,
        tx_id: &Hash,
    ) -> Result<ProcessedTransaction> {
        let timeout = Duration::from_secs(60);
        let start = Instant::now();
        let poll_interval = Duration::from_secs(1);

        // First try to get the transaction, retry if null
        let mut tx = match self.get_processed_transaction(tx_id).await {
            Ok(Some(tx)) => tx,
            Ok(None) => {
                // Transaction not found, start polling
                loop {
                    tokio::time::sleep(poll_interval).await;
                    if start.elapsed() >= timeout {
                        return Err(ArchError::TimeoutError(format!(
                            "Failed to retrieve processed transaction {} after 60 seconds",
                            tx_id
                        )));
                    }
                    match self.get_processed_transaction(tx_id).await? {
                        Some(tx) => break tx,
                        None => continue,
                    }
                }
            }
            Err(e) => return Err(e),
        };

        // Now wait for the transaction to finish processing
        while !is_transaction_finalized(&tx) {
            tokio::time::sleep(poll_interval).await;
            if start.elapsed() >= timeout {
                return Err(ArchError::TimeoutError(format!(
                    "Transaction {} did not reach final status after 60 seconds",
                    tx_id
                )));
            }
            match self.get_processed_transaction(tx_id).await? {
                Some(updated_tx) => {
                    tx = updated_tx;
                }
                None => {
                    return Err(ArchError::TransactionError(format!(
                        "Transaction {} disappeared after being found",
                        tx_id
                    )));
                }
            }
        }

        Ok(tx)
    }

    /// Waits for multiple transactions to be processed, showing progress with a progress bar
    /// Returns a vector of processed transactions in the same order as the input transaction IDs
    pub async fn wait_for_processed_transactions(
        &self,
        tx_ids: Vec<Hash>,
    ) -> Result<Vec<ProcessedTransaction>> {
        let mut processed_transactions: Vec<ProcessedTransaction> =
            Vec::with_capacity(tx_ids.len());

        for tx_id in tx_ids {
            match self.wait_for_processed_transaction(&tx_id).await {
                Ok(tx) => processed_transactions.push(tx),
                Err(e) => {
                    return Err(ArchError::TransactionError(format!(
                        "Failed to process transaction {}: {}",
                        tx_id, e
                    )))
                }
            }
        }

        Ok(processed_transactions)
    }

    /// Get the best block hash
    pub async fn get_best_block_hash(&self) -> Result<Hash> {
        self.fetch_hash(GET_BEST_BLOCK_HASH, "best block hash")
            .await
    }

    /// Get the best finalized block hash
    pub async fn get_best_finalized_block_hash(&self) -> Result<Hash> {
        self.fetch_hash(GET_BEST_FINALIZED_BLOCK_HASH, "best finalized block hash")
            .await
    }

    /// Shared helper: call a no-params RPC method and parse the result as a Hash
    async fn fetch_hash(&self, method: &str, description: &str) -> Result<Hash> {
        match self.call_method_raw(method).await? {
            Some(value) => {
                let hash_str = value.as_str().ok_or_else(|| {
                    ArchError::ParseError(format!("Failed to get {}", description))
                })?;
                Ok(Hash::from_str(hash_str)?)
            }
            None => Err(ArchError::NotFound(format!("{} not found", description))),
        }
    }

    /// Get the block hash for a given height
    pub async fn get_block_hash(&self, block_height: u64) -> Result<String> {
        match self
            .call_method_with_params_raw(GET_BLOCK_HASH, block_height)
            .await?
        {
            Some(value) => value.as_str().map(|s| s.to_string()).ok_or_else(|| {
                ArchError::ParseError("Failed to get block hash as string".to_string())
            }),
            None => Err(ArchError::NotFound(format!(
                "Block hash not found for height: {}",
                block_height
            ))),
        }
    }

    /// Get the current block count
    pub async fn get_block_count(&self) -> Result<u64> {
        match self.call_method(GET_BLOCK_COUNT).await? {
            Some(count) => Ok(count),
            None => Err(ArchError::NotFound("Block count not found".to_string())),
        }
    }

    /// Get block by hash with signatures only
    pub async fn get_block_by_hash(&self, block_hash: &str) -> Result<Option<Block>> {
        // For signatures only, we can just pass the block hash directly
        self.call_method_with_params(GET_BLOCK, block_hash).await
    }

    /// Get full block by hash with complete transaction details
    pub async fn get_full_block_by_hash(&self, block_hash: &str) -> Result<Option<FullBlock>> {
        self.fetch_full_block(GET_BLOCK, block_hash).await
    }

    /// Get block by height with signatures only
    pub async fn get_block_by_height(&self, block_height: u64) -> Result<Option<Block>> {
        self.call_method_with_params(GET_BLOCK_BY_HEIGHT, block_height)
            .await
    }

    /// Get full block by height with complete transaction details
    pub async fn get_full_block_by_height(&self, block_height: u64) -> Result<Option<FullBlock>> {
        self.fetch_full_block(GET_BLOCK_BY_HEIGHT, block_height)
            .await
    }

    /// Shared helper: fetch a full block with a given key (hash or height)
    async fn fetch_full_block<T: Serialize>(
        &self,
        method: &str,
        key: T,
    ) -> Result<Option<FullBlock>> {
        let params = vec![
            serde_json::to_value(key)?,
            serde_json::to_value(BlockTransactionFilter::Full)?,
        ];
        match self.process_result(self.post_data(method, params).await?)? {
            Some(value) => {
                let result = serde_json::from_value(value).map_err(|e| {
                    ArchError::ParseError(format!("Failed to deserialize FullBlock: {}", e))
                })?;
                Ok(Some(result))
            }
            None => Ok(None),
        }
    }

    /// Get account address for a public key
    pub async fn get_account_address(&self, pubkey: &Pubkey) -> Result<String> {
        match self.process_result(
            self.post_data(GET_ACCOUNT_ADDRESS, pubkey.serialize())
                .await?,
        )? {
            Some(value) => value.as_str().map(|s| s.to_string()).ok_or_else(|| {
                ArchError::ParseError("Failed to get account address as string".to_string())
            }),
            None => Err(ArchError::NotFound(format!(
                "Account address not found for pubkey: {}",
                pubkey
            ))),
        }
    }

    /// Get program accounts for a given program ID
    pub async fn get_program_accounts(
        &self,
        program_id: &Pubkey,
        filters: Option<Vec<AccountFilter>>,
    ) -> Result<Vec<ProgramAccount>> {
        // Format params as [program_id, filters]
        let params = json!([program_id.serialize(), filters]);
        match self
            .call_method_with_params(GET_PROGRAM_ACCOUNTS, params)
            .await?
        {
            Some(accounts) => Ok(accounts),
            None => Err(ArchError::NotFound(format!(
                "Program accounts not found for program ID: {}",
                program_id
            ))),
        }
    }

    pub async fn check_pre_anchor_conflict(&self, accounts: Vec<Pubkey>) -> Result<bool> {
        let params = accounts;
        match self
            .call_method_with_params(CHECK_PRE_ANCHOR_CONFLICT, params)
            .await?
        {
            Some(result) => Ok(result),
            None => Err(ArchError::RpcRequestFailed(
                "check_pre_anchor_conflict returned no result".to_string(),
            )),
        }
    }

    /// Get the network pubkey from the network
    pub async fn get_network_pubkey(&self) -> Result<String> {
        match self.call_method::<String>("get_network_pubkey").await? {
            Some(key) => Ok(key),
            None => Err(ArchError::NotFound("Network pubkey not found".to_string())),
        }
    }

    /// Send a single transaction
    pub async fn send_transaction(&self, transaction: RuntimeTransaction) -> Result<Hash> {
        match self.process_result(self.post_data(SEND_TRANSACTION, transaction).await?)? {
            Some(value) => {
                let tx_id_str = value.as_str().ok_or_else(|| {
                    ArchError::ParseError("Failed to get transaction ID as string".to_string())
                })?;
                Ok(Hash::from_str(tx_id_str)?)
            }
            None => Err(ArchError::TransactionError(
                "Failed to send transaction".to_string(),
            )),
        }
    }

    /// Send multiple transactions
    pub async fn send_transactions(
        &self,
        transactions: Vec<RuntimeTransaction>,
    ) -> Result<Vec<Hash>> {
        if transactions.len() > MAX_TX_BATCH_SIZE {
            return Err(ArchError::TransactionError(
                "Batch size exceeds maximum".to_string(),
            ));
        }

        match self
            .call_method_with_params::<Vec<RuntimeTransaction>, Vec<String>>(
                SEND_TRANSACTIONS,
                transactions,
            )
            .await?
        {
            Some(tx_ids) => {
                let mut parsed_tx_ids = Vec::new();
                for id in tx_ids {
                    let hash = Hash::from_str(&id)?;
                    parsed_tx_ids.push(hash);
                }
                Ok(parsed_tx_ids)
            }
            None => Err(ArchError::TransactionError(
                "Failed to send transactions".to_string(),
            )),
        }
    }

    /// Helper methods for RPC communication
    pub fn process_result(&self, response: String) -> Result<Option<Value>> {
        let result = from_str::<Value>(&response)
            .map_err(|e| ArchError::ParseError(format!("Failed to parse JSON: {}", e)))?;

        let result = match result {
            Value::Object(object) => object,
            _ => {
                return Err(ArchError::ParseError(
                    "Unexpected JSON structure".to_string(),
                ))
            }
        };

        if let Some(err) = result.get("error") {
            if let Value::Object(err_obj) = err {
                if let (Some(Value::Number(code)), Some(Value::String(message))) =
                    (err_obj.get("code"), err_obj.get("message"))
                {
                    if code.as_i64() == Some(RPC_NOT_FOUND_CODE) {
                        return Ok(None);
                    }
                    return Err(ArchError::RpcRequestFailed(format!(
                        "Code: {}, Message: {}",
                        code, message
                    )));
                }
            }
            return Err(ArchError::RpcRequestFailed(format!("{:?}", err)));
        }

        Ok(Some(result["result"].clone()))
    }

    async fn post(&self, method: &str) -> Result<String> {
        let json = json!({
            "jsonrpc": "2.0",
            "id": "curlycurl",
            "method": method,
        });
        self.transport.call(&json).await
    }

    pub async fn post_data<T: Serialize + std::fmt::Debug>(
        &self,
        method: &str,
        params: T,
    ) -> Result<String> {
        let json = json!({
            "jsonrpc": "2.0",
            "id": "curlycurl",
            "method": method,
            "params": params,
        });
        self.transport.call(&json).await
    }
}

/// Helper function to check if a transaction has reached a final status
pub(crate) fn is_transaction_finalized(tx: &ProcessedTransaction) -> bool {
    matches!(&tx.status, Status::Processed | Status::Failed(_))
}