Skip to content

Commit

Permalink
Merge pull request #190 from osmosis-labs/instantiate-vault-with-exis…
Browse files Browse the repository at this point in the history
…ting-local-staking-contract

Allow instantiating vault with existing local staking contract
  • Loading branch information
ethanfrey authored May 30, 2024
2 parents 4e8224b + 5898e98 commit 7c142c2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 21 deletions.
4 changes: 2 additions & 2 deletions contracts/provider/external-staking/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use mesh_native_staking::contract::sv::InstantiateMsg as NativeStakingInstantiat
use mesh_native_staking_proxy::contract::sv::mt::CodeId as NativeStakingProxyCodeId;
use mesh_vault::contract::sv::mt::CodeId as VaultCodeId;
use mesh_vault::contract::VaultContract;
use mesh_vault::msg::StakingInitInfo;
use mesh_vault::msg::{LocalStakingInfo, StakingInitInfo};

use mesh_sync::ValueRange;

Expand Down Expand Up @@ -70,7 +70,7 @@ fn setup<'app>(
};

let vault = vault_code
.instantiate(OSMO.to_owned(), Some(staking_init))
.instantiate(OSMO.to_owned(), Some(LocalStakingInfo::New(staking_init)))
.call(owner)?;

let remote_contact = AuthorizedEndpoint::new("connection-2", "wasm-osmo1foobarbaz");
Expand Down
6 changes: 5 additions & 1 deletion contracts/provider/native-staking-proxy/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use sylvia::multitest::{App, Proxy};

use mesh_vault::contract::sv::mt::VaultContractProxy;
use mesh_vault::contract::VaultContract;
use mesh_vault::msg::LocalStakingInfo;

use crate::contract;
use crate::contract::sv::mt::NativeStakingProxyContractProxy;
Expand Down Expand Up @@ -80,7 +81,10 @@ fn setup<'app>(

// Instantiates vault and staking
let vault = vault_code
.instantiate(OSMO.to_owned(), Some(staking_init_info))
.instantiate(
OSMO.to_owned(),
Some(LocalStakingInfo::New(staking_init_info)),
)
.with_label("Vault")
.call(owner)
.unwrap();
Expand Down
6 changes: 5 additions & 1 deletion contracts/provider/native-staking/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use mesh_native_staking_proxy::contract::sv::mt::{
use mesh_native_staking_proxy::contract::NativeStakingProxyContract;
use mesh_sync::ValueRange;
use mesh_vault::contract::sv::mt::VaultContractProxy;
use mesh_vault::msg::LocalStakingInfo;

use crate::contract;
use crate::contract::sv::mt::NativeStakingContractProxy;
Expand Down Expand Up @@ -275,7 +276,10 @@ fn releasing_proxy_stake() {

// Instantiates vault and staking contracts
let vault = vault_code
.instantiate(OSMO.to_owned(), Some(staking_init_info))
.instantiate(
OSMO.to_owned(),
Some(LocalStakingInfo::New(staking_init_info)),
)
.with_label("Vault")
.call(owner)
.unwrap();
Expand Down
50 changes: 36 additions & 14 deletions contracts/provider/vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::error::ContractError;
use crate::msg::{
AccountClaimsResponse, AccountDetailsResponse, AccountResponse, AllAccountsResponse,
AllAccountsResponseItem, AllActiveExternalStakingResponse, AllTxsResponse, AllTxsResponseItem,
ConfigResponse, LienResponse, StakingInitInfo, TxResponse,
ConfigResponse, LienResponse, LocalStakingInfo, TxResponse,
};
use crate::state::{Config, Lien, LocalStaking, UserInfo};
use crate::txs::Txs;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl VaultContract<'_> {
&self,
ctx: InstantiateCtx,
denom: String,
local_staking: Option<StakingInitInfo>,
local_staking: Option<LocalStakingInfo>,
) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;

Expand All @@ -99,18 +99,40 @@ impl VaultContract<'_> {
set_contract_version(ctx.deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

if let Some(local_staking) = local_staking {
// instantiate local_staking and handle reply
let msg = WasmMsg::Instantiate {
admin: local_staking.admin,
code_id: local_staking.code_id,
msg: local_staking.msg,
funds: vec![],
label: local_staking
.label
.unwrap_or_else(|| "Mesh Security Local Staking".to_string()),
};
let sub_msg = SubMsg::reply_on_success(msg, REPLY_ID_INSTANTIATE);
Ok(Response::new().add_submessage(sub_msg))
match local_staking {
LocalStakingInfo::Existing(exist) => {
let addr = exist.existing;

// Query for max slashing percentage
let query = LocalStakingApiQueryMsg::MaxSlash {};
let SlashRatioResponse {
slash_ratio_dsign, ..
} = ctx.deps.querier.query_wasm_smart(&addr, &query)?;

let local_staking = LocalStaking {
contract: LocalStakingApiHelper(ctx.deps.api.addr_validate(&addr)?),
max_slash: slash_ratio_dsign,
};

self.local_staking
.save(ctx.deps.storage, &Some(local_staking))?;
Ok(Response::new())
}
LocalStakingInfo::New(local_staking) => {
// instantiate local_staking and handle reply
let msg = WasmMsg::Instantiate {
admin: local_staking.admin,
code_id: local_staking.code_id,
msg: local_staking.msg,
funds: vec![],
label: local_staking
.label
.unwrap_or_else(|| "Mesh Security Local Staking".to_string()),
};
let sub_msg = SubMsg::reply_on_success(msg, REPLY_ID_INSTANTIATE);
Ok(Response::new().add_submessage(sub_msg))
}
}
} else {
self.local_staking.save(ctx.deps.storage, &None)?;
Ok(Response::new())
Expand Down
17 changes: 17 additions & 0 deletions contracts/provider/vault/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ pub struct StakingInitInfo {
pub label: Option<String>,
}

/// Information about the local staking contract used during vault instantiation
/// We use untagged so older clients that just used JSON encoded StakingInitInfo
/// will continue to work with no changes.
#[cw_serde]
pub enum LocalStakingInfo {
#[serde(untagged)]
Existing(ExistingContract),
#[serde(untagged)]
New(StakingInitInfo),
}

#[cw_serde]
pub struct ExistingContract {
/// Address of the local staking contract
pub existing: String,
}

#[cw_serde]
pub struct AccountResponse {
// Everything is denom, changing all Uint128 to coin with the same denom seems very inefficient
Expand Down
6 changes: 3 additions & 3 deletions contracts/provider/vault/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::contract::VaultContract;
use crate::error::ContractError;
use crate::msg::{
AccountResponse, AllAccountsResponseItem, AllActiveExternalStakingResponse, LienResponse,
StakingInitInfo,
LocalStakingInfo, StakingInitInfo,
};

const OSMO: &str = "OSMO";
Expand Down Expand Up @@ -137,12 +137,12 @@ fn setup_inner<'app>(
proxy_code_id: native_staking_proxy_code.code_id(),
};

Some(StakingInitInfo {
Some(LocalStakingInfo::New(StakingInitInfo {
admin: None,
code_id: native_staking_code.code_id(),
msg: to_json_binary(&native_staking_inst_msg).unwrap(),
label: None,
})
}))
} else {
None
};
Expand Down

0 comments on commit 7c142c2

Please sign in to comment.