From 94c075452ef815d2e21229e6cccd6cba21b845fc Mon Sep 17 00:00:00 2001 From: "M. Daeva" Date: Sun, 9 Jul 2023 23:42:22 +0300 Subject: [PATCH] added create job tests --- contracts/warp-controller/src/error.rs | 3 + contracts/warp-controller/src/execute/job.rs | 16 +- .../src/tests/execute/job/test_create_job.rs | 495 +++++++++++++++++- .../warp-controller/src/tests/helpers.rs | 75 ++- .../warp-controller/src/util/variable.rs | 4 +- 5 files changed, 567 insertions(+), 26 deletions(-) diff --git a/contracts/warp-controller/src/error.rs b/contracts/warp-controller/src/error.rs index d82ba9d7..d63b6299 100644 --- a/contracts/warp-controller/src/error.rs +++ b/contracts/warp-controller/src/error.rs @@ -111,6 +111,9 @@ pub enum ContractError { #[error("Variable vector contains unused variables.")] ExcessVariablesInVector {}, + + #[error("Account balance smaller than job reward.")] + AccountBalanceSmallerThanJobReward {}, } impl From for ContractError { diff --git a/contracts/warp-controller/src/execute/job.rs b/contracts/warp-controller/src/execute/job.rs index 7d1c34e7..cd68490f 100644 --- a/contracts/warp-controller/src/execute/job.rs +++ b/contracts/warp-controller/src/execute/job.rs @@ -58,11 +58,7 @@ pub fn create_job( return Err(ContractError::ExcessVariablesInVector {}); } - if !msgs_valid(&data.msgs, &data.vars)? { - return Err(ContractError::MsgError { - msg: "msgs are invalid".to_string(), - }); - } + msgs_valid(&data.msgs, &data.vars)?; let q = ACCOUNTS() .idx @@ -76,6 +72,16 @@ pub fn create_job( Some(q) => q.1, }; + // compare contract balance with job reward + let account_balance = deps + .querier + .query_balance(&account.account, "uluna")? + .amount; + + if account_balance < data.reward { + Err(ContractError::AccountBalanceSmallerThanJobReward {})?; + } + // let mut msgs = vec![]; // for msg in data.msgs { // msgs.push(serde_json_wasm::from_str::(msg.as_str())?) diff --git a/contracts/warp-controller/src/tests/execute/job/test_create_job.rs b/contracts/warp-controller/src/tests/execute/job/test_create_job.rs index ad0e44ec..91496f15 100644 --- a/contracts/warp-controller/src/tests/execute/job/test_create_job.rs +++ b/contracts/warp-controller/src/tests/execute/job/test_create_job.rs @@ -1,26 +1,503 @@ +use cosmwasm_std::{ + coin, + testing::{mock_dependencies, mock_env, mock_info}, + to_binary, BankMsg, CosmosMsg, DistributionMsg, GovMsg, IbcMsg, IbcTimeout, IbcTimeoutBlock, + ReplyOn, Response, StakingMsg, SubMsg, Uint128, Uint64, VoteOption, WasmMsg, +}; + +use crate::{ + contract::execute, + tests::helpers::{init_warp_account, instantiate_warp, ACCOUNT_BALANCE, MIN_REWARD}, + ContractError, +}; + +use account::GenericMsg; +use controller::{ + condition::{BlockExpr, Condition, Expr, NumOp}, + job::CreateJobMsg, + variable::{StaticVariable, Variable, VariableKind}, + ExecuteMsg, +}; + #[test] -fn test_create_job_success() {} +fn test_create_job_success() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "send funds job".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap(); + + let expected = Response::new() + .add_attributes(vec![ + ("action", "create_job"), + ("job_id", "1"), + ("job_owner", "terra1vladvladvladvladvladvladvladvladvl1000"), + ("job_name", "send funds job"), + ("job_status", "\"Pending\""), + ("job_condition", "{\"expr\":{\"block_height\":{\"comparator\":\"42\",\"op\":\"gte\"}}}"), + ("job_msgs", "[\"{\\\"bank\\\":{\\\"send\\\":{\\\"to_address\\\":\\\"vlad\\\",\\\"amount\\\":[{\\\"denom\\\":\\\"uluna\\\",\\\"amount\\\":\\\"1000\\\"}]}}}\"]"), + ("job_reward", "10000"), + ("job_creation_fee", "0"), + ("job_last_updated_time", "1571797419") + ]) + .add_submessage(SubMsg { + id: 0, + msg: CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: "terra1vladvladvladvladvladvladvladvladvl2000".to_string(), + msg: to_binary(&account::ExecuteMsg::Generic(GenericMsg {msgs: vec![ + CosmosMsg::Bank(BankMsg::Send { + to_address: "cosmos2contract".to_string(), + amount: vec![coin(10000, "uluna")] }) + ]})).unwrap(), + funds: vec![], + }), + gas_limit: None, + reply_on: ReplyOn::Never, + }) + .add_submessage(SubMsg { + id: 0, + msg: CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: "terra1vladvladvladvladvladvladvladvladvl2000".to_string(), + msg: to_binary(&account::ExecuteMsg::Generic(GenericMsg {msgs: vec![ + CosmosMsg::Bank(BankMsg::Send { + to_address: "vlad".to_string(), + amount: vec![coin(0, "uluna")] }) + ]})).unwrap(), + funds: vec![], + }), + gas_limit: None, + reply_on: ReplyOn::Never, + }); + + assert_eq!(res, expected); +} #[test] -fn test_create_job_success_multiple_msgs_all_types() {} +fn test_create_job_success_multiple_msgs_all_types() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let cosmos_msgs: Vec = vec![ + CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: "contract".to_string(), + msg: to_binary("test").unwrap(), + funds: vec![coin(AMOUNT_TO_SEND, DENOM)], + }), + CosmosMsg::Bank(BankMsg::Send { + to_address: "vlad2".to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }), + CosmosMsg::Gov(GovMsg::Vote { + proposal_id: 0, + vote: VoteOption::Yes, + }), + CosmosMsg::Staking(StakingMsg::Delegate { + validator: "vladidator".to_string(), + amount: coin(AMOUNT_TO_SEND, DENOM), + }), + CosmosMsg::Distribution(DistributionMsg::SetWithdrawAddress { + address: "vladdress".to_string(), + }), + CosmosMsg::Ibc(IbcMsg::Transfer { + channel_id: "channel_vlad".to_string(), + to_address: "vlad3".to_string(), + amount: coin(AMOUNT_TO_SEND, DENOM), + timeout: IbcTimeout::with_block(IbcTimeoutBlock { + revision: 0, + height: 0, + }), + }), + CosmosMsg::Stargate { + type_url: "utl".to_string(), + value: Default::default(), + }, + ]; + + let msgs = cosmos_msgs + .iter() + .map(|msg| serde_json_wasm::to_string(&msg).unwrap()) + .collect(); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "multiple jobs".to_string(), + description: format!("multiple jobs").to_string(), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs, + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap(); + + let expected = Response::new() + .add_attributes(vec![ + ("action", "create_job"), + ("job_id", "1"), + ("job_owner", "terra1vladvladvladvladvladvladvladvladvl1000"), + ("job_name", "multiple jobs"), + ("job_status", "\"Pending\""), + ("job_condition", "{\"expr\":{\"block_height\":{\"comparator\":\"42\",\"op\":\"gte\"}}}"), + ("job_msgs", "[\"{\\\"wasm\\\":{\\\"execute\\\":{\\\"contract_addr\\\":\\\"contract\\\",\\\"msg\\\":\\\"InRlc3Qi\\\",\\\"funds\\\":[{\\\"denom\\\":\\\"uluna\\\",\\\"amount\\\":\\\"1000\\\"}]}}}\",\"{\\\"bank\\\":{\\\"send\\\":{\\\"to_address\\\":\\\"vlad2\\\",\\\"amount\\\":[{\\\"denom\\\":\\\"uluna\\\",\\\"amount\\\":\\\"1000\\\"}]}}}\",\"{\\\"gov\\\":{\\\"vote\\\":{\\\"proposal_id\\\":0,\\\"vote\\\":\\\"yes\\\"}}}\",\"{\\\"staking\\\":{\\\"delegate\\\":{\\\"validator\\\":\\\"vladidator\\\",\\\"amount\\\":{\\\"denom\\\":\\\"uluna\\\",\\\"amount\\\":\\\"1000\\\"}}}}\",\"{\\\"distribution\\\":{\\\"set_withdraw_address\\\":{\\\"address\\\":\\\"vladdress\\\"}}}\",\"{\\\"ibc\\\":{\\\"transfer\\\":{\\\"channel_id\\\":\\\"channel_vlad\\\",\\\"to_address\\\":\\\"vlad3\\\",\\\"amount\\\":{\\\"denom\\\":\\\"uluna\\\",\\\"amount\\\":\\\"1000\\\"},\\\"timeout\\\":{\\\"block\\\":{\\\"revision\\\":0,\\\"height\\\":0},\\\"timestamp\\\":null}}}}\",\"{\\\"stargate\\\":{\\\"type_url\\\":\\\"utl\\\",\\\"value\\\":\\\"\\\"}}\"]"), + ("job_reward", "10000"), + ("job_creation_fee", "0"), + ("job_last_updated_time", "1571797419") + ]) + .add_submessage(SubMsg { + id: 0, + msg: CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: "terra1vladvladvladvladvladvladvladvladvl2000".to_string(), + msg: to_binary(&account::ExecuteMsg::Generic(GenericMsg {msgs: vec![ + CosmosMsg::Bank(BankMsg::Send { + to_address: "cosmos2contract".to_string(), + amount: vec![coin(10000, "uluna")] }) + ]})).unwrap(), + funds: vec![], + }), + gas_limit: None, + reply_on: ReplyOn::Never, + }) + .add_submessage(SubMsg { + id: 0, + msg: CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: "terra1vladvladvladvladvladvladvladvladvl2000".to_string(), + msg: to_binary(&account::ExecuteMsg::Generic(GenericMsg {msgs: vec![ + CosmosMsg::Bank(BankMsg::Send { + to_address: "vlad".to_string(), + amount: vec![coin(0, "uluna")] }) + ]})).unwrap(), + funds: vec![], + }), + gas_limit: None, + reply_on: ReplyOn::Never, + }); + + assert_eq!(res, expected); +} #[test] -fn test_create_job_no_account() {} +fn test_create_job_no_account() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let mut deps = mock_dependencies(); + let env = mock_env(); + let info = mock_info("vlad", &[]); + + let _instantiate_res = instantiate_warp( + deps.as_mut(), + env.clone(), + info.clone(), + Some(info.sender.to_string()), + Some(info.sender.to_string()), + Uint64::new(0), + Uint128::new(0), + Uint64::new(0), + Uint64::new(0), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + ) + .unwrap(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "send funds job".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(res, ContractError::AccountDoesNotExist {}); +} #[test] -fn test_create_job_no_funds_in_account() {} +fn test_create_job_not_enough_funds_in_account() { + const REWARD: u128 = ACCOUNT_BALANCE * 2; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "send funds job".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(res, ContractError::AccountBalanceSmallerThanJobReward {}); +} #[test] -fn test_create_job_reward_too_small() {} +fn test_create_job_reward_too_small() { + const REWARD: u128 = MIN_REWARD - 1; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "send funds job".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(res, ContractError::RewardTooSmall {}); +} #[test] -fn test_create_job_invalid_condition() {} +fn test_create_job_invalid_condition() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let var = Variable::Static(StaticVariable { + kind: VariableKind::Uint, + name: "var1".to_string(), + value: "var1 value".to_string(), + update_fn: None, + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "send funds job".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![var], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(res, ContractError::InvalidVariables {}); +} #[test] -fn test_create_job_invalid_msgs() {} +fn test_create_job_invalid_msgs() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let sg_msg: CosmosMsg = CosmosMsg::Stargate { + type_url: "utl".to_string(), + value: Default::default(), + }; + + let fake_msg = serde_json_wasm::to_string(&sg_msg) + .unwrap() + .replace("type_url", "type_urm"); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "send funds job".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![fake_msg], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(&res.to_string(), "Error deserializing data"); +} #[test] -fn test_create_job_name_too_short() {} +fn test_create_job_name_too_short() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: "".to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(res, ContractError::NameTooShort {}); +} #[test] -fn test_create_job_name_too_long() {} +fn test_create_job_name_too_long() { + const REWARD: u128 = MIN_REWARD; + const AMOUNT_TO_SEND: u128 = 1_000; + const DENOM: &str = "uluna"; + const RECEIVER: &str = "vlad"; + const TARGET_BLOCK_HEIGHT: u64 = 42; + const MAX_JOB_DESC_LENGTH: usize = 140; + + let (mut deps, env, mut info, account_address) = init_warp_account(); + + let bank_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send { + to_address: RECEIVER.to_string(), + amount: vec![coin(AMOUNT_TO_SEND, DENOM)], + }); + + let create_job_msg = ExecuteMsg::CreateJob(CreateJobMsg { + name: format!("{}", "q".repeat(MAX_JOB_DESC_LENGTH + 1)).to_string(), + description: format!("send {AMOUNT_TO_SEND} {DENOM} to {RECEIVER}"), + labels: vec![], + condition: Condition::Expr(Box::new(Expr::BlockHeight(BlockExpr { + comparator: Uint64::from(TARGET_BLOCK_HEIGHT), + op: NumOp::Gte, + }))), + msgs: vec![serde_json_wasm::to_string(&bank_msg).unwrap()], + vars: vec![], + recurring: false, + requeue_on_evict: false, + reward: Uint128::from(REWARD), + assets_to_withdraw: None, + }); + + info.sender = account_address; + let res = execute(deps.as_mut(), env, info, create_job_msg).unwrap_err(); + + assert_eq!(res, ContractError::NameTooLong {}); +} diff --git a/contracts/warp-controller/src/tests/helpers.rs b/contracts/warp-controller/src/tests/helpers.rs index 6c611e4d..bed589b3 100644 --- a/contracts/warp-controller/src/tests/helpers.rs +++ b/contracts/warp-controller/src/tests/helpers.rs @@ -1,15 +1,22 @@ -use crate::contract::{instantiate, reply}; -use crate::execute::account::create_account; -use crate::ContractError; -use controller::account::CreateAccountMsg; -use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage}; use cosmwasm_std::{ - Attribute, DepsMut, Env, Event, MessageInfo, OwnedDeps, Reply, Response, SubMsgResponse, - SubMsgResult, Uint128, Uint64, + coin, + testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage}, + Addr, Attribute, DepsMut, Empty, Env, Event, MessageInfo, OwnedDeps, Reply, Response, + SubMsgResponse, SubMsgResult, Uint128, Uint64, }; -use controller::InstantiateMsg; +use controller::{account::CreateAccountMsg, InstantiateMsg}; +use crate::{ + contract::{instantiate, reply}, + execute::account::create_account, + ContractError, +}; + +pub const MIN_REWARD: u128 = 10_000; +pub const ACCOUNT_BALANCE: u128 = 20_000; + +#[allow(clippy::too_many_arguments)] pub fn instantiate_warp( deps: DepsMut, env: Env, @@ -40,7 +47,7 @@ pub fn instantiate_warp( q_max, }; - return instantiate(deps, env.clone(), info.clone(), instantiate_msg.clone()); + instantiate(deps, env, info, instantiate_msg) } pub fn create_warp_account( @@ -87,5 +94,53 @@ pub fn create_warp_account( let reply_res = reply(deps.as_mut(), env, reply_msg); - return (create_account_res, reply_res); + (create_account_res, reply_res) +} + +pub fn init_warp_account() -> ( + OwnedDeps, + Env, + MessageInfo, + Addr, +) { + let mut deps = mock_dependencies(); + let env = mock_env(); + let info = mock_info("vlad", &[]); + + let _instantiate_res = instantiate_warp( + deps.as_mut(), + env.clone(), + info.clone(), + Some(info.sender.to_string()), + Some(info.sender.to_string()), + Uint64::new(0), + Uint128::new(MIN_REWARD), + Uint64::new(0), + Uint64::new(0), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + Default::default(), + ) + .unwrap(); + + let (_create_account_res, reply_res) = + create_warp_account(&mut deps, env.clone(), info.clone(), Uint64::new(0)); + + let account_addr = Addr::unchecked(get_attr(&reply_res.unwrap(), "account_address")); + + // mock account balance + deps.querier = MockQuerier::new(&[(account_addr.as_str(), &[coin(ACCOUNT_BALANCE, "uluna")])]); + + (deps, env, info, account_addr) +} + +fn get_attr(res: &Response, attr: &str) -> String { + res.attributes + .iter() + .find(|attribute| attribute.key == attr) + .unwrap() + .value + .to_owned() } diff --git a/contracts/warp-controller/src/util/variable.rs b/contracts/warp-controller/src/util/variable.rs index 27319ac6..c15ee80a 100644 --- a/contracts/warp-controller/src/util/variable.rs +++ b/contracts/warp-controller/src/util/variable.rs @@ -308,7 +308,7 @@ fn replace_in_string(value: String, vars: &[Variable]) -> Result, vars: &Vec) -> Result { +pub fn msgs_valid(msgs: &Vec, vars: &Vec) -> Result<(), ContractError> { let mut parsed_msgs: Vec = vec![]; for msg in msgs { let mut replaced_msg = msg.clone(); @@ -367,7 +367,7 @@ pub fn msgs_valid(msgs: &Vec, vars: &Vec) -> Result(&replaced_msg)?) } //todo: check if msgs valid - Ok(true) + Ok(()) } pub fn apply_var_fn(