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 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use fastcrypto::encoding::Base64;
use futures::stream;
use futures::StreamExt;
use futures_core::Stream;
use jsonrpsee::core::client::Subscription;
use std::collections::BTreeMap;
use std::future;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use sui_json_rpc_types::DevInspectArgs;
use sui_json_rpc_types::SuiData;
use crate::error::{Error, SuiRpcResult};
use crate::RpcClient;
use sui_json_rpc_api::{
CoinReadApiClient, GovernanceReadApiClient, IndexerApiClient, MoveUtilsClient, ReadApiClient,
WriteApiClient,
};
use sui_json_rpc_types::CheckpointPage;
use sui_json_rpc_types::{
Balance, Checkpoint, CheckpointId, Coin, CoinPage, DelegatedStake, DevInspectResults,
DryRunTransactionBlockResponse, DynamicFieldPage, EventFilter, EventPage, ObjectsPage,
ProtocolConfigResponse, SuiCoinMetadata, SuiCommittee, SuiEvent, SuiGetPastObjectRequest,
SuiMoveNormalizedModule, SuiObjectDataOptions, SuiObjectResponse, SuiObjectResponseQuery,
SuiPastObjectResponse, SuiTransactionBlockEffects, SuiTransactionBlockResponse,
SuiTransactionBlockResponseOptions, SuiTransactionBlockResponseQuery, TransactionBlocksPage,
TransactionFilter,
};
use sui_types::balance::Supply;
use sui_types::base_types::{ObjectID, SequenceNumber, SuiAddress, TransactionDigest};
use sui_types::dynamic_field::DynamicFieldName;
use sui_types::event::EventID;
use sui_types::messages_checkpoint::CheckpointSequenceNumber;
use sui_types::quorum_driver_types::ExecuteTransactionRequestType;
use sui_types::sui_serde::BigInt;
use sui_types::sui_system_state::sui_system_state_summary::SuiSystemStateSummary;
use sui_types::transaction::{Transaction, TransactionData, TransactionKind};
const WAIT_FOR_LOCAL_EXECUTION_TIMEOUT: Duration = Duration::from_secs(60);
const WAIT_FOR_LOCAL_EXECUTION_DELAY: Duration = Duration::from_millis(200);
const WAIT_FOR_LOCAL_EXECUTION_INTERVAL: Duration = Duration::from_secs(2);
/// The main read API structure with functions for retrieving data about different objects and transactions
#[derive(Debug)]
pub struct ReadApi {
api: Arc<RpcClient>,
}
impl ReadApi {
pub(crate) fn new(api: Arc<RpcClient>) -> Self {
Self { api }
}
/// Return a paginated response with the objects owned by the given address, or an error upon failure.
///
/// Note that if the address owns more than `QUERY_MAX_RESULT_LIMIT` objects (default is 50),
/// the pagination is not accurate, because previous page may have been updated when the next page is fetched.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let owned_objects = sui
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_owned_objects(
&self,
address: SuiAddress,
query: Option<SuiObjectResponseQuery>,
cursor: Option<ObjectID>,
limit: Option<usize>,
) -> SuiRpcResult<ObjectsPage> {
Ok(self
.api
.http
.get_owned_objects(address, query, cursor, limit)
.await?)
}
/// Return a paginated response with the dynamic fields owned by the given [ObjectID], or an error upon failure.
///
/// The return type is a list of `DynamicFieldInfo` objects, where the field name is always present,
/// represented as a Move `Value`.
///
/// If the field is a dynamic field, returns the ID of the Field object (which contains both the name and the value).
/// If the field is a dynamic object field, it returns the ID of the Object (the value of the field).
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::{ObjectID, SuiAddress};
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let owned_objects = sui
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// // this code example assumes that there are previous owned objects
/// let object = owned_objects.data.get(0).expect(&format!(
/// "No owned objects for this address {}",
/// address
/// ));
/// let object_data = object.data.as_ref().expect(&format!(
/// "No object data for this SuiObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let dynamic_fields = sui
/// .read_api()
/// .get_dynamic_fields(object_id, None, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_dynamic_fields(
&self,
object_id: ObjectID,
cursor: Option<ObjectID>,
limit: Option<usize>,
) -> SuiRpcResult<DynamicFieldPage> {
Ok(self
.api
.http
.get_dynamic_fields(object_id, cursor, limit)
.await?)
}
/// Return the dynamic field object information for a specified object.
pub async fn get_dynamic_field_object(
&self,
parent_object_id: ObjectID,
name: DynamicFieldName,
) -> SuiRpcResult<SuiObjectResponse> {
Ok(self
.api
.http
.get_dynamic_field_object(parent_object_id, name)
.await?)
}
/// Return a parsed past object for the provided [ObjectID] and version, or an error upon failure.
///
/// An object's version increases (though it is not guaranteed that it increases always by 1) when
/// the object is mutated. A past object can be used to understand how the object changed over time,
/// i.e. what was the total balance at a specific version.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::{ObjectID, SuiAddress};
/// use sui_json_rpc_types::SuiObjectDataOptions;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let owned_objects = sui
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// // this code example assumes that there are previous owned objects
/// let object = owned_objects.data.get(0).expect(&format!(
/// "No owned objects for this address {}",
/// address
/// ));
/// let object_data = object.data.as_ref().expect(&format!(
/// "No object data for this SuiObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let version = object_data.version;
/// let past_object = sui
/// .read_api()
/// .try_get_parsed_past_object(
/// object_id,
/// version,
/// SuiObjectDataOptions {
/// show_type: true,
/// show_owner: true,
/// show_previous_transaction: true,
/// show_display: true,
/// show_content: true,
/// show_bcs: true,
/// show_storage_rebate: true,
/// },
/// )
/// .await?;
/// Ok(())
/// }
///```
pub async fn try_get_parsed_past_object(
&self,
object_id: ObjectID,
version: SequenceNumber,
options: SuiObjectDataOptions,
) -> SuiRpcResult<SuiPastObjectResponse> {
Ok(self
.api
.http
.try_get_past_object(object_id, version, Some(options))
.await?)
}
/// Return a list of [SuiPastObjectResponse] objects, or an error upon failure.
///
/// See [this function](ReadApi::try_get_parsed_past_object) for more details about past objects.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::{ObjectID, SuiAddress};
/// use sui_json_rpc_types::{SuiObjectDataOptions, SuiGetPastObjectRequest};
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let owned_objects = sui
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// // this code example assumes that there are previous owned objects
/// let object = owned_objects.data.get(0).expect(&format!(
/// "No owned objects for this address {}",
/// address
/// ));
/// let object_data = object.data.as_ref().expect(&format!(
/// "No object data for this SuiObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let version = object_data.version;
/// let past_object = sui
/// .read_api()
/// .try_get_parsed_past_object(
/// object_id,
/// version,
/// SuiObjectDataOptions {
/// show_type: true,
/// show_owner: true,
/// show_previous_transaction: true,
/// show_display: true,
/// show_content: true,
/// show_bcs: true,
/// show_storage_rebate: true,
/// },
/// )
/// .await?;
/// let past_object = past_object.into_object()?;
/// let multi_past_object = sui
/// .read_api()
/// .try_multi_get_parsed_past_object(
/// vec![SuiGetPastObjectRequest {
/// object_id: past_object.object_id,
/// version: past_object.version,
/// }],
/// SuiObjectDataOptions {
/// show_type: true,
/// show_owner: true,
/// show_previous_transaction: true,
/// show_display: true,
/// show_content: true,
/// show_bcs: true,
/// show_storage_rebate: true,
/// },
/// )
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn try_multi_get_parsed_past_object(
&self,
past_objects: Vec<SuiGetPastObjectRequest>,
options: SuiObjectDataOptions,
) -> SuiRpcResult<Vec<SuiPastObjectResponse>> {
Ok(self
.api
.http
.try_multi_get_past_objects(past_objects, Some(options))
.await?)
}
/// Return a [SuiObjectResponse] based on the provided [ObjectID] and [SuiObjectDataOptions], or an error upon failure.
///
/// The [SuiObjectResponse] contains two fields:
/// 1) `data` for the object's data (see [SuiObjectData](sui_json_rpc_types::SuiObjectData)),
/// 2) `error` for the error (if any) (see [SuiObjectResponseError](sui_types::error::SuiObjectResponseError)).
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use sui_json_rpc_types::SuiObjectDataOptions;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let owned_objects = sui
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// // this code example assumes that there are previous owned objects
/// let object = owned_objects.data.get(0).expect(&format!(
/// "No owned objects for this address {}",
/// address
/// ));
/// let object_data = object.data.as_ref().expect(&format!(
/// "No object data for this SuiObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let object = sui.read_api().get_object_with_options(object_id,
/// SuiObjectDataOptions {
/// show_type: true,
/// show_owner: true,
/// show_previous_transaction: true,
/// show_display: true,
/// show_content: true,
/// show_bcs: true,
/// show_storage_rebate: true,
/// },
/// ).await?;
/// Ok(())
/// }
/// ```
pub async fn get_object_with_options(
&self,
object_id: ObjectID,
options: SuiObjectDataOptions,
) -> SuiRpcResult<SuiObjectResponse> {
Ok(self.api.http.get_object(object_id, Some(options)).await?)
}
/// Return a list of [SuiObjectResponse] from the given vector of [ObjectID]s and [SuiObjectDataOptions], or an error upon failure.
///
/// If only one object is needed, use the [get_object_with_options](ReadApi::get_object_with_options) function instead.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use sui_json_rpc_types::SuiObjectDataOptions;
/// use std::str::FromStr;
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let owned_objects = sui
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// // this code example assumes that there are previous owned objects
/// let object = owned_objects.data.get(0).expect(&format!(
/// "No owned objects for this address {}",
/// address
/// ));
/// let object_data = object.data.as_ref().expect(&format!(
/// "No object data for this SuiObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let object_ids = vec![object_id]; // and other object ids
/// let object = sui.read_api().multi_get_object_with_options(object_ids,
/// SuiObjectDataOptions {
/// show_type: true,
/// show_owner: true,
/// show_previous_transaction: true,
/// show_display: true,
/// show_content: true,
/// show_bcs: true,
/// show_storage_rebate: true,
/// },
/// ).await?;
/// Ok(())
/// }
/// ```
pub async fn multi_get_object_with_options(
&self,
object_ids: Vec<ObjectID>,
options: SuiObjectDataOptions,
) -> SuiRpcResult<Vec<SuiObjectResponse>> {
Ok(self
.api
.http
.multi_get_objects(object_ids, Some(options))
.await?)
}
/// Return An object's bcs content [`Vec<u8>`] based on the provided [ObjectID], or an error upon failure.
pub async fn get_move_object_bcs(&self, object_id: ObjectID) -> SuiRpcResult<Vec<u8>> {
let resp = self
.get_object_with_options(object_id, SuiObjectDataOptions::default().with_bcs())
.await?
.into_object()
.map_err(|e| {
Error::DataError(format!("Can't get bcs of object {:?}: {:?}", object_id, e))
})?;
// unwrap: requested bcs data
let move_object = resp.bcs.unwrap();
let raw_move_obj = move_object.try_into_move().ok_or(Error::DataError(format!(
"Object {:?} is not a MoveObject",
object_id
)))?;
Ok(raw_move_obj.bcs_bytes)
}
/// Return the total number of transaction blocks known to server, or an error upon failure.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let total_transaction_blocks = sui
/// .read_api()
/// .get_total_transaction_blocks()
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_total_transaction_blocks(&self) -> SuiRpcResult<u64> {
Ok(*self.api.http.get_total_transaction_blocks().await?)
}
/// Return a transaction and its effects in a [SuiTransactionBlockResponse] based on its
/// [TransactionDigest], or an error upon failure.
pub async fn get_transaction_with_options(
&self,
digest: TransactionDigest,
options: SuiTransactionBlockResponseOptions,
) -> SuiRpcResult<SuiTransactionBlockResponse> {
Ok(self
.api
.http
.get_transaction_block(digest, Some(options))
.await?)
}
/// Return a list of [SuiTransactionBlockResponse] based on the given vector of [TransactionDigest], or an error upon failure.
///
/// If only one transaction data is needed, use the
/// [get_transaction_with_options](ReadApi::get_transaction_with_options) function instead.
pub async fn multi_get_transactions_with_options(
&self,
digests: Vec<TransactionDigest>,
options: SuiTransactionBlockResponseOptions,
) -> SuiRpcResult<Vec<SuiTransactionBlockResponse>> {
Ok(self
.api
.http
.multi_get_transaction_blocks(digests, Some(options))
.await?)
}
/// Return the [SuiCommittee] information for the provided `epoch`, or an error upon failure.
///
/// The [SuiCommittee] contains the validators list and their information (name and stakes).
///
/// The argument `epoch` is either a known epoch id or `None` for the current epoch.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let committee_info = sui
/// .read_api()
/// .get_committee_info(None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_committee_info(
&self,
epoch: Option<BigInt<u64>>,
) -> SuiRpcResult<SuiCommittee> {
Ok(self.api.http.get_committee_info(epoch).await?)
}
/// Return a paginated response with all transaction blocks information, or an error upon failure.
pub async fn query_transaction_blocks(
&self,
query: SuiTransactionBlockResponseQuery,
cursor: Option<TransactionDigest>,
limit: Option<usize>,
descending_order: bool,
) -> SuiRpcResult<TransactionBlocksPage> {
Ok(self
.api
.http
.query_transaction_blocks(query, cursor, limit, Some(descending_order))
.await?)
}
/// Return the first four bytes of the chain's genesis checkpoint digest, or an error upon failure.
pub async fn get_chain_identifier(&self) -> SuiRpcResult<String> {
Ok(self.api.http.get_chain_identifier().await?)
}
/// Return a checkpoint, or an error upon failure.
///
/// A Sui checkpoint is a sequence of transaction sets that a quorum of validators
/// agree upon as having been executed within the Sui system.
pub async fn get_checkpoint(&self, id: CheckpointId) -> SuiRpcResult<Checkpoint> {
Ok(self.api.http.get_checkpoint(id).await?)
}
/// Return a paginated list of checkpoints, or an error upon failure.
pub async fn get_checkpoints(
&self,
cursor: Option<BigInt<u64>>,
limit: Option<usize>,
descending_order: bool,
) -> SuiRpcResult<CheckpointPage> {
Ok(self
.api
.http
.get_checkpoints(cursor, limit, descending_order)
.await?)
}
/// Return the sequence number of the latest checkpoint that has been executed, or an error upon failure.
pub async fn get_latest_checkpoint_sequence_number(
&self,
) -> SuiRpcResult<CheckpointSequenceNumber> {
Ok(*self
.api
.http
.get_latest_checkpoint_sequence_number()
.await?)
}
/// Return a stream of [SuiTransactionBlockResponse], or an error upon failure.
pub fn get_transactions_stream(
&self,
query: SuiTransactionBlockResponseQuery,
cursor: Option<TransactionDigest>,
descending_order: bool,
) -> impl Stream<Item = SuiTransactionBlockResponse> + '_ {
stream::unfold(
(vec![], cursor, true, query),
move |(mut data, cursor, first, query)| async move {
if let Some(item) = data.pop() {
Some((item, (data, cursor, false, query)))
} else if (cursor.is_none() && first) || cursor.is_some() {
let page = self
.query_transaction_blocks(
query.clone(),
cursor,
Some(100),
descending_order,
)
.await
.ok()?;
let mut data = page.data;
data.reverse();
data.pop()
.map(|item| (item, (data, page.next_cursor, false, query)))
} else {
None
}
},
)
}
/// Subscribe to a stream of transactions.
///
/// This is only available through WebSockets.
pub async fn subscribe_transaction(
&self,
filter: TransactionFilter,
) -> SuiRpcResult<impl Stream<Item = SuiRpcResult<SuiTransactionBlockEffects>>> {
let Some(c) = &self.api.ws else {
return Err(Error::Subscription(
"Subscription only supported by WebSocket client.".to_string(),
));
};
let subscription: Subscription<SuiTransactionBlockEffects> =
c.subscribe_transaction(filter).await?;
Ok(subscription.map(|item| Ok(item?)))
}
/// Return a map consisting of the move package name and the normalized module, or an error upon failure.
pub async fn get_normalized_move_modules_by_package(
&self,
package: ObjectID,
) -> SuiRpcResult<BTreeMap<String, SuiMoveNormalizedModule>> {
Ok(self
.api
.http
.get_normalized_move_modules_by_package(package)
.await?)
}
// TODO(devx): we can probably cache this given an epoch
/// Return the reference gas price, or an error upon failure.
pub async fn get_reference_gas_price(&self) -> SuiRpcResult<u64> {
Ok(*self.api.http.get_reference_gas_price().await?)
}
/// Dry run a transaction block given the provided transaction data. Returns an error upon failure.
///
/// Simulate running the transaction, including all standard checks, without actually running it.
/// This is useful for estimating the gas fees of a transaction before executing it.
/// You can also use it to identify any side-effects of a transaction before you execute it on the network.
pub async fn dry_run_transaction_block(
&self,
tx: TransactionData,
) -> SuiRpcResult<DryRunTransactionBlockResponse> {
Ok(self
.api
.http
.dry_run_transaction_block(Base64::from_bytes(&bcs::to_bytes(&tx)?))
.await?)
}
/// Return the inspection of the transaction block, or an error upon failure.
///
/// Use this function to inspect the current state of the network by running a programmable
/// transaction block without committing its effects on chain. Unlike
/// [dry_run_transaction_block](ReadApi::dry_run_transaction_block),
/// dev inspect will not validate whether the transaction block
/// would succeed or fail under normal circumstances, e.g.:
///
/// - Transaction inputs are not checked for ownership (i.e. you can
/// construct calls involving objects you do not own).
/// - Calls are not checked for visibility (you can call private functions on modules)
/// - Inputs of any type can be constructed and passed in, (including
/// Coins and other objects that would usually need to be constructed
/// with a move call).
/// - Function returns do not need to be used, even if they do not have `drop`.
///
/// Dev inspect's output includes a breakdown of results returned by every transaction
/// in the block, as well as the transaction's effects.
///
/// To run an accurate simulation of a transaction and understand whether
/// it will successfully validate and run,
/// use the [dry_run_transaction_block](ReadApi::dry_run_transaction_block) function instead.
pub async fn dev_inspect_transaction_block(
&self,
sender_address: SuiAddress,
tx: TransactionKind,
gas_price: Option<BigInt<u64>>,
epoch: Option<BigInt<u64>>,
additional_args: Option<DevInspectArgs>,
) -> SuiRpcResult<DevInspectResults> {
Ok(self
.api
.http
.dev_inspect_transaction_block(
sender_address,
Base64::from_bytes(&bcs::to_bytes(&tx)?),
gas_price,
epoch,
additional_args,
)
.await?)
}
/// Return the protocol config, or an error upon failure.
pub async fn get_protocol_config(
&self,
version: Option<BigInt<u64>>,
) -> SuiRpcResult<ProtocolConfigResponse> {
Ok(self.api.http.get_protocol_config(version).await?)
}
pub async fn try_get_object_before_version(
&self,
object_id: ObjectID,
version: SequenceNumber,
) -> SuiRpcResult<SuiPastObjectResponse> {
Ok(self
.api
.http
.try_get_object_before_version(object_id, version)
.await?)
}
}
/// Coin Read API provides the functionality needed to get information from the Sui network regarding the coins owned by an address.
#[derive(Debug, Clone)]
pub struct CoinReadApi {
api: Arc<RpcClient>,
}
impl CoinReadApi {
pub(crate) fn new(api: Arc<RpcClient>) -> Self {
Self { api }
}
/// Return a paginated response with the coins for the given address, or an error upon failure.
///
/// The coins can be filtered by `coin_type` (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
/// or use `None` for the default `Coin<SUI>`.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let coins = sui
/// .coin_read_api()
/// .get_coins(address, None, None, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_coins(
&self,
owner: SuiAddress,
coin_type: Option<String>,
cursor: Option<ObjectID>,
limit: Option<usize>,
) -> SuiRpcResult<CoinPage> {
Ok(self
.api
.http
.get_coins(owner, coin_type, cursor, limit)
.await?)
}
/// Return a paginated response with all the coins for the given address, or an error upon failure.
///
/// This function includes all coins. If needed to filter by coin type, use the `get_coins` method instead.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let coins = sui
/// .coin_read_api()
/// .get_all_coins(address, None, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_all_coins(
&self,
owner: SuiAddress,
cursor: Option<ObjectID>,
limit: Option<usize>,
) -> SuiRpcResult<CoinPage> {
Ok(self.api.http.get_all_coins(owner, cursor, limit).await?)
}
/// Return the coins for the given address as a stream.
///
/// The coins can be filtered by `coin_type` (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
/// or use `None` for the default `Coin<SUI>`.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let coins = sui
/// .coin_read_api()
/// .get_coins_stream(address, None);
/// Ok(())
/// }
/// ```
pub fn get_coins_stream(
&self,
owner: SuiAddress,
coin_type: Option<String>,
) -> impl Stream<Item = Coin> + '_ {
stream::unfold(
(
vec![],
/* cursor */ None,
/* has_next_page */ true,
coin_type,
),
move |(mut data, cursor, has_next_page, coin_type)| async move {
if let Some(item) = data.pop() {
Some((item, (data, cursor, /* has_next_page */ true, coin_type)))
} else if has_next_page {
let page = self
.get_coins(owner, coin_type.clone(), cursor, Some(100))
.await
.ok()?;
let mut data = page.data;
data.reverse();
data.pop().map(|item| {
(
item,
(data, page.next_cursor, page.has_next_page, coin_type),
)
})
} else {
None
}
},
)
}
/// Return a list of coins for the given address, or an error upon failure.
///
/// Note that the function selects coins to meet or exceed the requested `amount`.
/// If that it is not possible, it will fail with an insufficient fund error.
///
/// The coins can be filtered by `coin_type` (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
/// or use `None` to use the default `Coin<SUI>`.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let coins = sui
/// .coin_read_api()
/// .select_coins(address, None, 5, vec![])
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn select_coins(
&self,
address: SuiAddress,
coin_type: Option<String>,
amount: u128,
exclude: Vec<ObjectID>,
) -> SuiRpcResult<Vec<Coin>> {
let mut total = 0u128;
let coins = self
.get_coins_stream(address, coin_type)
.filter(|coin: &Coin| future::ready(!exclude.contains(&coin.coin_object_id)))
.take_while(|coin: &Coin| {
let ready = future::ready(total < amount);
total += coin.balance as u128;
ready
})
.collect::<Vec<_>>()
.await;
if total < amount {
return Err(Error::InsufficientFund { address, amount });
}
Ok(coins)
}
/// Return the balance for the given coin type owned by address, or an error upon failure.
///
/// Note that this function sums up all the balances of all the coins matching
/// the given coin type. By default, if `coin_type` is set to `None`,
/// it will use the default `Coin<SUI>`.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let balance = sui
/// .coin_read_api()
/// .get_balance(address, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_balance(
&self,
owner: SuiAddress,
coin_type: Option<String>,
) -> SuiRpcResult<Balance> {
Ok(self.api.http.get_balance(owner, coin_type).await?)
}
/// Return a list of balances for each coin type owned by the given address,
/// or an error upon failure.
///
/// Note that this function groups the coins by coin type, and sums up all their balances.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// use std::str::FromStr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let address = SuiAddress::from_str("0x0000....0000")?;
/// let all_balances = sui
/// .coin_read_api()
/// .get_all_balances(address)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_all_balances(&self, owner: SuiAddress) -> SuiRpcResult<Vec<Balance>> {
Ok(self.api.http.get_all_balances(owner).await?)
}
/// Return the coin metadata (name, symbol, description, decimals, etc.) for a given coin type,
/// or an error upon failure.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let coin_metadata = sui
/// .coin_read_api()
/// .get_coin_metadata("0x2::sui::SUI".to_string())
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_coin_metadata(
&self,
coin_type: String,
) -> SuiRpcResult<Option<SuiCoinMetadata>> {
Ok(self.api.http.get_coin_metadata(coin_type).await?)
}
/// Return the total supply for a given coin type, or an error upon failure.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let total_supply = sui
/// .coin_read_api()
/// .get_total_supply("0x2::sui::SUI".to_string())
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_total_supply(&self, coin_type: String) -> SuiRpcResult<Supply> {
Ok(self.api.http.get_total_supply(coin_type).await?)
}
}
/// Event API provides the functionality to fetch, query, or subscribe to events on the Sui network.
#[derive(Clone)]
pub struct EventApi {
api: Arc<RpcClient>,
}
impl EventApi {
pub(crate) fn new(api: Arc<RpcClient>) -> Self {
Self { api }
}
/// Return a stream of events, or an error upon failure.
///
/// Subscription is only possible via WebSockets.
/// For a list of possible event filters, see [EventFilter].
///
/// # Examples
///
/// ```rust, no_run
/// use futures::StreamExt;
/// use std::str::FromStr;
/// use sui_json_rpc_types::EventFilter;
/// use sui_sdk::SuiClientBuilder;
/// use sui_types::base_types::SuiAddress;
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default()
/// .ws_url("wss://rpc.mainnet.sui.io:443")
/// .build("https://fullnode.mainnet.sui.io:443")
/// .await?;
/// let mut subscribe_all = sui
/// .event_api()
/// .subscribe_event(EventFilter::All([]))
/// .await?;
/// loop {
/// println!("{:?}", subscribe_all.next().await);
/// }
/// Ok(())
/// }
/// ```
pub async fn subscribe_event(
&self,
filter: EventFilter,
) -> SuiRpcResult<impl Stream<Item = SuiRpcResult<SuiEvent>>> {
match &self.api.ws {
Some(c) => {
let subscription: Subscription<SuiEvent> = c.subscribe_event(filter).await?;
Ok(subscription.map(|item| Ok(item?)))
}
_ => Err(Error::Subscription(
"Subscription only supported by WebSocket client.".to_string(),
)),
}
}
/// Return a list of events for the given transaction digest, or an error upon failure.
pub async fn get_events(&self, digest: TransactionDigest) -> SuiRpcResult<Vec<SuiEvent>> {
Ok(self.api.http.get_events(digest).await?)
}
/// Return a paginated response with events for the given event filter, or an error upon failure.
///
/// The ordering of the events can be set with the `descending_order` argument.
/// For a list of possible event filters, see [EventFilter].
pub async fn query_events(
&self,
query: EventFilter,
cursor: Option<EventID>,
limit: Option<usize>,
descending_order: bool,
) -> SuiRpcResult<EventPage> {
Ok(self
.api
.http
.query_events(query, cursor, limit, Some(descending_order))
.await?)
}
/// Return a stream of events for the given event filter.
///
/// The ordering of the events can be set with the `descending_order` argument.
/// For a list of possible event filters, see [EventFilter].
pub fn get_events_stream(
&self,
query: EventFilter,
cursor: Option<EventID>,
descending_order: bool,
) -> impl Stream<Item = SuiEvent> + '_ {
stream::unfold(
(vec![], cursor, true, query),
move |(mut data, cursor, first, query)| async move {
if let Some(item) = data.pop() {
Some((item, (data, cursor, false, query)))
} else if (cursor.is_none() && first) || cursor.is_some() {
let page = self
.query_events(query.clone(), cursor, Some(100), descending_order)
.await
.ok()?;
let mut data = page.data;
data.reverse();
data.pop()
.map(|item| (item, (data, page.next_cursor, false, query)))
} else {
None
}
},
)
}
}
/// Quorum API that provides functionality to execute a transaction block and submit it to the fullnode(s).
#[derive(Clone)]
pub struct QuorumDriverApi {
api: Arc<RpcClient>,
}
impl QuorumDriverApi {
pub(crate) fn new(api: Arc<RpcClient>) -> Self {
Self { api }
}
/// Execute a transaction with a FullNode client. `request_type`
/// defaults to `ExecuteTransactionRequestType::WaitForLocalExecution`.
/// When `ExecuteTransactionRequestType::WaitForLocalExecution` is used,
/// but returned `confirmed_local_execution` is false, the client will
/// keep retry for WAIT_FOR_LOCAL_EXECUTION_RETRY_COUNT times. If it
/// still fails, it will return an error.
pub async fn execute_transaction_block(
&self,
tx: Transaction,
options: SuiTransactionBlockResponseOptions,
request_type: Option<ExecuteTransactionRequestType>,
) -> SuiRpcResult<SuiTransactionBlockResponse> {
let (tx_bytes, signatures) = tx.to_tx_bytes_and_signatures();
let request_type = request_type.unwrap_or_else(|| options.default_execution_request_type());
let start = Instant::now();
let response = self
.api
.http
.execute_transaction_block(
tx_bytes.clone(),
signatures.clone(),
Some(options.clone()),
// Ignore the request type as we emulate WaitForLocalExecution below.
// It will default to WaitForEffectsCert on the RPC nodes.
None,
)
.await?;
if let ExecuteTransactionRequestType::WaitForEffectsCert = request_type {
return Ok(response);
}
// JSON-RPC ignores WaitForLocalExecution, so simulate it by polling for the transaction.
let mut poll_response = tokio::time::timeout(WAIT_FOR_LOCAL_EXECUTION_TIMEOUT, async {
// Apply a short delay to give the full node a chance to catch up.
tokio::time::sleep(WAIT_FOR_LOCAL_EXECUTION_DELAY).await;
let mut interval = tokio::time::interval(WAIT_FOR_LOCAL_EXECUTION_INTERVAL);
loop {
interval.tick().await;
if let Ok(poll_response) = self
.api
.http
.get_transaction_block(*tx.digest(), Some(options.clone()))
.await
{
break poll_response;
}
}
})
.await
.map_err(|_| {
Error::FailToConfirmTransactionStatus(*tx.digest(), start.elapsed().as_secs())
})?;
poll_response.confirmed_local_execution = Some(true);
Ok(poll_response)
}
}
/// Governance API provides the staking functionality.
#[derive(Debug, Clone)]
pub struct GovernanceApi {
api: Arc<RpcClient>,
}
impl GovernanceApi {
pub(crate) fn new(api: Arc<RpcClient>) -> Self {
Self { api }
}
/// Return a list of [DelegatedStake] objects for the given address, or an error upon failure.
pub async fn get_stakes(&self, owner: SuiAddress) -> SuiRpcResult<Vec<DelegatedStake>> {
Ok(self.api.http.get_stakes(owner).await?)
}
/// Return the [SuiCommittee] information for the given `epoch`, or an error upon failure.
///
/// The argument `epoch` is the known epoch id or `None` for the current epoch.
///
/// # Examples
///
/// ```rust,no_run
/// use sui_sdk::SuiClientBuilder;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let sui = SuiClientBuilder::default().build_localnet().await?;
/// let committee_info = sui
/// .governance_api()
/// .get_committee_info(None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_committee_info(
&self,
epoch: Option<BigInt<u64>>,
) -> SuiRpcResult<SuiCommittee> {
Ok(self.api.http.get_committee_info(epoch).await?)
}
/// Return the latest SUI system state object on-chain, or an error upon failure.
///
/// Use this method to access system's information, such as the current epoch,
/// the protocol version, the reference gas price, the total stake, active validators,
/// and much more. See the [SuiSystemStateSummary] for all the available fields.
pub async fn get_latest_sui_system_state(&self) -> SuiRpcResult<SuiSystemStateSummary> {
Ok(self.api.http.get_latest_sui_system_state().await?)
}
/// Return the reference gas price for the network, or an error upon failure.
pub async fn get_reference_gas_price(&self) -> SuiRpcResult<u64> {
Ok(*self.api.http.get_reference_gas_price().await?)
}
}