Skip to content

Commit

Permalink
feat: refactor factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik committed Dec 14, 2023
1 parent 95f7eaf commit b3dc684
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
28 changes: 16 additions & 12 deletions contracts/core/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::error::{ContractError, ContractResult};
use cosmwasm_std::{
attr, ensure_eq, ensure_ne, entry_point, to_json_binary, Binary, CosmosMsg, Decimal, Deps,
DepsMut, Env, MessageInfo, Response, StdResult, Uint128, WasmMsg,
attr, ensure_eq, ensure_ne, entry_point, to_json_binary, Attribute, Binary, CosmosMsg, Decimal,
Deps, DepsMut, Env, MessageInfo, Response, StdResult, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use lido_staking_base::helpers::answer::response;
use lido_staking_base::msg::core::{ExecuteMsg, InstantiateMsg, QueryMsg};
use lido_staking_base::msg::token::ExecuteMsg as TokenExecuteMsg;
use lido_staking_base::state::core::CONFIG;
use neutron_sdk::{
bindings::{msg::NeutronMsg, query::NeutronQuery},
NeutronResult,
};
use neutron_sdk::bindings::{msg::NeutronMsg, query::NeutronQuery};
use std::str::FromStr;
use std::vec;
const CONTRACT_NAME: &str = concat!("crates.io:lido-neutron-contracts__", env!("CARGO_PKG_NAME"));
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

Expand All @@ -21,11 +20,17 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> NeutronResult<Response> {
) -> ContractResult<Response<NeutronMsg>> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

CONFIG.save(deps.storage, &msg.into())?;
Ok(Response::default())
CONFIG.save(deps.storage, &msg.clone().into())?;
let attrs: Vec<Attribute> = vec![
attr("token_contract", msg.token_contract),
attr("puppeteer_contract", msg.puppeteer_contract),
attr("strategy_contract", msg.strategy_contract),
attr("owner", msg.owner),
];
Ok(response("instantiate", CONTRACT_NAME, attrs))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -116,8 +121,7 @@ fn execute_bond(
})?,
funds: vec![],
})];

Ok(Response::default().add_messages(msgs).add_attributes(attrs))
Ok(response("execute-bond", CONTRACT_NAME, attrs).add_messages(msgs))
}

fn check_denom(_denom: String) -> ContractResult<()> {
Expand Down Expand Up @@ -155,7 +159,7 @@ fn execute_update_config(
attrs.push(attr("owner", owner));
}
CONFIG.save(deps.storage, &config)?;
Ok(Response::default().add_attributes(attrs))
Ok(response("execute-update_config", CONTRACT_NAME, attrs))
}

fn execute_unbond(
Expand Down
45 changes: 31 additions & 14 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use crate::{
error::ContractResult,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
state::{Config, State, CONFIG, STATE},
};
use cosmwasm_std::{
attr, entry_point, instantiate2_address, to_json_binary, Binary, CodeInfoResponse, CosmosMsg,
Deps, DepsMut, Env, HexBinary, MessageInfo, Response, StdError, StdResult, WasmMsg,
Deps, DepsMut, Env, HexBinary, MessageInfo, Response, StdResult, WasmMsg,
};
use cw2::set_contract_version;
use lido_staking_base::msg::core::InstantiateMsg as CoreInstantiateMsg;
use lido_staking_base::msg::token::InstantiateMsg as TokenInstantiateMsg;
use neutron_sdk::{bindings::query::NeutronQuery, NeutronResult};
use lido_staking_base::{
helpers::answer::response, msg::core::InstantiateMsg as CoreInstantiateMsg,
};
use neutron_sdk::{
bindings::{msg::NeutronMsg, query::NeutronQuery},
NeutronResult,
};

const CONTRACT_NAME: &str = concat!("crates.io:lido-neutron-contracts__", env!("CARGO_PKG_NAME"));
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -20,20 +26,28 @@ pub fn instantiate(
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> NeutronResult<Response> {
) -> ContractResult<Response<NeutronMsg>> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

CONFIG.save(
deps.storage,
&Config {
salt: msg.salt,
salt: msg.salt.to_string(),
token_code_id: msg.token_code_id,
core_code_id: msg.core_code_id,
owner: info.sender.to_string(),
subdenom: msg.subdenom,
subdenom: msg.subdenom.to_string(),
},
)?;
Ok(Response::default())

let attrs = vec![
attr("salt", msg.salt),
attr("token_code_id", msg.token_code_id.to_string()),
attr("core_code_id", msg.core_code_id.to_string()),
attr("owner", info.sender),
attr("subdenom", msg.subdenom),
];
Ok(response("instantiate", CONTRACT_NAME, attrs))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -49,13 +63,17 @@ pub fn execute(
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> NeutronResult<Response> {
) -> ContractResult<Response<NeutronMsg>> {
match msg {
ExecuteMsg::Init {} => execute_init(deps, env, info),
}
}

fn execute_init(deps: DepsMut, env: Env, _info: MessageInfo) -> NeutronResult<Response> {
fn execute_init(
deps: DepsMut,
env: Env,
_info: MessageInfo,
) -> ContractResult<Response<NeutronMsg>> {
let config = CONFIG.load(deps.storage)?;
let canonical_self_address = deps.api.addr_canonicalize(env.contract.address.as_str())?;
let mut attrs = vec![attr("action", "init")];
Expand All @@ -65,11 +83,10 @@ fn execute_init(deps: DepsMut, env: Env, _info: MessageInfo) -> NeutronResult<Re
let salt = config.salt.as_bytes();

let token_address =
instantiate2_address(&token_contract_checksum, &canonical_self_address, salt)
.map_err(|e| StdError::generic_err(format!("failed to calc token address: {e:?}")))?;
instantiate2_address(&token_contract_checksum, &canonical_self_address, salt)?;
attrs.push(attr("token_address", token_address.to_string()));
let core_address = instantiate2_address(&core_contract_checksum, &canonical_self_address, salt)
.map_err(|e| StdError::generic_err(format!("failed to calc core address: {e:?}")))?;
let core_address =
instantiate2_address(&core_contract_checksum, &canonical_self_address, salt)?;
attrs.push(attr("core_address", core_address.to_string()));

let core_contract = deps.api.addr_humanize(&core_address)?.to_string();
Expand Down Expand Up @@ -107,7 +124,7 @@ fn execute_init(deps: DepsMut, env: Env, _info: MessageInfo) -> NeutronResult<Re
}),
];

Ok(Response::default().add_messages(msgs).add_attributes(attrs))
Ok(response("execute-init", CONTRACT_NAME, attrs).add_messages(msgs))
}

fn get_code_checksum(deps: Deps, code_id: u64) -> NeutronResult<HexBinary> {
Expand Down
22 changes: 17 additions & 5 deletions contracts/factory/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use cosmwasm_std::Instantiate2AddressError;
use cosmwasm_std::{Instantiate2AddressError, StdError};
use neutron_sdk::NeutronError;

use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
Instantiate2Address {
err: Instantiate2AddressError,
code_id: u64,
},
#[error("{0}")]
Std(#[from] StdError),

#[error("{0}")]
NeutronError(#[from] NeutronError),
#[error("Could not calculcate instantiate2 address: {0}")]
Instantiate2AddressError(#[from] Instantiate2AddressError),
#[error("Unauthorized")]
Unauthorized {},
#[error("Unimplemented")]
Unimplemented {},
#[error("Unknown")]
Unknown {},
}

pub type ContractResult<T> = Result<T, ContractError>;

0 comments on commit b3dc684

Please sign in to comment.