1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
pub mod ballots;
pub mod error;
pub mod msg;
#[cfg(test)]
mod multitest;
pub mod state;

use serde::de::DeserializeOwned;
use serde::Serialize;

use ballots::ballots;
pub use error::ContractError;
use state::{
    next_id, proposals, Config, Proposal, ProposalListResponse, ProposalResponse,
    TextProposalListResponse, Votes, VotingRules, CONFIG, TEXT_PROPOSALS,
};

use cosmwasm_std::{
    Addr, BlockInfo, CustomQuery, Deps, DepsMut, Env, MessageInfo, Order, StdResult, Storage,
};
use cw_storage_plus::Bound;
use cw_utils::maybe_addr;
use tg3::{
    Status, Vote, VoteInfo, VoteListResponse, VoteResponse, VoterDetail, VoterListResponse,
    VoterResponse,
};
use tg4::{Member, Tg4Contract};
use tg_bindings::TgradeMsg;
use tg_utils::Expiration;

type Response = cosmwasm_std::Response<TgradeMsg>;

pub fn instantiate<Q: CustomQuery>(
    deps: DepsMut<Q>,
    rules: VotingRules,
    group_addr: &str,
) -> Result<Response, ContractError> {
    let group_contract = Tg4Contract(deps.api.addr_validate(group_addr).map_err(|_| {
        ContractError::InvalidGroup {
            addr: group_addr.to_owned(),
        }
    })?);

    let cfg = Config {
        rules,
        group_contract,
    };

    cfg.rules.validate()?;
    CONFIG.save(deps.storage, &cfg)?;

    Ok(Response::default())
}

pub fn propose<P, Q: CustomQuery>(
    deps: DepsMut<Q>,
    env: Env,
    info: MessageInfo,
    title: String,
    description: String,
    proposal: P,
) -> Result<Response, ContractError>
where
    P: DeserializeOwned + Serialize,
{
    let cfg = CONFIG.load(deps.storage)?;

    // Only members of the multisig can create a proposal
    // Additional check if points >= 1
    let vote_power = cfg
        .group_contract
        .is_voting_member(&deps.querier, info.sender.as_str())?;

    // calculate expiry time
    let expires =
        Expiration::at_timestamp(env.block.time.plus_seconds(cfg.rules.voting_period_secs()));

    // create a proposal
    let mut prop = Proposal {
        title,
        description,
        created_by: info.sender.to_string(),
        start_height: env.block.height,
        expires,
        proposal,
        status: Status::Open,
        votes: Votes::yes(vote_power),
        rules: cfg.rules,
        total_points: cfg.group_contract.total_points(&deps.querier)?,
    };
    prop.update_status(&env.block);
    let id = next_id(deps.storage)?;
    proposals().save(deps.storage, id, &prop)?;

    // add the first yes vote from voter
    ballots().create_ballot(deps.storage, &info.sender, id, vote_power, Vote::Yes)?;

    let resp = msg::ProposalCreationResponse { proposal_id: id };

    Ok(Response::new()
        .add_attribute("action", "propose")
        .add_attribute("sender", info.sender)
        .add_attribute("proposal_id", id.to_string())
        .add_attribute("status", format!("{:?}", prop.status))
        .set_data(cosmwasm_std::to_binary(&resp)?))
}

pub fn vote<P, Q: CustomQuery>(
    deps: DepsMut<Q>,
    env: Env,
    info: MessageInfo,
    proposal_id: u64,
    vote: Vote,
) -> Result<Response, ContractError>
where
    P: Serialize + DeserializeOwned,
{
    // ensure proposal exists and can be voted on
    let mut prop = proposals().load(deps.storage, proposal_id)?;

    if ![Status::Open, Status::Passed, Status::Rejected].contains(&prop.status) {
        return Err(ContractError::NotOpen {});
    }

    // if they are not expired
    if prop.expires.is_expired(&env.block) {
        return Err(ContractError::Expired {});
    }

    // use a snapshot of "start of proposal"
    // Must be a member of voting group and have voting power >= 1
    let cfg = CONFIG.load(deps.storage)?;
    let vote_power =
        cfg.group_contract
            .was_voting_member(&deps.querier, &info.sender, prop.start_height)?;

    // cast vote if no vote previously cast
    ballots().create_ballot(deps.storage, &info.sender, proposal_id, vote_power, vote)?;

    // update vote tally
    prop.votes.add_vote(vote, vote_power);
    prop.update_status(&env.block);
    proposals::<P>().save(deps.storage, proposal_id, &prop)?;

    Ok(Response::new()
        .add_attribute("action", "vote")
        .add_attribute("sender", info.sender)
        .add_attribute("proposal_id", proposal_id.to_string())
        .add_attribute("status", format!("{:?}", prop.status)))
}

/// Checks if a given proposal is passed and can then be executed, and returns it.
/// Notice that this call is mutable, so, better execute the returned proposal after this succeeds,
/// as you you wouldn't be able to execute it in the future (If the contract call errors, this status
/// change will be reverted / ignored).
pub fn mark_executed<P>(
    storage: &mut dyn Storage,
    env: Env,
    proposal_id: u64,
) -> Result<Proposal<P>, ContractError>
where
    P: Serialize + DeserializeOwned,
{
    let mut proposal = proposals::<P>().load(storage, proposal_id)?;
    // Update Status
    proposal.update_status(&env.block);
    // We allow execution even after the proposal "expiration" as long as all votes come in before
    // that point. If it was approved on time, it can be executed any time.
    if proposal.current_status(&env.block) != Status::Passed {
        return Err(ContractError::WrongExecuteStatus {});
    }

    // Set it to executed
    proposal.status = Status::Executed;
    proposals::<P>().save(storage, proposal_id, &proposal)?;
    Ok(proposal)
}

pub fn execute_text<P, Q: CustomQuery>(
    deps: DepsMut<Q>,
    id: u64,
    proposal: Proposal<P>,
) -> Result<(), ContractError>
where
    P: Serialize + DeserializeOwned,
{
    TEXT_PROPOSALS.save(deps.storage, id, &proposal.into())?;

    Ok(())
}

pub fn close<P, Q: CustomQuery>(
    deps: DepsMut<Q>,
    env: Env,
    info: MessageInfo,
    proposal_id: u64,
) -> Result<Response, ContractError>
where
    P: Serialize + DeserializeOwned,
{
    // anyone can trigger this if the vote passed

    let mut prop = proposals().load(deps.storage, proposal_id)?;

    if prop.status == Status::Rejected {
        return Err(ContractError::NotOpen {});
    }

    prop.update_status(&env.block);

    if [Status::Executed, Status::Passed]
        .iter()
        .any(|x| *x == prop.status)
    {
        return Err(ContractError::WrongCloseStatus {});
    }
    if !prop.expires.is_expired(&env.block) {
        return Err(ContractError::NotExpired {});
    }

    prop.status = Status::Rejected;
    proposals::<P>().save(deps.storage, proposal_id, &prop)?;

    Ok(Response::new()
        .add_attribute("action", "close")
        .add_attribute("sender", info.sender)
        .add_attribute("proposal_id", proposal_id.to_string()))
}

pub fn query_rules<Q: CustomQuery>(deps: Deps<Q>) -> StdResult<VotingRules> {
    let cfg = CONFIG.load(deps.storage)?;
    Ok(cfg.rules)
}

pub fn query_proposal<P, Q: CustomQuery>(
    deps: Deps<Q>,
    env: Env,
    id: u64,
) -> StdResult<ProposalResponse<P>>
where
    P: Serialize + DeserializeOwned,
{
    let prop = proposals().load(deps.storage, id)?;
    let status = prop.current_status(&env.block);
    let rules = prop.rules;
    Ok(ProposalResponse {
        id,
        title: prop.title,
        description: prop.description,
        proposal: prop.proposal,
        created_by: prop.created_by,
        status,
        expires: prop.expires,
        rules,
        total_points: prop.total_points,
        votes: prop.votes,
    })
}

fn map_proposal<P>(
    block: &BlockInfo,
    item: StdResult<(u64, Proposal<P>)>,
) -> StdResult<ProposalResponse<P>> {
    let (id, prop) = item?;
    let status = prop.current_status(block);
    Ok(ProposalResponse {
        id,
        title: prop.title,
        description: prop.description,
        proposal: prop.proposal,
        created_by: prop.created_by,
        status,
        expires: prop.expires,
        rules: prop.rules,
        total_points: prop.total_points,
        votes: prop.votes,
    })
}

pub fn list_proposals<P, Q: CustomQuery>(
    deps: Deps<Q>,
    env: Env,
    start_after: Option<u64>,
    limit: usize,
) -> StdResult<ProposalListResponse<P>>
where
    P: Serialize + DeserializeOwned,
{
    let start = start_after.map(Bound::exclusive);
    let props: StdResult<Vec<_>> = proposals()
        .range(deps.storage, start, None, Order::Ascending)
        .take(limit)
        .map(|p| map_proposal(&env.block, p))
        .collect();

    Ok(ProposalListResponse { proposals: props? })
}

pub fn list_text_proposals<Q: CustomQuery>(
    deps: Deps<Q>,
    start_after: Option<u64>,
    limit: usize,
) -> StdResult<TextProposalListResponse> {
    let start = start_after.map(Bound::exclusive);
    let props: StdResult<Vec<_>> = TEXT_PROPOSALS
        .range(deps.storage, start, None, Order::Ascending)
        .take(limit)
        .map(|r| r.map(|(_, p)| p))
        .collect();

    Ok(TextProposalListResponse { proposals: props? })
}

pub fn reverse_proposals<P, Q: CustomQuery>(
    deps: Deps<Q>,
    env: Env,
    start_before: Option<u64>,
    limit: usize,
) -> StdResult<ProposalListResponse<P>>
where
    P: Serialize + DeserializeOwned,
{
    let end = start_before.map(Bound::exclusive);
    let props: StdResult<Vec<_>> = proposals()
        .range(deps.storage, None, end, Order::Descending)
        .take(limit)
        .map(|p| map_proposal(&env.block, p))
        .collect();

    Ok(ProposalListResponse { proposals: props? })
}

pub fn query_vote<Q: CustomQuery>(
    deps: Deps<Q>,
    proposal_id: u64,
    voter: String,
) -> StdResult<VoteResponse> {
    let voter_addr = deps.api.addr_validate(&voter)?;
    let prop = ballots()
        .ballots
        .may_load(deps.storage, (proposal_id, &voter_addr))?;
    let vote = prop.map(|b| VoteInfo {
        proposal_id,
        voter,
        vote: b.vote,
        points: b.points,
    });
    Ok(VoteResponse { vote })
}

pub fn list_votes<Q: CustomQuery>(
    deps: Deps<Q>,
    proposal_id: u64,
    start_after: Option<String>,
    limit: usize,
) -> StdResult<VoteListResponse> {
    let addr = maybe_addr(deps.api, start_after)?;
    let start = addr.as_ref().map(Bound::exclusive);

    let votes: StdResult<Vec<_>> = ballots()
        .ballots
        .prefix(proposal_id)
        .range(deps.storage, start, None, Order::Ascending)
        .take(limit)
        .map(|item| {
            let (voter, ballot) = item?;
            Ok(VoteInfo {
                proposal_id,
                voter: voter.into(),
                vote: ballot.vote,
                points: ballot.points,
            })
        })
        .collect();

    Ok(VoteListResponse { votes: votes? })
}

pub fn list_votes_by_voter<Q: CustomQuery>(
    deps: Deps<Q>,
    voter: String,
    start_after: Option<u64>,
    limit: usize,
) -> StdResult<VoteListResponse> {
    let voter_addr = deps.api.addr_validate(&voter)?;
    // PrimaryKey of that IndexMap is (proposal_id, voter_address) -> (u64, Addr)
    let start = start_after.map(|m| Bound::exclusive((m, voter_addr.clone())));

    let votes: StdResult<Vec<_>> = ballots()
        .ballots
        .idx
        .voter
        .prefix(voter_addr)
        .range(deps.storage, start, None, Order::Ascending)
        .take(limit)
        .map(|item| {
            let ((proposal_id, _), ballot) = item?;
            Ok(VoteInfo {
                proposal_id,
                voter: ballot.voter.into(),
                vote: ballot.vote,
                points: ballot.points,
            })
        })
        .collect();

    Ok(VoteListResponse { votes: votes? })
}

pub fn query_voter<Q: CustomQuery>(deps: Deps<Q>, voter: String) -> StdResult<VoterResponse> {
    let cfg = CONFIG.load(deps.storage)?;
    let voter_addr = deps.api.addr_validate(&voter)?;
    let points = cfg.group_contract.is_member(&deps.querier, &voter_addr)?;

    Ok(VoterResponse { points })
}

pub fn list_voters<Q: CustomQuery>(
    deps: Deps<Q>,
    start_after: Option<String>,
    limit: Option<u32>,
) -> StdResult<VoterListResponse> {
    let cfg = CONFIG.load(deps.storage)?;
    let voters = cfg
        .group_contract
        .list_members(&deps.querier, start_after, limit)?
        .into_iter()
        .map(|Member { addr, points, .. }| VoterDetail { addr, points })
        .collect();
    Ok(VoterListResponse { voters })
}

pub fn query_group_contract<Q: CustomQuery>(deps: Deps<Q>) -> StdResult<Addr> {
    let cfg = CONFIG.load(deps.storage)?;
    Ok(cfg.group_contract.addr())
}