Skip to content

Commit

Permalink
sui-graphql-client: add method for querying total supply of a coin (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten authored Oct 1, 2024
1 parent c055420 commit 74b91c1
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ impl Client {
})
}

/// Get the coin metadata for the coin type.
pub async fn coin_metadata(&self, coin_type: &str) -> Result<Option<CoinMetadata>, Error> {
let operation = CoinMetadataQuery::build(CoinMetadataArgs { coin_type });
let response = self.run_query(&operation).await?;
Expand All @@ -327,6 +328,16 @@ impl Client {
Ok(response.data.and_then(|x| x.coin_metadata))
}

/// Get total supply for the coin type.
pub async fn total_supply(&self, coin_type: &str) -> Result<Option<u64>, Error> {
let coin_metadata = self.coin_metadata(coin_type).await?;

coin_metadata
.and_then(|c| c.supply)
.map(|c| c.try_into())
.transpose()
}

// ===========================================================================
// Checkpoints API
// ===========================================================================
Expand Down Expand Up @@ -908,4 +919,22 @@ mod tests {
}
assert!(num_coins > 0);
}

#[tokio::test]
async fn test_total_supply() {
for (n, _) in NETWORKS {
let client = Client::new(n).unwrap();
let ts = client.total_supply("0x2::sui::SUI").await;
assert!(
ts.is_ok(),
"Total supply query failed for network: {n}. Error: {}",
ts.unwrap_err()
);
assert_eq!(
ts.unwrap().unwrap(),
10_000_000_000,
"Total supply mismatch for network: {n}"
);
}
}
}

0 comments on commit 74b91c1

Please sign in to comment.