-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolana.hpp
More file actions
1159 lines (984 loc) · 32.3 KB
/
solana.hpp
File metadata and controls
1159 lines (984 loc) · 32.3 KB
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
#pragma once
#include <cpr/cpr.h>
#include <sodium.h>
#include <unistd.h>
#include <boost/asio.hpp>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <thread>
#include <vector>
#include "base58.hpp"
#include "base64.hpp"
#include "websocket.hpp"
namespace net = boost::asio; // from <boost/asio.hpp>
namespace solana {
using json = nlohmann::json;
const std::string NATIVE_MINT = "So11111111111111111111111111111111111111112";
const std::string MEMO_PROGRAM_ID =
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr";
const std::string MAINNET_BETA = "https://api.mainnet-beta.solana.com";
const std::string DEVNET = "https://api.devnet.solana.com";
const int MAXIMUM_NUMBER_OF_BLOCKS_FOR_TRANSACTION = 152;
const int MINIMUM_SLOT_PER_EPOCH = 32;
struct PublicKey {
static const auto SIZE = crypto_sign_PUBLICKEYBYTES;
typedef std::array<uint8_t, SIZE> array_t;
array_t data;
static PublicKey empty();
static PublicKey fromBase58(const std::string &b58);
bool operator==(const PublicKey &other) const;
std::string toBase58() const;
};
/**
* PublicKey to json
*/
void to_json(json &j, const PublicKey &key);
/**
* PublicKey from json
*/
void from_json(const json &j, PublicKey &key);
struct PrivateKey {
static const size_t SIZE = crypto_sign_SECRETKEYBYTES;
typedef std::array<uint8_t, SIZE> array_t;
array_t data;
std::vector<uint8_t> signMessage(const std::vector<uint8_t> message) const;
};
struct Keypair {
PublicKey publicKey;
PrivateKey privateKey;
static Keypair fromFile(const std::string &path);
};
struct Version {
uint64_t feature_set;
std::string solana_core;
};
/**
* Version from json
*/
void from_json(const json &j, Version &version);
uint64_t trailingZeros(uint64_t n);
uint64_t nextPowerOfTwo(uint64_t n);
struct EpochSchedule {
uint64_t firstNormalEpoch;
uint64_t firstNormalSlot;
uint64_t leaderScheduleSlotOffset;
uint64_t slotsPerEpoch;
bool warmup;
uint64_t getEpoch(uint64_t slot) const {
return this->getEpochAndSlotIndex(slot)[0];
}
std::vector<uint64_t> getEpochAndSlotIndex(uint64_t slot) const {
std::vector<uint64_t> info;
if (slot < this->firstNormalSlot) {
const auto epoch =
trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) -
trailingZeros(MINIMUM_SLOT_PER_EPOCH) - 1;
const auto epochLen = this->getSlotsInEpoch(epoch);
const auto slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
info.push_back(epoch);
info.push_back(slotIndex);
return info;
} else {
const auto normalSlotIndex = slot - this->firstNormalSlot;
const auto normalEpochIndex =
floor(normalSlotIndex / this->slotsPerEpoch);
const auto epoch = this->firstNormalEpoch + normalEpochIndex;
const auto slotIndex = normalSlotIndex % this->slotsPerEpoch;
info.push_back(epoch);
info.push_back(slotIndex);
return info;
}
}
uint64_t getFirstSlotInEpoch(uint64_t epoch) const {
if (epoch <= this->firstNormalEpoch) {
return ((1 << epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
} else {
return ((epoch - this->firstNormalEpoch) * this->slotsPerEpoch +
this->firstNormalSlot);
}
}
uint64_t getLastSlotInEpoch(uint64_t epoch) const {
return this->getFirstSlotInEpoch(epoch) + this->getSlotsInEpoch(epoch) - 1;
}
uint64_t getSlotsInEpoch(uint64_t epoch) const {
if (epoch < this->firstNormalEpoch) {
return 1 << (epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
} else {
return this->slotsPerEpoch;
}
}
};
/**
* EpochSchedule from json
*/
void from_json(const json &j, EpochSchedule &epochschedule);
struct StakeActivation {
uint64_t active;
uint64_t inactive;
std::string state;
};
void from_json(const json &j, StakeActivation &stakeactivation);
struct InflationGovernor {
double foundation;
double foundationTerm;
double initial;
double taper;
double terminal;
};
void from_json(const json &j, InflationGovernor &inflationgovernor);
struct TokenSupply {
std::string amount;
uint8_t decimals;
double uiAmount;
std::string uiAmountString;
};
void from_json(const json &j, TokenSupply &tokensupply);
struct BlockProduction {
uint64_t firstSlot;
uint64_t lastSlot;
std::vector<std::pair<std::string, std::vector<uint64_t>>> byIdentity;
};
void from_json(const json &j, BlockProduction &blockproduction);
struct TokenAccountInfo {
bool executable;
std::string owner;
uint64_t lamports;
json data;
uint64_t rentEpoch;
};
void from_json(const json &j, TokenAccountInfo &tokenAccountInfo);
struct TokenAccountsByOwner {
std::string pubkey;
TokenAccountInfo account;
};
void from_json(const json &j, TokenAccountsByOwner &tokenAccountsByOwner);
/**
* Account metadata used to define instructions
*/
struct AccountMeta {
PublicKey pubkey;
bool isSigner;
bool isWritable;
bool operator<(const AccountMeta &other) const;
};
struct Instruction {
PublicKey programId;
std::vector<AccountMeta> accounts;
std::vector<uint8_t> data;
};
namespace CompactU16 {
void encode(uint16_t num, std::vector<uint8_t> &buffer);
void encode(const std::vector<uint8_t> &vec, std::vector<uint8_t> &buffer);
}; // namespace CompactU16
struct Blockhash {
PublicKey publicKey;
uint64_t lastValidBlockHeight;
};
/**
* Data slice argument to limit the returned account data
*/
struct DataSlice {
/** offset of data slice */
uint16_t offset;
/** length of data slice */
uint16_t number;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DataSlice, offset, number);
/**
* the most-recent return data generated by an instruction in the transaction
*/
struct TransactionReturnData {
/**
* the program that generated the return data, as base-58 encoded Pubkey
*/
std::string programId;
/**
* the return data itself, as base-64 encoded binary data
*/
std::string data;
};
/**
* TransactionReturnData from json
*/
void from_json(const json &j, TransactionReturnData &data);
/**
* The level of commitment desired when querying state
*/
enum class Commitment : short {
/**
* Query the most recent block which has reached 1 confirmation by the
* connected node
*/
PROCESSED,
/**
* Query the most recent block which has reached 1 confirmation by the cluster
*/
CONFIRMED,
/**
* Query the most recent block which has been finalized by the cluster
*/
FINALIZED,
};
NLOHMANN_JSON_SERIALIZE_ENUM(Commitment,
{
{Commitment::PROCESSED, "processed"},
{Commitment::CONFIRMED, "confirmed"},
{Commitment::FINALIZED, "finalized"},
})
struct GetStakeActivationConfig {
/** The level of commitment desired */
std::optional<Commitment> commitment = std::nullopt;
std::optional<uint64_t> epoch = std::nullopt;
/** The minimum slot that the request can be evaluated at */
std::optional<uint64_t> minContextSlot = std::nullopt;
};
void to_json(json &j, const GetStakeActivationConfig &config);
struct commitmentconfig {
/** The level of commitment desired */
std::optional<Commitment> commitment = std::nullopt;
};
void to_json(json &j, const commitmentconfig &config);
struct mintOrProgramIdConfig {
std::optional<std::string> mint = std::nullopt;
std::optional<std::string> programId = std::nullopt;
};
void to_json(json &j, const mintOrProgramIdConfig &config);
struct TokenAccountsByOwnerConfig {
std::optional<Commitment> commitment = std::nullopt;
std::optional<std::string> encoding = std::nullopt;
std::optional<DataSlice> dataSlice = std::nullopt;
std::optional<uint64_t> minContextSlot = std::nullopt;
};
void to_json(json &j, const TokenAccountsByOwnerConfig &config);
struct GetBlocksConfig {
std::optional<uint64_t> end_slot = std::nullopt;
std::optional<Commitment> commitment = std::nullopt;
};
void to_json(json &j, const GetBlocksConfig &config);
struct SimulatedTransactionResponse {
/**
* Error if transaction failed, null if transaction succeeded.
*/
std::optional<std::string> err = std::nullopt;
/**
* array of accounts with the same length as the accounts.addresses array in
* the request
*/
std::optional<std::vector<std::string>> accounts = std::nullopt;
/**
* Array of log messages the transaction instructions output during execution,
* null if simulation failed before the transaction was able to execute (for
* example due to an invalid blockhash or signature verification failure)
*/
std::optional<std::vector<std::string>> logs = std::nullopt;
/**
* The number of compute budget units consumed during the processing of this
* transaction
*/
std::optional<uint64_t> unitsConsumed = std::nullopt;
/**
* the most-recent return data generated by an instruction in the transaction
*/
std::optional<TransactionReturnData> returnData = std::nullopt;
};
/**
* SimulatedTransactionResponse from json
*/
void from_json(const json &j, SimulatedTransactionResponse &res);
struct GetSlotConfig {
/** The level of commitment desired */
std::optional<Commitment> commitment = std::nullopt;
/** The minimum slot that the request can be evaluated at */
std::optional<uint64_t> minContextSlot = std::nullopt;
};
/**
* convert GetSlotConfig to json
*/
void to_json(json &j, const GetSlotConfig &config);
struct BlockProductionConfig {
std::optional<Commitment> commitment = std::nullopt;
std::optional<json> range = std::nullopt;
std::optional<std::string> identity = std::nullopt;
};
void to_json(json &j, const BlockProductionConfig &config);
struct LargestAccountsConfig {
std::optional<Commitment> commitment = std::nullopt;
std::optional<std::string> filter = std::nullopt;
};
void to_json(json &j, const LargestAccountsConfig &config);
struct SignatureStatus {
/** when the transaction was processed */
uint64_t slot;
/** the number of blocks that have been confirmed and voted on in the fork
* containing `slot` */
std::optional<uint64_t> confirmations = std::nullopt;
/** transaction error, if any */
std::optional<std::string> err = std::nullopt;
/** cluster confirmation status, if data available. Possible responses:
* `processed`, `confirmed`, `finalized` */
Commitment confirmationStatus;
};
struct EpochInfo {
uint64_t absoluteSlot;
uint64_t blockHeight;
uint64_t epoch;
uint64_t slotIndex;
uint64_t slotsInEpoch;
uint64_t transactionCount;
};
void from_json(const json &j, EpochInfo &epochinfo);
struct Nodes {
std::optional<uint64_t> featureSet = std::nullopt;
std::optional<std::string> gossip = std::nullopt;
std::optional<std::string> pubkey = std::nullopt;
std::optional<std::string> rpc = std::nullopt;
std::optional<uint64_t> shredVersion = std::nullopt;
std::optional<std::string> tpu = std::nullopt;
std::optional<std::string> version = std::nullopt;
};
void from_json(const json &j, Nodes &nodes);
struct LargestAccounts {
uint64_t lamports;
std::string address;
};
void from_json(const json &j, LargestAccounts &largestaccounts);
struct RecentPerformanceSamples {
uint64_t numSlots;
uint64_t numTransactions;
uint64_t samplePeriodSecs;
uint64_t slot;
};
void from_json(const json &j,
RecentPerformanceSamples &recentperformancesamples);
struct getFeeForMessageRes {
std::optional<uint64_t> value = std::nullopt;
};
void from_json(const json &j, getFeeForMessageRes &res);
/**
* SignatureStatus to json
*/
void to_json(json &j, const SignatureStatus &status);
/**
* SignatureStatus from json
*/
void from_json(const json &j, SignatureStatus &status);
struct GetSupplyConfig {
/** The level of commitment desired */
std::optional<Commitment> commitment = std::nullopt;
/** The minimum slot that the request can be evaluated at */
std::optional<bool> excludeNonCirculatingAccountsList = std::nullopt;
};
struct GetVoteAccountsConfig {
std::optional<Commitment> commitment = std::nullopt;
std::optional<std::string> votePubkey = std::nullopt;
std::optional<bool> keepUnstakedDelinquents = std::nullopt;
std::optional<uint64_t> delinquentSlotDistance = std::nullopt;
};
struct GetSignatureAddressConfig {
std::optional<uint64_t> limit = std::nullopt;
std::optional<std::string> before = std::nullopt;
std::optional<std::string> until = std::nullopt;
std::optional<Commitment> commitment = std::nullopt;
std::optional<uint64_t> minContextSlot = std::nullopt;
};
struct Supply {
uint64_t circulating;
uint64_t nonCirculating;
std::optional<std::vector<std::string>> nonCirculatingAccounts = std::nullopt;
uint64_t total;
};
void from_json(const json &j, Supply &supply);
struct TokenAccountBalance {
std::string amount;
uint64_t decimals;
double uiAmount;
std::string uiAmountString;
};
void from_json(const json &j, TokenAccountBalance &tokenaccountbalance);
struct Current {
uint64_t commission;
bool epochVoteAccount;
std::vector<std::vector<uint64_t>> epochCredits;
std::string nodePubkey;
uint64_t lastVote;
uint64_t activatedStake;
std::string votePubkey;
};
struct Delinquent {
uint64_t commission;
bool epochVoteAccount;
std::vector<std::vector<uint64_t>> epochCredits;
std::string nodePubkey;
uint64_t lastVote;
uint64_t activatedStake;
std::string votePubkey;
};
struct VoteAccounts {
std::vector<Current> current;
std::vector<Delinquent> delinquent;
};
struct SignaturesAddress {
std::optional<uint64_t> blockTime = std::nullopt;
std::optional<Commitment> confirmationStatus = std::nullopt;
std::optional<std::string> err = std::nullopt;
std::optional<std::string> memo = std::nullopt;
std::string signature;
uint64_t slot;
};
void from_json(const json &j, SignaturesAddress &signaturesaddress);
void to_json(json &j, const GetSupplyConfig &config);
void to_json(json &j, const GetVoteAccountsConfig &config);
struct TokenLargestAccounts {
std::string address;
std::string amount;
uint8_t decimals;
double uiAmount;
std::string uiAmountString;
};
void from_json(const json &j, TokenLargestAccounts &tokenlargestaccounts);
/**
* Extra contextual information for RPC responses
*/
struct Context {
uint64_t slot;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Context, slot);
/**
* RPC Response with extra contextual information
*/
template <typename T>
struct RpcResponseAndContext {
/** response context */
Context context;
/** response value */
T value;
};
/**
* Information describing an account
*/
template <typename T>
struct AccountInfo {
/**
* boolean indicating if the account contains a program (and is strictly
* read-only)
*/
bool executable;
/**
* base-58 encoded Pubkey of the program this account has been assigned to
*/
PublicKey owner;
/**namespace net = boost::asio; // from <boost/asio.hpp>
* number of lamports assigned to this account
*/
uint64_t lamports;
/**
* data associated with the account as encoded binary
*/
T data;
/**
* the epoch at which this account will next owe rent
*/
uint64_t rentEpoch;
};
/**
* AccountInfo from json
*/
template <typename T>
void from_json(const json &j, AccountInfo<T> &info) {
info.executable = j["executable"];
info.owner = PublicKey::fromBase58(j["owner"]);
info.lamports = j["lamports"];
// check
assert(j["data"][1] == BASE64);
// b64 decode data for casting
const auto decoded_data = b64decode(j["data"][0]);
// decoded data should fit into T
if (decoded_data.size() != sizeof(T))
throw std::runtime_error("invalid response length " +
std::to_string(decoded_data.size()) +
" expected " + std::to_string(sizeof(T)));
// cast
info.data = T{};
memcpy(&info.data, decoded_data.data(), sizeof(T));
info.rentEpoch = j["rentEpoch"];
}
/**
* AccountInfo from json
*/
template <typename T>
void from_json(const json &j, std::optional<AccountInfo<T>> &info) {
if (j.is_null()) {
info = std::nullopt;
} else {
info = std::optional<AccountInfo<T>>{j};
}
}
/**
* An instruction to execute by a program
*/
struct CompiledInstruction {
uint8_t programIdIndex;
std::vector<uint8_t> accountIndices;
std::vector<uint8_t> data;
static CompiledInstruction fromInstruction(
const Instruction &ix, const std::vector<PublicKey> &accounts);
void serializeTo(std::vector<uint8_t> &buffer) const;
};
struct CompiledTransaction {
Blockhash recentBlockhash;
std::vector<PublicKey> accounts;
std::vector<CompiledInstruction> instructions;
uint8_t requiredSignatures;
uint8_t readOnlySignedAccounts;
uint8_t readOnlyUnsignedAccounts;
static CompiledTransaction fromInstructions(
const std::vector<Instruction> &instructions, const PublicKey &payer,
const Blockhash &blockhash);
void serializeTo(std::vector<uint8_t> &buffer) const;
/**
* sign the transaction
*/
static std::vector<uint8_t> signTransaction(const Keypair &keypair,
const std::vector<uint8_t> &tx);
/**
* sign the CompiledTransaction
*/
std::vector<uint8_t> sign(const Keypair &keypair) const;
};
namespace rpc {
const std::string JSON_PARSED = "jsonParsed";
using json = nlohmann::json;
json jsonRequest(const std::string &method, const json ¶ms = nullptr);
/**
* Read AccountInfo dumped in a file
* @param path Path to file
*/
template <typename T>
static T fromFile(const std::string &path) {
std::ifstream fileStream(path);
std::string fileContent(std::istreambuf_iterator<char>(fileStream), {});
auto response = json::parse(fileContent);
const std::string encoded = response["data"][0];
const std::string decoded = solana::b64decode(encoded);
if (decoded.size() != sizeof(T))
throw std::runtime_error("Invalid account data");
T accountInfo{};
memcpy(&accountInfo, decoded.data(), sizeof(T));
return accountInfo;
}
/**
* Configuration object for sendTransaction
*/
struct SendTransactionConfig {
/**
* if true, skip the preflight transaction checks (default: false)
*/
const std::optional<bool> skipPreflight = std::nullopt;
/**
* Commitment level to use for preflight (default: "finalized").
*/
const std::optional<Commitment> preflightCommitment = std::nullopt;
/**
* Encoding used for the transaction data. Either "base58" (slow, DEPRECATED),
* or "base64". (default: "base64", rpc default: "base58").
*/
const std::string encoding = BASE64;
/**
* Maximum number of times for the RPC node to retry sending the transaction
* to the leader. If this parameter not provided, the RPC node will retry the
* transaction until it is finalized or until the blockhash expires.
*/
const std::optional<uint8_t> maxRetries = std::nullopt;
/**
* set the minimum slot at which to perform preflight transaction checks.
*/
const std::optional<uint8_t> minContextSlot = std::nullopt;
};
/**
* SendTransactionConfig to json
*/
void to_json(json &j, const SendTransactionConfig &config);
///
/// Configuration object for simulateTransaction
struct SimulateTransactionConfig {
/**
* if true the transaction signatures will be verified (default: false,
* conflicts with replaceRecentBlockhash)
*/
const std::optional<bool> sigVerify = std::nullopt;
/**
* Commitment level to simulate the transaction at (default: "finalized").
*/
const std::optional<Commitment> commitment = std::nullopt;
/**
*if true the transaction recent blockhash will be replaced with the most
*recent blockhash. (default: false, conflicts with sigVerify)
*/
const std::optional<bool> replaceRecentBlockhash = std::nullopt;
/**
* An array of accounts to return, as base-58 encoded strings
*/
const std::optional<std::vector<std::string>> address = std::nullopt;
/**
* set the minimum slot that the request can be evaluated at.
*/
const std::optional<uint8_t> minContextSlot = std::nullopt;
};
/**
* convert SimulateTransactionConfig to json for RPC request param
*/
void to_json(json &j, const SimulateTransactionConfig &config);
/**
* Configuration object for changing `getAccountInfo` and
* `getMultipleAccountsInfo` query behavior
*/
struct GetAccountInfoConfig {
/**
* The level of commitment desired
*/
std::optional<std::string> commitment = std::nullopt;
/**
* The minimum slot that the request can be evaluated at
*/
std::optional<uint64_t> minContextSlot = std::nullopt;
/**
* Optional data slice to limit the returned account data
*/
std::optional<DataSlice> dataSlice = std::nullopt;
};
/**
* convert GetAccountInfoConfig to json
*/
void to_json(json &j, const GetAccountInfoConfig &config);
///
/// RPC HTTP Endpoints
class Connection {
public:
/**
* Initialize the rpc url and commitment levels to use.
* Initialize sodium
*/
Connection(const std::string &rpc_url = MAINNET_BETA);
/*
* send rpc request
* @return result from response
*/
json sendJsonRpcRequest(const json &body) const;
/**
* @deprecated
* Sign and send a transaction
* @return transaction signature
*/
[[deprecated]] std::string signAndSendTransaction(
const Keypair &keypair, const CompiledTransaction &tx,
bool skipPreflight = false,
const Commitment &preflightCommitment = Commitment::FINALIZED) const;
/**
* Sign and send a transaction
* @return transaction signature
*/
std::string sendTransaction(
const Keypair &keypair, const CompiledTransaction &tx,
const SendTransactionConfig &config = SendTransactionConfig()) const;
/**
* Send a transaction that has already been signed and serialized into the
* wire format
*/
std::string sendRawTransaction(
const std::vector<uint8_t> &tx,
const SendTransactionConfig &config = SendTransactionConfig()) const;
/**
* Send a transaction that has already been signed, serialized into the
* wire format, and encoded as a base64 string
*/
std::string sendEncodedTransaction(
const std::string &transaction,
const SendTransactionConfig &config = SendTransactionConfig()) const;
/**
* Simulate sending a transaction
* @return SimulatedTransactionResponse
*/
SimulatedTransactionResponse simulateTransaction(
const Keypair &keypair, const CompiledTransaction &tx,
const SimulateTransactionConfig &config =
SimulateTransactionConfig()) const;
/**
* Request an allocation of lamports to the specified address
*/
std::string requestAirdrop(const PublicKey &pubkey, uint64_t lamports) const;
/**
* Fetch the balance for the specified public key
*/
uint64_t getBalance(const PublicKey &pubkey) const;
/**
* Fetch a recent blockhash from the cluster
* @deprecated Deprecated since Solana v1.8.0. Please use {@link
* getLatestBlockhash} instead.
* @return Blockhash
*/
[[deprecated]] PublicKey getRecentBlockhash(
const Commitment &commitment = Commitment::FINALIZED) const;
/**
* Fetch the latest blockhash from the cluster
*/
Blockhash getLatestBlockhash(
const Commitment &commitment = Commitment::FINALIZED) const;
/**
* Returns the current block height of the node
*/
uint64_t getBlockHeight(
const Commitment &commitment = Commitment::FINALIZED) const;
/**
* Returns of the current Transaction has been confirmed or not
*/
bool confirmTransaction(std::string transactionSignature,
Commitment confirmLevel,
uint16_t timeout = 200) const;
/**
* Fetch the current statuses of a batch of signatures
*/
RpcResponseAndContext<std::vector<std::optional<SignatureStatus>>>
getSignatureStatuses(const std::vector<std::string> &signatures,
bool searchTransactionHistory = false) const;
/**
* Returns the current solana versions running on the node
**/
Version getVersion() const;
/**
* Returns the lowest slot that the node has information about in its ledger.
**/
uint64_t minimumLedgerSlot() const;
/**
* Returns the genesis hash
**/
std::string getGenesisHash() const;
/**
* Returns epoch schedule information from this cluster's genesis config
**/
EpochSchedule getEpochSchedule() const;
/**
* Returns the slot that has reached the given or default commitment level
**/
uint64_t getSlot(const GetSlotConfig &config = GetSlotConfig{}) const;
/**
* Returns the current slot leader
**/
std::string getSlotLeader(
const GetSlotConfig &config = GetSlotConfig{}) const;
/**
* Returns the slot of the lowest confirmed block that has not been purged
*from the ledger
**/
uint64_t getFirstAvailableBlock() const;
/**
* Returns epoch activation information for a stake account
**/
StakeActivation getStakeActivation(const PublicKey &pubkey,
const GetStakeActivationConfig &config =
GetStakeActivationConfig{}) const;
/**
* Returns the current inflation governor
**/
InflationGovernor getInflationGovernor(
const commitmentconfig &config = commitmentconfig{}) const;
/**
* Returns the current Transaction count from the ledger
**/
uint64_t getTransactionCount(
const GetSlotConfig &config = GetSlotConfig{}) const;
/**
* Returns information about the current epoch
**/
EpochInfo getEpochInfo(const GetSlotConfig &config = GetSlotConfig{}) const;
/**
* Returns minimum balance required to make account rent exempt
**/
uint64_t getMinimumBalanceForRentExemption(
const std::size_t dataLength,
const commitmentconfig &config = commitmentconfig{}) const;
/**
* Returns the estimated production time of a block.
*/
uint64_t getBlockTime(const uint64_t slot) const;
/**
*Returns information about all the nodes participating in the cluster
*/
std::vector<Nodes> getClusterNodes() const;
/**
*Get the fee the network will charge for a particular Message
*/
getFeeForMessageRes getFeeForMessage(
const std::string message,
const GetSlotConfig &config = GetSlotConfig{}) const;
/**
*Returns a list of recent performance samples, in reverse slot order.
*/
std::vector<RecentPerformanceSamples> getRecentPerformanceSamples(
std::size_t limit) const;
/**
*Returns the 20 largest accounts, by lamport balance
*/
RpcResponseAndContext<std::vector<LargestAccounts>> getLargestAccounts(
const LargestAccountsConfig &config = LargestAccountsConfig{}) const;
/**
* Fetch the current status of a signature
*/
RpcResponseAndContext<std::optional<SignatureStatus>> getSignatureStatus(
const std::string &signature,
bool searchTransactionHistory = false) const;
/**
* Returns the slot leaders for a given slot range
*/
std::vector<std::string> getSlotLeaders(uint64_t startSlot,
uint64_t limit) const;
/**
* Returns information about the current supply.
*/
RpcResponseAndContext<Supply> getSupply(
const GetSupplyConfig &config = GetSupplyConfig{}) const;
/**
* Returns the token balance of an SPL Token account.
*/
RpcResponseAndContext<TokenAccountBalance> getTokenAccountBalance(