Skip to content

Commit 9e631f1

Browse files
committed
feat: add balance lock in cli
1 parent 3419a50 commit 9e631f1

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

crates/cli/src/main.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use aligned_sdk::verification_layer::estimate_fee;
1919
use aligned_sdk::verification_layer::get_chain_id;
2020
use aligned_sdk::verification_layer::get_nonce_from_batcher;
2121
use aligned_sdk::verification_layer::get_nonce_from_ethereum;
22+
use aligned_sdk::verification_layer::lock_balance_in_aligned;
2223
use aligned_sdk::verification_layer::unlock_balance_in_aligned;
2324
use aligned_sdk::verification_layer::withdraw_balance_from_aligned;
2425
use aligned_sdk::verification_layer::{deposit_to_aligned, get_balance_in_aligned};
@@ -43,6 +44,7 @@ use crate::AlignedCommands::GetUserBalance;
4344
use crate::AlignedCommands::GetUserNonce;
4445
use crate::AlignedCommands::GetUserNonceFromEthereum;
4546
use crate::AlignedCommands::GetVkCommitment;
47+
use crate::AlignedCommands::LockFunds;
4648
use crate::AlignedCommands::Submit;
4749
use crate::AlignedCommands::UnlockFunds;
4850
use crate::AlignedCommands::VerifyProofOnchain;
@@ -70,7 +72,9 @@ pub enum AlignedCommands {
7072
)]
7173
DepositToBatcher(DepositToBatcherArgs),
7274
#[clap(about = "Unlocks funds from the batcher", name = "unlock-funds")]
73-
UnlockFunds(UnlockFundsArgs),
75+
UnlockFunds(LockUnlockFundsArgs),
76+
#[clap(about = "Lock funds in the batcher", name = "unlock-funds")]
77+
LockFunds(LockUnlockFundsArgs),
7478
#[clap(about = "Withdraw funds from the batcher", name = "withdraw-funds")]
7579
WithdrawFunds(WithdrawFundsArgs),
7680
#[clap(about = "Get user balance from the batcher", name = "get-user-balance")]
@@ -218,7 +222,7 @@ pub struct DepositToBatcherArgs {
218222

219223
#[derive(Parser, Debug)]
220224
#[command(version, about, long_about = None)]
221-
pub struct UnlockFundsArgs {
225+
pub struct LockUnlockFundsArgs {
222226
#[command(flatten)]
223227
private_key_type: PrivateKeyType,
224228
#[arg(
@@ -836,6 +840,50 @@ async fn main() -> Result<(), AlignedError> {
836840
}
837841
}
838842
}
843+
LockFunds(args) => {
844+
let eth_rpc_url = args.eth_rpc_url;
845+
let eth_rpc_provider =
846+
Provider::<Http>::try_from(eth_rpc_url.clone()).map_err(|e| {
847+
SubmitError::EthereumProviderError(format!(
848+
"Error while connecting to Ethereum: {}",
849+
e
850+
))
851+
})?;
852+
853+
let keystore_path = &args.private_key_type.keystore_path;
854+
let private_key = &args.private_key_type.private_key;
855+
856+
let mut wallet = if let Some(keystore_path) = keystore_path {
857+
let password = rpassword::prompt_password("Please enter your keystore password:")
858+
.map_err(|e| SubmitError::GenericError(e.to_string()))?;
859+
Wallet::decrypt_keystore(keystore_path, password)
860+
.map_err(|e| SubmitError::GenericError(e.to_string()))?
861+
} else if let Some(private_key) = private_key {
862+
private_key
863+
.parse::<LocalWallet>()
864+
.map_err(|e| SubmitError::GenericError(e.to_string()))?
865+
} else {
866+
warn!("Missing keystore or private key used for payment.");
867+
return Ok(());
868+
};
869+
870+
let chain_id = get_chain_id(eth_rpc_url.as_str()).await?;
871+
wallet = wallet.with_chain_id(chain_id);
872+
873+
let client = SignerMiddleware::new(eth_rpc_provider, wallet);
874+
875+
match lock_balance_in_aligned(&client, args.network.into()).await {
876+
Ok(receipt) => {
877+
info!(
878+
"Funds in batcher locked successfully. Receipt: 0x{:x}",
879+
receipt.transaction_hash
880+
);
881+
}
882+
Err(e) => {
883+
error!("Transaction failed: {:?}", e);
884+
}
885+
}
886+
}
839887
WithdrawFunds(args) => {
840888
if !args.amount.ends_with("ether") {
841889
error!("Amount should be in the format XX.XXether");

0 commit comments

Comments
 (0)