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

Custom Treasury - Burn Unwanted Native Tokens #823

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
151 changes: 145 additions & 6 deletions contracts/dao-dao-core/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_json, to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo,
Order, Reply, Response, StdError, StdResult, SubMsg, WasmMsg,
from_json, to_json_binary, Addr, BankMsg, BankQuery, Binary, Coin, CosmosMsg, Deps, DepsMut,
Empty, Env, MessageInfo, Order, Reply, Response, StdError, StdResult, SubMsg, WasmMsg,
};
use cw2::{get_contract_version, set_contract_version, ContractVersion};
use cw_paginate_storage::{paginate_map, paginate_map_keys, paginate_map_values};
use cw_storage_plus::Map;
use cw_utils::{parse_reply_instantiate_data, Duration};
use dao_interface::{
msg::{ExecuteMsg, InitialItem, InstantiateMsg, MigrateMsg, QueryMsg},
msg::{ExecuteMsg, InitialItem, InstantiateMsg, MigrateMsg, QueryMsg, SudoMsg},
query::{
AdminNominationResponse, Cw20BalanceResponse, DaoURIResponse, DumpStateResponse,
GetItemResponse, PauseInfoResponse, ProposalModuleCountResponse, SubDao,
Expand All @@ -21,11 +21,12 @@ use dao_interface::{
voting,
};

use crate::error::ContractError;
use crate::state::{
ACTIVE_PROPOSAL_MODULE_COUNT, ADMIN, CONFIG, CW20_LIST, CW721_LIST, ITEMS, NOMINATED_ADMIN,
PAUSED, PROPOSAL_MODULES, SUBDAO_LIST, TOTAL_PROPOSAL_MODULE_COUNT, VOTING_MODULE,
ACTIVE_PROPOSAL_MODULE_COUNT, ADMIN, CONFIG, CUSTOM_TREASURY_BOOL, CW20_LIST, CW721_LIST,
ITEMS, NOMINATED_ADMIN, PAUSED, PROPOSAL_MODULES, SUBDAO_LIST, TOTAL_PROPOSAL_MODULE_COUNT,
VOTING_MODULE,
};
use crate::{error::ContractError, state::ACCEPTED_NATIVE_TOKENS};

pub(crate) const CONTRACT_NAME: &str = "crates.io:dao-dao-core";
pub(crate) const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -91,6 +92,7 @@ pub fn instantiate(

TOTAL_PROPOSAL_MODULE_COUNT.save(deps.storage, &0)?;
ACTIVE_PROPOSAL_MODULE_COUNT.save(deps.storage, &0)?;
CUSTOM_TREASURY_BOOL.save(deps.storage, &false)?;

Ok(Response::new()
.add_attribute("action", "instantiate")
Expand Down Expand Up @@ -140,6 +142,11 @@ pub fn execute(
ExecuteMsg::UpdateCw721List { to_add, to_remove } => {
execute_update_cw721_list(deps, env, info.sender, to_add, to_remove)
}
ExecuteMsg::UpdateTokenList {
bool,
to_add,
to_remove,
} => execute_update_token_list(deps, env, info.sender, bool, to_add, to_remove),
ExecuteMsg::UpdateVotingModule { module } => {
execute_update_voting_module(env, info.sender, module)
}
Expand Down Expand Up @@ -430,6 +437,38 @@ fn do_update_addr_list(
Ok(())
}

fn do_update_token_list(
deps: DepsMut,
map: Map<String, Empty>,
bool: Option<bool>,
to_add: Vec<String>,
to_remove: Vec<String>,
) -> Result<(), ContractError> {
let enabled = CUSTOM_TREASURY_BOOL.load(deps.storage)?;

if !enabled && Some(bool).is_some() {
if bool == Some(true) {
CUSTOM_TREASURY_BOOL.update(deps.storage, |_| Ok::<bool, StdError>(true))?;
}
} else if enabled && bool == Some(false) {
CUSTOM_TREASURY_BOOL.update(deps.storage, |_| Ok::<bool, StdError>(false))?;
// TODO: should this be cleaned to fresh state?
return Ok(());
}

let to_add = to_add.into_iter().map(|a| a).collect::<Vec<String>>();
let to_remove = to_remove.into_iter().map(|a| a).collect::<Vec<String>>();

for token in to_add {
map.save(deps.storage, token, &Empty {})?;
}
for token in to_remove {
map.remove(deps.storage, token);
}

Ok(())
}

pub fn execute_update_cw20_list(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -473,6 +512,21 @@ pub fn execute_update_cw721_list(
Ok(Response::default().add_attribute("action", "update_cw721_list"))
}

pub fn execute_update_token_list(
deps: DepsMut,
env: Env,
sender: Addr,
bool: Option<bool>,
to_add: Vec<String>,
to_remove: Vec<String>,
) -> Result<Response, ContractError> {
if env.contract.address != sender {
return Err(ContractError::Unauthorized {});
}
do_update_token_list(deps, ACCEPTED_NATIVE_TOKENS, bool, to_add, to_remove)?;
Ok(Response::default().add_attribute("action", "update_cw721_list"))
}

pub fn execute_set_item(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -578,6 +632,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::GetItem { key } => query_get_item(deps, key),
QueryMsg::Info {} => query_info(deps),
QueryMsg::ListItems { start_after, limit } => query_list_items(deps, start_after, limit),
QueryMsg::NativeTokenList { start_after, limit } => {
query_native_token_list(deps, start_after, limit)
}
QueryMsg::PauseInfo {} => query_paused(deps, env),
QueryMsg::ProposalModules { start_after, limit } => {
query_proposal_modules(deps, start_after, limit)
Expand Down Expand Up @@ -761,6 +818,20 @@ pub fn query_list_items(
)?)
}

pub fn query_native_token_list(
deps: Deps,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<Binary> {
to_json_binary(&paginate_map_keys(
deps,
&ACCEPTED_NATIVE_TOKENS,
start_after.clone(),
limit,
cosmwasm_std::Order::Descending,
)?)
}

pub fn query_cw20_list(
deps: Deps,
start_after: Option<String>,
Expand Down Expand Up @@ -868,6 +939,52 @@ pub fn query_proposal_module_count(deps: Deps) -> StdResult<Binary> {
})
}

pub fn remove_unwanted_balance(
deps: DepsMut,
env: Env,
start_after: Option<String>,
) -> Result<Response, ContractError> {
// runs every 4 hours
if env.block.height % 4800 != 0 {
return Ok(Response::new());
};
// verify custom treasury is enabled
let enabled = CUSTOM_TREASURY_BOOL.load(deps.storage)?;
if !enabled {
return Ok(Response::new());
};

let contract = env.contract.address;
// Query bank for contract balance of native denoms
let native_balance: cosmwasm_std::AllBalanceResponse =
deps.querier
.query(&cosmwasm_std::QueryRequest::Bank(BankQuery::AllBalances {
address: contract.to_string(),
}))?;
// Query contract for accepted native denoms
let accepted_native_tokens = paginate_map_keys(
deps.as_ref(),
&ACCEPTED_NATIVE_TOKENS,
start_after.map(|a| a),
None,
cosmwasm_std::Order::Descending,
)?;
// Check if any native_balance denom is not included in accepted_addrs
let unwanted_native_tokens = native_balance
.amount
.into_iter()
.filter(|coin: &Coin| !is_denom_accepted(&coin.denom, &accepted_native_tokens))
.collect();

// burn all unwanted tokens from treasury
let send_to_community_pool = BankMsg::Burn {
amount: unwanted_native_tokens,
};

let res = Response::new();
Ok(res.add_message(send_to_community_pool))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
let ContractVersion { version, .. } = get_contract_version(deps.storage)?;
Expand Down Expand Up @@ -1037,11 +1154,33 @@ pub(crate) fn derive_proposal_module_prefix(mut dividend: usize) -> StdResult<St
Ok(prefix.chars().rev().collect())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
match msg {
SudoMsg::ClockEndBlock { start_after } => remove_unwanted_balance(deps, env, start_after),
}
}

fn is_denom_accepted(denom: &str, accepted_native_tokens: &[String]) -> bool {
accepted_native_tokens
.iter()
.any(|addr| addr.as_str() == denom)
}
fn is_cw20_accepted(cw20addr: &Addr, accepted_cw20s: &[String]) -> bool {
accepted_cw20s.iter().any(|addr| addr == cw20addr)
}
fn is_cw721_accepted(cw721addr: &Addr, accepted_cw721s: &[String]) -> bool {
accepted_cw721s.iter().any(|addr| addr == cw721addr)
}

#[cfg(test)]
mod test {
use crate::contract::derive_proposal_module_prefix;
use std::collections::HashSet;

#[test]
fn test_serialize_bank_res() {}

#[test]
fn test_prefix_generation() {
assert_eq!("A", derive_proposal_module_prefix(0).unwrap());
Expand Down
5 changes: 5 additions & 0 deletions contracts/dao-dao-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub const CW20_LIST: Map<Addr, Empty> = Map::new("cw20s");
/// Set of cw721 tokens that have been registered with this contract's
/// treasury.
pub const CW721_LIST: Map<Addr, Empty> = Map::new("cw721s");
/// Set of native tokens that have been registered with this contract's
/// treasury
pub const ACCEPTED_NATIVE_TOKENS: Map<String, Empty> = Map::new("accepted_native_tokens");

pub const CUSTOM_TREASURY_BOOL: Item<bool> = Item::new("custom_treasury_bool");

/// List of SubDAOs associated to this DAO. Each SubDAO has an optional charter.
pub const SUBDAO_LIST: Map<&Addr, Option<String>> = Map::new("sub_daos");
Loading