Skip to content

Commit

Permalink
feat: Use ID types in client api (#1191)
Browse files Browse the repository at this point in the history
Related issues:
- Closes #1121

This PR update the `FuelClient` API to use ID types in place of string
serialized IDs. Now, consumers of the API must directly pass a client
type when performing a method call. Internally, these ID types are then
converted to GraphQL types to be used in the GraphQL API. This API
update represents a breaking change.
  • Loading branch information
Brandon Vrooman authored May 31, 2023
1 parent ea438b7 commit 819081e
Show file tree
Hide file tree
Showing 27 changed files with 227 additions and 348 deletions.
23 changes: 4 additions & 19 deletions bin/e2e-test-client/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ impl Wallet {
/// returns the balance associated with a wallet
pub async fn balance(&self, asset_id: Option<AssetId>) -> anyhow::Result<u64> {
self.client
.balance(
&self.address.to_string(),
Some(asset_id.unwrap_or_default().to_string().as_str()),
)
.balance(&self.address, Some(&asset_id.unwrap_or_default()))
.await
.context("failed to retrieve balance")
}
Expand All @@ -118,7 +115,7 @@ impl Wallet {
results = self
.client
.coins(
&self.address.to_string(),
&self.address,
None,
PaginationRequest {
cursor: None,
Expand All @@ -145,17 +142,11 @@ impl Wallet {
asset_id: Option<AssetId>,
) -> anyhow::Result<Transaction> {
let asset_id = asset_id.unwrap_or_default();
let asset_id_string = asset_id.to_string();
let asset_id_str = asset_id_string.as_str();
let total_amount = transfer_amount + BASE_AMOUNT;
// select coins
let coins = &self
.client
.coins_to_spend(
self.address.to_string().as_str(),
vec![(asset_id_str, total_amount, None)],
None,
)
.coins_to_spend(&self.address, vec![(asset_id, total_amount, None)], None)
.await?[0];

// build transaction
Expand Down Expand Up @@ -217,17 +208,11 @@ impl Wallet {

pub async fn deploy_contract(&self, config: ContractConfig) -> anyhow::Result<()> {
let asset_id = AssetId::zeroed();
let asset_id_string = asset_id.to_string();
let asset_id_str = asset_id_string.as_str();
let total_amount = BASE_AMOUNT;
// select coins
let coins = &self
.client
.coins_to_spend(
self.address.to_string().as_str(),
vec![(asset_id_str, total_amount, None)],
None,
)
.coins_to_spend(&self.address, vec![(asset_id, total_amount, None)], None)
.await?[0];

let ContractConfig {
Expand Down
8 changes: 4 additions & 4 deletions bin/e2e-test-client/src/tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub async fn receipts(ctx: &TestContext) -> Result<(), Failed> {

let mut queries = vec![];
for i in 0..100 {
let tx_id = result.tx_id.to_string();
queries.push(async move { (ctx.alice.client.receipts(tx_id.as_str()).await, i) });
let tx_id = result.tx_id;
queries.push(async move { (ctx.alice.client.receipts(&tx_id).await, i) });
}

let queries = futures::future::join_all(queries).await;
Expand Down Expand Up @@ -80,11 +80,11 @@ pub async fn run_contract_large_state(ctx: &TestContext) -> Result<(), Failed> {
// `0xc8ca903bfaba051c55d827c3bd957a325a3f80bceeb87c6e49d308ad39cf48d7` in the
// `test_data/large_state/contract.json`.
// contract_config.calculate_contract_id();
let contract_id = contract_config.contract_id.to_string();
let contract_id = contract_config.contract_id;
println!("\nThe `contract_id` of the contract with large state: {contract_id}");

// if the contract is not deployed yet, let's deploy it
let result = ctx.bob.client.contract(contract_id.as_str()).await;
let result = ctx.bob.client.contract(&contract_id).await;
if result?.is_none() {
let deployment_request = ctx.bob.deploy_contract(contract_config);

Expand Down
8 changes: 2 additions & 6 deletions bin/e2e-test-client/src/tests/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub async fn basic_transfer(ctx: &TestContext) -> Result<(), Failed> {
// wait until bob sees the transaction
timeout(
ctx.config.sync_timeout(),
ctx.bob
.client
.await_transaction_commit(&result.tx_id.to_string()),
ctx.bob.client.await_transaction_commit(&result.tx_id),
)
.await??;
println!("\nThe tx id of the transfer: {}", result.tx_id);
Expand Down Expand Up @@ -52,9 +50,7 @@ pub async fn transfer_back(ctx: &TestContext) -> Result<(), Failed> {
// wait until alice sees the transaction
timeout(
ctx.config.sync_timeout(),
ctx.alice
.client
.await_transaction_commit(&result.tx_id.to_string()),
ctx.alice.client.await_transaction_commit(&result.tx_id),
)
.await??;

Expand Down
11 changes: 8 additions & 3 deletions bin/fuel-core-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![deny(unused_crate_dependencies)]
use clap::Parser;
use fuel_core_client::client::FuelClient;
use fuel_core_types::fuel_tx::Transaction;
use fuel_core_types::fuel_tx::{
Transaction,
TxId,
};
use serde_json::json;

#[derive(Parser)]
Expand Down Expand Up @@ -53,11 +56,13 @@ impl CliArgs {
println!("{:?}", result.unwrap());
}
TransactionCommands::Get { id } => {
let tx = client.transaction(id.as_str()).await.unwrap();
let tx_id = id.parse::<TxId>().expect("invalid transaction id");
let tx = client.transaction(&tx_id).await.unwrap();
println!("{:?}", json!(tx).to_string())
}
TransactionCommands::Receipts { id } => {
let receipts = client.receipts(id.as_str()).await.unwrap().unwrap();
let tx_id = id.parse::<TxId>().expect("invalid transaction id");
let receipts = client.receipts(&tx_id).await.unwrap().unwrap();
println!("{:?}", json!(receipts).to_string())
}
},
Expand Down
Loading

0 comments on commit 819081e

Please sign in to comment.