Skip to content

Commit 7ba92c5

Browse files
committed
feat: unblock time checks in withdraw funds cli
1 parent bc37b40 commit 7ba92c5

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

crates/cli/src/main.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::io::BufReader;
44
use std::io::Write;
55
use std::path::PathBuf;
66
use std::str::FromStr;
7+
use std::time::Duration;
8+
use std::time::SystemTime;
9+
use std::time::UNIX_EPOCH;
710

811
use aligned_sdk::aggregation_layer;
912
use aligned_sdk::aggregation_layer::AggregationModeVerificationData;
@@ -19,6 +22,7 @@ use aligned_sdk::verification_layer::estimate_fee;
1922
use aligned_sdk::verification_layer::get_chain_id;
2023
use aligned_sdk::verification_layer::get_nonce_from_batcher;
2124
use aligned_sdk::verification_layer::get_nonce_from_ethereum;
25+
use aligned_sdk::verification_layer::get_unlock_block_time;
2226
use aligned_sdk::verification_layer::lock_balance_in_aligned;
2327
use aligned_sdk::verification_layer::unlock_balance_in_aligned;
2428
use aligned_sdk::verification_layer::withdraw_balance_from_aligned;
@@ -29,6 +33,7 @@ use clap::Parser;
2933
use clap::Subcommand;
3034
use clap::ValueEnum;
3135
use env_logger::Env;
36+
use ethers::core::k256::pkcs8::der::asn1::UtcTime;
3237
use ethers::prelude::*;
3338
use ethers::utils::format_ether;
3439
use ethers::utils::hex;
@@ -922,6 +927,54 @@ async fn main() -> Result<(), AlignedError> {
922927
return Ok(());
923928
};
924929

930+
let unlock_block_time = match get_unlock_block_time(
931+
wallet.address(),
932+
&eth_rpc_url,
933+
args.network.clone().into(),
934+
)
935+
.await
936+
{
937+
Ok(value) => value,
938+
Err(e) => {
939+
error!("Failed to get : {:?}", e);
940+
return Ok(());
941+
}
942+
};
943+
944+
let current_timestamp = SystemTime::now()
945+
.duration_since(UNIX_EPOCH)
946+
.expect("Time went backwards")
947+
.as_secs();
948+
949+
let unlock_time = UtcTime::from_unix_duration(Duration::from_secs(unlock_block_time))
950+
.expect("invalid unlock time");
951+
let now_time =
952+
UtcTime::from_system_time(SystemTime::now()).expect("invalid system time");
953+
954+
let retry_after_minutes = if unlock_block_time > current_timestamp {
955+
(unlock_block_time - current_timestamp) / 60
956+
} else {
957+
0
958+
};
959+
960+
if unlock_block_time == 0 {
961+
error!("Funds are locked, you need to unlock them first.");
962+
return Ok(());
963+
}
964+
965+
if unlock_block_time > current_timestamp {
966+
warn!(
967+
"Funds are still locked. You need to wait {} minutes before being able to withdraw after unlocking the funds.\n\
968+
Unlocks in block time: {}\n\
969+
Current time: {}",
970+
retry_after_minutes,
971+
format_utc_time(unlock_time),
972+
format_utc_time(now_time)
973+
);
974+
975+
return Ok(());
976+
}
977+
925978
let chain_id = get_chain_id(eth_rpc_url.as_str()).await?;
926979
wallet = wallet.with_chain_id(chain_id);
927980

@@ -1208,3 +1261,17 @@ pub async fn get_user_balance(
12081261
))
12091262
}
12101263
}
1264+
1265+
fn format_utc_time(date: UtcTime) -> String {
1266+
let dt = date.to_date_time();
1267+
1268+
format!(
1269+
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}Z",
1270+
dt.year(),
1271+
dt.month(),
1272+
dt.day(),
1273+
dt.hour(),
1274+
dt.minutes(),
1275+
dt.seconds()
1276+
)
1277+
}

0 commit comments

Comments
 (0)