Skip to content

Commit c633acd

Browse files
feat: add start_offset to get_address_queue_with_proofs (#1793)
* propagate API error messages * feat: add start_offset to get_address_queue_with_proofs
1 parent d9c398f commit c633acd

File tree

8 files changed

+137
-54
lines changed

8 files changed

+137
-54
lines changed

forester-utils/src/instructions/address_batch_update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ where
8383
debug!("Requesting {} total elements from indexer", total_elements);
8484

8585
let indexer_update_info = indexer
86-
.get_address_queue_with_proofs(merkle_tree_pubkey, total_elements as u16, None)
86+
.get_address_queue_with_proofs(merkle_tree_pubkey, total_elements as u16, None, None)
8787
.await
8888
.map_err(|e| {
8989
error!("Failed to get batch address update info: {:?}", e);

forester/tests/batched_address_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ async fn test_address_batched() {
172172
.unwrap();
173173

174174
let photon_address_queue_with_proofs = photon_indexer
175-
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None)
175+
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None, None)
176176
.await
177177
.unwrap();
178178

179179
let test_indexer_address_queue_with_proofs = env
180180
.indexer
181-
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None)
181+
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None, None)
182182
.await
183183
.unwrap();
184184

@@ -207,13 +207,13 @@ async fn test_address_batched() {
207207

208208
if (i + 1) % 10 == 0 {
209209
let photon_address_queue_with_proofs = photon_indexer
210-
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None)
210+
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None, None)
211211
.await
212212
.unwrap();
213213

214214
let test_indexer_address_queue_with_proofs = env
215215
.indexer
216-
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None)
216+
.get_address_queue_with_proofs(&address_merkle_tree_pubkey, 10, None, None)
217217
.await
218218
.unwrap();
219219

sdk-libs/client/src/indexer/indexer_trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ pub trait Indexer: std::marker::Send + std::marker::Sync {
180180
&mut self,
181181
merkle_tree_pubkey: &Pubkey,
182182
zkp_batch_size: u16,
183+
start_offset: Option<u64>,
183184
config: Option<IndexerRpcConfig>,
184185
) -> Result<Response<BatchAddressUpdateIndexerResponse>, IndexerError>;
185186

sdk-libs/client/src/indexer/photon_indexer.rs

Lines changed: 125 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ impl PhotonIndexer {
126126
result.ok_or_else(|| IndexerError::missing_result(context, "value not present"))
127127
}
128128

129+
fn extract_result_with_error_check<T>(
130+
context: &str,
131+
error: Option<Box<photon_api::models::GetBatchAddressUpdateInfoPost200ResponseError>>,
132+
result: Option<T>,
133+
) -> Result<T, IndexerError> {
134+
if let Some(error) = error {
135+
let error_message = error
136+
.message
137+
.unwrap_or_else(|| "Unknown API error".to_string());
138+
return Err(IndexerError::ApiError(format!(
139+
"API error in {} (code: {:?}): {}",
140+
context, error.code, error_message
141+
)));
142+
}
143+
144+
Self::extract_result(context, result)
145+
}
146+
129147
fn build_account_params(
130148
&self,
131149
address: Option<Address>,
@@ -174,7 +192,11 @@ impl Indexer for PhotonIndexer {
174192
request,
175193
)
176194
.await?;
177-
let api_response = Self::extract_result("get_compressed_account", result.result)?;
195+
let api_response = Self::extract_result_with_error_check(
196+
"get_compressed_account",
197+
result.error,
198+
result.result.map(|r| *r),
199+
)?;
178200
if api_response.context.slot < config.slot {
179201
return Err(IndexerError::IndexerNotSyncedToSlot);
180202
}
@@ -212,7 +234,11 @@ impl Indexer for PhotonIndexer {
212234
request,
213235
)
214236
.await?;
215-
let api_response = Self::extract_result("get_compressed_account", result.result)?;
237+
let api_response = Self::extract_result_with_error_check(
238+
"get_compressed_account_by_hash",
239+
result.error,
240+
result.result.map(|r| *r),
241+
)?;
216242
if api_response.context.slot < config.slot {
217243
return Err(IndexerError::IndexerNotSyncedToSlot);
218244
}
@@ -359,8 +385,11 @@ impl Indexer for PhotonIndexer {
359385
)
360386
.await?;
361387

362-
let api_response =
363-
Self::extract_result("get_compressed_account_balance", result.result)?;
388+
let api_response = Self::extract_result_with_error_check(
389+
"get_compressed_account_balance",
390+
result.error,
391+
result.result.map(|r| *r),
392+
)?;
364393
if api_response.context.slot < config.slot {
365394
return Err(IndexerError::IndexerNotSyncedToSlot);
366395
}
@@ -396,8 +425,11 @@ impl Indexer for PhotonIndexer {
396425
)
397426
.await?;
398427

399-
let api_response =
400-
Self::extract_result("get_compressed_balance_by_owner", result.result)?;
428+
let api_response = Self::extract_result_with_error_check(
429+
"get_compressed_balance_by_owner",
430+
result.error,
431+
result.result.map(|r| *r),
432+
)?;
401433
if api_response.context.slot < config.slot {
402434
return Err(IndexerError::IndexerNotSyncedToSlot);
403435
}
@@ -436,8 +468,11 @@ impl Indexer for PhotonIndexer {
436468
)
437469
.await?;
438470

439-
let api_response =
440-
Self::extract_result("get_compressed_mint_token_holders", result.result)?;
471+
let api_response = Self::extract_result_with_error_check(
472+
"get_compressed_mint_token_holders",
473+
result.error,
474+
result.result.map(|r| *r),
475+
)?;
441476
if api_response.context.slot < config.slot {
442477
return Err(IndexerError::IndexerNotSyncedToSlot);
443478
}
@@ -485,8 +520,11 @@ impl Indexer for PhotonIndexer {
485520
)
486521
.await?;
487522

488-
let api_response =
489-
Self::extract_result("get_compressed_token_account_balance", result.result)?;
523+
let api_response = Self::extract_result_with_error_check(
524+
"get_compressed_token_account_balance",
525+
result.error,
526+
result.result.map(|r| *r),
527+
)?;
490528
if api_response.context.slot < config.slot {
491529
return Err(IndexerError::IndexerNotSyncedToSlot);
492530
}
@@ -677,8 +715,11 @@ impl Indexer for PhotonIndexer {
677715
)
678716
.await?;
679717

680-
let response =
681-
Self::extract_result("get_compressed_token_accounts_by_owner", result.result)?;
718+
let response = Self::extract_result_with_error_check(
719+
"get_compressed_token_accounts_by_owner",
720+
result.error,
721+
result.result.map(|r| *r),
722+
)?;
682723
if response.context.slot < config.slot {
683724
return Err(IndexerError::IndexerNotSyncedToSlot);
684725
}
@@ -737,9 +778,10 @@ impl Indexer for PhotonIndexer {
737778
)
738779
.await?;
739780

740-
let api_response = Self::extract_result(
781+
let api_response = Self::extract_result_with_error_check(
741782
"get_compressed_token_balances_by_owner_v2",
742-
result.result,
783+
result.error,
784+
result.result.map(|r| *r),
743785
)?;
744786
if api_response.context.slot < config.slot {
745787
return Err(IndexerError::IndexerNotSyncedToSlot);
@@ -786,8 +828,11 @@ impl Indexer for PhotonIndexer {
786828
)
787829
.await?;
788830

789-
let api_response =
790-
Self::extract_result("get_compressed_token_balances_by_owner", result.result)?;
831+
let api_response = Self::extract_result_with_error_check(
832+
"get_compressed_token_balances_by_owner",
833+
result.error,
834+
result.result.map(|r| *r),
835+
)?;
791836
if api_response.context.slot < config.slot {
792837
return Err(IndexerError::IndexerNotSyncedToSlot);
793838
}
@@ -836,8 +881,11 @@ impl Indexer for PhotonIndexer {
836881
)
837882
.await?;
838883

839-
let api_response =
840-
Self::extract_result("get_compression_signatures_for_account", result.result)?;
884+
let api_response = Self::extract_result_with_error_check(
885+
"get_compression_signatures_for_account",
886+
result.error,
887+
result.result.map(|r| *r),
888+
)?;
841889
if api_response.context.slot < config.slot {
842890
return Err(IndexerError::IndexerNotSyncedToSlot);
843891
}
@@ -884,8 +932,11 @@ impl Indexer for PhotonIndexer {
884932
)
885933
.await?;
886934

887-
let api_response =
888-
Self::extract_result("get_compression_signatures_for_address", result.result)?;
935+
let api_response = Self::extract_result_with_error_check(
936+
"get_compression_signatures_for_address",
937+
result.error,
938+
result.result.map(|r| *r),
939+
)?;
889940
if api_response.context.slot < config.slot {
890941
return Err(IndexerError::IndexerNotSyncedToSlot);
891942
}
@@ -937,8 +988,11 @@ impl Indexer for PhotonIndexer {
937988
)
938989
.await?;
939990

940-
let api_response =
941-
Self::extract_result("get_compression_signatures_for_owner", result.result)?;
991+
let api_response = Self::extract_result_with_error_check(
992+
"get_compression_signatures_for_owner",
993+
result.error,
994+
result.result.map(|r| *r),
995+
)?;
942996
if api_response.context.slot < config.slot {
943997
return Err(IndexerError::IndexerNotSyncedToSlot);
944998
}
@@ -991,8 +1045,11 @@ impl Indexer for PhotonIndexer {
9911045
)
9921046
.await?;
9931047

994-
let api_response =
995-
Self::extract_result("get_compression_signatures_for_token_owner", result.result)?;
1048+
let api_response = Self::extract_result_with_error_check(
1049+
"get_compression_signatures_for_token_owner",
1050+
result.error,
1051+
result.result.map(|r| *r),
1052+
)?;
9961053
if api_response.context.slot < config.slot {
9971054
return Err(IndexerError::IndexerNotSyncedToSlot);
9981055
}
@@ -1032,7 +1089,11 @@ impl Indexer for PhotonIndexer {
10321089
)
10331090
.await?;
10341091

1035-
let _api_response = Self::extract_result("get_indexer_health", result.result)?;
1092+
let _api_response = Self::extract_result_with_error_check(
1093+
"get_indexer_health",
1094+
result.error,
1095+
result.result,
1096+
)?;
10361097

10371098
Ok(true)
10381099
})
@@ -1050,7 +1111,11 @@ impl Indexer for PhotonIndexer {
10501111
photon_api::apis::default_api::get_indexer_slot_post(&self.configuration, request)
10511112
.await?;
10521113

1053-
let result = Self::extract_result("get_indexer_slot", result.result)?;
1114+
let result = Self::extract_result_with_error_check(
1115+
"get_indexer_slot",
1116+
result.error,
1117+
result.result,
1118+
)?;
10541119
Ok(result)
10551120
})
10561121
.await
@@ -1167,8 +1232,11 @@ impl Indexer for PhotonIndexer {
11671232
)
11681233
.await?;
11691234

1170-
let api_response =
1171-
Self::extract_result("get_multiple_compressed_accounts", result.result)?;
1235+
let api_response = Self::extract_result_with_error_check(
1236+
"get_multiple_compressed_accounts",
1237+
result.error,
1238+
result.result.map(|r| *r),
1239+
)?;
11721240
if api_response.context.slot < config.slot {
11731241
return Err(IndexerError::IndexerNotSyncedToSlot);
11741242
}
@@ -1223,14 +1291,17 @@ impl Indexer for PhotonIndexer {
12231291

12241292
let result = result?;
12251293

1226-
let api_response =
1227-
match Self::extract_result("get_multiple_new_address_proofs", result.result) {
1228-
Ok(proofs) => proofs,
1229-
Err(e) => {
1230-
error!("Failed to extract proofs: {:?}", e);
1231-
return Err(e);
1232-
}
1233-
};
1294+
let api_response = match Self::extract_result_with_error_check(
1295+
"get_multiple_new_address_proofs",
1296+
result.error,
1297+
result.result.map(|r| *r),
1298+
) {
1299+
Ok(proofs) => proofs,
1300+
Err(e) => {
1301+
error!("Failed to extract proofs: {:?}", e);
1302+
return Err(e);
1303+
}
1304+
};
12341305
if api_response.context.slot < config.slot {
12351306
return Err(IndexerError::IndexerNotSyncedToSlot);
12361307
}
@@ -1330,7 +1401,11 @@ impl Indexer for PhotonIndexer {
13301401
request,
13311402
)
13321403
.await?;
1333-
let api_response = Self::extract_result("get_validity_proof_v2", result.result)?;
1404+
let api_response = Self::extract_result_with_error_check(
1405+
"get_validity_proof_v2",
1406+
result.error,
1407+
result.result.map(|r| *r),
1408+
)?;
13341409
if api_response.context.slot < config.slot {
13351410
return Err(IndexerError::IndexerNotSyncedToSlot);
13361411
}
@@ -1368,7 +1443,11 @@ impl Indexer for PhotonIndexer {
13681443
)
13691444
.await?;
13701445

1371-
let api_response = Self::extract_result("get_validity_proof", result.result)?;
1446+
let api_response = Self::extract_result_with_error_check(
1447+
"get_validity_proof",
1448+
result.error,
1449+
result.result.map(|r| *r),
1450+
)?;
13721451
if api_response.context.slot < config.slot {
13731452
return Err(IndexerError::IndexerNotSyncedToSlot);
13741453
}
@@ -1392,41 +1471,41 @@ impl Indexer for PhotonIndexer {
13921471
&mut self,
13931472
_merkle_tree_pubkey: &Pubkey,
13941473
_zkp_batch_size: u16,
1474+
_start_offset: Option<u64>,
13951475
_config: Option<IndexerRpcConfig>,
13961476
) -> Result<Response<BatchAddressUpdateIndexerResponse>, IndexerError> {
13971477
#[cfg(not(feature = "v2"))]
13981478
unimplemented!("get_address_queue_with_proofs");
13991479
#[cfg(feature = "v2")]
14001480
{
1401-
println!("v2 get_address_queue_with_proofs");
14021481
let merkle_tree_pubkey = _merkle_tree_pubkey;
14031482
let limit = _zkp_batch_size;
1483+
let start_queue_index = _start_offset;
14041484
let config = _config.unwrap_or_default();
14051485
self.retry(config.retry_config, || async {
14061486
let merkle_tree = Hash::from_bytes(merkle_tree_pubkey.to_bytes().as_ref())?;
14071487
let request = photon_api::models::GetBatchAddressUpdateInfoPostRequest {
14081488
params: Box::new(
14091489
photon_api::models::GetBatchAddressUpdateInfoPostRequestParams {
14101490
limit,
1411-
start_queue_index: None,
1491+
start_queue_index,
14121492
tree: merkle_tree.to_base58(),
14131493
},
14141494
),
14151495
..Default::default()
14161496
};
14171497

1418-
println!("request: {:?}", request);
1419-
14201498
let result = photon_api::apis::default_api::get_batch_address_update_info_post(
14211499
&self.configuration,
14221500
request,
14231501
)
14241502
.await?;
14251503

1426-
println!("result: {:?}", result);
1427-
1428-
let api_response =
1429-
Self::extract_result("get_batch_address_update_info", result.result)?;
1504+
let api_response = Self::extract_result_with_error_check(
1505+
"get_batch_address_update_info",
1506+
result.error,
1507+
result.result.map(|r| *r),
1508+
)?;
14301509
if api_response.context.slot < config.slot {
14311510
return Err(IndexerError::IndexerNotSyncedToSlot);
14321511
}

0 commit comments

Comments
 (0)