Skip to content

Commit 53a54dc

Browse files
Guocorkxgreenx
andauthored
Fix client coins endpoint to return all asset types (#2984)
## Linked Issues/PRs Closes #2960 <!-- List of related issues/PRs --> ## Description <!-- List of detailed changes --> This PR fixes the behavior of the `coins` endpoint on the client side. Previously, if `asset_id` was `None`, it defaulted to `AssetId::default()`, which is incorrect. The expected behavior (and what the query side already supports) is to return all assets when `asset_id` is `None`. ## Checklist - [ ] Breaking changes are clearly marked as such in the PR description and changelog - [ ] New behavior is reflected in tests - [ ] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [✓] I have reviewed the code myself - [ ] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --------- Co-authored-by: Green Baneling <[email protected]>
1 parent 9cff985 commit 53a54dc

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

.changes/fixed/2984.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix client coins endpoint so that passing `None` for `asset_id` no longer defaults to `AssetId::default()` but correctly returns all asset types.

crates/client/src/client.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,10 +1486,7 @@ impl FuelClient {
14861486
request: PaginationRequest<String>,
14871487
) -> io::Result<PaginatedResult<types::Coin, String>> {
14881488
let owner: schema::Address = (*owner).into();
1489-
let asset_id: schema::AssetId = match asset_id {
1490-
Some(asset_id) => (*asset_id).into(),
1491-
None => schema::AssetId::default(),
1492-
};
1489+
let asset_id = asset_id.map(|id| (*id).into());
14931490
let args = CoinsConnectionArgs::from((owner, asset_id, request));
14941491
let query = schema::coins::CoinsQuery::build(args);
14951492

crates/client/src/client/schema/coins.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ pub struct CoinsConnectionArgs {
5959
pub last: Option<i32>,
6060
}
6161

62-
impl From<(Address, AssetId, PaginationRequest<String>)> for CoinsConnectionArgs {
63-
fn from(r: (Address, AssetId, PaginationRequest<String>)) -> Self {
62+
impl From<(Address, Option<AssetId>, PaginationRequest<String>)> for CoinsConnectionArgs {
63+
fn from(r: (Address, Option<AssetId>, PaginationRequest<String>)) -> Self {
6464
match r.2.direction {
6565
PageDirection::Forward => CoinsConnectionArgs {
6666
filter: CoinFilterInput {
6767
owner: r.0,
68-
asset_id: Some(r.1),
68+
asset_id: r.1,
6969
},
7070
after: r.2.cursor,
7171
before: None,
@@ -75,7 +75,7 @@ impl From<(Address, AssetId, PaginationRequest<String>)> for CoinsConnectionArgs
7575
PageDirection::Backward => CoinsConnectionArgs {
7676
filter: CoinFilterInput {
7777
owner: r.0,
78-
asset_id: Some(r.1),
78+
asset_id: r.1,
7979
},
8080
after: None,
8181
before: r.2.cursor,

tests/tests/coin.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,48 @@ async fn get_coins_forwards_backwards(
175175
assert!(!coins.results.is_empty());
176176
assert_eq!(coins.results.len(), 10);
177177
}
178+
179+
#[tokio::test]
180+
async fn returns_all_assets_when_asset_id_is_none() {
181+
let owner = Address::default();
182+
let asset_id_a = AssetId::from([1u8; 32]);
183+
let asset_id_b = AssetId::from([2u8; 32]);
184+
185+
// Generate coins with different assets—like 5 of asset A and 5 of asset B
186+
let mut coin_generator = CoinConfigGenerator::new();
187+
let coins: Vec<_> = (0..10usize)
188+
.map(|i| CoinConfig {
189+
owner,
190+
amount: (i + 1) as Word,
191+
asset_id: if i <= 5 { asset_id_a } else { asset_id_b },
192+
..coin_generator.generate()
193+
})
194+
.collect();
195+
196+
// setup server & client
197+
let srv = setup_service(coins).await;
198+
let client = FuelClient::from(srv.bound_address);
199+
200+
// Query coins where the `asset_id` is `None`.
201+
let coins = client
202+
.coins(
203+
&owner,
204+
None,
205+
PaginationRequest {
206+
cursor: None,
207+
results: 10,
208+
direction: PageDirection::Forward,
209+
},
210+
)
211+
.await
212+
.unwrap();
213+
214+
// Collect all the `asset_id`s.
215+
let asset_ids: std::collections::HashSet<AssetId> =
216+
coins.results.iter().map(|c| c.asset_id).collect();
217+
218+
// run test
219+
assert!(asset_ids.contains(&asset_id_a));
220+
assert!(asset_ids.contains(&asset_id_b));
221+
assert_eq!(coins.results.len(), 10);
222+
}

0 commit comments

Comments
 (0)