10
10
ChainAbstractionInitialTxInfo , MessageSource ,
11
11
} ,
12
12
error:: RpcError ,
13
+ metrics:: { ChainAbstractionNoBridgingNeededType , ChainAbstractionTransactionType } ,
13
14
state:: AppState ,
14
15
storage:: irn:: OperationType ,
15
16
utils:: {
@@ -119,6 +120,9 @@ async fn handler_internal(
119
120
"The transaction is a native token transfer with value: {:?}" ,
120
121
transfer_value
121
122
) ;
123
+ state
124
+ . metrics
125
+ . add_ca_no_bridging_needed ( ChainAbstractionNoBridgingNeededType :: NativeTokenTransfer ) ;
122
126
return Ok ( no_bridging_needed_response. into_response ( ) ) ;
123
127
}
124
128
let transaction_data = initial_transaction. input . clone ( ) ;
@@ -141,6 +145,9 @@ async fn handler_internal(
141
145
. is_none ( )
142
146
{
143
147
error ! ( "The destination address is not a supported bridging asset contract" ) ;
148
+ state. metrics . add_ca_no_bridging_needed (
149
+ ChainAbstractionNoBridgingNeededType :: AssetNotSupported ,
150
+ ) ;
144
151
return Ok ( no_bridging_needed_response. into_response ( ) ) ;
145
152
} ;
146
153
@@ -164,6 +171,11 @@ async fn handler_internal(
164
171
state. metrics . clone ( ) ,
165
172
)
166
173
. await ?;
174
+ state. metrics . add_ca_gas_estimation (
175
+ simulated_gas_used,
176
+ initial_transaction. chain_id . clone ( ) ,
177
+ ChainAbstractionTransactionType :: Transfer ,
178
+ ) ;
167
179
// Save the initial tx gas estimation to the cache
168
180
{
169
181
let state = state. clone ( ) ;
@@ -222,6 +234,9 @@ async fn handler_internal(
222
234
}
223
235
if asset_transfer_value. is_zero ( ) {
224
236
error ! ( "The transaction does not change any supported bridging assets" ) ;
237
+ state. metrics . add_ca_no_bridging_needed (
238
+ ChainAbstractionNoBridgingNeededType :: AssetNotSupported ,
239
+ ) ;
225
240
return Ok ( no_bridging_needed_response. into_response ( ) ) ;
226
241
}
227
242
@@ -244,6 +259,9 @@ async fn handler_internal(
244
259
Some ( ( symbol, decimals) ) => ( symbol, decimals) ,
245
260
None => {
246
261
error ! ( "The destination address is not a supported bridging asset contract" ) ;
262
+ state. metrics . add_ca_no_bridging_needed (
263
+ ChainAbstractionNoBridgingNeededType :: AssetNotSupported ,
264
+ ) ;
247
265
return Ok ( no_bridging_needed_response. into_response ( ) ) ;
248
266
}
249
267
} ;
@@ -260,6 +278,9 @@ async fn handler_internal(
260
278
. await ?;
261
279
let erc20_balance = U256 :: from_be_bytes ( erc20_balance. into ( ) ) ;
262
280
if erc20_balance >= asset_transfer_value {
281
+ state
282
+ . metrics
283
+ . add_ca_no_bridging_needed ( ChainAbstractionNoBridgingNeededType :: SufficientFunds ) ;
263
284
return Ok ( no_bridging_needed_response. into_response ( ) ) ;
264
285
}
265
286
let erc20_topup_value = asset_transfer_value - erc20_balance;
@@ -276,6 +297,7 @@ async fn handler_internal(
276
297
)
277
298
. await ?
278
299
else {
300
+ state. metrics . add_ca_insufficient_funds ( ) ;
279
301
return Ok ( Json ( PrepareResponse :: Error ( PrepareResponseError {
280
302
error : BridgingError :: InsufficientFunds ,
281
303
} ) )
@@ -302,6 +324,14 @@ async fn handler_internal(
302
324
)
303
325
. await ?;
304
326
let Some ( best_route) = quotes. first ( ) else {
327
+ state
328
+ . metrics
329
+ . add_ca_no_routes_found ( construct_metrics_bridging_route (
330
+ bridge_chain_id. clone ( ) ,
331
+ bridge_contract. to_string ( ) ,
332
+ request_payload. transaction . chain_id . clone ( ) ,
333
+ asset_transfer_contract. to_string ( ) ,
334
+ ) ) ;
305
335
return Ok ( Json ( PrepareResponse :: Error ( PrepareResponseError {
306
336
error : BridgingError :: NoRoutesAvailable ,
307
337
} ) )
@@ -325,6 +355,7 @@ async fn handler_internal(
325
355
"The current bridging asset balance on {} is {} less than the required topup amount:{}. The bridging fee is:{}" ,
326
356
from_address, current_bridging_asset_balance, required_topup_amount, bridging_fee
327
357
) ;
358
+ state. metrics . add_ca_insufficient_funds ( ) ;
328
359
return Ok ( Json ( PrepareResponse :: Error ( PrepareResponseError {
329
360
error : BridgingError :: InsufficientFunds ,
330
361
} ) )
@@ -346,6 +377,14 @@ async fn handler_internal(
346
377
)
347
378
. await ?;
348
379
let Some ( best_route) = quotes. first ( ) else {
380
+ state
381
+ . metrics
382
+ . add_ca_no_routes_found ( construct_metrics_bridging_route (
383
+ bridge_chain_id. clone ( ) ,
384
+ bridge_contract. to_string ( ) ,
385
+ request_payload. transaction . chain_id . clone ( ) ,
386
+ asset_transfer_contract. to_string ( ) ,
387
+ ) ) ;
349
388
return Ok ( Json ( PrepareResponse :: Error ( PrepareResponseError {
350
389
error : BridgingError :: NoRoutesAvailable ,
351
390
} ) )
@@ -448,7 +487,7 @@ async fn handler_internal(
448
487
. await ?;
449
488
for ( index, simulation_result) in simulation_results. simulation_results . iter ( ) . enumerate ( ) {
450
489
// Making sure the simulation input matches the transaction input
451
- let curr_route = routes. get ( index) . ok_or_else ( || {
490
+ let curr_route = routes. get_mut ( index) . ok_or_else ( || {
452
491
RpcError :: SimulationFailed ( "The route index is out of bounds" . to_string ( ) )
453
492
} ) ?;
454
493
if simulation_result. transaction . input != curr_route. input {
@@ -458,9 +497,25 @@ async fn handler_internal(
458
497
) ) ;
459
498
}
460
499
461
- routes [ index ] . gas_limit = U64 :: from (
500
+ curr_route . gas_limit = U64 :: from (
462
501
( simulation_result. transaction . gas * ( 100 + ESTIMATED_GAS_SLIPPAGE as u64 ) ) / 100 ,
463
502
) ;
503
+
504
+ // Get the transaction type for metrics based on the assumption that the first transaction is an approval
505
+ // and the rest are bridging transactions
506
+ let tx_type = if simulation_results. simulation_results . len ( ) == 1 {
507
+ // If there is only one transaction, it's a bridging transaction
508
+ ChainAbstractionTransactionType :: Bridge
509
+ } else if index == 0 {
510
+ ChainAbstractionTransactionType :: Approve
511
+ } else {
512
+ ChainAbstractionTransactionType :: Bridge
513
+ } ;
514
+ state. metrics . add_ca_gas_estimation (
515
+ simulation_result. transaction . gas ,
516
+ curr_route. chain_id . clone ( ) ,
517
+ tx_type,
518
+ ) ;
464
519
}
465
520
466
521
// Save the bridging transaction to the IRN
@@ -578,3 +633,15 @@ async fn handler_internal(
578
633
. into_response ( ) ,
579
634
) ;
580
635
}
636
+
637
+ fn construct_metrics_bridging_route (
638
+ from_chain_id : String ,
639
+ from_contract : String ,
640
+ to_chain_id : String ,
641
+ to_contract : String ,
642
+ ) -> String {
643
+ format ! (
644
+ "{}:{}->{}:{}" ,
645
+ from_chain_id, from_contract, to_chain_id, to_contract
646
+ )
647
+ }
0 commit comments