From cec6fe71513980be31d240dad64fde4716fef7db Mon Sep 17 00:00:00 2001 From: ismellike Date: Mon, 16 Sep 2024 14:20:21 -0500 Subject: [PATCH] Remove adjustments of stats SnapshotMap does not play nicely with adjustments, so this will need a separate data structure in another release. --- .../schema/arena-league-module.json | 89 ++++--------------- .../schema/arena-tournament-module.json | 89 ++++--------------- .../schema/arena-wager-module.json | 89 ++++--------------- .../arena-interface/src/competition/msg.rs | 6 +- .../arena-interface/src/competition/stats.rs | 14 +-- packages/cw-competition-base/src/contract.rs | 78 ++++------------ scripts/src/tests/arena_wager_module.rs | 1 + scripts/state.json | 6 +- 8 files changed, 73 insertions(+), 299 deletions(-) diff --git a/contracts/arena-league-module/schema/arena-league-module.json b/contracts/arena-league-module/schema/arena-league-module.json index 191d030..5faae00 100644 --- a/contracts/arena-league-module/schema/arena-league-module.json +++ b/contracts/arena-league-module/schema/arena-league-module.json @@ -416,34 +416,6 @@ }, "additionalProperties": false }, - { - "type": "object", - "required": [ - "remove_stats" - ], - "properties": { - "remove_stats": { - "type": "object", - "required": [ - "competition_id", - "stats" - ], - "properties": { - "competition_id": { - "$ref": "#/definitions/Uint128" - }, - "stats": { - "type": "array", - "items": { - "$ref": "#/definitions/MemberStatsRemoveMsg" - } - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false - }, { "type": "object", "required": [ @@ -1006,25 +978,6 @@ }, "additionalProperties": false }, - "MemberStatsRemoveMsg": { - "type": "object", - "required": [ - "addr", - "stats" - ], - "properties": { - "addr": { - "type": "string" - }, - "stats": { - "type": "array", - "items": { - "$ref": "#/definitions/StatsRemoveMsg" - } - } - }, - "additionalProperties": false - }, "MigrateMsg": { "oneOf": [ { @@ -1086,13 +1039,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -1219,24 +1174,6 @@ "uint" ] }, - "StatsRemoveMsg": { - "type": "object", - "required": [ - "height", - "name" - ], - "properties": { - "height": { - "type": "integer", - "format": "uint64", - "minimum": 0.0 - }, - "name": { - "type": "string" - } - }, - "additionalProperties": false - }, "Timestamp": { "description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```", "allOf": [ @@ -2762,13 +2699,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -3040,13 +2979,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -3254,13 +3195,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, diff --git a/contracts/arena-tournament-module/schema/arena-tournament-module.json b/contracts/arena-tournament-module/schema/arena-tournament-module.json index 5465666..9f27144 100644 --- a/contracts/arena-tournament-module/schema/arena-tournament-module.json +++ b/contracts/arena-tournament-module/schema/arena-tournament-module.json @@ -416,34 +416,6 @@ }, "additionalProperties": false }, - { - "type": "object", - "required": [ - "remove_stats" - ], - "properties": { - "remove_stats": { - "type": "object", - "required": [ - "competition_id", - "stats" - ], - "properties": { - "competition_id": { - "$ref": "#/definitions/Uint128" - }, - "stats": { - "type": "array", - "items": { - "$ref": "#/definitions/MemberStatsRemoveMsg" - } - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false - }, { "type": "object", "required": [ @@ -934,25 +906,6 @@ }, "additionalProperties": false }, - "MemberStatsRemoveMsg": { - "type": "object", - "required": [ - "addr", - "stats" - ], - "properties": { - "addr": { - "type": "string" - }, - "stats": { - "type": "array", - "items": { - "$ref": "#/definitions/StatsRemoveMsg" - } - } - }, - "additionalProperties": false - }, "MigrateMsg": { "oneOf": [ { @@ -998,13 +951,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -1131,24 +1086,6 @@ "uint" ] }, - "StatsRemoveMsg": { - "type": "object", - "required": [ - "height", - "name" - ], - "properties": { - "height": { - "type": "integer", - "format": "uint64", - "minimum": 0.0 - }, - "name": { - "type": "string" - } - }, - "additionalProperties": false - }, "Timestamp": { "description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```", "allOf": [ @@ -2655,13 +2592,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -2933,13 +2872,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -3147,13 +3088,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, diff --git a/contracts/arena-wager-module/schema/arena-wager-module.json b/contracts/arena-wager-module/schema/arena-wager-module.json index 0f02040..0ec81a7 100644 --- a/contracts/arena-wager-module/schema/arena-wager-module.json +++ b/contracts/arena-wager-module/schema/arena-wager-module.json @@ -416,34 +416,6 @@ }, "additionalProperties": false }, - { - "type": "object", - "required": [ - "remove_stats" - ], - "properties": { - "remove_stats": { - "type": "object", - "required": [ - "competition_id", - "stats" - ], - "properties": { - "competition_id": { - "$ref": "#/definitions/Uint128" - }, - "stats": { - "type": "array", - "items": { - "$ref": "#/definitions/MemberStatsRemoveMsg" - } - } - }, - "additionalProperties": false - } - }, - "additionalProperties": false - }, { "type": "object", "required": [ @@ -852,25 +824,6 @@ }, "additionalProperties": false }, - "MemberStatsRemoveMsg": { - "type": "object", - "required": [ - "addr", - "stats" - ], - "properties": { - "addr": { - "type": "string" - }, - "stats": { - "type": "array", - "items": { - "$ref": "#/definitions/StatsRemoveMsg" - } - } - }, - "additionalProperties": false - }, "MigrateMsg": { "oneOf": [ { @@ -916,13 +869,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -1049,24 +1004,6 @@ "uint" ] }, - "StatsRemoveMsg": { - "type": "object", - "required": [ - "height", - "name" - ], - "properties": { - "height": { - "type": "integer", - "format": "uint64", - "minimum": 0.0 - }, - "name": { - "type": "string" - } - }, - "additionalProperties": false - }, "Timestamp": { "description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```", "allOf": [ @@ -2318,13 +2255,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -2596,13 +2535,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, @@ -2810,13 +2751,15 @@ { "type": "object", "required": [ - "height", "name", "value" ], "properties": { "height": { - "type": "integer", + "type": [ + "integer", + "null" + ], "format": "uint64", "minimum": 0.0 }, diff --git a/packages/arena-interface/src/competition/msg.rs b/packages/arena-interface/src/competition/msg.rs index e2ea1b9..e6162b2 100644 --- a/packages/arena-interface/src/competition/msg.rs +++ b/packages/arena-interface/src/competition/msg.rs @@ -11,7 +11,7 @@ use cw_utils::Expiration; use serde::de::DeserializeOwned; use serde::Serialize; -use super::stats::{MemberStatsMsg, MemberStatsRemoveMsg, StatMsg, StatTableEntry, StatType}; +use super::stats::{MemberStatsMsg, StatMsg, StatTableEntry, StatType}; #[cw_serde] pub struct InstantiateBase { @@ -81,10 +81,6 @@ pub enum ExecuteBase { competition_id: Uint128, stats: Vec, }, - RemoveStats { - competition_id: Uint128, - stats: Vec, - }, UpdateStatTypes { competition_id: Uint128, to_add: Vec, diff --git a/packages/arena-interface/src/competition/stats.rs b/packages/arena-interface/src/competition/stats.rs index 40a18a6..0bda3d0 100644 --- a/packages/arena-interface/src/competition/stats.rs +++ b/packages/arena-interface/src/competition/stats.rs @@ -7,18 +7,6 @@ pub struct MemberStatsMsg { pub stats: Vec, } -#[cw_serde] -pub struct MemberStatsRemoveMsg { - pub addr: String, - pub stats: Vec, -} - -#[cw_serde] -pub struct StatsRemoveMsg { - pub name: String, - pub height: u64, -} - #[cw_serde] #[serde(untagged)] pub enum StatMsg { @@ -31,7 +19,7 @@ pub enum StatMsg { HistoricalStat { name: String, value: StatValue, - height: u64, + height: Option, }, // Variant for querying stats table StatWithAggregation { diff --git a/packages/cw-competition-base/src/contract.rs b/packages/cw-competition-base/src/contract.rs index 8e1a5ad..96ea5e6 100644 --- a/packages/cw-competition-base/src/contract.rs +++ b/packages/cw-competition-base/src/contract.rs @@ -12,8 +12,8 @@ use arena_interface::{ }, state::{Competition, CompetitionResponse, CompetitionStatus, Config, Evidence}, stats::{ - MemberStatsMsg, MemberStatsRemoveMsg, StatAggregationType, StatMsg, StatTableEntry, - StatType, StatValue, StatValueType, + MemberStatsMsg, StatAggregationType, StatMsg, StatTableEntry, StatType, StatValue, + StatValueType, }, }, core::{CompetitionModuleResponse, ProposeMessage, TaxConfigurationResponse}, @@ -432,10 +432,6 @@ impl< competition_id, stats, } => self.execute_input_stats(deps, env, info, competition_id, stats), - ExecuteBase::RemoveStats { - competition_id, - stats, - } => self.execute_remove_stats(deps, env, info, competition_id, stats), ExecuteBase::ExecuteCompetitionHook { competition_id: _, distribution: _, @@ -1236,47 +1232,6 @@ impl< .add_attribute("competition_id", competition_id.to_string())) } - pub fn execute_remove_stats( - &self, - deps: DepsMut, - _env: Env, - info: MessageInfo, - competition_id: Uint128, - stats: Vec, - ) -> Result { - // Check if the competition exists and the sender is authorized - let competition = self - .competitions - .load(deps.storage, competition_id.u128())?; - self.inner_validate_auth(&info.sender, &competition, false)?; - - for update in stats { - let addr = deps.api.addr_validate(&update.addr)?; - for stat in update.stats { - // Check if the stat type exists - if !self - .stat_types - .has(deps.storage, (competition_id.u128(), &stat.name)) - { - return Err(CompetitionError::StatTypeNotFound { - name: stat.name.clone(), - }); - } - - // Remove the stat - self.stats.remove( - deps.storage, - (competition_id.u128(), &addr, &stat.name), - stat.height, - )?; - } - } - - Ok(Response::new() - .add_attribute("action", "remove_stats") - .add_attribute("competition_id", competition_id.to_string())) - } - pub fn query( &self, deps: Deps, @@ -1410,7 +1365,12 @@ impl< ) -> StdResult { let mut sum = self .stats - .load(deps.storage, (competition_id.u128(), addr, &stat_type.name))?; + .load(deps.storage, (competition_id.u128(), addr, &stat_type.name)) + .unwrap_or(match stat_type.value_type { + StatValueType::Bool => StatValue::Bool(false), + StatValueType::Decimal => StatValue::Decimal(Decimal::zero()), + StatValueType::Uint => StatValue::Uint(Uint128::zero()), + }); let mut count = 1u64; for item in self @@ -1499,18 +1459,18 @@ impl< // Iterate over all stat_types for stat_type in &stat_types { - let value = self.stats.load( + if let Ok(value) = self.stats.load( deps.storage, (competition_id.u128(), &addr, &stat_type.name), - )?; - height_to_stats - .entry(env.block.height) - .or_default() - .push(StatMsg::HistoricalStat { - name: stat_type.name.clone(), - value, - height: env.block.height, - }); + ) { + height_to_stats.entry(env.block.height).or_default().push( + StatMsg::HistoricalStat { + name: stat_type.name.clone(), + value, + height: None, + }, + ); + } // For each stat_type, iterate over its changelog entries let changelog = self @@ -1534,7 +1494,7 @@ impl< .push(StatMsg::HistoricalStat { name: stat_type.name.clone(), value, - height, + height: Some(height), }); } } diff --git a/scripts/src/tests/arena_wager_module.rs b/scripts/src/tests/arena_wager_module.rs index d86af44..9f528c8 100644 --- a/scripts/src/tests/arena_wager_module.rs +++ b/scripts/src/tests/arena_wager_module.rs @@ -1070,6 +1070,7 @@ fn test_wager_with_aggregate_stats() -> anyhow::Result<()> { *stats_table[0].stats[0].value(), StatValue::Decimal(Decimal::percent(20)), ); + mock.next_block()?; Ok(()) } diff --git a/scripts/state.json b/scripts/state.json index 0993d55..08f9210 100644 --- a/scripts/state.json +++ b/scripts/state.json @@ -16,11 +16,11 @@ "arena_competition_enrollment": 6403, "arena_core": 6440, "arena_escrow": 6398, - "arena_league_module": 6700, + "arena_league_module": 6760, "arena_payment_registry": 6405, "arena_token_gateway": 6604, - "arena_tournament_module": 6701, - "arena_wager_module": 6699, + "arena_tournament_module": 6761, + "arena_wager_module": 6759, "dao_dao_core": 6611 }, "default": {}