Skip to content

Commit

Permalink
new rule: CheckPassedProposals
Browse files Browse the repository at this point in the history
  • Loading branch information
nlipartiia-hacken committed Dec 22, 2022
1 parent 76c05d8 commit f46152a
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 19 deletions.
9 changes: 6 additions & 3 deletions contracts/cw-croncat/src/balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -292,3 +289,9 @@ impl<'a> Balancer<'a> for RoundRobinBalancer {
Ok(())
}
}

impl Default for RoundRobinBalancer {
fn default() -> RoundRobinBalancer {
RoundRobinBalancer::new(BalancerMode::ActivationOrder)
}
}
6 changes: 1 addition & 5 deletions contracts/cw-croncat/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions contracts/cw-croncat/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<SubMsg>, ContractError> = balances
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
42 changes: 40 additions & 2 deletions contracts/cw-rules/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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
Expand Down Expand Up @@ -87,6 +89,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
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 }) => {
Expand Down Expand Up @@ -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<QueryResponse> {
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<QueryMultiResponse> {
Expand Down Expand Up @@ -256,6 +291,9 @@ fn query_construct(deps: Deps, queries: Vec<CroncatQuery>) -> StdResult<QueryCon
proposal_id,
status,
}) => 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),
}?;
Expand Down
16 changes: 14 additions & 2 deletions contracts/cw-rules/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
limit: Option<u64>,
},
ProposalCount {},
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
Expand All @@ -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<ProposalResponse>,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
pub struct AnyChoiceProposal {
pub status: Status,
Expand Down
23 changes: 23 additions & 0 deletions packages/cw-croncat-core/schema/croncat.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,17 @@
}
}
},
"CheckPassedProposals": {
"type": "object",
"required": [
"dao_address"
],
"properties": {
"dao_address": {
"type": "string"
}
}
},
"CheckProposalStatus": {
"type": "object",
"required": [
Expand Down Expand Up @@ -717,6 +728,18 @@
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"check_passed_proposals"
],
"properties": {
"check_passed_proposals": {
"$ref": "#/definitions/CheckPassedProposals"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
23 changes: 23 additions & 0 deletions packages/cw-croncat-core/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@
}
}
},
"CheckPassedProposals": {
"type": "object",
"required": [
"dao_address"
],
"properties": {
"dao_address": {
"type": "string"
}
}
},
"CheckProposalStatus": {
"type": "object",
"required": [
Expand Down Expand Up @@ -786,6 +797,18 @@
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"check_passed_proposals"
],
"properties": {
"check_passed_proposals": {
"$ref": "#/definitions/CheckPassedProposals"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
23 changes: 23 additions & 0 deletions packages/cw-croncat-core/schema/get_agent_tasks_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@
}
}
},
"CheckPassedProposals": {
"type": "object",
"required": [
"dao_address"
],
"properties": {
"dao_address": {
"type": "string"
}
}
},
"CheckProposalStatus": {
"type": "object",
"required": [
Expand Down Expand Up @@ -430,6 +441,18 @@
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"check_passed_proposals"
],
"properties": {
"check_passed_proposals": {
"$ref": "#/definitions/CheckPassedProposals"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
23 changes: 23 additions & 0 deletions packages/cw-croncat-core/schema/get_state_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@
}
}
},
"CheckPassedProposals": {
"type": "object",
"required": [
"dao_address"
],
"properties": {
"dao_address": {
"type": "string"
}
}
},
"CheckProposalStatus": {
"type": "object",
"required": [
Expand Down Expand Up @@ -588,6 +599,18 @@
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"check_passed_proposals"
],
"properties": {
"check_passed_proposals": {
"$ref": "#/definitions/CheckPassedProposals"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
23 changes: 23 additions & 0 deletions packages/cw-croncat-core/schema/get_task_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@
}
}
},
"CheckPassedProposals": {
"type": "object",
"required": [
"dao_address"
],
"properties": {
"dao_address": {
"type": "string"
}
}
},
"CheckProposalStatus": {
"type": "object",
"required": [
Expand Down Expand Up @@ -430,6 +441,18 @@
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"check_passed_proposals"
],
"properties": {
"check_passed_proposals": {
"$ref": "#/definitions/CheckPassedProposals"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
Loading

0 comments on commit f46152a

Please sign in to comment.