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
use base64::Engine;
use jsonrpsee::{
core::{client::ClientT, ClientError as Error},
http_client::{HeaderMap, HttpClient, HttpClientBuilder},
rpc_params,
};
use url::Url;
use crate::primitives::*;
#[derive(Clone)]
pub struct Client {
agent: HttpClient,
}
impl Client {
pub fn new(url: Url) -> Client {
Client {
agent: HttpClientBuilder::default().build(url).unwrap(),
}
}
pub fn new_with_credentials(url: Url, username: String, password: String) -> Client {
let mut s = username;
s.push(':');
s.push_str(&password);
let auth = format!(
"Basic {}",
&*base64::prelude::BASE64_STANDARD.encode(s.as_bytes())
);
let mut headers = HeaderMap::new();
headers.insert("Authorization", auth.parse().unwrap());
Client {
agent: HttpClientBuilder::default()
.set_headers(headers)
.build(url)
.unwrap(),
}
}
/// Returns a list of addresses owned by client.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// Vector of accounts owned by the client.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.accounts();
/// # })
/// ```
pub async fn accounts(&self) -> Result<Vec<Account>, Error> {
let params = rpc_params![];
self.agent.request("accounts", params).await
}
/// Returns the height of most recent block.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// The current block height the client is on.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.block_number().await;
/// # })
/// ```
pub async fn block_number(&self) -> Result<u32, Error> {
let params = rpc_params![];
self.agent.request("blockNumber", params).await
}
/// Returns information on the current consensus state.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// String describing the consensus state. `"established"` is the value for a good state, other values indicate bad state.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.consensus().await;
/// # })
/// ```
pub async fn consensus(&self) -> Result<String, Error> {
let params = rpc_params![];
self.agent.request("consensus", params).await
}
/// Creates a new account and stores its private key in the client store.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// Information on the wallet that was created using the command.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.create_account().await;
/// # })
/// ```
pub async fn create_account(&self) -> Result<Wallet, Error> {
let params = rpc_params![];
self.agent.request("createAccount", params).await
}
/// Creates and signs a transaction without sending it. The transaction can then be send via `sendRawTransaction` without accidentally replaying it.
///
/// # Arguments
///
/// * `raw_transaction`: `&primitives::OutgoingTransaction`
///
/// # Returns
///
/// The Hex-encoded transaction.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let tx = nimiq_rpc::primitives::OutgoingTransaction {
/// from: "NQ32 R6DB VFM5 M931 7X4E 0N5Q LJ56 9QCR 4T42".to_string(),
/// to: "NQ74 61S8 2FD3 RVPG HU09 1Y57 77E6 BL38 TQH3".to_string(),
/// value: 100, //Lunas
/// fee: 0
/// };
/// let result = client.create_raw_transaction(&tx).await;
/// # })
/// ```
pub async fn create_raw_transaction(
&self,
raw_transaction: &OutgoingTransaction,
) -> Result<String, Error> {
let params = rpc_params![raw_transaction];
self.agent.request("createRawTransaction", params).await
}
/// Returns details for the account of given address.
///
/// # Arguments
///
/// * `String`: Address to check for balance.
///
/// # Returns
///
/// Details about the account. Returns the default empty basic account for non-existing accounts.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_account("ad25610feb43d75307763d3f010822a757027429").await;
/// # })
/// ```
pub async fn get_account(&self, id: &str) -> Result<Account, Error> {
let params = rpc_params![id];
self.agent.request("getAccount", params).await
}
/// Returns an Accounts tree chunk.
///
/// # Arguments
///
/// * `String`: Block hash on which the accounts tree chunk is going to be retrieved.
/// * `String`: Start prefix for the accounts tree chunk to retrieve.
///
/// # Returns
///
/// An Accounts tree chunk containing the tail prefix and the length of the chunk.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// let client = Client::new("http://seed-host.com:8648".to_string());
/// let result = client.get_accounts_tree_chunk("14c91f6d6f3a0b62271e546bb09461231ab7e4d1ddc2c3e1b93de52d48a1da87", "").await;
/// ```
pub async fn get_accounts_tree_chunk(
&self,
block_hash: &str,
start_prefix: &str,
) -> Result<AccountsTreeChunk, Error> {
let params = rpc_params![block_hash, start_prefix];
self.agent.request("getAccountsTreeChunk", params).await
}
/// Returns the balance of the account of given address.
///
/// # Arguments
///
/// * `String`: Address to check for balance.
///
/// # Returns
///
/// Details about the account. The current balance at the specified address (in smallest unit).
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_balance("ad25610feb43d75307763d3f010822a757027429").await;
/// # })
/// ```
pub async fn get_balance(&self, id: &str) -> Result<u64, Error> {
let params = rpc_params![id];
self.agent.request("getBalance", params).await
}
/// Returns information about a block by hash.
///
/// # Arguments
///
/// * `String`: Hash of the block to gather information on.
/// * `Boolean`: If `true` it returns the full transaction objects, if `false` only the hashes of the transactions.
///
/// # Returns
///
/// A block object or `null` when no block was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_block_by_hash("14c91f6d6f3a0b62271e546bb09461231ab7e4d1ddc2c3e1b93de52d48a1da87", false).await;
/// # })
/// ```
pub async fn get_block_by_hash(
&self,
block_hash: &str,
full_transactions: bool,
) -> Result<Block, Error> {
let params = rpc_params![block_hash, full_transactions];
self.agent.request("getBlockByHash", params).await
}
/// Returns information about a block by block number.
///
/// # Arguments
///
/// * `Int`: The height of the block to gather information on.
/// * `Boolean`: If `true` it returns the full transaction objects, if `false` only the hashes of the transactions.
///
/// # Returns
///
/// A block object or `null` when no block was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_block_by_number(1234, false).await;
/// # })
/// ```
pub async fn get_block_by_number(
&self,
block_number: u32,
full_transactions: bool,
) -> Result<Block, Error> {
let params = rpc_params![block_number, full_transactions];
self.agent.request("getBlockByNumber", params).await
}
/// Returns a template to build the next block for mining. This will consider pool instructions when connected to a pool.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// A block template object.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_block_template().await;
/// # })
/// ```
pub async fn get_block_template(&self) -> Result<FullBlock, Error> {
let params = rpc_params![];
self.agent.request("getBlockTemplate", params).await
}
/// Returns the number of transactions in a block from a block matching the given block hash.
///
/// # Arguments
///
/// * `String`: Hash of the block.
///
/// # Returns
///
/// Number of transactions in the block found, or `null`, when no block was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_block_transaction_count_by_hash("dfe7d166f2c86bd10fa4b1f29cd06c13228f893167ce9826137c85758645572f").await;
/// # })
/// ```
pub async fn get_block_transaction_count_by_hash(
&self,
block_hash: &str,
) -> Result<u16, Error> {
let params = rpc_params![block_hash];
self.agent
.request("getBlockTransactionCountByHash", params)
.await
}
/// Returns the number of transactions in a block matching the given block number.
///
/// # Arguments
///
/// * `Int`: Height of the block.
///
/// # Returns
///
/// Number of transactions in the block found, or `null`, when no block was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_block_transaction_count_by_number(76415).await;
/// # })
/// ```
pub async fn get_block_transaction_count_by_number(
&self,
block_number: u32,
) -> Result<u16, Error> {
let params = rpc_params![block_number];
self.agent
.request("getBlockTransactionCountByNumber", params)
.await
}
/// Returns information about a transaction by block hash and transaction index position.
///
/// # Arguments
///
/// * `String`: Hash of the block containing the transaction
/// * `Int`: Index of the transaction in the block
///
/// # Returns
///
/// A transaction object or `null` when no transaction was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_transaction_by_block_hash_and_index("dfe7d166f2c86bd10fa4b1f29cd06c13228f893167ce9826137c85758645572f", 20).await;
/// # })
/// ```
pub async fn get_transaction_by_block_hash_and_index(
&self,
block_hash: &str,
index: u16,
) -> Result<Transaction, Error> {
let params = rpc_params![block_hash, index];
self.agent
.request("getTransactionByBlockHashAndIndex", params)
.await
}
/// Returns information about a transaction by block number and transaction index position.
///
/// # Arguments
///
/// * `Int`: Height of the block containing the transaction
/// * `Int`: Index of the transaction in the block
///
/// # Returns
///
/// A transaction object or `null` when no transaction was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_transaction_by_block_number_and_index(76415, 20).await;
/// # })
/// ```
pub async fn get_transaction_by_block_number_and_index(
&self,
block_number: u32,
index: u16,
) -> Result<Transaction, Error> {
let params = rpc_params![block_number, index];
self.agent
.request("getTransactionByBlockNumberAndIndex", params)
.await
}
/// Returns the information about a transaction requested by transaction hash.
///
/// # Arguments
///
/// * `String`: Hash of a transaction
///
/// # Returns
///
/// A transaction object or `null` when no transaction was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_transaction_by_hash("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554").await;
/// # })
/// ```
pub async fn get_transaction_by_hash(
&self,
transaction_hash: &str,
) -> Result<TransactionDetails, Error> {
let params = rpc_params![transaction_hash];
self.agent.request("getTransactionByHash", params).await
}
/// Returns the information about a transaction requested by transaction hash.
///
/// # Arguments
///
/// * `String`: Hash of a transaction
///
/// # Returns
///
/// A transaction object or `null` when no transaction was found.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_transaction_by_hash_2("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554").await;
/// # })
/// ```
pub async fn get_transaction_by_hash_2(
&self,
transaction_hash: &str,
) -> Result<TransactionDetails2, Error> {
let params = rpc_params![transaction_hash];
self.agent.request("getTransactionByHash2", params).await
}
/// Returns the receipt of a transaction by transaction hash.
/// `Note` That the receipt is not available for pending transactions.
///
/// # Arguments
///
/// * `String`: Hash of a transaction
///
/// # Returns
///
/// A transaction receipt object, or `null` when no receipt was found
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_transaction_receipt("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554").await;
/// # })
/// ```
pub async fn get_transaction_receipt(
&self,
transaction_hash: &str,
) -> Result<TransactionReceipt, Error> {
let params = rpc_params![transaction_hash];
self.agent.request("getTransactionReceipt", params).await
}
/// Returns the latest transactions successfully performed by or for an address.
/// `Note` That this information might change when blocks are rewinded on the local state due to forks.
///
/// # Arguments
///
/// * `String`: Address of which transactions should be gathered.
/// * `Int`: Number of transactions that shall be returned.
///
/// # Returns
///
/// Vector of transactions linked to the requested address.
/// `Note` The array will not contain more than the requested amount of transactions, but might contain less, even when more transactions happened. Any interpretation of the length of this array might result in worng assumptions.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_transactions_by_address("NQ69 9A4A MB83 HXDQ 4J46 BH5R 4JFF QMA9 C3GN", 10).await;
/// # })
/// ```
pub async fn get_transactions_by_address(
&self,
address: &str,
amount: u16,
) -> Result<Vec<TransactionDetails>, Error> {
let params = rpc_params![address, amount];
self.agent.request("getTransactionsByAddress", params).await
}
/// Returns instructions to mine the next block. This will consider pool instructions when connected to a pool.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// Mining work instructions
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_work().await;
/// # })
/// ```
pub async fn get_work(&self) -> Result<GetWork, Error> {
let params = rpc_params![];
self.agent.request("getWork", params).await
}
/// Returns the number of hashes per second that the node is mining with.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// Number of hashes per second.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.hashrate().await;
/// # })
/// ```
pub async fn hashrate(&self) -> Result<f64, Error> {
let params = rpc_params![];
self.agent.request("hashrate", params).await
}
/// Sets the log level of the node.
///
/// # Arguments
///
/// * `String`: Tag: If `'*'` the log level is set globally, otherwise the log level is applied only on this tag.
/// * `String`: Minimum log level to display. (Valid options: `trace`, `verbose`, `debug`, `info`, `warn`, `error`, `assert`)
///
/// # Returns
///
/// `true`
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.log("*", "log").await;
/// # })
/// ```
pub async fn log(&self, tag: &str, level: &str) -> Result<bool, Error> {
let params = rpc_params![tag, level];
self.agent.request("log", params).await
}
pub async fn mempool_content(&self) -> Result<Vec<String>, Error> {
let params = rpc_params![];
self.agent.request("mempoolContent", params).await
}
pub async fn miner_address(&self) -> Result<String, Error> {
let params = rpc_params![];
self.agent.request("minerAddress", params).await
}
pub async fn miner_threads(&self) -> Result<u8, Error> {
let params = rpc_params![];
self.agent.request("minerThreads", params).await
}
pub async fn miner_threads_with_update(&self, threads: u16) -> Result<u16, Error> {
let params = rpc_params![threads];
self.agent.request("minerThreads", params).await
}
pub async fn min_fee_per_byte(&self) -> Result<u32, Error> {
let params = rpc_params![];
self.agent.request("minFeePerByte", params).await
}
pub async fn min_fee_per_byte_with_update(&self, fee: u32) -> Result<u32, Error> {
let params = rpc_params![fee];
self.agent.request("minFeePerByte", params).await
}
/// Returns `true` if client is actively mining new blocks.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// Returns `true` if the client is mining, otherwise `false`.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.mining().await;
/// # })
/// ```
pub async fn mining(&self) -> Result<bool, Error> {
let params = rpc_params![];
self.agent.request("mining", params).await
}
/// Returns number of peers currently connected to the client.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// Integer of the number of connected peers.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.peer_count().await;
/// # })
/// ```
pub async fn peer_count(&self) -> Result<i8, Error> {
let params = rpc_params![];
self.agent.request("peerCount", params).await
}
pub async fn peer_list(&self) -> Result<Vec<PeerList>, Error> {
let params = rpc_params![];
self.agent.request("peerList", params).await
}
pub async fn peer_state(&self, peer_address: &str) -> Result<PeerState, Error> {
let params = rpc_params![peer_address];
self.agent.request("peerState", params).await
}
pub async fn peer_state_with_update(
&self,
peer_address: &str,
set: &str,
) -> Result<PeerState, Error> {
let params = rpc_params![peer_address, set];
self.agent.request("peerState", params).await
}
pub async fn pool_confirmed_balance(&self) -> Result<u64, Error> {
let params = rpc_params![];
self.agent.request("poolConfirmedBalance", params).await
}
pub async fn pool_connection_state(&self) -> Result<u8, Error> {
let params = rpc_params![];
self.agent.request("poolConnectionState", params).await
}
/// Sends a signed message call transaction or a contract creation, if the data field contains code.
///
/// # Arguments
///
/// * `String`: The hex encoded signed transaction
///
/// # Returns
///
/// The Hex-encoded transaction hash.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let tx = nimiq_rpc::primitives::OutgoingTransaction {
/// from: "NQ32 R6DB VFM5 M931 7X4E 0N5Q LJ56 9QCR 4T42".to_string(),
/// to: "NQ74 61S8 2FD3 RVPG HU09 1Y57 77E6 BL38 TQH3".to_string(),
/// value: 100, //Lunas
/// fee: 0
/// };
/// let result = client.create_raw_transaction(&tx).await;
/// let hash = client.send_raw_transaction(&result.unwrap()).await;
/// # })
/// ```
pub async fn send_raw_transaction(&self, transaction_hash: &str) -> Result<String, Error> {
let params = rpc_params![transaction_hash];
self.agent.request("sendRawTransaction", params).await
}
/// Creates new message call transaction or a contract creation, if the data field contains code.
///
/// # Arguments
///
/// * `OutgoingTransaction`: The transaction object
///
/// # Returns
///
/// The Hex-encoded transaction hash.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let tx = nimiq_rpc::primitives::OutgoingTransaction {
/// from: "NQ32 R6DB VFM5 M931 7X4E 0N5Q LJ56 9QCR 4T42".to_string(),
/// to: "NQ74 61S8 2FD3 RVPG HU09 1Y57 77E6 BL38 TQH3".to_string(),
/// value: 100, //Lunas
/// fee: 0
/// };
/// let result = client.send_transaction(&tx).await;
/// # })
/// ```
pub async fn send_transaction(
&self,
transaction: &OutgoingTransaction,
) -> Result<String, Error> {
let params = rpc_params![transaction];
self.agent.request("sendTransaction", params).await
}
/// Submits a block to the node. When the block is valid, the node will forward it to other nodes in the network.
///
/// # Arguments
///
/// * `String`: Hex-encoded full block (including header, interlink and body). When submitting work from `getWork`, remember to include the `suffix`.
///
/// # Returns
///
/// Nothing
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.submit_block("0da1....234").await;
/// # })
/// ```
pub async fn submit_block(&self, full_block: &str) -> Result<(), Error> {
let params = rpc_params![full_block];
self.agent.request("submitBlock", params).await
}
/// Returns an object with data about the sync status or `false`.
///
/// # Arguments
///
/// * `none`
///
/// # Returns
///
/// An object with sync status data or `false`, when not syncing
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.syncing().await;
/// # })
/// ```
pub async fn syncing(&self) -> Result<Syncing, Error> {
let params = rpc_params![];
self.agent.request("syncing", params).await
}
/// Returns a constant
///
/// # Arguments
///
/// * `String`: Name of the constant to be obtained.
///
/// # Returns
///
/// An integer with the value of the constant.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.get_constant("BaseMiniConsensusAgent.MEMPOOL_DELAY_MIN").await;
/// # })
/// ```
pub async fn get_constant(&self, constant: &str) -> Result<u64, Error> {
let params = rpc_params![constant];
self.agent.request("constant", params).await
}
/// Sets a constant with the value provided and returns the new value on success
///
/// # Arguments
///
/// * `String`: Name of the constant to be set.
/// * `Int`: The value to be set to the constant.
///
/// # Returns
///
/// An integer with the new value of the constant.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.set_constant("BaseMiniConsensusAgent.MEMPOOL_DELAY_MIN", 6).await;
/// # })
/// ```
pub async fn set_constant(&self, constant: &str, value: u64) -> Result<u64, Error> {
let params = rpc_params![constant, value];
self.agent.request("constant", params).await
}
/// Resets a constant and returns the new value on success
///
/// # Arguments
///
/// * `String`: Name of the constant to be reset.
///
/// # Returns
///
/// An integer with the new value of the constant.
///
/// # Example
///
/// ```
/// use nimiq_rpc::Client;
/// use url::Url;
/// # tokio_test::block_on(async {
/// let client = Client::new(Url::parse("http://seed-host.com:8648").unwrap());
/// let result = client.reset_constant("BaseMiniConsensusAgent.MEMPOOL_DELAY_MIN").await;
/// # })
/// ```
pub async fn reset_constant(&self, constant: &str) -> Result<u64, Error> {
let params = rpc_params![constant, "reset"];
self.agent.request("constant", params).await
}
}