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
use isahc::prelude::*; use super::primitives; #[derive(Debug, serde::Serialize)] pub struct RPCRequest <T> { jsonrpc: String, method: String, params: T, id: String } #[derive(Debug, Default, serde_derive::Deserialize)] pub struct RPCError { pub jsonrpc: String, #[serde(skip_deserializing)] pub error: Error, pub id: String } #[derive(Debug, Default, serde_derive::Deserialize)] pub struct Error { pub code: String, pub message: String } #[derive(Debug, serde::Deserialize)] #[serde(untagged)] enum RPCResponse <T> { Success(RPCSuccess<T>), Error(RPCError) } #[derive(Debug, serde::Deserialize)] struct RPCSuccess <T> { pub id: String, pub jsonrpc: String, pub result: T } #[derive(Debug)] pub struct Client { host: String, id: u64 } impl Client { pub fn new(host: &str) -> Client { Client { host: String::from(host), id: 1 } } fn send<T: serde::Serialize>(&self, method: &str, params: T) -> Response<isahc::Body> { let body = serde_json::to_vec(&RPCRequest { jsonrpc: String::from("2.0"), method: String::from(method), id: String::from("1"), params: ¶ms }).unwrap(); isahc::post(&self.host, body) .unwrap() } /// Returns a list of addresses owned by client. /// /// # Arguments /// /// * `none` /// /// # Returns /// /// Vector of accounts owned by the client. /// /// # Example /// /// ``` /// use nimiq_rpc::Client; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.accounts(); /// ``` pub fn accounts(&self) -> Result<Vec<super::primitives::Account>, Error>{ let response = self.send("accounts", ()).json::<RPCResponse<Vec<primitives::Account>>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// Returns the height of most recent block. /// /// # Arguments /// /// * `none` /// /// # Returns /// /// The current block height the client is on. /// /// # Example /// /// ``` /// use nimiq_rpc::Client; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.block_number(); /// ``` pub fn block_number(&self) -> Result<u64, Error>{ let response = self.send("blockNumber", ()).json::<RPCResponse<u64>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.consensus(); /// ``` pub fn consensus(&self) -> Result<String, Error>{ let response = self.send("consensus", ()).json::<RPCResponse<String>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.create_account(); /// ``` pub fn create_account(&self) -> Result<primitives::Wallet, Error>{ let response = self.send("createAccount", ()).json::<RPCResponse<primitives::Wallet>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let tx = nimiq_rpc::primitives::OutgoingTransaction { /// from: "NQ32 R6DB VFM5 M931 7X4E 0N5Q LJ56 9QCR 4T42", /// to: "NQ74 61S8 2FD3 RVPG HU09 1Y57 77E6 BL38 TQH3", /// value: 100, //Lunas /// fee: 0 /// }; /// let result = client.create_raw_transaction(&tx); /// ``` pub fn create_raw_transaction(&self, raw_transaction: &primitives::OutgoingTransaction) -> Result<String, Error>{ let response = self.send("createRawTransaction", raw_transaction).json::<RPCResponse<String>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_account("ad25610feb43d75307763d3f010822a757027429"); /// ``` pub fn get_account(&self, id: &str) -> Result<primitives::Account, Error>{ let response = self.send("getAccount", id).json::<RPCResponse<primitives::Account>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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 smalest unit). /// /// # Example /// /// ``` /// use nimiq_rpc::Client; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_balance("ad25610feb43d75307763d3f010822a757027429"); /// ``` pub fn get_balance(&self, id: &str) -> Result<u64, Error>{ let response = self.send("getBalance", id).json::<RPCResponse<u64>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_block_by_hash("14c91f6d6f3a0b62271e546bb09461231ab7e4d1ddc2c3e1b93de52d48a1da87", false); /// ``` pub fn get_block_by_hash(&self, block_hash: &str, full_transactions: bool) -> Result<primitives::Block, Error>{ let response = self.send("getBlockByHash", (block_hash, full_transactions)).json::<RPCResponse<primitives::Block>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_block_by_number(1234, false); /// ``` pub fn get_block_by_number(&self, block_number: u64, full_transactions: bool) -> Result<primitives::Block, Error>{ let response = self.send("getBlockByNumber", (block_number, full_transactions)).json::<RPCResponse<primitives::Block>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_block_template(); /// ``` pub fn get_block_template(&self) -> Result<primitives::FullBlock, Error>{ let response = self.send("getBlockTemplate", ()).json::<RPCResponse<primitives::FullBlock>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_block_transaction_count_by_hash("dfe7d166f2c86bd10fa4b1f29cd06c13228f893167ce9826137c85758645572f"); /// ``` pub fn get_block_transaction_count_by_hash(&self, block_hash: &str) -> Result<u16, Error>{ let response = self.send("getBlockTransactionCountByHash", block_hash).json::<RPCResponse<u16>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_block_transaction_count_by_number(76415); /// ``` pub fn get_block_transaction_count_by_number(&self, block_number: u64) -> Result<u16, Error>{ let response = self.send("getBlockTransactionCountByNumber", block_number).json::<RPCResponse<u16>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_transaction_by_block_hash_and_index("dfe7d166f2c86bd10fa4b1f29cd06c13228f893167ce9826137c85758645572f", 20); /// ``` pub fn get_transaction_by_block_hash_and_index(&self, block_hash: &str, index: u64) -> Result<primitives::Transaction, Error>{ let response = self.send("getTransactionByBlockHashAndIndex", (block_hash, index)).json::<RPCResponse<primitives::Transaction>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_transaction_by_block_number_and_index(76415, 20); /// ``` pub fn get_transaction_by_block_number_and_index(&self, block_number: u64, index: u16) -> Result<primitives::Transaction, Error>{ let response = self.send("getTransactionByBlockNumberAndIndex", (block_number, index)).json::<RPCResponse<primitives::Transaction>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_transaction_by_hash("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554"); /// ``` pub fn get_transaction_by_hash(&self, transaction_hash: &str) -> Result<primitives::Transaction, Error>{ let response = self.send("getTransactionByHash", transaction_hash).json::<RPCResponse<primitives::Transaction>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_transaction_receipt("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554"); /// ``` pub fn get_transaction_receipt(&self, transaction_hash: &str) -> Result<primitives::TransactionReceipt, Error>{ let response = self.send("getTransactionReceipt", transaction_hash).json::<RPCResponse<primitives::TransactionReceipt>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_transactions_by_address("NQ69 9A4A MB83 HXDQ 4J46 BH5R 4JFF QMA9 C3GN", 10); /// ``` pub fn get_transactions_by_address(&self, address: &str, amount: u16) -> Result<Vec<primitives::Transaction>, Error>{ let response = self.send("getTransactionsByAddress", (address, amount)).json::<RPCResponse<Vec<primitives::Transaction>>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.get_work(); /// ``` pub fn get_work(&self) -> Result<primitives::GetWork, Error>{ let response = self.send("getWork", ()).json::<RPCResponse<primitives::GetWork>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.hashrate(); /// ``` pub fn hashrate(&self) -> Result<f64, Error>{ let response = self.send("hashrate", ()).json::<RPCResponse<f64>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.log("*", "log"); /// ``` pub fn log(&self, tag: &str, level: &str) -> Result<bool, Error>{ let response = self.send("log", (tag, level)).json::<RPCResponse<bool>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn mempool_content(&self) -> Result<Vec<String>, Error>{ let response = self.send("mempoolContent", ()).json::<RPCResponse<Vec<String>>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn miner_address(&self) -> Result<String, Error>{ let response = self.send("minerAddress", ()).json::<RPCResponse<String>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn miner_threads(&self) -> Result<u8, Error>{ let response = self.send("minerThreads", ()).json::<RPCResponse<u8>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn miner_threads_with_update(&self, threads: u16) -> Result<u16, Error>{ let response = self.send("minerThreads", threads).json::<RPCResponse<u16>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn min_fee_per_byte(&self) -> Result<u32, Error>{ let response = self.send("minFeePerByte", ()).json::<RPCResponse<u32>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn min_fee_per_byte_with_update(&self, fee: u32) -> Result<u32, Error>{ let response = self.send("minFeePerByte", fee).json::<RPCResponse<u32>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.mining(); /// ``` pub fn mining(&self) -> Result<bool, Error>{ let response = self.send("mining", ()).json::<RPCResponse<bool>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// Returns number of peers currently connected to the client. /// /// # Arguments /// /// * `none` /// /// # Returns /// /// Integer of the number of connected peers. /// /// # Example /// /// ``` /// use nimiq_rpc::Client; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.peer_count(); /// ``` pub fn peer_count(&self) -> Result<i8, Error>{ let response = self.send("peerCount", ()).json::<RPCResponse<i8>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn peer_list(&self) -> Result<Vec<primitives::PeerList>, Error>{ let response = self.send("peerList", ()).json::<RPCResponse<Vec<primitives::PeerList>>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn peer_state(&self, peer_address: &str) -> Result<primitives::PeerState, Error>{ let response = self.send("peerState", peer_address).json::<RPCResponse<primitives::PeerState>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn peer_state_with_update(&self, peer_address: &str, set: &str) -> Result<primitives::PeerState, Error>{ let response = self.send("peerState", (peer_address, set)).json::<RPCResponse<primitives::PeerState>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn pool_confirmed_balance(&self) -> Result<u64, Error>{ let response = self.send("poolConfirmedBalance", ()).json::<RPCResponse<u64>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } pub fn pool_connection_state(&self) -> Result<u8, Error>{ let response = self.send("poolConnectionState", ()).json::<RPCResponse<u8>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let tx = nimiq_rpc::primitives::OutgoingTransaction { /// from: "NQ32 R6DB VFM5 M931 7X4E 0N5Q LJ56 9QCR 4T42", /// to: "NQ74 61S8 2FD3 RVPG HU09 1Y57 77E6 BL38 TQH3", /// value: 100, //Lunas /// fee: 0 /// }; /// let result = client.create_raw_transaction(&tx); /// let hash = client.send_raw_transaction(&result); /// ``` pub fn send_raw_transaction(&self, transaction_hash: &str) -> Result<String, Error>{ let response = self.send("sendRawTransaction", transaction_hash).json::<RPCResponse<String>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let tx = nimiq_rpc::primitives::OutgoingTransaction { /// from: "NQ32 R6DB VFM5 M931 7X4E 0N5Q LJ56 9QCR 4T42", /// to: "NQ74 61S8 2FD3 RVPG HU09 1Y57 77E6 BL38 TQH3", /// value: 100, //Lunas /// fee: 0 /// }; /// let result = client.send_transaction(&tx); /// ``` pub fn send_transaction(&self, transaction: &primitives::OutgoingTransaction) -> Result<String, Error>{ let response = self.send("sendTransaction", transaction).json::<RPCResponse<String>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.submit_block("0da1....234"); /// ``` pub fn submit_block(&self, full_block: &str) -> Result<(), Error>{ let response = self.send("submitBlock", full_block).json::<RPCResponse<()>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } /// 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; /// let client = Client::new("http://seed-host.com:8648"); /// let result = client.syncing(); /// ``` pub fn syncing(&self) -> Result<primitives::Syncing, Error>{ let response = self.send("syncing", ()).json::<RPCResponse<primitives::Syncing>>(); match response { Ok(RPCResponse::Success(v)) => Ok(v.result), Ok(RPCResponse::Error(v)) => Err(v.error), Err(_) => panic!("Couldn't deserialize response from server!") } } }