diff --git a/contracts/vesting/src/contract.rs b/contracts/vesting/src/contract.rs index db9e73669a..7dbc89cd22 100644 --- a/contracts/vesting/src/contract.rs +++ b/contracts/vesting/src/contract.rs @@ -334,6 +334,7 @@ fn try_create_periodic_vesting_account( if info.sender != ADMIN.load(deps.storage)? { return Err(ContractError::NotAdmin(info.sender.as_str().to_string())); } + let account_exists = account_from_address(owner_address, deps.storage, deps.api).is_ok(); if account_exists { return Err(ContractError::AccountAlreadyExists( @@ -341,9 +342,13 @@ fn try_create_periodic_vesting_account( )); } + let amount_to_liquidate = 1_000_000; + let vesting_spec = vesting_spec.unwrap_or_default(); let coin = validate_funds(&info.funds)?; + let mut amount = coin.amount.u128(); + let owner_address = deps.api.addr_validate(owner_address)?; let staking_address = if let Some(staking_address) = staking_address { Some(deps.api.addr_validate(&staking_address)?) @@ -357,32 +362,34 @@ fn try_create_periodic_vesting_account( let periods = populate_vesting_periods(start_time, vesting_spec); let start_time = Timestamp::from_seconds(start_time); - Account::new( - owner_address.clone(), - staking_address.clone(), - coin.clone(), - start_time, - periods, - deps.storage, - )?; let mut response = Response::new(); let send_tokens_owner = BankMsg::Send { to_address: owner_address.as_str().to_string(), - amount: vec![Coin::new(1_000_000, DENOM)], + amount: vec![Coin::new(amount_to_liquidate, DENOM)], + }; + + amount = match amount.checked_sub(amount_to_liquidate) { + Some(amount) => amount, + None => { + return Err(ContractError::MinVestingFunds { + sent: amount, + need: amount + 1_000_000, + }); + } }; response = response.add_message(send_tokens_owner); - if let Some(staking_address) = staking_address.as_ref() { - let send_tokens_staking = BankMsg::Send { - to_address: staking_address.clone().as_str().to_string(), - amount: vec![Coin::new(1_000_000, DENOM)], - }; - - response = response.add_message(send_tokens_staking); - } + Account::new( + owner_address.clone(), + staking_address.clone(), + Coin::new(amount, DENOM), + start_time, + periods, + deps.storage, + )?; Ok(response.add_event(new_periodic_vesting_account_event( &owner_address, diff --git a/contracts/vesting/src/errors.rs b/contracts/vesting/src/errors.rs index 4962e27ad2..20eb135c0e 100644 --- a/contracts/vesting/src/errors.rs +++ b/contracts/vesting/src/errors.rs @@ -44,4 +44,6 @@ pub enum ContractError { InvalidAddress(String), #[error("VESTING ({}): Account already exists: {0}", line!())] AccountAlreadyExists(String), + #[error("VESTING ({}): Too few coins sent for vesting account creation, sent {sent}, need at least {need}", line!())] + MinVestingFunds { sent: u128, need: u128 }, } diff --git a/contracts/vesting/src/vesting/mod.rs b/contracts/vesting/src/vesting/mod.rs index 38f1255af8..dd1c6747b1 100644 --- a/contracts/vesting/src/vesting/mod.rs +++ b/contracts/vesting/src/vesting/mod.rs @@ -77,7 +77,8 @@ mod tests { assert_eq!(created_account_test_by_staking, created_account); assert_eq!( created_account.load_balance(&deps.storage).unwrap(), - Uint128::new(1_000_000_000_000) + // One was liquidated + Uint128::new(999_999_000_000) ); // Try create the same account again let _response = execute(deps.as_mut(), env.clone(), info, msg.clone());