Skip to content

Commit

Permalink
Merge branch 'feat/factory-address-registry' of github.com:hadronlabs…
Browse files Browse the repository at this point in the history
…-org/lionco-contracts into feat/phone-book-follow-up
  • Loading branch information
ratik committed Nov 28, 2024
2 parents 88d5db5 + 4e62195 commit c03a400
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 127 deletions.
123 changes: 70 additions & 53 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use cosmwasm_std::{
attr, instantiate2_address, to_json_binary, Binary, CodeInfoResponse, CosmosMsg, Deps, DepsMut,
Env, HexBinary, MessageInfo, Response, Uint128, WasmMsg,
Env, HexBinary, MessageInfo, Response, StdResult, Uint128, WasmMsg,
};
use drop_helpers::answer::response;
use drop_helpers::phonebook::{
Expand All @@ -12,7 +12,6 @@ use drop_helpers::phonebook::{
VALIDATORS_SET_CONTRACT, WITHDRAWAL_MANAGER_CONTRACT, WITHDRAWAL_VOUCHER_CONTRACT,
};
use drop_staking_base::error::factory::ContractResult;
use drop_staking_base::state::factory::Phonebook;
use drop_staking_base::state::splitter::Config as SplitterConfig;
use drop_staking_base::{
msg::factory::{
Expand Down Expand Up @@ -209,35 +208,54 @@ pub fn instantiate(
deps.api.addr_humanize(&lsm_share_bond_provider_address)?;
let native_bond_provider_contract = deps.api.addr_humanize(&native_bond_provider_address)?;

STATE.save(deps.storage, CORE_CONTRACT, &core_contract.clone())?;
STATE.save(
deps.storage,
&Phonebook::new([
(CORE_CONTRACT, core_contract.clone()),
(
WITHDRAWAL_MANAGER_CONTRACT,
withdrawal_manager_contract.clone(),
),
(REWARDS_MANAGER_CONTRACT, rewards_manager_contract.clone()),
(TOKEN_CONTRACT, token_contract.clone()),
(PUPPETEER_CONTRACT, puppeteer_contract.clone()),
(
WITHDRAWAL_VOUCHER_CONTRACT,
withdrawal_voucher_contract.clone(),
),
(STRATEGY_CONTRACT, strategy_contract.clone()),
(VALIDATORS_SET_CONTRACT, validators_set_contract.clone()),
(DISTRIBUTION_CONTRACT, distribution_contract.clone()),
(SPLITTER_CONTRACT, splitter_contract.clone()),
(
LSM_SHARE_BOND_PROVIDER_CONTRACT,
lsm_share_bond_provider_contract.clone(),
),
(
NATIVE_BOND_PROVIDER_CONTRACT,
native_bond_provider_contract.clone(),
),
(REWARDS_PUMP_CONTRACT, rewards_pump_contract.clone()),
]),
WITHDRAWAL_MANAGER_CONTRACT,
&withdrawal_manager_contract.clone(),
)?;
STATE.save(
deps.storage,
REWARDS_MANAGER_CONTRACT,
&rewards_manager_contract.clone(),
)?;
STATE.save(deps.storage, TOKEN_CONTRACT, &token_contract.clone())?;
STATE.save(
deps.storage,
PUPPETEER_CONTRACT,
&puppeteer_contract.clone(),
)?;
STATE.save(
deps.storage,
WITHDRAWAL_VOUCHER_CONTRACT,
&withdrawal_voucher_contract.clone(),
)?;
STATE.save(deps.storage, STRATEGY_CONTRACT, &strategy_contract.clone())?;
STATE.save(
deps.storage,
VALIDATORS_SET_CONTRACT,
&validators_set_contract.clone(),
)?;
STATE.save(
deps.storage,
DISTRIBUTION_CONTRACT,
&distribution_contract.clone(),
)?;
STATE.save(deps.storage, SPLITTER_CONTRACT, &splitter_contract.clone())?;
STATE.save(
deps.storage,
LSM_SHARE_BOND_PROVIDER_CONTRACT,
&lsm_share_bond_provider_contract.clone(),
)?;
STATE.save(
deps.storage,
NATIVE_BOND_PROVIDER_CONTRACT,
&native_bond_provider_contract.clone(),
)?;
STATE.save(
deps.storage,
REWARDS_PUMP_CONTRACT,
&rewards_pump_contract.clone(),
)?;

let msgs = vec![
Expand Down Expand Up @@ -455,26 +473,29 @@ pub fn query(deps: Deps<NeutronQuery>, _env: Env, msg: QueryMsg) -> ContractResu
}

fn query_state(deps: Deps<NeutronQuery>) -> ContractResult<Binary> {
let state = STATE.load(deps.storage)?;
Ok(to_json_binary(&state.map)?)
let state = STATE.range(deps.storage, None, None, cosmwasm_std::Order::Ascending);
let out = state
.collect::<StdResult<Vec<_>>>()?
.into_iter()
.map(|(k, v)| (k, v.into_string()))
.collect::<HashMap<String, String>>();
Ok(to_json_binary(&out)?)
}

fn query_locate(deps: Deps<NeutronQuery>, items: Vec<String>) -> ContractResult<Binary> {
let mut contracts: HashMap<String, String> = HashMap::new();
let state = STATE.load(deps.storage)?;

for item in items {
let item_key = item.clone();
let contract = state.get_as_result(&item_key)?;
contracts.insert(item.clone(), contract.to_string());
let addr = STATE.load(deps.storage, &item)?;
contracts.insert(item, addr.into_string());
}
Ok(to_json_binary(&contracts)?)
}

fn query_pause_info(deps: Deps<NeutronQuery>) -> ContractResult<Binary> {
let state = STATE.load(deps.storage)?;
let core_contract = state.get_as_result(CORE_CONTRACT)?;
let withdrawal_manager_contract = state.get_as_result(WITHDRAWAL_MANAGER_CONTRACT)?;
let rewards_manager_contract = state.get_as_result(REWARDS_MANAGER_CONTRACT)?;
let core_contract = STATE.load(deps.storage, CORE_CONTRACT)?;
let withdrawal_manager_contract = STATE.load(deps.storage, WITHDRAWAL_MANAGER_CONTRACT)?;
let rewards_manager_contract = STATE.load(deps.storage, REWARDS_MANAGER_CONTRACT)?;

to_json_binary(&drop_staking_base::state::factory::PauseInfoResponse {
core: deps
Expand Down Expand Up @@ -517,10 +538,9 @@ pub fn execute(

fn exec_pause(deps: DepsMut, info: MessageInfo) -> ContractResult<Response<NeutronMsg>> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let state = STATE.load(deps.storage)?;
let core_contract = state.get_as_result(CORE_CONTRACT)?;
let withdrawal_manager_contract = state.get_as_result(WITHDRAWAL_MANAGER_CONTRACT)?;
let rewards_manager_contract = state.get_as_result(REWARDS_MANAGER_CONTRACT)?;
let core_contract = STATE.load(deps.storage, CORE_CONTRACT)?;
let withdrawal_manager_contract = STATE.load(deps.storage, WITHDRAWAL_MANAGER_CONTRACT)?;
let rewards_manager_contract = STATE.load(deps.storage, REWARDS_MANAGER_CONTRACT)?;

let attrs = vec![attr("action", "pause")];
let messages = vec![
Expand Down Expand Up @@ -551,10 +571,9 @@ fn exec_pause(deps: DepsMut, info: MessageInfo) -> ContractResult<Response<Neutr

fn exec_unpause(deps: DepsMut, info: MessageInfo) -> ContractResult<Response<NeutronMsg>> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let state = STATE.load(deps.storage)?;
let core_contract = state.get_as_result(CORE_CONTRACT)?;
let withdrawal_manager_contract = state.get_as_result(WITHDRAWAL_MANAGER_CONTRACT)?;
let rewards_manager_contract = state.get_as_result(REWARDS_MANAGER_CONTRACT)?;
let core_contract = STATE.load(deps.storage, CORE_CONTRACT)?;
let withdrawal_manager_contract = STATE.load(deps.storage, WITHDRAWAL_MANAGER_CONTRACT)?;
let rewards_manager_contract = STATE.load(deps.storage, REWARDS_MANAGER_CONTRACT)?;
let attrs = vec![attr("action", "unpause")];
let messages = vec![
get_proxied_message(
Expand Down Expand Up @@ -601,9 +620,8 @@ fn execute_update_config(
) -> ContractResult<Response<NeutronMsg>> {
let attrs = vec![attr("action", "update-config")];
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let state = STATE.load(deps.storage)?;
let core_contract = state.get_as_result(CORE_CONTRACT)?;
let validators_set_contract = state.get_as_result(VALIDATORS_SET_CONTRACT)?;
let core_contract = STATE.load(deps.storage, CORE_CONTRACT)?;
let validators_set_contract = STATE.load(deps.storage, VALIDATORS_SET_CONTRACT)?;
let mut messages = vec![];
match msg {
UpdateConfigMsg::Core(msg) => messages.push(get_proxied_message(
Expand All @@ -628,9 +646,8 @@ fn execute_proxy_msg(
info: MessageInfo,
msg: ProxyMsg,
) -> ContractResult<Response<NeutronMsg>> {
let state = STATE.load(deps.storage)?;
let validators_set_contract = state.get_as_result(VALIDATORS_SET_CONTRACT)?;
let puppeteer_contract = state.get_as_result(PUPPETEER_CONTRACT)?;
let validators_set_contract = STATE.load(deps.storage, VALIDATORS_SET_CONTRACT)?;
let puppeteer_contract = STATE.load(deps.storage, PUPPETEER_CONTRACT)?;
let mut messages = vec![];
let attrs = vec![attr("action", "proxy-call")];
cw_ownable::assert_owner(deps.storage, &info.sender)?;
Expand Down
127 changes: 87 additions & 40 deletions contracts/factory/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use drop_staking_base::{
CoreParams, ExecuteMsg, FeeParams, InstantiateMsg, LsmShareBondParams, NativeBondParams,
QueryMsg, UpdateConfigMsg, ValidatorSetMsg,
},
state::factory::{CodeIds, Phonebook, RemoteOpts, Timeout, STATE},
state::factory::{CodeIds, RemoteOpts, Timeout, STATE},
};
use drop_staking_base::{
msg::{
Expand Down Expand Up @@ -52,45 +52,92 @@ fn set_default_factory_state(deps: DepsMut<NeutronQuery>) {
STATE
.save(
deps.storage,
&Phonebook::new([
(TOKEN_CONTRACT, Addr::unchecked("token_contract")),
(CORE_CONTRACT, Addr::unchecked("core_contract")),
(PUPPETEER_CONTRACT, Addr::unchecked("puppeteer_contract")),
(
WITHDRAWAL_MANAGER_CONTRACT,
Addr::unchecked("withdrawal_manager_contract"),
),
(
WITHDRAWAL_VOUCHER_CONTRACT,
Addr::unchecked("withdrawal_voucher_contract"),
),
(STRATEGY_CONTRACT, Addr::unchecked("strategy_contract")),
(
VALIDATORS_SET_CONTRACT,
Addr::unchecked("validators_set_contract"),
),
(
DISTRIBUTION_CONTRACT,
Addr::unchecked("distribution_contract"),
),
(
REWARDS_MANAGER_CONTRACT,
Addr::unchecked("rewards_manager_contract"),
),
(
REWARDS_PUMP_CONTRACT,
Addr::unchecked("rewards_pump_contract"),
),
(SPLITTER_CONTRACT, Addr::unchecked("splitter_contract")),
(
LSM_SHARE_BOND_PROVIDER_CONTRACT,
Addr::unchecked("lsm_share_bond_provider_contract"),
),
(
NATIVE_BOND_PROVIDER_CONTRACT,
Addr::unchecked("native_bond_provider_contract"),
),
]),
TOKEN_CONTRACT,
&Addr::unchecked("token_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
CORE_CONTRACT,
&Addr::unchecked("core_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
PUPPETEER_CONTRACT,
&Addr::unchecked("puppeteer_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
WITHDRAWAL_MANAGER_CONTRACT,
&Addr::unchecked("withdrawal_manager_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
WITHDRAWAL_VOUCHER_CONTRACT,
&Addr::unchecked("withdrawal_voucher_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
STRATEGY_CONTRACT,
&Addr::unchecked("strategy_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
VALIDATORS_SET_CONTRACT,
&Addr::unchecked("validators_set_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
DISTRIBUTION_CONTRACT,
&Addr::unchecked("distribution_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
REWARDS_MANAGER_CONTRACT,
&Addr::unchecked("rewards_manager_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
REWARDS_PUMP_CONTRACT,
&Addr::unchecked("rewards_pump_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
SPLITTER_CONTRACT,
&Addr::unchecked("splitter_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
LSM_SHARE_BOND_PROVIDER_CONTRACT,
&Addr::unchecked("lsm_share_bond_provider_contract"),
)
.unwrap();
STATE
.save(
deps.storage,
NATIVE_BOND_PROVIDER_CONTRACT,
&Addr::unchecked("native_bond_provider_contract"),
)
.unwrap();
}
Expand Down
36 changes: 2 additions & 34 deletions packages/base/src/state/factory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Addr;
use cw_storage_plus::Item;
use std::hash::Hash;
use cw_storage_plus::Map;

#[cw_serde]
pub struct CodeIds {
Expand Down Expand Up @@ -44,35 +43,4 @@ pub struct PauseInfoResponse {
pub rewards_manager: drop_helpers::pause::PauseInfoResponse,
}

#[cw_serde]
pub struct Phonebook {
pub map: std::collections::HashMap<String, Addr>,
}

pub const STATE: Item<Phonebook> = Item::new("state");

impl Phonebook {
pub fn get_as_result<'a>(
&'a self,
key: &'a str,
) -> Result<&Addr, crate::error::factory::ContractError> {
self.map.get(key).ok_or(
crate::error::factory::ContractError::ContractAddressNotFound {
name: key.to_string(),
},
)
}

pub fn new<K, const N: usize>(arr: [(K, Addr); N]) -> Self
where
// Bounds from impl:
K: Eq + Hash + Into<String> + Clone + std::fmt::Display,
{
let map = arr
.iter()
.clone()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect();
Self { map }
}
}
pub const STATE: Map<&str, Addr> = Map::new("state");

0 comments on commit c03a400

Please sign in to comment.