Skip to content

Commit

Permalink
introduce lockdrop vault mock
Browse files Browse the repository at this point in the history
  • Loading branch information
sotnikov-s committed Jan 31, 2023
1 parent 68f259b commit 6de98b6
Show file tree
Hide file tree
Showing 13 changed files with 1,003 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

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

32 changes: 32 additions & 0 deletions contracts/dao/voting/lockdrop-vault/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "lockdrop-vault"
version = "0.1.0"
authors = ["Sergei Sotnikov <sergei.s@p2p.org>"]
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/neutron/neutron-dao"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []

[dependencies]
cosmwasm-std = { version = "1.0.0" }
cw-storage-plus = "0.13"
cw2 = "0.13"
schemars = "0.8"
serde = { version = "1.0.147", default-features = false, features = ["derive"] }
thiserror = { version = "1.0" }
cwd-macros = { path = "../../../../packages/cwd-macros" }
cwd-interface = { path = "../../../../packages/cwd-interface" }
neutron-lockdrop-vault = { path = "../../../../packages/neutron-lockdrop-vault" }

[dev-dependencies]
cosmwasm-schema = { version = "1.0.0" }
cw-multi-test = "0.13"
anyhow = "1.0.57"
3 changes: 3 additions & 0 deletions contracts/dao/voting/lockdrop-vault/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Neutron Lockdrop Voting Vault

This contract is not really a voting vault. It's rather an interface to get voting power from a Lockdrop contract. It's not possible to Bond or Unbond funds to this vault cause these ExecuteMsg handlers are introduced just to make the contract comply with the voting vault interface.
31 changes: 31 additions & 0 deletions contracts/dao/voting/lockdrop-vault/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};
use cosmwasm_std::Addr;
use cwd_interface::voting::{
InfoResponse, TotalPowerAtHeightResponse, VotingPowerAtHeightResponse,
};
use neutron_lockdrop_vault::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use neutron_lockdrop_vault::types::Config;

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(MigrateMsg), &out_dir);

export_schema(&schema_for!(InfoResponse), &out_dir);
export_schema(&schema_for!(TotalPowerAtHeightResponse), &out_dir);
export_schema(&schema_for!(VotingPowerAtHeightResponse), &out_dir);

// Auto TS code generation expects the query return type as QueryNameResponse
// Here we map query resonses to the correct name
export_schema_with_title(&schema_for!(Addr), &out_dir, "DaoResponse");
export_schema_with_title(&schema_for!(Config), &out_dir, "GetConfigResponse");
}
233 changes: 233 additions & 0 deletions contracts/dao/voting/lockdrop-vault/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, Uint128,
};
use cw2::set_contract_version;
use cwd_interface::voting::{TotalPowerAtHeightResponse, VotingPowerAtHeightResponse};
use cwd_interface::Admin;

use crate::state::{CONFIG, DAO};
use neutron_lockdrop_vault::error::ContractError;
use neutron_lockdrop_vault::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use neutron_lockdrop_vault::types::Config;

pub(crate) const CONTRACT_NAME: &str = "crates.io:neutron-lockdrop-vault";
pub(crate) const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let owner = msg
.owner
.as_ref()
.map(|owner| match owner {
Admin::Address { addr } => deps.api.addr_validate(addr),
Admin::CoreModule {} => Ok(info.sender.clone()),
})
.transpose()?;
let manager = msg
.manager
.map(|manager| deps.api.addr_validate(&manager))
.transpose()?;

let config = Config {
description: msg.description,
lockdrop_contract: deps.api.addr_validate(&msg.lockdrop_contract)?,
owner,
manager,
};
CONFIG.save(deps.storage, &config)?;
DAO.save(deps.storage, &info.sender)?;

Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute("description", config.description)
.add_attribute(
"owner",
config
.owner
.map(|a| a.to_string())
.unwrap_or_else(|| "None".to_string()),
)
.add_attribute("lockdrop_contract", config.lockdrop_contract)
.add_attribute(
"manager",
config
.manager
.map(|a| a.to_string())
.unwrap_or_else(|| "None".to_string()),
))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Bond {} => execute_bond(deps, env, info),
ExecuteMsg::Unbond { amount } => execute_unbond(deps, env, info, amount),
ExecuteMsg::UpdateConfig {
owner,
lockdrop_contract,
manager,
description,
} => execute_update_config(deps, info, owner, lockdrop_contract, manager, description),
}
}

pub fn execute_bond(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
) -> Result<Response, ContractError> {
unimplemented!()
}

pub fn execute_unbond(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_amount: Uint128,
) -> Result<Response, ContractError> {
unimplemented!()
}

pub fn execute_update_config(
deps: DepsMut,
info: MessageInfo,
new_owner: Option<String>,
new_lockdrop_contract: String,
new_manager: Option<String>,
new_description: String,
) -> Result<Response, ContractError> {
let mut config: Config = CONFIG.load(deps.storage)?;
if Some(info.sender.clone()) != config.owner && Some(info.sender.clone()) != config.manager {
return Err(ContractError::Unauthorized {});
}

let new_owner = new_owner
.map(|new_owner| deps.api.addr_validate(&new_owner))
.transpose()?;
let new_lockdrop_contract = deps.api.addr_validate(&new_lockdrop_contract)?;
let new_manager = new_manager
.map(|new_manager| deps.api.addr_validate(&new_manager))
.transpose()?;

if Some(info.sender.clone()) != config.owner && new_owner != config.owner {
return Err(ContractError::OnlyOwnerCanChangeOwner {});
};
if Some(info.sender) != config.owner
&& new_lockdrop_contract != config.clone().lockdrop_contract
{
return Err(ContractError::OnlyOwnerCanChangeLockdropContract {});
};

config.owner = new_owner;
config.lockdrop_contract = new_lockdrop_contract;
config.manager = new_manager;
config.description = new_description;

CONFIG.save(deps.storage, &config)?;
Ok(Response::new()
.add_attribute("action", "update_config")
.add_attribute("description", config.description)
.add_attribute(
"owner",
config
.owner
.map(|a| a.to_string())
.unwrap_or_else(|| "None".to_string()),
)
.add_attribute("lockdrop_contract", config.lockdrop_contract)
.add_attribute(
"manager",
config
.manager
.map(|a| a.to_string())
.unwrap_or_else(|| "None".to_string()),
))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::VotingPowerAtHeight { address, height } => {
to_binary(&query_voting_power_at_height(deps, env, address, height)?)
}
QueryMsg::TotalPowerAtHeight { height } => {
to_binary(&query_total_power_at_height(deps, env, height)?)
}
QueryMsg::Info {} => query_info(deps),
QueryMsg::Dao {} => query_dao(deps),
QueryMsg::Description {} => query_description(deps),
QueryMsg::GetConfig {} => query_config(deps),
QueryMsg::ListBonders { start_after, limit } => {
query_list_bonders(deps, start_after, limit)
}
}
}

pub fn query_voting_power_at_height(
_deps: Deps,
_env: Env,
_address: String,
_height: Option<u64>,
) -> StdResult<VotingPowerAtHeightResponse> {
// TODO: implement once the lockdrop contract is implemented.
unimplemented!()
}

pub fn query_total_power_at_height(
_deps: Deps,
_env: Env,
_height: Option<u64>,
) -> StdResult<TotalPowerAtHeightResponse> {
// TODO: implement once the lockdrop contract is implemented.
unimplemented!()
}

pub fn query_info(deps: Deps) -> StdResult<Binary> {
let info = cw2::get_contract_version(deps.storage)?;
to_binary(&cwd_interface::voting::InfoResponse { info })
}

pub fn query_dao(deps: Deps) -> StdResult<Binary> {
let dao = DAO.load(deps.storage)?;
to_binary(&dao)
}

pub fn query_description(deps: Deps) -> StdResult<Binary> {
let config = CONFIG.load(deps.storage)?;
to_binary(&config.description)
}

pub fn query_config(deps: Deps) -> StdResult<Binary> {
let config = CONFIG.load(deps.storage)?;
to_binary(&config)
}

pub fn query_list_bonders(
_deps: Deps,
_start_after: Option<String>,
_limit: Option<u32>,
) -> StdResult<Binary> {
// TODO: implement once the lockdrop contract is implemented.
unimplemented!()
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
// Set contract to version to latest
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
}
5 changes: 5 additions & 0 deletions contracts/dao/voting/lockdrop-vault/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod contract;
pub mod state;

#[cfg(test)]
mod tests;
6 changes: 6 additions & 0 deletions contracts/dao/voting/lockdrop-vault/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use cosmwasm_std::Addr;
use cw_storage_plus::Item;
use neutron_lockdrop_vault::types::Config;

pub const CONFIG: Item<Config> = Item::new("config");
pub const DAO: Item<Addr> = Item::new("dao");
Loading

0 comments on commit 6de98b6

Please sign in to comment.