Skip to main content

amadeus_runtime/consensus/bic/
lockup.rs

1use crate::consensus::consensus_apply::ApplyEnv;
2use crate::consensus::consensus_kv::{kv_delete, kv_get, kv_increment, kv_put};
3use crate::{Result, bcat};
4
5/// Create a lock vault for a receiver with a specified amount and unlock epoch
6pub fn create_lock(env: &mut ApplyEnv, receiver: &[u8], symbol: &[u8], amount: i128, unlock_epoch: u64) -> Result<()> {
7    if amount <= 0 {
8        return Err("invalid_amount");
9    }
10
11    let vault_index = kv_increment(env, b"bic:lockup:unique_index", 1)?;
12    let vault_value = bcat(&[unlock_epoch.to_string().as_bytes(), b"-", amount.to_string().as_bytes(), b"-", symbol]);
13    kv_put(env, &bcat(&[b"bic:lockup:vault:", receiver, b":", vault_index.to_string().as_bytes()]), &vault_value)?;
14    Ok(())
15}
16
17/// Unlock a vault and transfer funds to caller
18pub fn call_unlock(env: &mut ApplyEnv, args: Vec<Vec<u8>>) -> Result<()> {
19    if args.len() != 1 {
20        return Err("invalid_args");
21    }
22    let vault_index = args[0].as_slice();
23
24    let vault_key = bcat(&[b"bic:lockup:vault:", &env.caller_env.account_caller, b":", vault_index]);
25
26    let vault = kv_get(env, &vault_key)?.ok_or("invalid_vault")?;
27
28    let vault_parts: Vec<Vec<u8>> = vault.split(|&b| b == b'-').map(|seg| seg.to_vec()).collect();
29    if vault_parts.len() < 3 {
30        return Err("invalid_vault_format");
31    }
32
33    let unlock_epoch =
34        std::str::from_utf8(&vault_parts[0]).ok().and_then(|s| s.parse::<u64>().ok()).ok_or("invalid_unlock_epoch")?;
35    let amount = std::str::from_utf8(&vault_parts[1])
36        .ok()
37        .and_then(|s| s.parse::<i128>().ok())
38        .ok_or("invalid_unlock_amount")?;
39    let symbol = &vault_parts[2];
40
41    if env.caller_env.entry_epoch < unlock_epoch {
42        return Err("vault_is_locked");
43    }
44
45    kv_increment(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":balance:", symbol]), amount)?;
46    kv_delete(env, &vault_key)?;
47    Ok(())
48}