From f46152ac42d8967dc7674b802c31996b07a9742b Mon Sep 17 00:00:00 2001 From: Nino Lipartiia Date: Thu, 22 Dec 2022 16:10:00 +0200 Subject: [PATCH] new rule: CheckPassedProposals --- contracts/cw-croncat/src/balancer.rs | 9 ++-- contracts/cw-croncat/src/helpers.rs | 6 +-- contracts/cw-croncat/src/owner.rs | 12 +++--- contracts/cw-rules/src/contract.rs | 42 ++++++++++++++++++- contracts/cw-rules/src/types.rs | 16 ++++++- packages/cw-croncat-core/schema/croncat.json | 23 ++++++++++ .../cw-croncat-core/schema/execute_msg.json | 23 ++++++++++ .../schema/get_agent_tasks_response.json | 23 ++++++++++ .../schema/get_state_response.json | 23 ++++++++++ .../schema/get_task_response.json | 23 ++++++++++ .../schema/get_tasks_by_owner_response.json | 23 ++++++++++ .../schema/get_tasks_response.json | 23 ++++++++++ .../get_tasks_with_queries_response.json | 23 ++++++++++ .../cw-croncat-core/schema/query_msg.json | 23 ++++++++++ .../cw-rules-core/schema/croncat_query.json | 23 ++++++++++ .../cw-rules-core/schema/query_construct.json | 23 ++++++++++ packages/cw-rules-core/schema/query_msg.json | 35 ++++++++++++++++ packages/cw-rules-core/src/msg.rs | 5 ++- packages/cw-rules-core/src/types.rs | 6 +++ 19 files changed, 365 insertions(+), 19 deletions(-) diff --git a/contracts/cw-croncat/src/balancer.rs b/contracts/cw-croncat/src/balancer.rs index 87644e4d..970dcf39 100644 --- a/contracts/cw-croncat/src/balancer.rs +++ b/contracts/cw-croncat/src/balancer.rs @@ -44,9 +44,6 @@ pub struct RoundRobinBalancer { } impl RoundRobinBalancer { - pub fn default() -> RoundRobinBalancer { - RoundRobinBalancer::new(BalancerMode::ActivationOrder) - } pub fn new(mode: BalancerMode) -> RoundRobinBalancer { RoundRobinBalancer { mode } } @@ -292,3 +289,9 @@ impl<'a> Balancer<'a> for RoundRobinBalancer { Ok(()) } } + +impl Default for RoundRobinBalancer { + fn default() -> RoundRobinBalancer { + RoundRobinBalancer::new(BalancerMode::ActivationOrder) + } +} diff --git a/contracts/cw-croncat/src/helpers.rs b/contracts/cw-croncat/src/helpers.rs index 1e6d91df..16eaabe2 100644 --- a/contracts/cw-croncat/src/helpers.rs +++ b/contracts/cw-croncat/src/helpers.rs @@ -177,11 +177,7 @@ impl<'a> CwCroncat<'a> { // It's possible there are more "covered tasks" than total tasks, // so use saturating subtraction to hit zero and not go below let total_tasks_needing_agents = total_tasks.saturating_sub(num_tasks_covered); - let remainder = if total_tasks_needing_agents % max_tasks == 0 { - 0 - } else { - 1 - }; + let remainder = u64::from(total_tasks_needing_agents % max_tasks != 0); total_tasks_needing_agents / max_tasks + remainder } else { 0 diff --git a/contracts/cw-croncat/src/owner.rs b/contracts/cw-croncat/src/owner.rs index fef6ff55..f1951e18 100644 --- a/contracts/cw-croncat/src/owner.rs +++ b/contracts/cw-croncat/src/owner.rs @@ -204,7 +204,7 @@ impl<'a> CwCroncat<'a> { // Querier guarantees to returns up-to-date data, including funds sent in this handle message // https://github.com/CosmWasm/wasmd/blob/master/x/wasm/internal/keeper/keeper.go#L185-L192 - let state_balances = deps.querier.query_all_balances(&env.contract.address)?; + let state_balances = deps.querier.query_all_balances(env.contract.address)?; let mut has_fund_err = false; let messages: Result, ContractError> = balances @@ -303,7 +303,7 @@ impl<'a> CwCroncat<'a> { .time_slots .range(deps.storage, None, None, Order::Ascending) .skip(from_index_unwrap as usize) - .take(limit_unwrap as usize) + .take(limit_unwrap) .map(|res| { let res = res.unwrap(); SlotResponse { @@ -317,7 +317,7 @@ impl<'a> CwCroncat<'a> { .block_slots .range(deps.storage, None, None, Order::Ascending) .skip(from_index_unwrap as usize) - .take(limit_unwrap as usize) + .take(limit_unwrap) .map(|res| { let res = res.unwrap(); SlotResponse { @@ -331,7 +331,7 @@ impl<'a> CwCroncat<'a> { .balances .range(deps.storage, None, None, Order::Ascending) .skip(from_index_unwrap as usize) - .take(limit_unwrap as usize) + .take(limit_unwrap) .map(|res| { let res = res.unwrap(); BalancesResponse { @@ -350,7 +350,7 @@ impl<'a> CwCroncat<'a> { .time_map_queries .range(deps.storage, None, None, Order::Ascending) .skip(from_index_unwrap as usize) - .take(limit_unwrap as usize) + .take(limit_unwrap) .map(|res| { let res = res.unwrap(); SlotWithQueriesResponse { @@ -364,7 +364,7 @@ impl<'a> CwCroncat<'a> { .block_map_queries .range(deps.storage, None, None, Order::Ascending) .skip(from_index_unwrap as usize) - .take(limit_unwrap as usize) + .take(limit_unwrap) .map(|res| { let res = res.unwrap(); SlotWithQueriesResponse { diff --git a/contracts/cw-rules/src/contract.rs b/contracts/cw-rules/src/contract.rs index 19dcc84d..fb7fb215 100644 --- a/contracts/cw-rules/src/contract.rs +++ b/contracts/cw-rules/src/contract.rs @@ -1,5 +1,7 @@ use cw_rules_core::msg::{QueryConstruct, QueryConstructResponse}; -use cw_rules_core::types::{CheckOwnerOfNft, CheckProposalStatus, CroncatQuery, HasBalanceGte}; +use cw_rules_core::types::{ + CheckOwnerOfNft, CheckPassedProposals, CheckProposalStatus, CroncatQuery, HasBalanceGte, +}; // use schemars::JsonSchema; // use serde::{Deserialize, Serialize}; @@ -21,7 +23,7 @@ use cw_rules_core::msg::{ExecuteMsg, InstantiateMsg, QueryMsg, QueryResponse}; //use cosmwasm_std::from_binary; //use crate::msg::QueryMultiResponse; -use crate::types::dao::{ProposalResponse, QueryDao, Status}; +use crate::types::dao::{ProposalListResponse, ProposalResponse, QueryDao, Status}; use generic_query::GenericQuery; // version info for migration info @@ -87,6 +89,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { proposal_id, status, )?), + QueryMsg::CheckPassedProposals(CheckPassedProposals { dao_address }) => { + to_binary(&query_dao_proposals(deps, dao_address)?) + } QueryMsg::GenericQuery(query) => to_binary(&generic_query(deps, query)?), QueryMsg::SmartQuery(query) => to_binary(&smart_query(deps, query)?), QueryMsg::QueryConstruct(QueryConstruct { queries }) => { @@ -203,6 +208,36 @@ fn query_dao_proposal_status( }) } +// Check for passed proposals +// Return the first passed proposal +fn query_dao_proposals(deps: Deps, dao_address: String) -> StdResult { + let dao_addr = deps.api.addr_validate(&dao_address)?; + // Query the amount of proposals + let proposal_count = deps + .querier + .query_wasm_smart(dao_addr.clone(), &QueryDao::ProposalCount {})?; + let res: ProposalListResponse = deps.querier.query_wasm_smart( + dao_addr, + &QueryDao::ListProposals { + start_after: None, + limit: Some(proposal_count), + }, + )?; + + for proposal_response in &res.proposals { + if proposal_response.proposal.status == Status::Passed { + return Ok(QueryResponse { + result: true, + data: to_binary(&proposal_response.id)?, + }); + } + } + Ok(QueryResponse { + result: false, + data: to_binary(&res.proposals)?, + }) +} + // // // GOAL: // // // Parse a generic query response, and inject input for the next query // // fn query_chain(deps: Deps, env: Env) -> StdResult { @@ -256,6 +291,9 @@ fn query_construct(deps: Deps, queries: Vec) -> StdResult query_dao_proposal_status(deps, dao_address, proposal_id, status), + CroncatQuery::CheckPassedProposals(CheckPassedProposals { dao_address }) => { + query_dao_proposals(deps, dao_address) + } CroncatQuery::GenericQuery(query) => generic_query(deps, query), CroncatQuery::SmartQuery(query) => smart_query(deps, query), }?; diff --git a/contracts/cw-rules/src/types.rs b/contracts/cw-rules/src/types.rs index c14b0430..583104ed 100644 --- a/contracts/cw-rules/src/types.rs +++ b/contracts/cw-rules/src/types.rs @@ -10,7 +10,14 @@ pub mod dao { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum QueryDao { - Proposal { proposal_id: u64 }, + Proposal { + proposal_id: u64, + }, + ListProposals { + start_after: Option, + limit: Option, + }, + ProposalCount {}, } #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)] @@ -19,7 +26,12 @@ pub mod dao { pub id: u64, pub proposal: AnyChoiceProposal, } - // + + #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] + pub struct ProposalListResponse { + pub proposals: Vec, + } + #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)] pub struct AnyChoiceProposal { pub status: Status, diff --git a/packages/cw-croncat-core/schema/croncat.json b/packages/cw-croncat-core/schema/croncat.json index 94a331c6..be29fddd 100644 --- a/packages/cw-croncat-core/schema/croncat.json +++ b/packages/cw-croncat-core/schema/croncat.json @@ -506,6 +506,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -717,6 +728,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/execute_msg.json b/packages/cw-croncat-core/schema/execute_msg.json index 4f5d11f4..1876b92a 100644 --- a/packages/cw-croncat-core/schema/execute_msg.json +++ b/packages/cw-croncat-core/schema/execute_msg.json @@ -575,6 +575,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -786,6 +797,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/get_agent_tasks_response.json b/packages/cw-croncat-core/schema/get_agent_tasks_response.json index 3bf92210..4b6624e6 100644 --- a/packages/cw-croncat-core/schema/get_agent_tasks_response.json +++ b/packages/cw-croncat-core/schema/get_agent_tasks_response.json @@ -219,6 +219,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -430,6 +441,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/get_state_response.json b/packages/cw-croncat-core/schema/get_state_response.json index f72806ec..c6ca6cbc 100644 --- a/packages/cw-croncat-core/schema/get_state_response.json +++ b/packages/cw-croncat-core/schema/get_state_response.json @@ -377,6 +377,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -588,6 +599,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/get_task_response.json b/packages/cw-croncat-core/schema/get_task_response.json index 8e8d0293..7735e1e0 100644 --- a/packages/cw-croncat-core/schema/get_task_response.json +++ b/packages/cw-croncat-core/schema/get_task_response.json @@ -219,6 +219,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -430,6 +441,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/get_tasks_by_owner_response.json b/packages/cw-croncat-core/schema/get_tasks_by_owner_response.json index 8ed0a1b4..8ae8ed9c 100644 --- a/packages/cw-croncat-core/schema/get_tasks_by_owner_response.json +++ b/packages/cw-croncat-core/schema/get_tasks_by_owner_response.json @@ -215,6 +215,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -426,6 +437,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/get_tasks_response.json b/packages/cw-croncat-core/schema/get_tasks_response.json index 4edac6ab..9914fb29 100644 --- a/packages/cw-croncat-core/schema/get_tasks_response.json +++ b/packages/cw-croncat-core/schema/get_tasks_response.json @@ -215,6 +215,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -426,6 +437,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/get_tasks_with_queries_response.json b/packages/cw-croncat-core/schema/get_tasks_with_queries_response.json index fb43ddde..2b321cac 100644 --- a/packages/cw-croncat-core/schema/get_tasks_with_queries_response.json +++ b/packages/cw-croncat-core/schema/get_tasks_with_queries_response.json @@ -133,6 +133,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -231,6 +242,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-croncat-core/schema/query_msg.json b/packages/cw-croncat-core/schema/query_msg.json index 269736a3..6c545a9f 100644 --- a/packages/cw-croncat-core/schema/query_msg.json +++ b/packages/cw-croncat-core/schema/query_msg.json @@ -462,6 +462,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -673,6 +684,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-rules-core/schema/croncat_query.json b/packages/cw-rules-core/schema/croncat_query.json index f17d959f..e34513a5 100644 --- a/packages/cw-rules-core/schema/croncat_query.json +++ b/packages/cw-rules-core/schema/croncat_query.json @@ -62,6 +62,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ @@ -143,6 +155,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ diff --git a/packages/cw-rules-core/schema/query_construct.json b/packages/cw-rules-core/schema/query_construct.json index 231f5895..48485b41 100644 --- a/packages/cw-rules-core/schema/query_construct.json +++ b/packages/cw-rules-core/schema/query_construct.json @@ -69,6 +69,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -167,6 +178,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-rules-core/schema/query_msg.json b/packages/cw-rules-core/schema/query_msg.json index b198f5be..27afc641 100644 --- a/packages/cw-rules-core/schema/query_msg.json +++ b/packages/cw-rules-core/schema/query_msg.json @@ -86,6 +86,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ @@ -179,6 +191,17 @@ } } }, + "CheckPassedProposals": { + "type": "object", + "required": [ + "dao_address" + ], + "properties": { + "dao_address": { + "type": "string" + } + } + }, "CheckProposalStatus": { "type": "object", "required": [ @@ -277,6 +300,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "check_passed_proposals" + ], + "properties": { + "check_passed_proposals": { + "$ref": "#/definitions/CheckPassedProposals" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/packages/cw-rules-core/src/msg.rs b/packages/cw-rules-core/src/msg.rs index c8b026aa..de802dcf 100644 --- a/packages/cw-rules-core/src/msg.rs +++ b/packages/cw-rules-core/src/msg.rs @@ -1,4 +1,6 @@ -use crate::types::{CheckOwnerOfNft, CheckProposalStatus, CroncatQuery, HasBalanceGte}; +use crate::types::{ + CheckOwnerOfNft, CheckPassedProposals, CheckProposalStatus, CroncatQuery, HasBalanceGte, +}; use generic_query::GenericQuery; //use cw_croncat_core::types::Rule; //use cosmwasm_std::Coin; @@ -30,6 +32,7 @@ pub enum QueryMsg { HasBalanceGte(HasBalanceGte), CheckOwnerOfNft(CheckOwnerOfNft), CheckProposalStatus(CheckProposalStatus), + CheckPassedProposals(CheckPassedProposals), GenericQuery(GenericQuery), // Full evaluations QueryConstruct(QueryConstruct), diff --git a/packages/cw-rules-core/src/types.rs b/packages/cw-rules-core/src/types.rs index 75afeda0..36a0e6b5 100644 --- a/packages/cw-rules-core/src/types.rs +++ b/packages/cw-rules-core/src/types.rs @@ -32,6 +32,7 @@ pub enum CroncatQuery { HasBalanceGte(HasBalanceGte), CheckOwnerOfNft(CheckOwnerOfNft), CheckProposalStatus(CheckProposalStatus), + CheckPassedProposals(CheckPassedProposals), GenericQuery(GenericQuery), SmartQuery(SmartQueryHead), } @@ -55,3 +56,8 @@ pub struct CheckProposalStatus { pub proposal_id: u64, pub status: Status, } + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct CheckPassedProposals { + pub dao_address: String, +}