Skip to content

Commit

Permalink
[indexer][graphql] use to_canonical_string consistently for ObjectID …
Browse files Browse the repository at this point in the history
…and StructTag (MystenLabs#14670)

## Description 

Went through the code and there seems to be a few more places where we
should switch to using `to_canonical_string`.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
emmazzz authored Nov 7, 2023
1 parent d1cf8e4 commit ef3fd58
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
34 changes: 28 additions & 6 deletions crates/sui-graphql-rpc/src/context_data/db_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ use sui_indexer::{
types_v2::OwnerType,
PgConnectionPoolConfig,
};
use sui_json_rpc::name_service::{Domain, NameRecord, NameServiceConfig};
use sui_json_rpc::{
coin_api::parse_to_type_tag,
name_service::{Domain, NameRecord, NameServiceConfig},
};
use sui_json_rpc_types::{
EventFilter as RpcEventFilter, ProtocolConfigResponse, Stake as SuiStake,
SuiTransactionBlockEffects,
Expand Down Expand Up @@ -1298,7 +1301,14 @@ impl PgManager {
coin_type: Option<String>,
) -> Result<Option<Balance>, Error> {
let address = address.into_vec();
let coin_type = coin_type.unwrap_or("0x2::sui::SUI".to_string());
let coin_type_tag = parse_to_type_tag(coin_type);
if coin_type_tag.is_err() {
// The provided `coin_type` cannot be parsed to a type tag so return None here.
return Ok(None);
}
let coin_type = coin_type_tag
.unwrap()
.to_canonical_string(/* with_prefix */ true);
let result = self.get_balance(address, coin_type).await?;

match result {
Expand Down Expand Up @@ -1378,7 +1388,15 @@ impl PgManager {
Coin::try_from(stored_obj)
.map_err(|e| eprintln!("Error converting object to coin: {:?}", e))
.ok()
.map(|coin| Edge::new(coin.move_obj.native_object.id().to_string(), coin))
.map(|coin| {
Edge::new(
coin.move_obj
.native_object
.id()
.to_canonical_string(/* with_prefix */ true),
coin,
)
})
}));
Ok(Some(connection))
} else {
Expand Down Expand Up @@ -1502,7 +1520,7 @@ impl PgManager {
let obj_filter = ObjectFilter {
package: None,
module: None,
ty: Some(MoveObjectType::staked_sui().to_string()),
ty: Some(MoveObjectType::staked_sui().to_canonical_string(/* with_prefix */ true)),
owner: Some(address),
object_ids: None,
object_keys: None,
Expand Down Expand Up @@ -1551,7 +1569,9 @@ impl PgManager {
.collect::<Vec<_>>();

for stk in stakes {
let cursor = stk.staked_sui_id.to_string();
let cursor = stk
.staked_sui_id
.to_canonical_string(/* with_prefix */ true);
let stake = Stake::from(stk);
edges.push(Edge::new(cursor, stake));
}
Expand Down Expand Up @@ -1628,7 +1648,9 @@ impl PgManager {
package: SuiAddress::from_array(**e.package_id),
name: e.transaction_module.to_string(),
}),
event_type: Some(MoveType::new(e.type_.to_string())),
event_type: Some(MoveType::new(
e.type_.to_canonical_string(/* with_prefix */ true),
)),
senders: Some(vec![Address {
address: SuiAddress::from_array(e.sender.to_inner()),
}]),
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-graphql-rpc/src/types/move_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ mod tests {

fn json<T: Serialize>(layout: value::MoveTypeLayout, data: T) -> Result<Json> {
let tag: TypeTag = (&layout).try_into().expect("Error fetching type tag");
let type_ = MoveType::new(tag.to_string());
let type_ = MoveType::new(tag.to_canonical_string(/* with_prefix */ true));
let bcs = Base64(bcs::to_bytes(&data).unwrap());
MoveValue { type_, bcs }.json_impl(layout)
}
Expand Down
6 changes: 4 additions & 2 deletions crates/sui-indexer/src/apis/coin_api_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl CoinReadApiServer for CoinReadApiV2 {
}

// Normalize coin type tag and default to Gas
let coin_type = parse_to_type_tag(coin_type)?.to_string();
let coin_type =
parse_to_type_tag(coin_type)?.to_canonical_string(/* with_prefix */ true);

let cursor = match cursor {
Some(c) => c,
Expand Down Expand Up @@ -98,7 +99,8 @@ impl CoinReadApiServer for CoinReadApiV2 {
coin_type: Option<String>,
) -> RpcResult<Balance> {
// Normalize coin type tag and default to Gas
let coin_type = parse_to_type_tag(coin_type)?.to_string();
let coin_type =
parse_to_type_tag(coin_type)?.to_canonical_string(/* with_prefix */ true);

let mut results = self
.inner
Expand Down
6 changes: 4 additions & 2 deletions crates/sui-indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,8 @@ impl IndexerReader {
coin_struct: StructTag,
) -> Result<Option<SuiCoinMetadata>, IndexerError> {
let package_id = coin_struct.address.into();
let coin_metadata_type = CoinMetadata::type_(coin_struct).to_string();
let coin_metadata_type =
CoinMetadata::type_(coin_struct).to_canonical_string(/* with_prefix */ true);
let coin_metadata_obj_id =
get_single_obj_id_from_package_publish(self, package_id, coin_metadata_type)?;
if let Some(id) = coin_metadata_obj_id {
Expand All @@ -1590,7 +1591,8 @@ impl IndexerReader {

fn get_total_supply(&self, coin_struct: StructTag) -> Result<Supply, IndexerError> {
let package_id = coin_struct.address.into();
let treasury_cap_type = TreasuryCap::type_(coin_struct).to_string();
let treasury_cap_type =
TreasuryCap::type_(coin_struct).to_canonical_string(/* with_prefix */ true);
let treasury_cap_obj_id =
get_single_obj_id_from_package_publish(self, package_id, treasury_cap_type.clone())?
.ok_or(IndexerError::GenericError(format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl IndexerAnalyticalStore for PgIndexerAnalyticalStore {
.filter_map(|queried_move_metrics| {
let package = ObjectID::from_bytes(queried_move_metrics.move_package.clone()).ok();
let package_str = match package {
Some(p) => p.to_string(),
Some(p) => p.to_canonical_string(/* with_prefix */ true),
None => {
tracing::error!(
"Failed to parse move package ID: {:?}",
Expand Down

0 comments on commit ef3fd58

Please sign in to comment.