jib 0.10.0

Jib is a library for the Solana blockchain that lets you efficiently pack instructions into transactions and submit them via a TPU client.
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
//! # Jib
//!
//! Jib is a simple Rust library that efficiently packs a vector of Solana instructions into maximum size and account length transactions
//! and then sends them over the network. It can be used with a RPC node to send the transactions async using Tokio green threads or with a TPU client
//! which will send the transactions to the current leader and confirm them. It also has the ability to retry failed transactions and pack them into new transactions.
//!
//! ## Example Usage
//!
//! In this example we load a list of mint accounts we want to update the metadata for, and then we create a vector of instructions with the update_metadata_accounts_v2 instruction for each mint.
//! We then pass this vector of instructions to Jib and it will pack them into the most efficient transactions possible and send them to the network.
//!
//! ```ignore
//! fn main() -> Result<()> {
//!     // Load our keypair file.
//!     let keypair = solana_sdk::signature::read_keypair_file("keypair.json").unwrap();
//!
//!     // Initialize Jib with our keypair and desired network.
//!     let mut jib = Jib::new(vec![keypair], "https://frosty-forest-fields.solana-mainnet.quiknode.pro")?;
//!
//!     let mut instructions = vec![];
//!
//!     // Load mint addresses from a file.
//!     let addresses: Vec<String> =
//!         serde_json::from_reader(std::fs::File::open("collection_mints.json")?)?;
//!
//!     // Create an instruction for each mint.
//!     for address in addresses {
//!         let metadata = derive_metadata_pda(&Pubkey::from_str(&address).unwrap());
//!
//!         let ix = update_metadata_accounts_v2(
//!             mpl_token_metadata::ID,
//!             metadata,
//!             // Jib takes the payer by value but we can access it via this fn.
//!             jib.payer().pubkey(),
//!             None,
//!             None,
//!             Some(true),
//!             None,
//!         );
//!
//!         instructions.push(ix);
//!     }
//!
//!     // Set the instructions to be executed.
//!     jib.set_instructions(instructions);
//!
//!     // Run it.
//!     let results = jib.hoist()?;
//!
//!     // Do something with the results.
//!     for result in results {
//!         if result.is_success() {
//!             println!("Success: {}", result.signature().unwrap());
//!         } else {
//!             println!("Failure: {}", result.error().unwrap());
//!         }
//!     }
//!
//!     Ok(())
//! }
//! ```

use std::{str::FromStr, sync::Arc, time::Duration};

#[cfg(feature = "tpu")]
use {
    indicatif::{MultiProgress, ProgressStyle},
    solana_client::{
        rpc_client::RpcClient as BlockingRpcClient,
        rpc_request::MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS,
        tpu_client::{TpuClient, TpuClientConfig, TpuSenderError},
    },
    solana_quic_client::{QuicConfig, QuicConnectionManager, QuicPool},
    solana_sdk::signers::Signers,
    std::{collections::HashMap, thread::sleep, time::Instant},
};

use futures_util::{lock::Mutex, stream::FuturesOrdered, StreamExt};
use indicatif::ProgressBar;
use ratelimit::Ratelimiter;
use serde::{Deserialize, Serialize};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
    commitment_config::CommitmentConfig, compute_budget::ComputeBudgetInstruction,
    instruction::Instruction, message::Message, signature::Keypair, signature::Signature,
    signer::Signer, transaction::Transaction,
};
use tracing::debug;

mod error;

use error::JibError;

const MAX_TX_LEN: usize = 1232;

const TX_BATCH_SIZE: usize = 10;

// Send at ~100 TPS
#[cfg(feature = "tpu")]
const SEND_TRANSACTION_INTERVAL: Duration = Duration::from_millis(10);
// Retry batch send after 4 seconds
#[cfg(feature = "tpu")]
const TRANSACTION_RESEND_INTERVAL: Duration = Duration::from_secs(4);

/// The Network enum is used to set the RPC URL to use for finding the current leader.
/// The default value is Devnet.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Network {
    #[default]
    Devnet,
    MainnetBeta,
    Testnet,
    Localnet,
}

impl FromStr for Network {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "devnet" => Ok(Network::Devnet),
            "mainnet" => Ok(Network::MainnetBeta),
            "testnet" => Ok(Network::Testnet),
            "localnet" => Ok(Network::Localnet),
            _ => Err(()),
        }
    }
}

impl std::fmt::Display for Network {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Network::Devnet => write!(f, "devnet"),
            Network::MainnetBeta => write!(f, "mainnet"),
            Network::Testnet => write!(f, "testnet"),
            Network::Localnet => write!(f, "localnet"),
        }
    }
}

impl Network {
    pub fn url(&self) -> &'static str {
        match self {
            Network::Devnet => "https://api.devnet.solana.com",
            Network::MainnetBeta => "https://api.mainnet-beta.solana.com",
            Network::Testnet => "https://api.testnet.solana.com",
            Network::Localnet => "http://127.0.0.1:8899",
        }
    }
}

/// The Jib struct is the main entry point for the library.
/// It is used to create a new Jib instance, set the RPC URL, set the instructions, pack the instructions into transactions
/// and finally submit the transactions to the network.
pub struct Jib {
    #[cfg(feature = "tpu")]
    tpu_client: Option<TpuClient<QuicPool, QuicConnectionManager, QuicConfig>>,
    client: Arc<RpcClient>,
    signers: Vec<Keypair>,
    ixes: Vec<Instruction>,
    compute_budget: u32,
    priority_fee: u64,
    batch_size: usize,
    rate_limit: u64,
}

/// A library Result value indicating Success or Failure and containing information about each result type.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum JibResult {
    /// The transaction was successful and contains the signature of the transaction.
    Success(String),
    /// The transaction failed and contains the transaction and error message.
    Failure(JibFailedTransaction),
}

impl JibResult {
    /// Returns true if the result is a success.
    pub fn is_success(&self) -> bool {
        match self {
            JibResult::Success(_) => true,
            JibResult::Failure(_) => false,
        }
    }

    /// Returns true if the result is a failure.
    pub fn is_failure(&self) -> bool {
        !self.is_success()
    }

    pub fn get_failure(self) -> Option<JibFailedTransaction> {
        match self {
            JibResult::Success(_) => None,
            JibResult::Failure(f) => Some(f),
        }
    }

    /// Parses the message from a failure or returns None if the result is a success.
    pub fn message(&self) -> Option<Message> {
        match self {
            JibResult::Success(_) => None,
            JibResult::Failure(f) => Some(f.message.clone()),
        }
    }

    /// Parses the error message from a failure or returns None if the result is a success.
    pub fn error(&self) -> Option<String> {
        match self {
            JibResult::Success(_) => None,
            JibResult::Failure(f) => Some(f.error.clone()),
        }
    }

    /// Parses the signature from a success or returns None if the result is a failure.
    pub fn signature(&self) -> Option<String> {
        match self {
            JibResult::Success(s) => Some(s.clone()),
            JibResult::Failure(_) => None,
        }
    }
}

/// A failed transaction with the error message.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct JibFailedTransaction {
    pub signature: Signature,
    pub message: Message,
    pub error: String,
}

impl Jib {
    /// Create a new Jib instance. You should pass in all the signers you want to use for the transactions.
    pub fn new(signers: Vec<Keypair>, rpc_url: String) -> Result<Self, JibError> {
        let client = Arc::new(RpcClient::new_with_commitment(
            rpc_url,
            CommitmentConfig::confirmed(),
        ));

        Ok(Self {
            #[cfg(feature = "tpu")]
            tpu_client: None,
            client,
            signers,
            ixes: Vec::new(),
            compute_budget: 200_000,
            priority_fee: 0,
            batch_size: 10,
            rate_limit: 10,
        })
    }

    #[cfg(feature = "tpu")]
    pub fn create_tpu_client(&mut self, url: &str) -> Result<(), JibError> {
        let rpc_client = Arc::new(BlockingRpcClient::new_with_commitment(
            url.to_string(),
            CommitmentConfig::confirmed(),
        ));
        let wss = url.replace("http", "ws");

        let tpu_config = TpuClientConfig { fanout_slots: 1 };
        let tpu_client = TpuClient::new(rpc_client, &wss, tpu_config)
            .map_err(|e| JibError::FailedToCreateTpuClient(e.to_string()))?;

        self.tpu_client = Some(tpu_client);
        Ok(())
    }

    /// Set the instructions to use for the transactions. This should be a vector of instructions that you wish to submit to the network.
    pub fn set_instructions(&mut self, ixes: Vec<Instruction>) {
        self.ixes = ixes;
    }

    /// Set the signers to use for the transactions. This should be a vector of signers that you wish to use to sign the transactions.
    pub fn set_signers(&mut self, signers: Vec<Keypair>) {
        self.signers = signers;
    }

    /// Set the compute budget to use for the transactions. If not set
    /// no compute budget is set which defaults to the system standard of 200,000.
    pub fn set_compute_budget(&mut self, compute_budget: u32) {
        self.compute_budget = compute_budget;
    }

    /// Set the priority fee to use for the transactions. If not set no priority fee
    /// is set. Units are micro-lamports per compute unit.
    pub fn set_priority_fee(&mut self, priority_fee: u64) {
        self.priority_fee = priority_fee;
    }

    /// Set the batch size to use for the transactions. This defaults to 10.
    pub fn set_batch_size(&mut self, batch_size: usize) {
        self.batch_size = batch_size;
    }

    /// Set the rate limit to use for the transactions. This defaults to 10.
    pub fn set_rate_limit(&mut self, rate_limit: u64) {
        self.rate_limit = rate_limit;
    }

    /// Get the first signer that is being used by the Jib instance, the transaction fee payer.
    pub fn payer(&self) -> &Keypair {
        self.signers.first().unwrap()
    }

    pub async fn retry_failed(
        &mut self,
        failed_transactions: Vec<JibFailedTransaction>,
    ) -> Result<Vec<JibResult>, JibError> {
        let mut status_tasks = FuturesOrdered::new();
        let retries = Arc::new(Mutex::new(Vec::new()));

        let ratelimiter = Ratelimiter::builder(self.rate_limit, Duration::from_secs(1))
            .max_tokens(self.rate_limit)
            .initial_available(self.rate_limit)
            .build()
            .unwrap();

        let pb = ProgressBar::new(failed_transactions.len() as u64);
        pb.set_message("Checking failed transaction statuses...");
        for tx in failed_transactions {
            // First we check the status of all the failed transactions to filter out any that have already been confirmed.
            let pb = pb.clone();
            let client = Arc::clone(&self.client);
            let retries = Arc::clone(&retries);

            // Wait for the ratelimiter to allow the transaction to be sent.
            if let Err(sleep) = ratelimiter.try_wait() {
                tokio::time::sleep(sleep).await;
                continue;
            }

            let task = tokio::spawn(async move {
                let res = client.confirm_transaction(&tx.signature).await;

                // Retry any errors or unconfirmed transactions.
                if res.is_err() || !res.unwrap() {
                    let mut retries = retries.lock().await;
                    retries.push(tx);
                }
            });
            status_tasks.push_back(task);

            pb.inc(1);
        }
        pb.finish();

        while let Some(result) = status_tasks.next().await {
            // We just need to wait for all the tasks to complete.
            match result {
                Ok(_) => {}
                Err(e) => {
                    // Log here? Tasks shouldn't panic.
                    return Err(JibError::TransactionError(e.to_string()));
                }
            }
        }

        // Signers needs to be a vec of references to keypairs which can't be cloned. Might be better to just use a normal loop or par_iter
        // to build all the transactions and then send them async without confirmation. Then we can check the statuses of all the transactions again.

        let retries_vec = Arc::try_unwrap(retries).unwrap().into_inner();
        let mut send_tasks = FuturesOrdered::new();

        let ratelimiter = Ratelimiter::builder(self.rate_limit, Duration::from_secs(1))
            .max_tokens(self.rate_limit)
            .initial_available(self.rate_limit)
            .build()
            .unwrap();

        println!("Found {} failed transactions to retry", retries_vec.len());

        let pb = ProgressBar::new(retries_vec.len() as u64);
        pb.set_message("Resending transactions...");
        for tx in retries_vec.into_iter() {
            let signers: Vec<Keypair> = self.signers.iter().map(|k| k.insecure_clone()).collect();
            let pb = pb.clone();
            let client = Arc::clone(&self.client);

            // Wait for the ratelimiter to allow the transaction to be sent.
            if let Err(sleep) = ratelimiter.try_wait() {
                tokio::time::sleep(sleep).await;
                continue;
            }

            let task = tokio::spawn(async move {
                let signers_ref: Vec<&Keypair> = signers.iter().collect();

                let mut tx = Transaction::new_unsigned(tx.message.clone());
                let blockhash = client.get_latest_blockhash().await.unwrap();
                tx.sign(&signers_ref, blockhash);

                pb.inc(1);
                let res = client.send_and_confirm_transaction(&tx.clone()).await;
                match res {
                    Ok(signature) => JibResult::Success(signature.to_string()),
                    Err(e) => JibResult::Failure(JibFailedTransaction {
                        signature: tx.signatures[0],
                        message: tx.message.clone(),
                        error: e.to_string(),
                    }),
                }
            });
            send_tasks.push_back(task);
        }
        pb.finish();

        let mut results = Vec::new();

        while let Some(result) = send_tasks.next().await {
            match result {
                Ok(result) => results.push(result),
                Err(e) => {
                    return Err(JibError::TransactionError(e.to_string()));
                }
            }
        }

        Ok(results)
    }

    /// Used to repack failed transactions into new transactions with a fresh blockhash and signatures.
    /// These transacations can be resubmitted with `hoist_with_transactions`.
    pub async fn repack_failed(
        &mut self,
        failed_transactions: Vec<JibFailedTransaction>,
    ) -> Result<Vec<Transaction>, JibError> {
        let mut packed_transactions = Vec::new();

        let mut tasks = FuturesOrdered::new();
        let retries = Arc::new(Mutex::new(Vec::new()));

        let signers: Vec<&Keypair> = self.signers.iter().map(|k| k as &Keypair).collect();

        let ratelimiter = Ratelimiter::builder(self.rate_limit, Duration::from_secs(1))
            .max_tokens(self.rate_limit)
            .initial_available(self.rate_limit)
            .build()
            .unwrap();

        let pb = ProgressBar::new(failed_transactions.len() as u64);
        pb.set_message("Checking failed transaction statuses...");
        for tx in failed_transactions {
            // First we check the status of all the failed transactions to filter out any that have already been confirmed.
            let pb = pb.clone();
            let client = Arc::clone(&self.client);
            let retries = Arc::clone(&retries);

            // Wait for the ratelimiter to allow the transaction to be sent.
            if let Err(sleep) = ratelimiter.try_wait() {
                tokio::time::sleep(sleep).await;
                continue;
            }

            let task = tokio::spawn(async move {
                let res = client.confirm_transaction(&tx.signature).await;

                // Retry any errors or unconfirmed transactions.
                if res.is_err() || !res.unwrap() {
                    let mut retries = retries.lock().await;
                    retries.push(tx);
                }
            });
            tasks.push_back(task);

            pb.inc(1);
        }
        pb.finish();

        while let Some(result) = tasks.next().await {
            // We just need to wait for all the tasks to complete.
            match result {
                Ok(_) => {}
                Err(e) => {
                    // Log here? Tasks shouldn't panic.
                    return Err(JibError::TransactionError(e.to_string()));
                }
            }
        }

        let retries = retries.lock().await;

        println!("Found {} failed transactions to retry", retries.len());

        let pb = ProgressBar::new(retries.len() as u64);
        pb.set_message("Repacking transactions...");
        for tx in retries.iter() {
            let mut tx = Transaction::new_unsigned(tx.message.clone());
            let blockhash = self
                .client
                .get_latest_blockhash()
                .await
                .map_err(|_| JibError::NoRecentBlockhash)?;
            tx.sign(&signers, blockhash);

            packed_transactions.push(tx);
            pb.inc(1);
        }
        pb.finish();

        Ok(packed_transactions)
    }

    /// Pack the instructions and submit them to the network. This will return a vector of results.
    pub async fn hoist(&mut self) -> Result<Vec<JibResult>, JibError> {
        if self.ixes.is_empty() {
            return Err(JibError::NoInstructions);
        }

        let mut packed_transactions = Vec::new();
        let mut results = Vec::new();

        let mut instructions = Vec::new();
        let payer_pubkey = self.signers.first().ok_or(JibError::NoSigners)?.pubkey();

        if self.compute_budget != 200_000 {
            instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
                self.compute_budget,
            ));
        }
        if self.priority_fee != 0 {
            instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
                self.priority_fee,
            ));
        }

        let mut current_transaction =
            Transaction::new_with_payer(&instructions, Some(&payer_pubkey));
        let signers: Vec<&Keypair> = self.signers.iter().map(|k| k as &Keypair).collect();

        let mut latest_blockhash = self
            .client
            .get_latest_blockhash()
            .await
            .map_err(|_| JibError::NoRecentBlockhash)?;

        let mut ixes = self.ixes.clone();

        for ix in ixes.iter_mut() {
            instructions.push(ix.clone());
            let mut tx = Transaction::new_with_payer(&instructions, Some(&payer_pubkey));
            tx.sign(&signers, latest_blockhash);

            let tx_len = bincode::serialize(&tx).unwrap().len();

            debug!("tx_len: {}", tx_len);

            if tx_len > MAX_TX_LEN || tx.message.account_keys.len() > 64 {
                packed_transactions.push(current_transaction.clone());
                debug!("Packed instructions: {}", instructions.len());

                // Clear instructions except for the last one that pushed the transaction over the size limit.
                // Check for compute budget and priority fees again and add them to the front of the instructions.
                instructions = vec![];
                if self.compute_budget != 200_000 {
                    instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
                        self.compute_budget,
                    ));
                }
                if self.priority_fee != 0 {
                    instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
                        self.priority_fee,
                    ));
                }
                instructions.push(ix.clone());
            } else {
                current_transaction = tx;
            }

            if packed_transactions.len() == TX_BATCH_SIZE {
                results.extend(self._hoist(packed_transactions.clone()).await?);
                packed_transactions.clear();

                // Refresh blockhash.
                latest_blockhash = self
                    .client
                    .get_latest_blockhash()
                    .await
                    .map_err(|_| JibError::NoRecentBlockhash)?;
            }
        }
        packed_transactions.push(current_transaction);
        results.extend(self._hoist(packed_transactions.clone()).await?);

        Ok(results)
    }

    pub async fn hoist_with_transactions(
        &mut self,
        packed_transactions: Vec<Transaction>,
    ) -> Result<Vec<JibResult>, JibError> {
        let results = self._hoist(packed_transactions).await?;

        Ok(results)
    }

    async fn _hoist(
        &self,
        packed_transactions: Vec<Transaction>,
    ) -> Result<Vec<JibResult>, JibError> {
        let mut tasks = FuturesOrdered::new();

        let ratelimiter = Ratelimiter::builder(self.rate_limit, Duration::from_secs(1))
            .max_tokens(self.rate_limit)
            .initial_available(self.rate_limit)
            .build()
            .unwrap();

        let pb = ProgressBar::new(packed_transactions.len() as u64);
        pb.set_message("Sending batch of transactions...");
        for tx in packed_transactions {
            let pb = pb.clone();
            let client = Arc::clone(&self.client);

            // Wait for the ratelimiter to allow the transaction to be sent.
            if let Err(sleep) = ratelimiter.try_wait() {
                tokio::time::sleep(sleep).await;
                continue;
            }

            let task = tokio::spawn(async move {
                let res = client.send_and_confirm_transaction(&tx.clone()).await;
                pb.inc(1);
                match res {
                    Ok(signature) => JibResult::Success(signature.to_string()),
                    Err(e) => JibResult::Failure(JibFailedTransaction {
                        signature: tx.signatures[0],
                        message: tx.message.clone(),
                        error: e.to_string(),
                    }),
                }
            });
            tasks.push_back(task);
        }

        let mut results = Vec::new();

        while let Some(result) = tasks.next().await {
            match result {
                Ok(result) => results.push(result),
                Err(e) => {
                    return Err(JibError::TransactionError(e.to_string()));
                }
            }
        }
        pb.finish_and_clear();

        Ok(results)
    }

    /// Pack the instructions into transactions without sending them.
    pub async fn pack(&mut self) -> Result<Vec<Transaction>, JibError> {
        if self.ixes.is_empty() {
            return Err(JibError::NoInstructions);
        }

        let mut packed_transactions = Vec::new();

        let mut instructions = Vec::new();
        let payer_pubkey = self.signers.first().ok_or(JibError::NoSigners)?.pubkey();

        if self.compute_budget != 200_000 {
            instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
                self.compute_budget,
            ));
        }
        if self.priority_fee != 0 {
            instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
                self.priority_fee,
            ));
        }

        let mut current_transaction =
            Transaction::new_with_payer(&instructions, Some(&payer_pubkey));
        let signers: Vec<&Keypair> = self.signers.iter().map(|k| k as &Keypair).collect();

        let latest_blockhash = self
            .client
            .get_latest_blockhash()
            .await
            .map_err(|_| JibError::NoRecentBlockhash)?;

        for ix in self.ixes.iter_mut() {
            instructions.push(ix.clone());
            let mut tx = Transaction::new_with_payer(&instructions, Some(&payer_pubkey));
            tx.sign(&signers, latest_blockhash);

            let tx_len = bincode::serialize(&tx).unwrap().len();

            debug!("tx_len: {}", tx_len);

            if tx_len > MAX_TX_LEN || tx.message.account_keys.len() > 64 {
                packed_transactions.push(current_transaction.clone());
                debug!("Packed instructions: {}", instructions.len());

                instructions = vec![];
                if self.compute_budget != 200_000 {
                    instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
                        self.compute_budget,
                    ));
                }
                if self.priority_fee != 0 {
                    instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
                        self.priority_fee,
                    ));
                }
                instructions.push(ix.clone());
            } else {
                current_transaction = tx;
            }
        }
        packed_transactions.push(current_transaction);

        Ok(packed_transactions)
    }

    /// Pack the instructions into transactions and submit them to the network via the TPU client.
    /// This will display a spinner while the transactions are being submitted.
    #[cfg(feature = "tpu")]
    pub async fn hoist_via_tpu(&mut self) -> Result<Vec<JibResult>, JibError> {
        let packed_transactions = self.pack().await?;

        let results = self.submit_packed_transactions(packed_transactions).await?;

        Ok(results)
    }

    /// Submit pre-packed transactions to the network via the TPU client. This will display a spinner while the transactions are being submitted.
    #[cfg(feature = "tpu")]
    pub async fn submit_packed_transactions(
        &mut self,
        transactions: Vec<Transaction>,
    ) -> Result<Vec<JibResult>, JibError> {
        let signers: Vec<&Keypair> = self.signers.iter().map(|k| k as &Keypair).collect();

        let messages = transactions
            .as_slice()
            .iter()
            .map(|tx| tx.message.clone())
            .collect::<Vec<_>>();

        let mut results = vec![];

        let mpb = MultiProgress::new();
        let pb = mpb.add(ProgressBar::new(messages.len() as u64));
        pb.set_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.blue} {msg} {wide_bar:.cyan/blue} {pos:>7}/{len:7} {eta_precise}",
                )
                .unwrap(),
        );
        pb.set_message("Sending transactions");

        for chunk in messages.chunks(self.batch_size) {
            let res = self
                .send_and_confirm_messages_with_spinner(chunk, &signers, &mpb)
                .await
                .map_err(|e| JibError::TransactionError(e.to_string()))?;

            results.extend(res);

            pb.inc(chunk.len() as u64);
        }

        Ok(results)
    }

    // Pulled from tpu_client code and modified to return Jib results for better error handling and retrying failed transactions.
    #[cfg(feature = "tpu")]
    async fn send_and_confirm_messages_with_spinner<T: Signers>(
        &self,
        messages: &[Message],
        signers: &T,
        mpb: &MultiProgress,
    ) -> Result<Vec<JibResult>, TpuSenderError> {
        let tpu_client = self.tpu_client.as_ref().ok_or(TpuSenderError::Custom(
            "TPU client not initialized".to_string(),
        ))?;

        let mut expired_blockhash_retries = 5;

        let spinner = mpb.add(ProgressBar::new(42));
        spinner.set_style(
            ProgressStyle::default_spinner()
                .template("{spinner:.green} {wide_msg}")
                .unwrap(),
        );
        spinner.enable_steady_tick(Duration::from_millis(100));

        let mut jib_results = Vec::with_capacity(messages.len());

        let mut transactions = messages
            .iter()
            .enumerate()
            .map(|(i, message)| (i, Transaction::new_unsigned(message.clone())))
            .collect::<Vec<_>>();

        let total_transactions = transactions.len();

        let mut transaction_errors = vec![None; transactions.len()];
        let mut confirmed_transactions = 0;
        let mut block_height = self.client.get_block_height().await?;

        while expired_blockhash_retries > 0 {
            let (blockhash, last_valid_block_height) = self
                .client
                .get_latest_blockhash_with_commitment(self.client.commitment())
                .await?;

            let mut pending_transactions: HashMap<Signature, (usize, Transaction)> = HashMap::new();
            for (i, ref mut transaction) in &mut transactions {
                transaction.try_sign(signers, blockhash)?;
                pending_transactions.insert(transaction.signatures[0], (*i, transaction.clone()));
            }

            let mut last_resend = Instant::now() - TRANSACTION_RESEND_INTERVAL;
            while block_height <= last_valid_block_height {
                let num_transactions = pending_transactions.len();

                // Periodically re-send all pending transactions
                if Instant::now().duration_since(last_resend) > TRANSACTION_RESEND_INTERVAL {
                    for (index, (_i, transaction)) in pending_transactions.values().enumerate() {
                        if !tpu_client.send_transaction(transaction) {
                            let _result = self.client.send_transaction(transaction).await.ok();
                        }
                        set_message_for_confirmed_transactions(
                            &spinner,
                            confirmed_transactions,
                            total_transactions,
                            None, //block_height,
                            last_valid_block_height,
                            &format!("Sending {}/{} transactions", index + 1, num_transactions,),
                        );
                        sleep(SEND_TRANSACTION_INTERVAL);
                    }
                    last_resend = Instant::now();
                }

                // Wait for the next block before checking for transaction statuses
                let mut block_height_refreshes = 10;
                set_message_for_confirmed_transactions(
                    &spinner,
                    confirmed_transactions,
                    total_transactions,
                    Some(block_height),
                    last_valid_block_height,
                    &format!("Waiting for next block, {} pending...", num_transactions),
                );
                let mut new_block_height = block_height;
                while block_height == new_block_height && block_height_refreshes > 0 {
                    sleep(Duration::from_millis(500));
                    new_block_height = self.client.get_block_height().await?;
                    block_height_refreshes -= 1;
                }
                block_height = new_block_height;

                // Collect statuses for the transactions, drop those that are confirmed
                let pending_signatures = pending_transactions.keys().cloned().collect::<Vec<_>>();
                for pending_signatures_chunk in
                    pending_signatures.chunks(MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS)
                {
                    if let Ok(result) = self
                        .client
                        .get_signature_statuses(pending_signatures_chunk)
                        .await
                    {
                        let statuses = result.value;
                        for (signature, status) in
                            pending_signatures_chunk.iter().zip(statuses.into_iter())
                        {
                            if let Some(status) = status {
                                if status.satisfies_commitment(self.client.commitment()) {
                                    if let Some((i, _)) = pending_transactions.remove(signature) {
                                        confirmed_transactions += 1;
                                        if status.err.is_some() {
                                            spinner.println(format!(
                                                "Failed transaction: {:?}",
                                                status
                                            ));
                                            jib_results.push(JibResult::Failure(
                                                JibFailedTransaction {
                                                    signature: *signature,
                                                    message: transactions[i].1.message.clone(),
                                                    error: status.err.clone().unwrap().to_string(),
                                                },
                                            ));
                                        } else {
                                            jib_results
                                                .push(JibResult::Success(signature.to_string()));
                                        }
                                        transaction_errors[i] = status.err;
                                    }
                                }
                            }
                        }
                    }
                    set_message_for_confirmed_transactions(
                        &spinner,
                        confirmed_transactions,
                        total_transactions,
                        Some(block_height),
                        last_valid_block_height,
                        "Checking transaction status...",
                    );
                }

                if pending_transactions.is_empty() {
                    return Ok(jib_results);
                }
            }

            transactions = pending_transactions.into_values().collect();
            spinner.println(format!(
                "Blockhash expired. {} retries remaining",
                expired_blockhash_retries
            ));
            expired_blockhash_retries -= 1;
        }
        Err(TpuSenderError::Custom("Max retries exceeded".into()))
    }
}

/* Pulled from tpu_client to support 'send_and_confirm_messages_with_spinner' function. */
#[cfg(feature = "tpu")]
fn set_message_for_confirmed_transactions(
    progress_bar: &ProgressBar,
    confirmed_transactions: u32,
    total_transactions: usize,
    block_height: Option<u64>,
    last_valid_block_height: u64,
    status: &str,
) {
    progress_bar.set_message(format!(
        "{:>5.1}% | {:<40}{}",
        confirmed_transactions as f64 * 100. / total_transactions as f64,
        status,
        match block_height {
            Some(block_height) => format!(
                " [block height {}; re-sign in {} blocks]",
                block_height,
                last_valid_block_height.saturating_sub(block_height),
            ),
            None => String::new(),
        },
    ));
}

#[cfg(test)]
mod tests {
    use super::*;
    use solana_sdk::signature::{Keypair, Signature};
    use solana_sdk::signer::Signer;
    use std::str::FromStr;

    // ---- Network enum tests ----

    #[test]
    fn test_network_from_str_valid() {
        assert_eq!(Network::from_str("devnet"), Ok(Network::Devnet));
        assert_eq!(Network::from_str("mainnet"), Ok(Network::MainnetBeta));
        assert_eq!(Network::from_str("testnet"), Ok(Network::Testnet));
        assert_eq!(Network::from_str("localnet"), Ok(Network::Localnet));
    }

    #[test]
    fn test_network_from_str_invalid() {
        assert!(Network::from_str("invalid").is_err());
        assert!(Network::from_str("Devnet").is_err());
        assert!(Network::from_str("").is_err());
    }

    #[test]
    fn test_network_display_roundtrip() {
        for variant in &[
            Network::Devnet,
            Network::MainnetBeta,
            Network::Testnet,
            Network::Localnet,
        ] {
            let display = variant.to_string();
            let parsed = Network::from_str(&display).expect("round-trip should succeed");
            assert_eq!(*variant, parsed);
        }
    }

    #[test]
    fn test_network_url() {
        assert_eq!(Network::Devnet.url(), "https://api.devnet.solana.com");
        assert_eq!(
            Network::MainnetBeta.url(),
            "https://api.mainnet-beta.solana.com"
        );
        assert_eq!(Network::Testnet.url(), "https://api.testnet.solana.com");
        assert_eq!(Network::Localnet.url(), "http://127.0.0.1:8899");
    }

    #[test]
    fn test_network_default_is_devnet() {
        assert_eq!(Network::default(), Network::Devnet);
    }

    // ---- JibResult tests ----

    fn make_success() -> JibResult {
        JibResult::Success("somesig123".to_string())
    }

    fn make_failure() -> JibResult {
        JibResult::Failure(JibFailedTransaction {
            signature: Signature::default(),
            message: Message::new(&[], None),
            error: "something went wrong".to_string(),
        })
    }

    #[test]
    fn test_jib_result_is_success() {
        assert!(make_success().is_success());
        assert!(!make_failure().is_success());
    }

    #[test]
    fn test_jib_result_is_failure() {
        assert!(!make_success().is_failure());
        assert!(make_failure().is_failure());
    }

    #[test]
    fn test_jib_result_signature() {
        assert_eq!(make_success().signature(), Some("somesig123".to_string()));
        assert_eq!(make_failure().signature(), None);
    }

    #[test]
    fn test_jib_result_error() {
        assert_eq!(make_success().error(), None);
        assert_eq!(
            make_failure().error(),
            Some("something went wrong".to_string())
        );
    }

    #[test]
    fn test_jib_result_message() {
        assert!(make_success().message().is_none());
        let msg = make_failure().message();
        assert!(msg.is_some());
    }

    #[test]
    fn test_jib_result_get_failure() {
        assert!(make_success().get_failure().is_none());
        let failed = make_failure().get_failure();
        assert!(failed.is_some());
        let failed = failed.unwrap();
        assert_eq!(failed.error, "something went wrong");
    }

    // ---- Jib construction & setters tests ----

    #[test]
    fn test_jib_new_defaults() {
        let kp = Keypair::new();
        let pubkey = kp.pubkey();
        let jib = Jib::new(vec![kp], "http://localhost:8899".to_string())
            .expect("Jib::new should succeed");

        assert_eq!(jib.compute_budget, 200_000);
        assert_eq!(jib.priority_fee, 0);
        assert_eq!(jib.batch_size, 10);
        assert_eq!(jib.rate_limit, 10);
        assert_eq!(jib.payer().pubkey(), pubkey);
    }

    #[test]
    fn test_jib_payer_returns_first_signer() {
        let kp1 = Keypair::new();
        let kp2 = Keypair::new();
        let expected = kp1.pubkey();
        let jib = Jib::new(vec![kp1, kp2], "http://localhost:8899".to_string()).unwrap();
        assert_eq!(jib.payer().pubkey(), expected);
    }

    #[test]
    fn test_jib_set_compute_budget() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        jib.set_compute_budget(400_000);
        assert_eq!(jib.compute_budget, 400_000);
    }

    #[test]
    fn test_jib_set_priority_fee() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        jib.set_priority_fee(5000);
        assert_eq!(jib.priority_fee, 5000);
    }

    #[test]
    fn test_jib_set_batch_size() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        jib.set_batch_size(25);
        assert_eq!(jib.batch_size, 25);
    }

    #[test]
    fn test_jib_set_rate_limit() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        jib.set_rate_limit(50);
        assert_eq!(jib.rate_limit, 50);
    }

    #[test]
    fn test_jib_set_instructions() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        assert!(jib.ixes.is_empty());

        let ix = Instruction::new_with_bytes(solana_sdk::system_program::id(), &[], vec![]);
        jib.set_instructions(vec![ix.clone(), ix]);
        assert_eq!(jib.ixes.len(), 2);
    }

    #[test]
    fn test_jib_set_signers() {
        let kp1 = Keypair::new();
        let mut jib = Jib::new(vec![kp1], "http://localhost:8899".to_string()).unwrap();
        assert_eq!(jib.signers.len(), 1);

        let kp2 = Keypair::new();
        let kp3 = Keypair::new();
        jib.set_signers(vec![kp2, kp3]);
        assert_eq!(jib.signers.len(), 2);
    }

    #[test]
    fn test_jib_new_empty_signers() {
        // new with empty signers succeeds (only fails at payer() time)
        let jib = Jib::new(vec![], "http://localhost:8899".to_string());
        assert!(jib.is_ok());
    }

    #[test]
    #[should_panic]
    fn test_jib_payer_panics_with_no_signers() {
        let jib = Jib::new(vec![], "http://localhost:8899".to_string()).unwrap();
        let _ = jib.payer(); // should panic because signers is empty
    }

    // ---- JibError display tests ----

    #[test]
    fn test_jib_error_display_no_instructions() {
        let err = JibError::NoInstructions;
        assert_eq!(err.to_string(), "No instructions to hoist");
    }

    #[test]
    fn test_jib_error_display_no_recent_blockhash() {
        let err = JibError::NoRecentBlockhash;
        assert_eq!(err.to_string(), "No recent blockhash");
    }

    #[test]
    fn test_jib_error_display_no_signers() {
        let err = JibError::NoSigners;
        assert_eq!(err.to_string(), "No signers found");
    }

    #[test]
    fn test_jib_error_display_transaction_error() {
        let err = JibError::TransactionError("timeout".to_string());
        assert_eq!(err.to_string(), "Transaction Error");
    }

    #[test]
    fn test_jib_error_display_batch_transaction_error() {
        let err = JibError::BatchTransactionError("batch fail".to_string());
        assert_eq!(err.to_string(), "Send batch transaction failed: batch fail");
    }

    #[test]
    fn test_jib_error_display_failed_to_create_tpu_client() {
        let err = JibError::FailedToCreateTpuClient("connection refused".to_string());
        assert_eq!(
            err.to_string(),
            "Failed to create the TPU client: connection refused"
        );
    }

    // ---- Edge case: hoist/pack with no instructions ----

    #[tokio::test]
    async fn test_hoist_no_instructions_returns_error() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        let result = jib.hoist().await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), JibError::NoInstructions));
    }

    #[tokio::test]
    async fn test_pack_no_instructions_returns_error() {
        let kp = Keypair::new();
        let mut jib = Jib::new(vec![kp], "http://localhost:8899".to_string()).unwrap();
        let result = jib.pack().await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), JibError::NoInstructions));
    }
}