Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix challange app #96

Merged
merged 53 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1109dde
replace duration enum
CyberHoward Sep 30, 2023
a772ced
formatting
CyberHoward Sep 30, 2023
a796e6e
small refactor
Buckram123 Oct 4, 2023
ae2ec4a
remove check_ins
Buckram123 Oct 4, 2023
d49b08b
voting object
Buckram123 Oct 4, 2023
3c6a914
update voting
Buckram123 Oct 4, 2023
5179336
add vote error
Buckram123 Oct 5, 2023
a2266f8
fix challenge status
Buckram123 Oct 5, 2023
068c486
challenge execute methods
Buckram123 Oct 5, 2023
84477e8
add expiration for voting
Buckram123 Oct 5, 2023
3b2847b
veto period start after votes counted
Buckram123 Oct 6, 2023
8034615
integrate voting in challenge
Buckram123 Oct 6, 2023
c99cb0b
fix remove voters
Buckram123 Oct 6, 2023
de5bc2a
add veto action
Buckram123 Oct 6, 2023
366738d
update queries
Buckram123 Oct 6, 2023
c2a33e5
add comments
Buckram123 Oct 9, 2023
3cb78f3
fix existing tests
Buckram123 Oct 9, 2023
ec77cdd
add challenge id to the response
Buckram123 Oct 9, 2023
42995db
format
Buckram123 Oct 9, 2023
a058198
previous votes
Buckram123 Oct 9, 2023
88b657b
wasm can't deserialize map
Buckram123 Oct 10, 2023
0dda5ca
fix friends
Buckram123 Oct 10, 2023
576dc2b
bugfix
Buckram123 Oct 10, 2023
9118043
rename to proposals, where it fits
Buckram123 Oct 10, 2023
f3f642a
add votes query
Buckram123 Oct 10, 2023
dc39dec
cast vote as abstract account
Buckram123 Oct 10, 2023
86bf03b
add veto tests
Buckram123 Oct 11, 2023
9c8f4a6
format
Buckram123 Oct 11, 2023
34e488c
add limits
Buckram123 Oct 11, 2023
0d8e64d
Doc-comment for SimpleVoting
Buckram123 Oct 11, 2023
fbd0c9c
clippy
Buckram123 Oct 11, 2023
3f10572
Bump challenge to 0.2.0
adairrr Oct 11, 2023
b00aee8
add validation, make description optional
Buckram123 Oct 12, 2023
497a74a
format
Buckram123 Oct 12, 2023
2ca5d81
duplicate addrs fix
Buckram123 Oct 13, 2023
2adb059
update proposal initiation and veto
Buckram123 Oct 16, 2023
8f1af63
update tests/fix bugs
Buckram123 Oct 17, 2023
b279cfe
clippy fixes
Buckram123 Oct 17, 2023
41df9bb
add readme for the challenge
Buckram123 Oct 17, 2023
ebf2919
small edit in readme
Buckram123 Oct 17, 2023
08100cc
Merge branch 'main' into fix/challange_app
Buckram123 Oct 17, 2023
cf953e3
Merge remote-tracking branch 'origin' into fix/challange_app
Buckram123 Oct 17, 2023
7da4b22
voting tests and fixes
Buckram123 Oct 18, 2023
4a4b40b
update challenge to the new simple_voting
Buckram123 Oct 18, 2023
1a3816f
add active_proposal to ChallengeEntryResponse
Buckram123 Oct 18, 2023
9c12138
update schema
Buckram123 Oct 18, 2023
a9b0627
update voting readme
Buckram123 Oct 18, 2023
c39bede
redundant take(1)
Buckram123 Oct 19, 2023
b341aa8
add proposal lifecylce
CyberHoward Oct 19, 2023
a474d75
formatting
CyberHoward Oct 19, 2023
ea40e6f
update proposal lifecylce
CyberHoward Oct 19, 2023
231da31
complete proposal lifecycle
CyberHoward Oct 19, 2023
d1d5df7
fix beta clippy
CyberHoward Oct 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions modules/contracts/apps/challenge/src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ fn create_challenge(
app.admin.assert_admin(deps.as_ref(), &info.sender)?;

let mut challenge = ChallengeEntry::new(challenge_req);
challenge.set_total_check_ins(&env)?;

//check that the challenge status is ChallengeStatus::Uninitialized
if challenge.status != ChallengeStatus::Uninitialized {
Expand Down Expand Up @@ -256,7 +255,7 @@ fn daily_check_in(
let mut challenge = CHALLENGE_LIST.load(deps.storage, challenge_id)?;

// If the challenge has ended, we set the status to Over and return
if env.block.time > challenge.end {
if challenge.end.is_expired(&env.block) {
match challenge.status {
ChallengeStatus::Active => {
challenge.status = ChallengeStatus::Over;
Expand All @@ -271,9 +270,8 @@ fn daily_check_in(
}
_ => {
return Err(AppError::Std(StdError::generic_err(format!(
"Challenge has ended. Challenge end_timestamp is {:?} current timestamp is {:?}",
challenge.end.seconds(),
env.block.time.seconds()
"Challenge has ended. Challenge end is {:?} current block is {:?}",
challenge.end, env.block
))));
}
}
Expand Down Expand Up @@ -458,15 +456,13 @@ fn charge_penalty(deps: DepsMut, app: &ChallengeApp, challenge_id: u64) -> AppRe
}

let compute_amount_per_friend = || -> Result<u128, AppError> {
if challenge.total_check_ins == 0 || num_friends == 0 {
if num_friends == 0 {
return Err(AppError::Std(StdError::generic_err(format!(
"Cannot compute amount per friend. total_check_ins: {}, num_friends: {}",
challenge.total_check_ins, num_friends
"Cannot compute amount per friend. num_friends: {}",
num_friends
Buckram123 marked this conversation as resolved.
Show resolved Hide resolved
))));
}
let amount_per_friend =
(challenge.collateral.amount.u128() / challenge.total_check_ins) / num_friends;
Ok(amount_per_friend)
Ok(challenge.strike_amount)
};

let reaminder = challenge.collateral.amount.u128() % num_friends;
Expand Down
21 changes: 4 additions & 17 deletions modules/contracts/apps/challenge/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use abstract_dex_adapter::msg::OfferAsset;
use cosmwasm_schema::QueryResponses;
use cosmwasm_std::Addr;
use cw_utils::Expiration;

abstract_app::app_msg_types!(ChallengeApp, ChallengeExecuteMsg, ChallengeQueryMsg);

Expand Down Expand Up @@ -122,29 +123,15 @@ pub struct ChallengeRequest {
/// Desciption of the challenge
pub description: String,
/// In what period challenge should end
pub end: DurationChoice,
pub end: Expiration,
/// How much should be paid out when challenge fails.
pub strike_amount: u128,
Buckram123 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Response for check_ins query
/// Returns a list of check ins
#[cosmwasm_schema::cw_serde]
pub struct CheckInsResponse(pub Vec<CheckIn>);

/// Duration for challenge
#[cosmwasm_schema::cw_serde]
pub enum DurationChoice {
/// One week
Week,
/// One month
Month,
/// Quarter of the year
Quarter,
/// One year
Year,
/// 100 years
OneHundredYears,
}

/// Response for vote query
#[cosmwasm_schema::cw_serde]
pub struct VoteResponse {
Expand Down
54 changes: 7 additions & 47 deletions modules/contracts/apps/challenge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use chrono::Duration;
use cosmwasm_std::{Addr, Deps, Env, StdError, StdResult, Timestamp};
use cw_address_like::AddressLike;
use cw_storage_plus::{Item, Map};
use cw_utils::Expiration;

use crate::msg::{ChallengeRequest, DurationChoice};
use crate::msg::ChallengeRequest;

pub const DAY: u64 = 86400;

Expand All @@ -18,8 +19,9 @@ pub struct ChallengeEntry {
pub name: String,
pub collateral: OfferAsset,
pub description: String,
pub end: Timestamp,
pub total_check_ins: u128,
pub end: Expiration,
/// Amount charged when a user fails a challenge.
pub strike_amount: u128,
pub status: ChallengeStatus,
pub admin_strikes: StrikeConfig,
}
Expand Down Expand Up @@ -48,54 +50,12 @@ impl ChallengeEntry {
name: request.name,
collateral: request.collateral,
description: request.description,
Buckram123 marked this conversation as resolved.
Show resolved Hide resolved
end: Self::to_timestamp(request.end),
total_check_ins: 0,
end: request.end,
strike_amount: 10,
Buckram123 marked this conversation as resolved.
Show resolved Hide resolved
status: ChallengeStatus::default(),
admin_strikes: StrikeConfig::default(),
}
}

/// Sets the total number of check-ins based on the end time.
pub fn set_total_check_ins(&mut self, env: &Env) -> StdResult<()> {
let now = env.block.time;

match self.end.seconds().checked_sub(now.seconds()) {
Some(duration_secs) => {
// Calculate the total number of check-ins
self.total_check_ins = duration_secs as u128 / DAY as u128;
Ok(())
}
None => {
// If the end time is in the past, set the total check ins to 0.
self.total_check_ins = 0;
// If the end time is in the past or there's an overflow in calculation, return an error.
Err(StdError::generic_err(format!(
"Cannot compute. challenge.end time is in the past: {}, now is: {}",
self.end, now
)))
}
}
}

pub fn to_timestamp(end: DurationChoice) -> Timestamp {
match end {
DurationChoice::Week => {
Timestamp::from_seconds(Duration::weeks(1).to_std().unwrap().as_secs())
}
DurationChoice::Month => {
Timestamp::from_seconds(Duration::days(30).to_std().unwrap().as_secs())
}
DurationChoice::Quarter => {
Timestamp::from_seconds(Duration::days(90).to_std().unwrap().as_secs())
}
DurationChoice::Year => {
Timestamp::from_seconds(Duration::days(365).to_std().unwrap().as_secs())
}
DurationChoice::OneHundredYears => {
Timestamp::from_seconds(Duration::days(365 * 100).to_std().unwrap().as_secs())
}
}
}
}
Buckram123 marked this conversation as resolved.
Show resolved Hide resolved

/// The status of a challenge. This can be used to trigger an automated Croncat job
Expand Down
9 changes: 6 additions & 3 deletions modules/contracts/apps/challenge/tests/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ use challenge_app::{
contract::{CHALLENGE_APP_ID, CHALLENGE_APP_VERSION},
msg::{
ChallengeQueryMsg, ChallengeRequest, ChallengeResponse, ChallengesResponse,
CheckInsResponse, DurationChoice, FriendsResponse, InstantiateMsg, VoteResponse,
CheckInsResponse, FriendsResponse, InstantiateMsg, VoteResponse,
},
state::{
ChallengeEntryUpdate, ChallengeStatus, CheckInStatus, Friend, UpdateFriendsOpKind, Vote,
DAY,
},
*,
};
use cosmwasm_std::{coin, Uint128};
use cosmwasm_std::{coin, Timestamp, Uint128};
use cw_asset::AssetInfo;
use cw_orch::{anyhow, deploy::Deploy, prelude::*};
use lazy_static::lazy_static;
Expand All @@ -35,7 +35,10 @@ lazy_static! {
name: "test".to_string(),
collateral: OfferAsset::new("denom", Uint128::new(100_000_000_000)),
description: "Test Challenge".to_string(),
end: DurationChoice::OneHundredYears,
end: cw_utils::Expiration::AtTime(Timestamp::from_nanos(
1571975019879305533 + 100000000000
)),
strike_amount: 100,
};
static ref ALICE_ADDRESS: String = "alice0x".to_string();
static ref BOB_ADDRESS: String = "bob0x".to_string();
Expand Down