Repository: DA0-DA0/dao-contracts
Pull request #317 (2022-06-01 22:48:00)

Initialize PROPOSAL_COUNT to zero.
Before if no proposals had been created `PROPOSAL_COUNT` would have no
value. This would cause trouble in queries as they would attempt to
load the value and locate nothing causing an error in frontends.


Patch:
diff --git a/contracts/cw-proposal-single/src/contract.rs b/contracts/cw-proposal-single/src/contract.rs
index 0a4356b1..a410e8af 100644
--- a/contracts/cw-proposal-single/src/contract.rs
+++ b/contracts/cw-proposal-single/src/contract.rs
@@ -60,6 +60,9 @@ pub fn instantiate(
         allow_revoting: msg.allow_revoting,
     };
 
+    // Initialize proposal count to zero so that queries return zero
+    // instead of None.
+    PROPOSAL_COUNT.save(deps.storage, &0)?;
     CONFIG.save(deps.storage, &config)?;
 
     Ok(Response::default()
diff --git a/contracts/cw-proposal-single/src/proposal.rs b/contracts/cw-proposal-single/src/proposal.rs
index af064a35..b0266302 100644
--- a/contracts/cw-proposal-single/src/proposal.rs
+++ b/contracts/cw-proposal-single/src/proposal.rs
@@ -33,7 +33,7 @@ pub struct Proposal {
 }
 
 pub fn advance_proposal_id(store: &mut dyn Storage) -> StdResult<u64> {
-    let id: u64 = PROPOSAL_COUNT.may_load(store)?.unwrap_or_default() + 1;
+    let id: u64 = PROPOSAL_COUNT.load(store)? + 1;
     PROPOSAL_COUNT.save(store, &id)?;
     Ok(id)
 }


Test patch:
diff --git a/contracts/cw-proposal-single/src/tests.rs b/contracts/cw-proposal-single/src/tests.rs
index 8ebad115..24144f72 100644
--- a/contracts/cw-proposal-single/src/tests.rs
+++ b/contracts/cw-proposal-single/src/tests.rs
@@ -3319,3 +3319,48 @@ fn test_migrate() {
 
     assert_eq!(config, new_config);
 }
+
+#[test]
+fn test_proposal_count_initialized_to_zero() {
+    let mut app = App::default();
+    let proposal_id = app.store_code(single_proposal_contract());
+    let core_addr = instantiate_with_staked_balances_governance(
+        &mut app,
+        proposal_id,
+        InstantiateMsg {
+            threshold: Threshold::ThresholdQuorum {
+                threshold: PercentageThreshold::Majority {},
+                quorum: PercentageThreshold::Percent(Decimal::percent(10)),
+            },
+            max_voting_period: Duration::Height(10),
+            only_members_execute: true,
+            allow_revoting: false,
+            deposit_info: None,
+        },
+        Some(vec![
+            Cw20Coin {
+                address: "ekez".to_string(),
+                amount: Uint128::new(10),
+            },
+            Cw20Coin {
+                address: "innactive".to_string(),
+                amount: Uint128::new(90),
+            },
+        ]),
+    );
+
+    let gov_state: cw_core::query::DumpStateResponse = app
+        .wrap()
+        .query_wasm_smart(core_addr, &cw_core::msg::QueryMsg::DumpState {})
+        .unwrap();
+    let proposal_modules = gov_state.proposal_modules;
+
+    assert_eq!(proposal_modules.len(), 1);
+    let proposal_single = proposal_modules.into_iter().next().unwrap();
+
+    let proposal_count: u64 = app
+        .wrap()
+        .query_wasm_smart(proposal_single, &QueryMsg::ProposalCount {})
+        .unwrap();
+    assert_eq!(proposal_count, 0);
+}