@@ -14,7 +14,7 @@ use aligned_sdk::core::{
1414} ;
1515use aligned_sdk:: sdk:: get_chain_id;
1616use aligned_sdk:: sdk:: get_next_nonce;
17- use aligned_sdk:: sdk:: get_payment_service_address ;
17+ use aligned_sdk:: sdk:: { deposit_to_aligned , get_balance_in_aligned } ;
1818use aligned_sdk:: sdk:: { get_vk_commitment, is_proof_verified, submit_multiple} ;
1919use clap:: Parser ;
2020use clap:: Subcommand ;
@@ -418,6 +418,10 @@ async fn main() -> Result<(), AlignedError> {
418418
419419 let amount = deposit_to_batcher_args. amount . replace ( "ether" , "" ) ;
420420
421+ let amount_ether = parse_ether ( & amount) . map_err ( |e| {
422+ SubmitError :: EthereumProviderError ( format ! ( "Error while parsing amount: {}" , e) )
423+ } ) ?;
424+
421425 let eth_rpc_url = deposit_to_batcher_args. eth_rpc_url ;
422426
423427 let eth_rpc_provider =
@@ -445,99 +449,41 @@ async fn main() -> Result<(), AlignedError> {
445449
446450 let client = SignerMiddleware :: new ( eth_rpc_provider. clone ( ) , wallet. clone ( ) ) ;
447451
448- let balance = client
449- . get_balance ( wallet. address ( ) , None )
452+ match deposit_to_aligned ( amount_ether, client, deposit_to_batcher_args. network . into ( ) )
450453 . await
451- . map_err ( |e| {
452- SubmitError :: EthereumProviderError ( format ! (
453- "Error while getting balance: {}" ,
454- e
455- ) )
456- } ) ?;
457-
458- let amount_ether = parse_ether ( & amount) . map_err ( |e| {
459- SubmitError :: EthereumProviderError ( format ! ( "Error while parsing amount: {}" , e) )
460- } ) ?;
461-
462- if amount_ether <= U256 :: from ( 0 ) {
463- error ! ( "Amount should be greater than 0" ) ;
464- return Ok ( ( ) ) ;
465- }
466-
467- if balance < amount_ether {
468- error ! ( "Insufficient funds to pay to the batcher. Please deposit some Ether in your wallet." ) ;
469- return Ok ( ( ) ) ;
470- }
471-
472- let batcher_addr = get_payment_service_address ( deposit_to_batcher_args. network . into ( ) ) ;
473-
474- let tx = TransactionRequest :: new ( )
475- . to ( batcher_addr)
476- . value ( amount_ether)
477- . from ( wallet. address ( ) ) ;
478-
479- info ! ( "Sending {} ether to the batcher" , amount) ;
480-
481- let tx = client
482- . send_transaction ( tx, None )
483- . await
484- . map_err ( |e| {
485- SubmitError :: EthereumProviderError ( format ! (
486- "Error while sending transaction: {}" ,
487- e
488- ) )
489- } ) ?
490- . await
491- . map_err ( |e| {
492- SubmitError :: EthereumProviderError ( format ! (
493- "Error while sending transaction: {}" ,
494- e
495- ) )
496- } ) ?;
497-
498- if let Some ( tx) = tx {
499- info ! (
500- "Payment sent to the batcher successfully. Tx: 0x{:x}" ,
501- tx. transaction_hash
502- ) ;
503- } else {
504- error ! ( "Transaction failed" ) ;
454+ {
455+ Ok ( receipt) => {
456+ info ! (
457+ "Payment sent to the batcher successfully. Tx: 0x{:x}" ,
458+ receipt. transaction_hash
459+ ) ;
460+ }
461+ Err ( e) => {
462+ error ! ( "Transaction failed: {:?}" , e) ;
463+ }
505464 }
506465 }
507466 GetUserBalance ( get_user_balance_args) => {
508- let eth_rpc_url = get_user_balance_args. eth_rpc_url ;
509-
510- let eth_rpc_provider = Provider :: < Http > :: try_from ( eth_rpc_url) . map_err ( |e| {
511- SubmitError :: EthereumProviderError ( format ! (
512- "Error while connecting to Ethereum: {}" ,
513- e
514- ) )
515- } ) ?;
516-
517- let user_address =
518- Address :: from_str ( & get_user_balance_args. user_address ) . map_err ( |e| {
519- SubmitError :: HexDecodingError ( format ! (
520- "Error while parsing user address: {}" ,
521- e
522- ) )
523- } ) ?;
524-
525- let batcher_addr = get_payment_service_address ( get_user_balance_args. network . into ( ) ) ;
526-
527- let balance = get_user_balance ( eth_rpc_provider, batcher_addr, user_address)
528- . await
529- . map_err ( |e| {
530- SubmitError :: EthereumProviderError ( format ! (
531- "Error while getting user balance: {}" ,
532- e
533- ) )
534- } ) ?;
535-
536- info ! (
537- "User {} has {} ether in the batcher" ,
467+ let user_address = H160 :: from_str ( & get_user_balance_args. user_address ) . unwrap ( ) ;
468+ match get_balance_in_aligned (
538469 user_address,
539- format_ether( balance)
540- ) ;
470+ & get_user_balance_args. eth_rpc_url ,
471+ get_user_balance_args. network . into ( ) ,
472+ )
473+ . await
474+ {
475+ Ok ( balance) => {
476+ info ! (
477+ "User {} has {} ether in the batcher" ,
478+ user_address,
479+ format_ether( balance)
480+ ) ;
481+ }
482+ Err ( e) => {
483+ error ! ( "Error while getting user balance: {:?}" , e) ;
484+ return Ok ( ( ) ) ;
485+ }
486+ }
541487 }
542488 }
543489
0 commit comments