Skip to content

Commit

Permalink
[GraphQL] Fix default coin type (MystenLabs#14804)
Browse files Browse the repository at this point in the history
## Description

If no coin type is provided, we should default to `0x2::sui::SUI`.
Previously we added no filter on the coin type, which meant that we were
returning arbitrary objects from the coin connection field.

## Test Plan

GraphiQL:

<TODO: Screenshot>
  • Loading branch information
amnn authored Nov 13, 2023
1 parent 6987a32 commit db92b14
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 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 @@ -93,6 +93,7 @@ use sui_types::{
digests::TransactionDigest,
dynamic_field::{DynamicFieldType, Field},
event::EventID,
gas_coin::GAS,
governance::StakedSui,
Identifier,
};
Expand Down Expand Up @@ -393,7 +394,7 @@ impl QueryBuilder {
descending_order: bool,
limit: i64,
address: Vec<u8>,
coin_type: Option<String>,
coin_type: String,
) -> objects::BoxedQuery<'a, Pg> {
let mut query = objects::dsl::objects.into_boxed();
if let Some(cursor) = cursor {
Expand All @@ -412,11 +413,9 @@ impl QueryBuilder {

query = query
.filter(objects::dsl::owner_id.eq(address))
.filter(objects::dsl::owner_type.eq(OwnerType::Address as i16)); // Leverage index on objects table
.filter(objects::dsl::owner_type.eq(OwnerType::Address as i16)) // Leverage index on objects table
.filter(objects::dsl::coin_type.eq(coin_type));

if let Some(coin_type) = coin_type {
query = query.filter(objects::dsl::coin_type.eq(coin_type));
}
query
}

Expand Down Expand Up @@ -695,7 +694,7 @@ impl PgManager {
async fn multi_get_coins(
&self,
address: Vec<u8>,
coin_type: Option<String>,
coin_type: String,
first: Option<u64>,
after: Option<String>,
last: Option<u64>,
Expand Down Expand Up @@ -1375,6 +1374,9 @@ impl PgManager {
before: Option<String>,
) -> Result<Option<Connection<String, Coin>>, Error> {
let address = address.into_vec();
let coin_type = coin_type.unwrap_or_else(|| {
GAS::type_().to_canonical_string(/* with_prefix */ true)
});

let coins = self
.multi_get_coins(address, coin_type, first, after, last, before)
Expand Down

0 comments on commit db92b14

Please sign in to comment.