Skip to content

Commit

Permalink
Additional layered fees
Browse files Browse the repository at this point in the history
#7
This reworks the message passing in the Arena protocol.
Instead of passing Arena tax config around, let's just store this in the Arena core and return when querying for tax. From here, we can send an ordered list of layered fees to automatically send when distributing an escrow. Additional layered fees are saved directly on the competition and are optional.
Also cleans up states on migrations.
  • Loading branch information
ismellike committed May 18, 2024
1 parent 655222e commit 16ff6a0
Show file tree
Hide file tree
Showing 51 changed files with 1,299 additions and 442 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
118 changes: 99 additions & 19 deletions contracts/arena-core/schema/arena-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@
"categories",
"competition_modules_instantiate_info",
"rulesets",
"tax"
"tax",
"tax_configuration"
],
"properties": {
"categories": {
Expand All @@ -181,6 +182,9 @@
},
"tax": {
"$ref": "#/definitions/Decimal"
},
"tax_configuration": {
"$ref": "#/definitions/TaxConfiguration"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -267,6 +271,32 @@
},
"additionalProperties": false
},
"TaxConfiguration": {
"type": "object",
"properties": {
"cw20_msg": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
},
{
"type": "null"
}
]
},
"cw721_msg": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
},
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
Expand Down Expand Up @@ -823,6 +853,42 @@
}
]
},
"FeeInformation_for_String": {
"type": "object",
"required": [
"receiver",
"tax"
],
"properties": {
"cw20_msg": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
},
{
"type": "null"
}
]
},
"cw721_msg": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
},
{
"type": "null"
}
]
},
"receiver": {
"type": "string"
},
"tax": {
"$ref": "#/definitions/Decimal"
}
},
"additionalProperties": false
},
"MemberPercentage_for_String": {
"type": "object",
"required": [
Expand Down Expand Up @@ -924,41 +990,31 @@
"ProposeMessage": {
"type": "object",
"required": [
"competition_id",
"description",
"id",
"title"
],
"properties": {
"description": {
"type": "string"
},
"distribution": {
"additional_layered_fees": {
"anyOf": [
{
"$ref": "#/definitions/Distribution_for_String"
"$ref": "#/definitions/FeeInformation_for_String"
},
{
"type": "null"
}
]
},
"id": {
"competition_id": {
"$ref": "#/definitions/Uint128"
},
"tax_cw20_msg": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
},
{
"type": "null"
}
]
"description": {
"type": "string"
},
"tax_cw721_msg": {
"distribution": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
"$ref": "#/definitions/Distribution_for_String"
},
{
"type": "null"
Expand Down Expand Up @@ -1498,6 +1554,30 @@
}
},
"additionalProperties": false
},
{
"description": "This query is used to get a competition's fee configuration for the Arena tax at its start height",
"type": "object",
"required": [
"tax_config"
],
"properties": {
"tax_config": {
"type": "object",
"required": [
"height"
],
"properties": {
"height": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
]
},
Expand Down
24 changes: 17 additions & 7 deletions contracts/arena-core/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
execute::{self, COMPETITION_MODULE_REPLY_ID},
query,
migrate, query,
state::{
competition_modules, CompetitionModule, COMPETITION_CATEGORIES_COUNT,
competition_modules, CompetitionModule, ARENA_TAX_CONFIG, COMPETITION_CATEGORIES_COUNT,
COMPETITION_MODULES_COUNT, KEYS, RULESETS_COUNT,
},
ContractError,
Expand All @@ -17,7 +17,7 @@ use cosmwasm_std::{
from_json, to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdError, StdResult, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw2::{ensure_from_older_version, set_contract_version};
use cw_utils::parse_reply_instantiate_data;
use dao_interface::{msg::ExecuteMsg as DAOCoreExecuteMsg, state::ModuleInstantiateCallback};

Expand Down Expand Up @@ -47,9 +47,10 @@ pub fn instantiate_extension(
COMPETITION_MODULES_COUNT.save(deps.storage, &Uint128::zero())?;
RULESETS_COUNT.save(deps.storage, &Uint128::zero())?;
COMPETITION_CATEGORIES_COUNT.save(deps.storage, &Uint128::zero())?;
crate::execute::update_tax(deps.branch(), &env, dao.clone(), extension.tax)?;
crate::execute::update_categories(deps.branch(), dao.clone(), extension.categories, vec![])?;
crate::execute::update_rulesets(deps.branch(), dao.clone(), extension.rulesets, vec![])?;
execute::update_tax(deps.branch(), &env, dao.clone(), extension.tax)?;
execute::update_categories(deps.branch(), dao.clone(), extension.categories, vec![])?;
execute::update_rulesets(deps.branch(), dao.clone(), extension.rulesets, vec![])?;
ARENA_TAX_CONFIG.save(deps.storage, &extension.tax_configuration)?;
let competition_response = crate::execute::update_competition_modules(
deps.branch(),
dao.clone(),
Expand Down Expand Up @@ -200,6 +201,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
category_id,
rulesets,
)),
QueryExt::TaxConfig { height } => {
to_json_binary(&query::arena_fee_config(deps, height)?)
}
},
_ => PrePropose::default().query(deps, env, msg),
};
Expand All @@ -208,7 +212,13 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let version = ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

if version.major == 1 && version.minor == 3 {
migrate::from_v1_3_to_v_1_4(deps.branch())?;
}

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
}
4 changes: 1 addition & 3 deletions contracts/arena-core/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ pub fn propose(
contract_addr: info.sender.to_string(),
msg: to_json_binary(
&cw_competition::msg::ExecuteBase::<Empty, Empty>::ProcessCompetition {
competition_id: msg.id,
competition_id: msg.competition_id,
distribution: msg.distribution,
tax_cw20_msg: msg.tax_cw20_msg,
tax_cw721_msg: msg.tax_cw721_msg,
},
)?,
funds: vec![],
Expand Down
1 change: 1 addition & 0 deletions contracts/arena-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod contract;
mod error;
pub mod execute;
mod migrate;
pub mod query;
pub mod state;

Expand Down
39 changes: 39 additions & 0 deletions contracts/arena-core/src/migrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use arena_core_interface::fees::TaxConfiguration;
use cosmwasm_std::{from_json, DepsMut, StdError, Uint128};

use crate::{
state::{ARENA_TAX_CONFIG, COMPETITION_CATEGORIES_COUNT, COMPETITION_MODULES_COUNT},
ContractError,
};

pub fn from_v1_3_to_v_1_4(deps: DepsMut) -> Result<(), ContractError> {
let prev_key = "competition-categories-count".as_bytes();

let competition_categories_count: Uint128 = from_json(
deps.storage
.get(prev_key)
.ok_or_else(|| StdError::not_found("State"))?,
)?;
deps.storage.remove(prev_key);
COMPETITION_CATEGORIES_COUNT.save(deps.storage, &competition_categories_count)?;

let prev_key = "competition-modules-count".as_bytes();

let competition_modules_count: Uint128 = from_json(
deps.storage
.get(prev_key)
.ok_or_else(|| StdError::not_found("State"))?,
)?;
deps.storage.remove(prev_key);
COMPETITION_MODULES_COUNT.save(deps.storage, &competition_modules_count)?;

ARENA_TAX_CONFIG.save(
deps.storage,
&TaxConfiguration {
cw20_msg: None,
cw721_msg: None,
},
)?;

Ok(())
}
12 changes: 10 additions & 2 deletions contracts/arena-core/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::state::{
competition_categories, get_rulesets_category_and_is_enabled_idx, CompetitionModule, KEYS, TAX,
competition_categories, get_rulesets_category_and_is_enabled_idx, CompetitionModule,
ARENA_TAX_CONFIG, KEYS, TAX,
};
use arena_core_interface::msg::{
CompetitionCategory, CompetitionModuleQuery, CompetitionModuleResponse, DumpStateResponse,
Ruleset,
Ruleset, TaxConfigurationResponse,
};
use cosmwasm_std::{Decimal, Deps, Empty, Env, StdResult, Uint128};
use cw_paginate::paginate_indexed_map;
Expand Down Expand Up @@ -235,3 +236,10 @@ pub fn is_valid_category_and_rulesets(

true
}

pub fn arena_fee_config(deps: Deps, height: u64) -> StdResult<TaxConfigurationResponse> {
Ok(ARENA_TAX_CONFIG.load(deps.storage)?.into_response(
TAX.may_load_at_height(deps.storage, height)?
.unwrap_or_default(),
))
}
12 changes: 7 additions & 5 deletions contracts/arena-core/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use arena_core_interface::msg::{CompetitionCategory, Ruleset};
use arena_core_interface::{
fees::TaxConfiguration,
msg::{CompetitionCategory, Ruleset},
};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Decimal, Uint128};
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, MultiIndex, SnapshotItem, SnapshotMap};

pub const COMPETITION_CATEGORIES_COUNT: Item<Uint128> = Item::new("competition-categories-count");
pub const COMPETITION_MODULES_COUNT: Item<Uint128> = Item::new("competition-modules-count");
pub const ARENA_TAX_CONFIG: Item<TaxConfiguration> = Item::new("arena_tax_config");
pub const COMPETITION_CATEGORIES_COUNT: Item<Uint128> = Item::new("competition_categories_count");
pub const COMPETITION_MODULES_COUNT: Item<Uint128> = Item::new("competition_modules_count");
pub const TAX: SnapshotItem<Decimal> = SnapshotItem::new(
"tax",
"tax__check",
Expand All @@ -20,7 +24,6 @@ pub const KEYS: SnapshotMap<String, Addr> = SnapshotMap::new(
);

// Competition Modules

#[cw_serde]
pub struct CompetitionModule {
pub key: String,
Expand Down Expand Up @@ -52,7 +55,6 @@ pub fn competition_modules<'a>(
}

// Competition Categories

pub struct CompetitionCategoryIndexes<'a> {
pub is_enabled: MultiIndex<'a, String, CompetitionCategory, u128>,
}
Expand Down
Loading

0 comments on commit 16ff6a0

Please sign in to comment.