Skip to content

Commit

Permalink
Upgrade Sylvia to 0.10, fix as many things as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Hartnell committed Apr 28, 2024
1 parent e8a8516 commit 35ddf5b
Show file tree
Hide file tree
Showing 32 changed files with 345 additions and 603 deletions.
253 changes: 58 additions & 195 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mesh-converter = { path = "./contracts/consumer/converter" }
mesh-simple-price-feed = { path = "./contracts/consumer/simple-price-feed" }
mesh-virtual-staking = { path = "./contracts/consumer/virtual-staking" }

sylvia = "0.8.1"
sylvia = "0.10.1"
cosmwasm-schema = "1.5.4"
cosmwasm-std = { version = "1.5.4", features = ["ibc3", "cosmwasm_1_3"] }
cw-storage-plus = "1.2.0"
Expand All @@ -39,7 +39,7 @@ itertools = "0.12.1"

# dev deps
anyhow = "1"
cw-multi-test = "0.16.5"
cw-multi-test = "0.20"
derivative = "2"
test-case = "3.3.1"

Expand Down
28 changes: 12 additions & 16 deletions contracts/consumer/converter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub struct ConverterContract<'a> {

#[cfg_attr(not(feature = "library"), sylvia::entry_points)]
#[contract]
#[error(ContractError)]
#[messages(converter_api as ConverterApi)]
#[sv::error(ContractError)]
#[sv::messages(converter_api as ConverterApi)]
impl ConverterContract<'_> {
pub const fn new() -> Self {
Self {
Expand All @@ -47,7 +47,7 @@ impl ConverterContract<'_> {
///
/// Discount is applied to foreign tokens after adjusting foreign/native price,
/// such that 0.3 discount means foreign assets have 70% of their value
#[msg(instantiate)]
#[sv::msg(instantiate)]
pub fn instantiate(
&self,
ctx: InstantiateCtx,
Expand Down Expand Up @@ -92,7 +92,7 @@ impl ConverterContract<'_> {
Ok(Response::new().add_submessage(init_msg))
}

#[msg(reply)]
#[sv::msg(reply)]
fn reply(&self, ctx: ReplyCtx, reply: Reply) -> Result<Response, ContractError> {
match reply.id {
REPLY_ID_INSTANTIATE => self.reply_init_callback(ctx.deps, reply.result.unwrap()),
Expand All @@ -114,7 +114,7 @@ impl ConverterContract<'_> {

/// This is only used for tests.
/// Ideally we want conditional compilation of these whole methods and the enum variants
#[msg(exec)]
#[sv::msg(exec)]
fn test_stake(
&self,
ctx: ExecCtx,
Expand All @@ -135,7 +135,7 @@ impl ConverterContract<'_> {

/// This is only used for tests.
/// Ideally we want conditional compilation of these whole methods and the enum variants
#[msg(exec)]
#[sv::msg(exec)]
fn test_unstake(
&self,
ctx: ExecCtx,
Expand All @@ -156,7 +156,7 @@ impl ConverterContract<'_> {

/// This is only used for tests.
/// Ideally we want conditional compilation of these whole methods and the enum variants
#[msg(exec)]
#[sv::msg(exec)]
fn test_burn(
&self,
ctx: ExecCtx,
Expand All @@ -175,7 +175,7 @@ impl ConverterContract<'_> {
}
}

#[msg(query)]
#[sv::msg(query)]
fn config(&self, ctx: QueryCtx) -> Result<ConfigResponse, ContractError> {
let config = self.config.load(ctx.deps.storage)?;
let virtual_staking = self.virtual_stake.load(ctx.deps.storage)?.into_string();
Expand All @@ -200,7 +200,7 @@ impl ConverterContract<'_> {
.add_attribute("validator", &validator)
.add_attribute("amount", amount.amount.to_string());

let msg = virtual_staking_api::ExecMsg::Bond { validator, amount };
let msg = virtual_staking_api::sv::ExecMsg::Bond { validator, amount };
let msg = WasmMsg::Execute {
contract_addr: self.virtual_stake.load(deps.storage)?.into(),
msg: to_json_binary(&msg)?,
Expand All @@ -224,7 +224,7 @@ impl ConverterContract<'_> {
.add_attribute("validator", &validator)
.add_attribute("amount", amount.amount.to_string());

let msg = virtual_staking_api::ExecMsg::Unbond { validator, amount };
let msg = virtual_staking_api::sv::ExecMsg::Unbond { validator, amount };
let msg = WasmMsg::Execute {
contract_addr: self.virtual_stake.load(deps.storage)?.into(),
msg: to_json_binary(&msg)?,
Expand All @@ -248,7 +248,7 @@ impl ConverterContract<'_> {
.add_attribute("validators", validators.join(","))
.add_attribute("amount", amount.amount.to_string());

let msg = virtual_staking_api::ExecMsg::Burn {
let msg = virtual_staking_api::sv::ExecMsg::Burn {
validators: validators.to_vec(),
amount,
};
Expand All @@ -272,6 +272,7 @@ impl ConverterContract<'_> {
}
);

// FIXME not sure how to get this to compile with latest sylvia
// get the price value (usage is a bit clunky, need to use trait and cannot chain Remote::new() with .querier())
// also see https://github.com/CosmWasm/sylvia/issues/181 to just store Remote in state
use price_feed_api::Querier;
Expand Down Expand Up @@ -349,14 +350,11 @@ impl ConverterContract<'_> {
}
}

#[contract]
#[messages(converter_api as ConverterApi)]
impl ConverterApi for ConverterContract<'_> {
type Error = ContractError;

/// Rewards tokens (in native staking denom) are sent alongside the message, and should be distributed to all
/// stakers who staked on this validator. This is tracked on the provider, so we send an IBC packet there.
#[msg(exec)]
fn distribute_reward(
&self,
mut ctx: ExecCtx,
Expand All @@ -382,7 +380,6 @@ impl ConverterApi for ConverterContract<'_> {
///
/// info.funds sent along with the message should be the sum of all rewards for all validators,
/// in the native staking denom.
#[msg(exec)]
fn distribute_rewards(
&self,
mut ctx: ExecCtx,
Expand Down Expand Up @@ -422,7 +419,6 @@ impl ConverterApi for ConverterContract<'_> {
///
/// Send validator set additions (entering the active validator set), jailings and tombstonings
/// to the external staking contract on the Consumer via IBC.
#[msg(exec)]
#[allow(clippy::too_many_arguments)]
fn valset_update(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct VirtualStakingMock<'a> {

#[contract]
#[error(ContractError)]
#[messages(virtual_staking_api as VirtualStakingApi)]
#[sv::messages(virtual_staking_api as VirtualStakingApi)]
impl VirtualStakingMock<'_> {
pub const fn new() -> Self {
Self {
Expand All @@ -51,7 +51,7 @@ impl VirtualStakingMock<'_> {
}
}

#[msg(instantiate)]
#[sv::msg(instantiate)]
pub fn instantiate(&self, ctx: InstantiateCtx) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;
let denom = ctx.deps.querier.query_bonded_denom()?;
Expand All @@ -63,15 +63,15 @@ impl VirtualStakingMock<'_> {
Ok(Response::new())
}

#[msg(query)]
#[sv::msg(query)]
fn config(&self, ctx: QueryCtx) -> Result<ConfigResponse, ContractError> {
let cfg = self.config.load(ctx.deps.storage)?;
let denom = cfg.denom;
let converter = cfg.converter.into_string();
Ok(ConfigResponse { denom, converter })
}

#[msg(query)]
#[sv::msg(query)]
fn stake(&self, ctx: QueryCtx, validator: String) -> Result<StakeResponse, ContractError> {
let stake = self
.stake
Expand All @@ -80,7 +80,7 @@ impl VirtualStakingMock<'_> {
Ok(StakeResponse { stake })
}

#[msg(query)]
#[sv::msg(query)]
fn all_stake(&self, ctx: QueryCtx) -> Result<AllStakeResponse, ContractError> {
let stakes = self
.stake
Expand All @@ -107,14 +107,14 @@ pub struct ConfigResponse {
}

#[contract]
#[messages(virtual_staking_api as VirtualStakingApi)]
#[sv::messages(virtual_staking_api as VirtualStakingApi)]
impl VirtualStakingApi for VirtualStakingMock<'_> {
type Error = ContractError;

/// Requests to bond tokens to a validator. This will be actually handled at the next epoch.
/// If the virtual staking module is over the max cap, it will trigger a rebalance.
/// If the max cap is 0, then this will immediately return an error.
#[msg(exec)]
#[sv::msg(exec)]
fn bond(&self, ctx: ExecCtx, validator: String, amount: Coin) -> Result<Response, Self::Error> {
nonpayable(&ctx.info)?;
let cfg = self.config.load(ctx.deps.storage)?;
Expand All @@ -137,7 +137,7 @@ impl VirtualStakingApi for VirtualStakingMock<'_> {
/// Requests to unbond tokens from a validator. This will be actually handled at the next epoch.
/// If the virtual staking module is over the max cap, it will trigger a rebalance in addition to unbond.
/// If the virtual staking contract doesn't have at least amount tokens staked to the given validator, this will return an error.
#[msg(exec)]
#[sv::msg(exec)]
fn unbond(
&self,
ctx: ExecCtx,
Expand Down Expand Up @@ -165,7 +165,7 @@ impl VirtualStakingApi for VirtualStakingMock<'_> {
/// Requests to unbond and burn tokens from a lists of validators (one or more). This will be actually handled at the next epoch.
/// If the virtual staking module is over the max cap, it will trigger a rebalance in addition to unbond.
/// If the virtual staking contract doesn't have at least amount tokens staked over the given validators, this will return an error.
#[msg(exec)]
#[sv::msg(exec)]
fn burn(
&self,
ctx: ExecCtx,
Expand Down
2 changes: 1 addition & 1 deletion contracts/consumer/remote-price-feed/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_schema::write_api;

use mesh_remote_price_feed::contract::{ContractExecMsg, ContractQueryMsg, InstantiateMsg};
use mesh_remote_price_feed::contract::sv::{ContractExecMsg, ContractQueryMsg, InstantiateMsg};

#[cfg(not(tarpaulin_include))]
fn main() {
Expand Down
21 changes: 6 additions & 15 deletions contracts/consumer/remote-price-feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use cosmwasm_std::{entry_point, Decimal, DepsMut, Env, IbcChannel, Response, Tim
use cw2::set_contract_version;
use cw_storage_plus::Item;
use cw_utils::nonpayable;
use mesh_apis::price_feed_api::SudoMsg;
use sylvia::types::{InstantiateCtx, QueryCtx};
use sylvia::types::{InstantiateCtx, QueryCtx, SudoCtx};
use sylvia::{contract, schemars};

use mesh_apis::price_feed_api::{self, PriceFeedApi, PriceResponse};
Expand Down Expand Up @@ -33,8 +32,8 @@ impl Default for RemotePriceFeedContract {

#[cfg_attr(not(feature = "library"), sylvia::entry_points)]
#[contract]
#[error(ContractError)]
#[messages(price_feed_api as PriceFeedApi)]
#[sv::error(ContractError)]
#[sv::messages(price_feed_api as PriceFeedApi)]
impl RemotePriceFeedContract {
pub fn new() -> Self {
Self {
Expand All @@ -50,7 +49,7 @@ impl RemotePriceFeedContract {
}
}

#[msg(instantiate)]
#[sv::msg(instantiate)]
pub fn instantiate(
&self,
mut ctx: InstantiateCtx,
Expand Down Expand Up @@ -83,14 +82,11 @@ impl RemotePriceFeedContract {
}
}

#[contract]
#[messages(price_feed_api as PriceFeedApi)]
impl PriceFeedApi for RemotePriceFeedContract {
type Error = ContractError;

/// Return the price of the foreign token. That is, how many native tokens
/// are needed to buy one foreign token.
#[msg(query)]
fn price(&self, ctx: QueryCtx) -> Result<PriceResponse, Self::Error> {
Ok(self
.price_keeper
Expand All @@ -99,14 +95,9 @@ impl PriceFeedApi for RemotePriceFeedContract {
native_per_foreign: rate,
})?)
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
let contract = RemotePriceFeedContract::new();

match msg {
SudoMsg::HandleEpoch {} => contract.scheduler.trigger(deps, &env),
fn handle_epoch(&self, ctx: SudoCtx) -> Result<Response, Self::Error> {
Ok(self.scheduler.trigger(ctx.deps, &ctx.env)?)
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/consumer/simple-price-feed/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_schema::write_api;

use mesh_simple_price_feed::contract::{ContractExecMsg, ContractQueryMsg, InstantiateMsg};
use mesh_simple_price_feed::contract::sv::{ContractExecMsg, ContractQueryMsg, InstantiateMsg};

#[cfg(not(tarpaulin_include))]
fn main() {
Expand Down
20 changes: 11 additions & 9 deletions contracts/consumer/simple-price-feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::{ensure_eq, Decimal, Response};
use cw2::set_contract_version;
use cw_storage_plus::Item;
use cw_utils::nonpayable;
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx};
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx, SudoCtx};
use sylvia::{contract, schemars};

use mesh_apis::price_feed_api::{self, PriceFeedApi, PriceResponse};
Expand All @@ -20,8 +20,8 @@ pub struct SimplePriceFeedContract<'a> {

#[cfg_attr(not(feature = "library"), sylvia::entry_points)]
#[contract]
#[error(ContractError)]
#[messages(price_feed_api as PriceFeedApi)]
#[sv::error(ContractError)]
#[sv::messages(price_feed_api as PriceFeedApi)]
impl SimplePriceFeedContract<'_> {
pub const fn new() -> Self {
Self {
Expand All @@ -31,7 +31,7 @@ impl SimplePriceFeedContract<'_> {

/// Sets up the contract with an initial price.
/// If the owner is not set in the message, it defaults to info.sender.
#[msg(instantiate)]
#[sv::msg(instantiate)]
pub fn instantiate(
&self,
ctx: InstantiateCtx,
Expand All @@ -53,7 +53,7 @@ impl SimplePriceFeedContract<'_> {
Ok(Response::new())
}

#[msg(exec)]
#[sv::msg(exec)]
fn update_price(
&self,
ctx: ExecCtx,
Expand All @@ -75,7 +75,7 @@ impl SimplePriceFeedContract<'_> {
Ok(Response::new())
}

#[msg(query)]
#[sv::msg(query)]
fn config(&self, ctx: QueryCtx) -> Result<ConfigResponse, ContractError> {
let config = self.config.load(ctx.deps.storage)?;
Ok(ConfigResponse {
Expand All @@ -85,18 +85,20 @@ impl SimplePriceFeedContract<'_> {
}
}

#[contract]
#[messages(price_feed_api as PriceFeedApi)]
impl PriceFeedApi for SimplePriceFeedContract<'_> {
type Error = ContractError;

/// Return the price of the foreign token. That is, how many native tokens
/// are needed to buy one foreign token.
#[msg(query)]
fn price(&self, ctx: QueryCtx) -> Result<PriceResponse, Self::Error> {
let config = self.config.load(ctx.deps.storage)?;
Ok(PriceResponse {
native_per_foreign: config.native_per_foreign,
})
}

/// Nothing needs to be done on the epoch
fn handle_epoch(&self, _ctx: SudoCtx) -> Result<Response, Self::Error> {
Ok(Response::new())
}
}
4 changes: 2 additions & 2 deletions contracts/osmosis-price-provider/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct OsmosisPriceProvider {

#[cfg_attr(not(feature = "library"), sylvia::entry_points)]
#[contract]
#[error(ContractError)]
#[sv::error(ContractError)]
impl OsmosisPriceProvider {
pub const fn new() -> Self {
Self {
Expand All @@ -30,7 +30,7 @@ impl OsmosisPriceProvider {
}
}

#[msg(instantiate)]
#[sv::msg(instantiate)]
pub fn instantiate(
&self,
ctx: InstantiateCtx,
Expand Down
Loading

0 comments on commit 35ddf5b

Please sign in to comment.