Skip to content

Commit

Permalink
Merge pull request #17 from hadronlabs-org/feat/LIDO-76-strategy-cont…
Browse files Browse the repository at this point in the history
…ract

feat: LIDO-76. Strategy contract
  • Loading branch information
oldremez authored Jan 15, 2024
2 parents 44ede70 + 0913cd5 commit 9bc9990
Show file tree
Hide file tree
Showing 52 changed files with 1,124 additions and 275 deletions.
168 changes: 112 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ resolver = "2"
cosmos-sdk-proto = { version = "0.20.0", default-features = false }
base64 = "0.21.2"
cw-ownable = "0.5.1"
neutron-sdk = { package = "neutron-sdk", git = "https://github.com/neutron-org/neutron-sdk", rev = "0312d1edfcdca00818e85fc14504be9479efd5ee" }
neutron-sdk = { package = "neutron-sdk", git = "https://github.com/neutron-org/neutron-sdk", rev = "9f0ed09746b4cc75e902315b0a0d5a3e0e5350a7" }
prost = "0.12.1"
prost-types = "0.12.1"
protobuf = "3.2.0"
Expand Down
16 changes: 5 additions & 11 deletions contracts/distribution/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use cosmwasm_std::{entry_point, to_json_binary, Attribute, Decimal, Deps, Uint128};
use cosmwasm_std::{entry_point, to_json_binary, Decimal, Deps, Event, Uint128};
use cosmwasm_std::{Binary, DepsMut, Env, MessageInfo, Response};
use cw2::set_contract_version;
use lido_staking_base::helpers::answer::response;
use lido_staking_base::error::distribution::{ContractError, ContractResult};
use lido_staking_base::msg::distribution::{Delegation, IdealDelegation, InstantiateMsg, QueryMsg};
use neutron_sdk::bindings::msg::NeutronMsg;
use neutron_sdk::bindings::query::NeutronQuery;
use neutron_sdk::NeutronResult;

use crate::error::{ContractError, ContractResult};

const CONTRACT_NAME: &str = concat!("crates.io:lido-staking__", env!("CARGO_PKG_NAME"));
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -18,16 +14,14 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let empty_attr: Vec<Attribute> = Vec::new();

Ok(response("instantiate", CONTRACT_NAME, empty_attr))
Ok(Response::new().add_event(Event::new(format!("{}-{}", CONTRACT_NAME, "instantiate"))))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(_deps: Deps<NeutronQuery>, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
match msg {
QueryMsg::CalcDeposit {
deposit,
Expand Down
1 change: 0 additions & 1 deletion contracts/distribution/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod contract;
pub mod error;

#[cfg(test)]
mod tests;
5 changes: 2 additions & 3 deletions contracts/distribution/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use cosmwasm_std::{
testing::{mock_env, MockApi, MockQuerier, MockStorage},
to_json_binary, OwnedDeps, Querier, Uint128,
to_json_binary, Empty, OwnedDeps, Querier, Uint128,
};
use lido_staking_base::msg::distribution::{Delegation, IdealDelegation, QueryMsg};
use neutron_sdk::bindings::query::NeutronQuery;
use std::marker::PhantomData;

fn mock_dependencies<Q: Querier + Default>() -> OwnedDeps<MockStorage, MockApi, Q, NeutronQuery> {
fn mock_dependencies<Q: Querier + Default>() -> OwnedDeps<MockStorage, MockApi, Q, Empty> {
OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
Expand Down
93 changes: 91 additions & 2 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ use cw2::set_contract_version;
use lido_staking_base::{
helpers::answer::response,
msg::core::{ExecuteMsg as CoreExecuteMsg, InstantiateMsg as CoreInstantiateMsg},
msg::distribution::InstantiateMsg as DistributionInstantiateMsg,
msg::strategy::InstantiateMsg as StrategyInstantiateMsg,
msg::token::{
ConfigResponse as TokenConfigResponse, InstantiateMsg as TokenInstantiateMsg,
QueryMsg as TokenQueryMsg,
},
msg::validatorset::InstantiateMsg as ValidatorsSetInstantiateMsg,
msg::withdrawal_manager::InstantiateMsg as WithdrawalManagerInstantiateMsg,
msg::withdrawal_voucher::InstantiateMsg as WithdrawalVoucherInstantiateMsg,
};
Expand Down Expand Up @@ -44,6 +47,9 @@ pub fn instantiate(
core_code_id: msg.core_code_id,
withdrawal_voucher_code_id: msg.withdrawal_voucher_code_id,
withdrawal_manager_code_id: msg.withdrawal_manager_code_id,
strategy_code_id: msg.strategy_code_id,
validators_set_code_id: msg.validators_set_code_id,
distribution_code_id: msg.distribution_code_id,
owner: info.sender.to_string(),
subdenom: msg.subdenom.to_string(),
},
Expand All @@ -61,6 +67,12 @@ pub fn instantiate(
"withdrawal_manager_code_id",
msg.withdrawal_manager_code_id.to_string(),
),
attr("strategy_code_id", msg.strategy_code_id.to_string()),
attr(
"validators_set_code_id",
msg.validators_set_code_id.to_string(),
),
attr("distribution_code_id", msg.distribution_code_id.to_string()),
attr("owner", info.sender),
attr("subdenom", msg.subdenom),
];
Expand Down Expand Up @@ -109,6 +121,12 @@ fn execute_init(
get_code_checksum(deps.as_ref(), config.withdrawal_voucher_code_id)?;
let withdrawal_manager_contract_checksum =
get_code_checksum(deps.as_ref(), config.withdrawal_manager_code_id)?;
let strategy_contract_checksum = get_code_checksum(deps.as_ref(), config.strategy_code_id)?;
let validators_set_contract_checksum =
get_code_checksum(deps.as_ref(), config.validators_set_code_id)?;
let distribution_contract_checksum =
get_code_checksum(deps.as_ref(), config.distribution_code_id)?;

let salt = config.salt.as_bytes();

let token_address =
Expand All @@ -117,6 +135,7 @@ fn execute_init(
let core_address =
instantiate2_address(&core_contract_checksum, &canonical_self_address, salt)?;
attrs.push(attr("core_address", core_address.to_string()));

let withdrawal_voucher_address = instantiate2_address(
&withdrawal_voucher_contract_checksum,
&canonical_self_address,
Expand All @@ -126,6 +145,7 @@ fn execute_init(
"withdrawal_voucher_address",
withdrawal_voucher_address.to_string(),
));

let withdrawal_manager_address = instantiate2_address(
&withdrawal_manager_contract_checksum,
&canonical_self_address,
Expand All @@ -136,6 +156,35 @@ fn execute_init(
withdrawal_manager_address.to_string(),
));

let strategy_address =
instantiate2_address(&strategy_contract_checksum, &canonical_self_address, salt)?;
attrs.push(attr("strategy_address", strategy_address.to_string()));

let validators_set_address = instantiate2_address(
&validators_set_contract_checksum,
&canonical_self_address,
salt,
)?;
attrs.push(attr(
"validators_set_address",
validators_set_address.to_string(),
));

attrs.push(attr(
"withdrawal_voucher_address",
withdrawal_voucher_address.to_string(),
));

let distribution_address = instantiate2_address(
&distribution_contract_checksum,
&canonical_self_address,
salt,
)?;
attrs.push(attr(
"distribution_address",
distribution_address.to_string(),
));

let core_contract = deps.api.addr_humanize(&core_address)?.to_string();
let token_contract = deps.api.addr_humanize(&token_address)?.to_string();
let withdrawal_voucher_contract = deps
Expand All @@ -146,11 +195,18 @@ fn execute_init(
.api
.addr_humanize(&withdrawal_manager_address)?
.to_string();
let strategy_contract = deps.api.addr_humanize(&strategy_address)?.to_string();
let validators_set_contract = deps.api.addr_humanize(&validators_set_address)?.to_string();
let distribution_contract = deps.api.addr_humanize(&distribution_address)?.to_string();

let state = State {
token_contract: token_contract.to_string(),
core_contract: core_contract.to_string(),
withdrawal_voucher_contract: withdrawal_voucher_contract.to_string(),
withdrawal_manager_contract: withdrawal_manager_contract.to_string(),
strategy_contract: strategy_contract.to_string(),
validators_set_contract: validators_set_contract.to_string(),
distribution_contract: distribution_contract.to_string(),
};

STATE.save(deps.storage, &state)?;
Expand All @@ -166,14 +222,47 @@ fn execute_init(
funds: vec![],
salt: Binary::from(salt),
}),
CosmosMsg::Wasm(WasmMsg::Instantiate2 {
admin: Some(env.contract.address.to_string()),
code_id: config.validators_set_code_id,
label: "validators set".to_string(),
msg: to_json_binary(&ValidatorsSetInstantiateMsg {
stats_contract: "neutron1x69dz0c0emw8m2c6kp5v6c08kgjxmu30f4a8w5".to_string(), //FIXME: mock address, replace with real one
core: env.contract.address.to_string(),
})?,
funds: vec![],
salt: Binary::from(salt),
}),
CosmosMsg::Wasm(WasmMsg::Instantiate2 {
admin: Some(env.contract.address.to_string()),
code_id: config.distribution_code_id,
label: "distribution".to_string(),
msg: to_json_binary(&DistributionInstantiateMsg {})?,
funds: vec![],
salt: Binary::from(salt),
}),
CosmosMsg::Wasm(WasmMsg::Instantiate2 {
admin: Some(env.contract.address.to_string()),
code_id: config.strategy_code_id,
label: "strategy".to_string(),
msg: to_json_binary(&StrategyInstantiateMsg {
core_address: env.contract.address.to_string(),
puppeteer_address: "neutron1x69dz0c0emw8m2c6kp5v6c08kgjxmu30f4a8w5".to_string(), //FIXME: mock address, replace with real one
validator_set_address: validators_set_contract.to_string(),
distribution_address: distribution_contract.to_string(),
denom: "uatom".to_string(),
})?,
funds: vec![],
salt: Binary::from(salt),
}),
CosmosMsg::Wasm(WasmMsg::Instantiate2 {
admin: Some(env.contract.address.to_string()),
code_id: config.core_code_id,
label: get_contract_label("core"),
msg: to_json_binary(&CoreInstantiateMsg {
token_contract: token_contract.to_string(),
puppeteer_contract: "".to_string(),
strategy_contract: "".to_string(),
puppeteer_contract: "neutron1x69dz0c0emw8m2c6kp5v6c08kgjxmu30f4a8w5".to_string(), //FIXME: mock address, replace with real one
strategy_contract: strategy_contract.to_string(),
withdrawal_voucher_contract: withdrawal_voucher_contract.to_string(),
withdrawal_manager_contract: withdrawal_manager_contract.to_string(),
base_denom: base_denom.to_string(),
Expand Down
3 changes: 3 additions & 0 deletions contracts/factory/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub struct InstantiateMsg {
pub core_code_id: u64,
pub withdrawal_voucher_code_id: u64,
pub withdrawal_manager_code_id: u64,
pub strategy_code_id: u64,
pub validators_set_code_id: u64,
pub distribution_code_id: u64,
pub salt: String,
pub subdenom: String,
}
Expand Down
6 changes: 6 additions & 0 deletions contracts/factory/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub struct Config {
pub core_code_id: u64,
pub withdrawal_voucher_code_id: u64,
pub withdrawal_manager_code_id: u64,
pub strategy_code_id: u64,
pub validators_set_code_id: u64,
pub distribution_code_id: u64,
pub owner: String,
pub salt: String,
pub subdenom: String,
Expand All @@ -18,6 +21,9 @@ pub struct State {
pub core_contract: String,
pub withdrawal_voucher_contract: String,
pub withdrawal_manager_contract: String,
pub strategy_contract: String,
pub validators_set_contract: String,
pub distribution_contract: String,
}

pub const CONFIG: Item<Config> = Item::new("config");
Expand Down
4 changes: 2 additions & 2 deletions contracts/interchain-interceptor-authz/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> NeutronResult<Response> {
) -> ContractResult<Response> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let owner = deps.api.addr_validate(&msg.owner)?;
Expand All @@ -62,7 +62,7 @@ pub fn instantiate(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
InterchainInterceptor::default().query(deps, env, msg)
}

Expand Down
1 change: 1 addition & 0 deletions contracts/interchain-interceptor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ serde = { workspace = true }
serde-json-wasm = { workspace = true }

lido-interchain-interceptor-base = { workspace = true }
lido-staking-base = { workspace = true }

[dev-dependencies]
cosmwasm-storage = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cosmwasm_schema::write_api;

use lido_interchain_interceptor::{
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg},
state::QueryMsg,
use lido_staking_base::{
msg::puppeteer::{ExecuteMsg, InstantiateMsg, MigrateMsg},
state::puppeteer::QueryMsg,
};

fn main() {
Expand Down
10 changes: 6 additions & 4 deletions contracts/interchain-interceptor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use cosmos_sdk_proto::cosmos::{
use cosmwasm_std::{entry_point, to_json_vec, CosmosMsg, Deps, Reply, StdError, SubMsg, Uint128};
use cosmwasm_std::{Binary, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
use lido_staking_base::{
msg::puppeteer::{ExecuteMsg, InstantiateMsg, MigrateMsg, Transaction},
state::puppeteer::Config,
};
use neutron_sdk::{
bindings::{
msg::{IbcFee, NeutronMsg},
Expand All @@ -28,13 +32,11 @@ use lido_interchain_interceptor_base::{
use prost::Message;

use crate::{
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, Transaction},
proto::cosmos::base::v1beta1::Coin as ProtoCoin,
proto::liquidstaking::staking::v1beta1::{
MsgBeginRedelegate, MsgRedeemTokensforShares as MsgRedeemTokensForShares,
MsgTokenizeShares, MsgTokenizeSharesResponse,
},
state::Config,
};

pub type InterchainInterceptor<'a> = InterchainIntercaptorBase<'a, Config, Transaction>;
Expand All @@ -49,7 +51,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> NeutronResult<Response> {
) -> ContractResult<Response> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let owner = deps.api.addr_validate(&msg.owner)?;
Expand All @@ -66,7 +68,7 @@ pub fn instantiate(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
InterchainInterceptor::default().query(deps, env, msg)
}

Expand Down
2 changes: 0 additions & 2 deletions contracts/interchain-interceptor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pub mod contract;
pub mod msg;
pub mod proto;
pub mod state;
6 changes: 6 additions & 0 deletions contracts/strategy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ serde = { workspace = true }
serde-json-wasm = { workspace = true }

neutron-sdk = { workspace = true }
lido-staking-base = { workspace = true }
lido-interchain-interceptor-base = { workspace = true }

lido-distribution = { path = "../distribution", default-features = false }
lido-interchain-interceptor = { path = "../interchain-interceptor", default-features = false }
lido-validators-set = { path = "../validators-set", default-features = false }

[dev-dependencies]
cosmwasm-storage = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions contracts/strategy/src/bin/lido-strategy-schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_schema::write_api;
use lido_strategy::{
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg},
state::QueryMsg,
use lido_staking_base::msg::{
distribution::QueryMsg,
strategy::{ExecuteMsg, InstantiateMsg, MigrateMsg},
};

fn main() {
Expand Down
Loading

0 comments on commit 9bc9990

Please sign in to comment.