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
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use std::{collections::BTreeMap, sync::Arc};
use fastcrypto::encoding::Base64;
use futures::{StreamExt, stream};
use futures_core::Stream;
use iota_json_rpc_api::{
GovernanceReadApiClient, IndexerApiClient, MoveUtilsClient, ReadApiClient, WriteApiClient,
};
use iota_json_rpc_types::{
Checkpoint, CheckpointId, CheckpointPage, DevInspectArgs, DevInspectResults,
DryRunTransactionBlockResponse, DynamicFieldPage, IotaData, IotaGetPastObjectRequest,
IotaMoveNormalizedModule, IotaObjectDataOptions, IotaObjectResponse, IotaObjectResponseQuery,
IotaPastObjectResponse, IotaTransactionBlockEffects, IotaTransactionBlockResponse,
IotaTransactionBlockResponseOptions, IotaTransactionBlockResponseQuery, ObjectsPage,
ProtocolConfigResponse, TransactionBlocksPage, TransactionFilter,
};
use iota_types::{
base_types::{IotaAddress, ObjectID, SequenceNumber, TransactionDigest},
dynamic_field::DynamicFieldName,
iota_serde::BigInt,
messages_checkpoint::CheckpointSequenceNumber,
transaction::{TransactionData, TransactionKind},
};
use jsonrpsee::core::client::Subscription;
use crate::{
RpcClient,
error::{Error, IotaRpcResult},
};
/// Defines methods for retrieving data about objects and transactions.
#[derive(Debug)]
pub struct ReadApi {
api: Arc<RpcClient>,
}
impl ReadApi {
pub(crate) fn new(api: Arc<RpcClient>) -> Self {
Self { api }
}
/// Get the objects owned by the given address. Results are paginated.
///
/// Note that if the address owns more than
/// [`QUERY_MAX_RESULT_LIMIT`](iota_json_rpc_api::QUERY_MAX_RESULT_LIMIT)
/// objects (default is 50), the pagination may not be accurate as the
/// previous page may have been updated before the next page is fetched.
///
/// # Examples
///
/// ```rust,no_run
/// use std::str::FromStr;
///
/// use iota_sdk::IotaClientBuilder;
/// use iota_types::base_types::IotaAddress;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let address = IotaAddress::from_str("0x0000....0000")?;
/// let owned_objects = iota
/// .read_api()
/// .get_owned_objects(address, None, None, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_owned_objects(
&self,
address: IotaAddress,
query: impl Into<Option<IotaObjectResponseQuery>>,
cursor: impl Into<Option<ObjectID>>,
limit: impl Into<Option<usize>>,
) -> IotaRpcResult<ObjectsPage> {
Ok(self
.api
.http
.get_owned_objects(address, query.into(), cursor.into(), limit.into())
.await?)
}
/// Get the dynamic fields owned by the given [ObjectID]. Results are
/// paginated.
///
/// If the field is a dynamic field, this method 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,
/// which is the value of the field.
///
/// # Examples
///
/// ```rust,no_run
/// use std::str::FromStr;
///
/// use iota_sdk::IotaClientBuilder;
/// use iota_types::base_types::{IotaAddress, ObjectID};
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let address = IotaAddress::from_str("0x0000....0000")?;
/// let owned_objects = iota
/// .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 IotaObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let dynamic_fields = iota
/// .read_api()
/// .get_dynamic_fields(object_id, None, None)
/// .await?;
/// Ok(())
/// }
/// ```
pub async fn get_dynamic_fields(
&self,
object_id: ObjectID,
cursor: impl Into<Option<ObjectID>>,
limit: impl Into<Option<usize>>,
) -> IotaRpcResult<DynamicFieldPage> {
Ok(self
.api
.http
.get_dynamic_fields(object_id, cursor.into(), limit.into())
.await?)
}
/// Get information for a specified dynamic field object by its parent
/// object ID and field name.
pub async fn get_dynamic_field_object(
&self,
parent_object_id: ObjectID,
name: DynamicFieldName,
) -> IotaRpcResult<IotaObjectResponse> {
Ok(self
.api
.http
.get_dynamic_field_object(parent_object_id, name)
.await?)
}
/// Get a parsed past object and version for the provided object ID.
///
/// An object's version increases when the object is mutated, though it is
/// not guaranteed that it increases always by 1. 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 std::str::FromStr;
///
/// use iota_json_rpc_types::IotaObjectDataOptions;
/// use iota_sdk::IotaClientBuilder;
/// use iota_types::base_types::{IotaAddress, ObjectID};
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let address = IotaAddress::from_str("0x0000....0000")?;
/// let owned_objects = iota
/// .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 IotaObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let version = object_data.version;
/// let past_object = iota
/// .read_api()
/// .try_get_parsed_past_object(object_id, version, IotaObjectDataOptions {
/// 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: IotaObjectDataOptions,
) -> IotaRpcResult<IotaPastObjectResponse> {
Ok(self
.api
.http
.try_get_past_object(object_id, version, Some(options))
.await?)
}
/// Get a list of parsed past objects.
///
/// See [Self::try_get_parsed_past_object] for more
/// details about past objects.
///
/// # Examples
///
/// ```rust,no_run
/// use std::str::FromStr;
///
/// use iota_json_rpc_types::{IotaGetPastObjectRequest, IotaObjectDataOptions};
/// use iota_sdk::IotaClientBuilder;
/// use iota_types::base_types::{IotaAddress, ObjectID};
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let address = IotaAddress::from_str("0x0000....0000")?;
/// let owned_objects = iota
/// .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 IotaObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let version = object_data.version;
/// let past_object = iota
/// .read_api()
/// .try_get_parsed_past_object(object_id, version, IotaObjectDataOptions {
/// 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 = iota
/// .read_api()
/// .try_multi_get_parsed_past_object(
/// vec![IotaGetPastObjectRequest {
/// object_id: past_object.object_id,
/// version: past_object.version,
/// }],
/// IotaObjectDataOptions {
/// 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<IotaGetPastObjectRequest>,
options: IotaObjectDataOptions,
) -> IotaRpcResult<Vec<IotaPastObjectResponse>> {
Ok(self
.api
.http
.try_multi_get_past_objects(past_objects, Some(options))
.await?)
}
/// Get an object by object ID with optional fields enabled by
/// [IotaObjectDataOptions].
///
/// # Examples
///
/// ```rust,no_run
/// use std::str::FromStr;
///
/// use iota_json_rpc_types::IotaObjectDataOptions;
/// use iota_sdk::IotaClientBuilder;
/// use iota_types::base_types::IotaAddress;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let address = IotaAddress::from_str("0x0000....0000")?;
/// let owned_objects = iota
/// .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 IotaObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let object = iota
/// .read_api()
/// .get_object_with_options(object_id, IotaObjectDataOptions {
/// 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: IotaObjectDataOptions,
) -> IotaRpcResult<IotaObjectResponse> {
Ok(self.api.http.get_object(object_id, Some(options)).await?)
}
/// Get a list of objects by their object IDs with optional fields enabled
/// by [IotaObjectDataOptions].
///
/// # Examples
///
/// ```rust,no_run
/// use std::str::FromStr;
///
/// use iota_json_rpc_types::IotaObjectDataOptions;
/// use iota_sdk::IotaClientBuilder;
/// use iota_types::base_types::IotaAddress;
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let address = IotaAddress::from_str("0x0000....0000")?;
/// let owned_objects = iota
/// .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 IotaObjectResponse {:?}",
/// object
/// ));
/// let object_id = object_data.object_id;
/// let object_ids = vec![object_id]; // and other object ids
/// let object = iota
/// .read_api()
/// .multi_get_object_with_options(object_ids, IotaObjectDataOptions {
/// 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: IotaObjectDataOptions,
) -> IotaRpcResult<Vec<IotaObjectResponse>> {
Ok(self
.api
.http
.multi_get_objects(object_ids, Some(options))
.await?)
}
/// Get a [bcs] serialized object's bytes by object ID.
pub async fn get_move_object_bcs(&self, object_id: ObjectID) -> IotaRpcResult<Vec<u8>> {
let resp = self
.get_object_with_options(object_id, IotaObjectDataOptions::default().with_bcs())
.await?
.into_object()
.map_err(|e| {
Error::Data(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::Data(format!(
"Object {:?} is not a MoveObject",
object_id
)))?;
Ok(raw_move_obj.bcs_bytes)
}
/// Get the total number of transaction blocks known to server.
///
/// # Examples
///
/// ```rust,no_run
/// use iota_sdk::IotaClientBuilder;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_localnet().await?;
/// let total_transaction_blocks = iota.read_api().get_total_transaction_blocks().await?;
/// Ok(())
/// }
/// ```
pub async fn get_total_transaction_blocks(&self) -> IotaRpcResult<u64> {
Ok(*self.api.http.get_total_transaction_blocks().await?)
}
/// Get a transaction and its effects by its digest with optional fields
/// enabled by [IotaTransactionBlockResponseOptions].
pub async fn get_transaction_with_options(
&self,
digest: TransactionDigest,
options: IotaTransactionBlockResponseOptions,
) -> IotaRpcResult<IotaTransactionBlockResponse> {
Ok(self
.api
.http
.get_transaction_block(digest, Some(options))
.await?)
}
/// Get a list of transactions and their effects by their digests with
/// optional fields enabled by [IotaTransactionBlockResponseOptions].
pub async fn multi_get_transactions_with_options(
&self,
digests: Vec<TransactionDigest>,
options: IotaTransactionBlockResponseOptions,
) -> IotaRpcResult<Vec<IotaTransactionBlockResponse>> {
Ok(self
.api
.http
.multi_get_transaction_blocks(digests, Some(options))
.await?)
}
/// Get filtered transaction blocks information. Results are paginated.
pub async fn query_transaction_blocks(
&self,
query: IotaTransactionBlockResponseQuery,
cursor: impl Into<Option<TransactionDigest>>,
limit: impl Into<Option<usize>>,
descending_order: bool,
) -> IotaRpcResult<TransactionBlocksPage> {
Ok(self
.api
.http
.query_transaction_blocks(query, cursor.into(), limit.into(), Some(descending_order))
.await?)
}
/// Get the first four bytes of the chain's genesis checkpoint digest in
/// hex format.
pub async fn get_chain_identifier(&self) -> IotaRpcResult<String> {
Ok(self.api.http.get_chain_identifier().await?)
}
/// Get a checkpoint by its ID.
pub async fn get_checkpoint(&self, id: CheckpointId) -> IotaRpcResult<Checkpoint> {
Ok(self.api.http.get_checkpoint(id).await?)
}
/// Return a list of checkpoints. Results are paginated.
pub async fn get_checkpoints(
&self,
cursor: impl Into<Option<BigInt<u64>>>,
limit: impl Into<Option<usize>>,
descending_order: bool,
) -> IotaRpcResult<CheckpointPage> {
Ok(self
.api
.http
.get_checkpoints(cursor.into(), limit.into(), descending_order)
.await?)
}
/// Get the sequence number of the latest checkpoint that has been
/// executed.
pub async fn get_latest_checkpoint_sequence_number(
&self,
) -> IotaRpcResult<CheckpointSequenceNumber> {
Ok(*self
.api
.http
.get_latest_checkpoint_sequence_number()
.await?)
}
/// Get a stream of transactions.
pub fn get_transactions_stream(
&self,
query: IotaTransactionBlockResponseQuery,
cursor: impl Into<Option<TransactionDigest>>,
descending_order: bool,
) -> impl Stream<Item = IotaTransactionBlockResponse> + '_ {
let cursor = cursor.into();
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,
) -> IotaRpcResult<impl Stream<Item = IotaRpcResult<IotaTransactionBlockEffects>>> {
let Some(c) = &self.api.ws else {
return Err(Error::Subscription(
"Subscription only supported by WebSocket client.".to_string(),
));
};
let subscription: Subscription<IotaTransactionBlockEffects> =
c.subscribe_transaction(filter).await?;
Ok(subscription.map(|item| Ok(item?)))
}
/// Get move modules by package ID, keyed by name.
pub async fn get_normalized_move_modules_by_package(
&self,
package: ObjectID,
) -> IotaRpcResult<BTreeMap<String, IotaMoveNormalizedModule>> {
Ok(self
.api
.http
.get_normalized_move_modules_by_package(package)
.await?)
}
// TODO(devx): we can probably cache this given an epoch
/// Get the reference gas pric.
pub async fn get_reference_gas_price(&self) -> IotaRpcResult<u64> {
Ok(*self.api.http.get_reference_gas_price().await?)
}
/// Dry run a transaction block given the provided transaction data.
///
/// This simulates 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,
) -> IotaRpcResult<DryRunTransactionBlockResponse> {
Ok(self
.api
.http
.dry_run_transaction_block(Base64::from_bytes(&bcs::to_bytes(&tx)?))
.await?)
}
/// Use this function to inspect the current state of the network by running
/// a programmable transaction block without committing its effects on
/// chain.
///
/// Unlike a dry run, this method 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`
///
/// This method'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
/// [Self::dry_run_transaction_block] instead.
pub async fn dev_inspect_transaction_block(
&self,
sender_address: IotaAddress,
tx: TransactionKind,
gas_price: impl Into<Option<BigInt<u64>>>,
epoch: impl Into<Option<BigInt<u64>>>,
additional_args: impl Into<Option<DevInspectArgs>>,
) -> IotaRpcResult<DevInspectResults> {
Ok(self
.api
.http
.dev_inspect_transaction_block(
sender_address,
Base64::from_bytes(&bcs::to_bytes(&tx)?),
gas_price.into(),
epoch.into(),
additional_args.into(),
)
.await?)
}
/// Get the protocol config by version.
///
/// The version defaults to the current version.
pub async fn get_protocol_config(
&self,
version: impl Into<Option<BigInt<u64>>>,
) -> IotaRpcResult<ProtocolConfigResponse> {
Ok(self.api.http.get_protocol_config(version.into()).await?)
}
/// Get an object by ID before the given version.
pub async fn try_get_object_before_version(
&self,
object_id: ObjectID,
version: SequenceNumber,
) -> IotaRpcResult<IotaPastObjectResponse> {
Ok(self
.api
.http
.try_get_object_before_version(object_id, version)
.await?)
}
}