Skip to content

Commit

Permalink
dao-voting-snip20-stake updraded and snip20 fixes in query responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-1857 committed Jan 16, 2024
1 parent 6196466 commit ba92655
Show file tree
Hide file tree
Showing 16 changed files with 2,208 additions and 1,710 deletions.
33 changes: 27 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ voting-v1 = { package = "dao-voting", version = "0.1.0" }
# cosmwasm-storage = { version = "1.1.11", package = "secret-cosmwasm-storage", default-features = false }
secret-utils = { path="./packages/utils/"}
# cosmwasm-std = { version = "1.1.11", package = "secret-cosmwasm-std", default-features = false }
secret-toolkit = { git = "https://github.com/scrtlabs/secret-toolkit", tag = "v0.8.0",features = ["viewing-key"] }
secret-toolkit = { git = "https://github.com/scrtlabs/secret-toolkit", tag = "v0.8.0",features = ["viewing-key","permit"] }
secret-storage-plus = { path = "./packages/storage-plus/"}
secret-cw-controllers = { path = "./packages/controllers/"}
secret-cw2 = { path="./packages/cw2/" }
Expand Down
62 changes: 27 additions & 35 deletions contracts/staking/snip20-stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::msg::{
ExecuteAnswer, GetHooksResponse, InstantiateAnswer, ListStakersResponse, QueryMsg,
Snip20ReceiveMsg, StakedValueResponse, StakerBalanceResponse, TotalValueResponse,
};
use crate::msg::{ExecuteMsg,InstantiateMsg, ReceiveMsg, ResponseStatus::Success};
use crate::msg::{ExecuteMsg, InstantiateMsg, ReceiveMsg, ResponseStatus::Success};
use crate::state::{
Config, BALANCE, CLAIMS, CONFIG, HOOKS, MAX_CLAIMS, STAKED_BALANCES, STAKED_TOTAL,
};
Expand All @@ -12,18 +12,18 @@ use cosmwasm_std::{
entry_point, from_binary, to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo,
Response, StdError, StdResult, Uint128,
};
use snip20_reference_impl::msg::ExecuteMsg::Transfer;
use dao_hooks::stake::{stake_hook_msgs, unstake_hook_msgs};
use dao_voting::duration::validate_duration;
use secret_cw2::set_contract_version;
use snip20_reference_impl::msg::ExecuteMsg::Transfer;

use secret_cw_controllers::ClaimsResponse;
pub use secret_toolkit::snip20::handle::{
burn_from_msg, burn_msg, decrease_allowance_msg, increase_allowance_msg, mint_msg,
send_from_msg, send_msg, transfer_from_msg, transfer_msg,
};
pub use secret_toolkit::snip20::query::{
allowance_query, balance_query, minters_query, token_info_query,
allowance_query, balance_query, minters_query,
};
use secret_toolkit::viewing_key::{ViewingKey, ViewingKeyStore};
use secret_utils::Duration;
Expand All @@ -45,18 +45,22 @@ pub fn instantiate(
// though this provides some protection against mistakes where the
// wrong address is provided.
let token_address = deps.api.addr_validate(&msg.token_address)?;
let _: secret_toolkit::snip20::TokenInfoResponse = deps
let token_info: snip20_reference_impl::msg::TokenInfo = deps
.querier
.query_wasm_smart(
msg.token_code_hash.clone(),
msg.token_code_hash.clone().unwrap(),
&token_address,
&secret_toolkit::snip20::QueryMsg::TokenInfo {},
)
.map_err(|_| ContractError::InvalidSnip20 {})?;
&snip20_reference_impl::msg::QueryMsg::TokenInfo { },
)?;
let _supply =token_info.total_supply.unwrap();

validate_duration(msg.unstaking_duration)?;

let config=Config { token_address, token_code_hash: msg.token_code_hash.clone(), unstaking_duration: msg.unstaking_duration };
let config = Config {
token_address,
token_code_hash: msg.token_code_hash.clone().unwrap(),
unstaking_duration: msg.unstaking_duration,
};
CONFIG.save(deps.storage, &config)?;

// Initialize state to zero. We do this instead of using
Expand Down Expand Up @@ -147,7 +151,9 @@ pub fn execute_stake(
let balance = BALANCE.load(deps.storage).unwrap_or_default();
let staked_total = STAKED_TOTAL.load(deps.storage).unwrap_or_default();
let amount_to_stake = math::amount_to_stake(staked_total, balance, amount);
let prev_balance = STAKED_BALANCES.get(deps.storage, &sender).unwrap_or_default();
let prev_balance = STAKED_BALANCES
.get(deps.storage, &sender)
.unwrap_or_default();
STAKED_BALANCES.insert(
deps.storage,
&sender,
Expand Down Expand Up @@ -228,13 +234,13 @@ pub fn execute_unstake(
)?;
match config.unstaking_duration {
None => {
let snip_send_msg = Transfer{
let snip_send_msg = Transfer {
recipient: info.sender.to_string(),
amount: amount_to_claim,
memo: None,
padding: None,
decoys: None,
entropy:None,
entropy: None,
};
let wasm_msg = cosmwasm_std::WasmMsg::Execute {
contract_addr: config.token_address.to_string(),
Expand Down Expand Up @@ -282,7 +288,7 @@ pub fn execute_claim(
return Err(ContractError::NothingToClaim {});
}
let config = CONFIG.load(deps.storage)?;
let cw_send_msg =Transfer {
let cw_send_msg = Transfer {
recipient: info.sender.to_string(),
amount: release,
memo: None,
Expand Down Expand Up @@ -388,7 +394,7 @@ pub fn try_set_key(
#[entry_point]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetConfig { key, address } => to_binary(&query_config(deps, address, key)?),
QueryMsg::GetConfig {} => to_binary(&query_config(deps)?),
#[cfg(feature = "iterator")]
QueryMsg::StakedBalanceAtHeight {
key,
Expand All @@ -408,12 +414,10 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::StakedValue { key, address } => {
to_binary(&query_staked_value(deps, env, key, address)?)
}
QueryMsg::TotalValue { key, address } => {
to_binary(&query_total_value(deps, env, address, key)?)
}
QueryMsg::TotalValue {} => to_binary(&query_total_value(deps, env)?),
QueryMsg::Claims { key, address } => to_binary(&query_claims(deps, key, address)?),
QueryMsg::GetHooks { key, address } => to_binary(&query_hooks(deps, address, key)?),
QueryMsg::ListStakers { key, address } => query_list_stakers(deps, key, address),
QueryMsg::GetHooks {} => to_binary(&query_hooks(deps)?),
QueryMsg::ListStakers {} => query_list_stakers(deps),
QueryMsg::Ownership {} => to_binary(&cw_ownable::get_ownership(deps.storage)?),
}
}
Expand Down Expand Up @@ -481,21 +485,12 @@ pub fn query_staked_value(
}
}

pub fn query_total_value(
deps: Deps,
_env: Env,
address: String,
key: String,
) -> StdResult<TotalValueResponse> {
authenticate(deps, deps.api.addr_validate(&address)?, key)?;

pub fn query_total_value(deps: Deps, _env: Env) -> StdResult<TotalValueResponse> {
let balance = BALANCE.load(deps.storage).unwrap_or_default();
Ok(TotalValueResponse { total: balance })
}

pub fn query_config(deps: Deps, address: String, key: String) -> StdResult<Config> {
authenticate(deps, deps.api.addr_validate(&address)?, key)?;

pub fn query_config(deps: Deps) -> StdResult<Config> {
let config = CONFIG.load(deps.storage)?;
Ok(config)
}
Expand All @@ -506,16 +501,13 @@ pub fn query_claims(deps: Deps, key: String, address: String) -> StdResult<Claim
CLAIMS.query_claims(deps, &deps.api.addr_validate(&address)?)
}

pub fn query_hooks(deps: Deps, address: String, key: String) -> StdResult<GetHooksResponse> {
authenticate(deps, deps.api.addr_validate(&address)?, key)?;

pub fn query_hooks(deps: Deps) -> StdResult<GetHooksResponse> {
Ok(GetHooksResponse {
hooks: HOOKS.query_hooks(deps)?.hooks,
})
}

pub fn query_list_stakers(deps: Deps, key: String, address: String) -> StdResult<Binary> {
authenticate(deps, deps.api.addr_validate(&address)?, key)?;
pub fn query_list_stakers(deps: Deps) -> StdResult<Binary> {
// let start_at = start_after
// .map(|addr| deps.api.addr_validate(&addr))
// .transpose()?;
Expand Down
10 changes: 5 additions & 5 deletions contracts/staking/snip20-stake/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct InstantiateMsg {
// Owner can update all configs including changing the owner. This will generally be a DAO.
pub owner: Option<String>,
pub token_address: String,
pub token_code_hash : String,
pub token_code_hash: Option<String>,
pub unstaking_duration: Option<Duration>,
}

Expand Down Expand Up @@ -85,15 +85,15 @@ pub enum QueryMsg {
#[returns(StakedValueResponse)]
StakedValue { key: String, address: String },
#[returns(TotalValueResponse)]
TotalValue { key: String, address: String },
TotalValue {},
#[returns(crate::state::Config)]
GetConfig { key: String, address: String },
GetConfig {},
#[returns(ClaimsResponse)]
Claims { key: String, address: String },
#[returns(GetHooksResponse)]
GetHooks { key: String, address: String },
GetHooks {},
#[returns(ListStakersResponse)]
ListStakers { key: String, address: String },
ListStakers {},
#[returns(::cw_ownable::Ownership::<::cosmwasm_std::Addr>)]
Ownership {},
}
Expand Down
1 change: 1 addition & 0 deletions contracts/staking/snip20-stake/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Config {
pub unstaking_duration: Option<Duration>,
}

pub const RESPONSE_BLOCK_SIZE: usize = 256;
pub const CONFIG: Item<Config> = Item::new("config");
pub const STAKED_TOTAL: Item<Uint128> = Item::new("total_staked");
pub const BALANCE: Item<Uint128> = Item::new("balance");
Expand Down
17 changes: 11 additions & 6 deletions contracts/voting/dao-voting-cw20-staked/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ library = []
cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-storage = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
cw2 = { workspace = true }
cw20 = { workspace = true }
cw20-base = { workspace = true, features = ["library"] }
serde={workspace=true}
schemars={ workspace=true }
secret-storage-plus = { workspace = true }
secret-utils = { workspace = true }
secret-cw2 = { workspace = true }
snip20-reference-impl = { workspace = true }
secret-toolkit = { workspace = true,features = ["permit"]}
snip20-stake = { workspace = true, features = ["library"] }
thiserror = { workspace = true }
dao-dao-macros = { workspace = true }
dao-interface = { workspace = true }
dao-voting = { workspace = true }
cw20-base={workspace=true}
cw-ownable={workspace=true}
secret-cw-controllers={ workspace=true }

[dev-dependencies]
cw-multi-test = { workspace = true }
secret-multi-test = { workspace = true }
Loading

0 comments on commit ba92655

Please sign in to comment.