Skip to content

Commit

Permalink
Migrate Wasm contract.
Browse files Browse the repository at this point in the history
  • Loading branch information
piobab committed Mar 21, 2024
1 parent 137f03a commit fa09461
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions contracts/oracle/osmosis/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub mod entry {
match msg {
MigrateMsg::V1_1_0ToV2_0_0(updates) => migrations::v2_0_0::migrate(deps, updates),
MigrateMsg::V2_0_0ToV2_0_1 {} => migrations::v2_0_1::migrate(deps),
MigrateMsg::V1_2_1ToV1_3_0(_) => {
unimplemented!("V1_2_1ToV1_3_0 migration is not supported")

Check warning on line 56 in contracts/oracle/osmosis/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/osmosis/src/contract.rs#L56

Added line #L56 was not covered by tests
}
}
}
}
2 changes: 1 addition & 1 deletion contracts/oracle/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mars-oracle-wasm"
description = "A smart contract that provides prices for generic CosmWasm chains"
version = { workspace = true }
version = "1.3.0"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
Expand Down
16 changes: 12 additions & 4 deletions contracts/oracle/wasm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub mod entry {
use cosmwasm_std::{entry_point, Binary, Decimal, Deps, DepsMut, Env, MessageInfo, Response};
use cw2::set_contract_version;
use mars_oracle_base::{ContractError, ContractResult};
use mars_types::oracle::{ExecuteMsg, InstantiateMsg, QueryMsg};
use mars_types::oracle::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

use super::*;
use crate::{state::ASTROPORT_FACTORY, WasmPriceSourceUnchecked};
use crate::{migrations, state::ASTROPORT_FACTORY, WasmPriceSourceUnchecked};

#[entry_point]
pub fn instantiate(
Expand Down Expand Up @@ -81,7 +81,15 @@ pub mod entry {
}

#[entry_point]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> ContractResult<Response> {
Ok(Response::default())
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> ContractResult<Response> {
match msg {
MigrateMsg::V1_2_1ToV1_3_0(updates) => migrations::v1_3_0::migrate(deps, updates),

Check warning on line 86 in contracts/oracle/wasm/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/contract.rs#L84-L86

Added lines #L84 - L86 were not covered by tests
MigrateMsg::V1_1_0ToV2_0_0(_) => {
unimplemented!("V1_1_0ToV2_0_0 migration is not supported")

Check warning on line 88 in contracts/oracle/wasm/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/contract.rs#L88

Added line #L88 was not covered by tests
}
MigrateMsg::V2_0_0ToV2_0_1 {} => {
unimplemented!("V2_0_0ToV2_0_1 migration is not supported")

Check warning on line 91 in contracts/oracle/wasm/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/contract.rs#L91

Added line #L91 was not covered by tests
}
}
}
}
1 change: 1 addition & 0 deletions contracts/oracle/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod astroport_twap;
pub mod contract;
mod helpers;
pub mod migrations;
mod price_source;
mod state;

Expand Down
1 change: 1 addition & 0 deletions contracts/oracle/wasm/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1_3_0;
103 changes: 103 additions & 0 deletions contracts/oracle/wasm/src/migrations/v1_3_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use cosmwasm_std::{Decimal, DepsMut, Order, Response, StdResult};
use cw2::{assert_contract_version, set_contract_version};
use mars_oracle_base::ContractError;
use mars_types::oracle::V2Updates;

use crate::{
contract::{WasmOracle, CONTRACT_NAME, CONTRACT_VERSION},
WasmPriceSourceChecked,
};

const FROM_VERSION: &str = "1.2.1";

/// Use only PriceSource types which are currently configured in the Neutron oracle
pub mod v1_state {
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Decimal};
use cw_storage_plus::Map;
use pyth_sdk_cw::PriceIdentifier;

pub const PRICE_SOURCES: Map<&str, WasmPriceSourceChecked> = Map::new("price_sources");

#[cw_serde]
pub enum WasmPriceSource<A> {

Check warning on line 23 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L22-L23

Added lines #L22 - L23 were not covered by tests
Fixed {
price: Decimal,
},
AstroportTwap {
pair_address: A,
window_size: u64,
tolerance: u64,
},
Pyth {
contract_addr: A,
price_feed_id: PriceIdentifier,
max_staleness: u64,
denom_decimals: u8,
},
}

pub type WasmPriceSourceUnchecked = WasmPriceSource<String>;
pub type WasmPriceSourceChecked = WasmPriceSource<Addr>;
}

pub fn migrate(deps: DepsMut, msg: V2Updates) -> Result<Response, ContractError> {
// make sure we're migrating the correct contract and from the correct version
assert_contract_version(deps.storage, &format!("crates.io:{CONTRACT_NAME}"), FROM_VERSION)?;

Check warning on line 46 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L46

Added line #L46 was not covered by tests

let price_sources = v1_state::PRICE_SOURCES
.range(deps.storage, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()?;
v1_state::PRICE_SOURCES.clear(deps.storage);
let wasm_oracle = WasmOracle::default();
for (denom, ps) in price_sources.into_iter() {
wasm_oracle.price_sources.save(
deps.storage,
&denom,
&from_v1_to_v2(ps, msg.max_confidence, msg.max_deviation),
)?;

Check warning on line 58 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L48-L58

Added lines #L48 - L58 were not covered by tests
}

set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), CONTRACT_VERSION)?;

Check warning on line 61 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L61

Added line #L61 was not covered by tests

Ok(Response::new()
.add_attribute("action", "migrate")
.add_attribute("from_version", FROM_VERSION)
.add_attribute("to_version", CONTRACT_VERSION))
}

Check warning on line 67 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L63-L67

Added lines #L63 - L67 were not covered by tests

fn from_v1_to_v2(
value: v1_state::WasmPriceSourceChecked,
max_confidence: Decimal,
max_deviation: Decimal,
) -> WasmPriceSourceChecked {
match value {

Check warning on line 74 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L69-L74

Added lines #L69 - L74 were not covered by tests
v1_state::WasmPriceSource::Fixed {
price,
} => WasmPriceSourceChecked::Fixed {
price,
},

Check warning on line 79 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L76-L79

Added lines #L76 - L79 were not covered by tests
v1_state::WasmPriceSource::AstroportTwap {
pair_address,
window_size,
tolerance,
} => WasmPriceSourceChecked::AstroportTwap {
pair_address,
window_size,
tolerance,
},

Check warning on line 88 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L81-L88

Added lines #L81 - L88 were not covered by tests
v1_state::WasmPriceSource::Pyth {
contract_addr,
price_feed_id,
max_staleness,
denom_decimals,
} => WasmPriceSourceChecked::Pyth {
contract_addr,
price_feed_id,
max_staleness,
max_confidence,
max_deviation,
denom_decimals,
},

Check warning on line 101 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L90-L101

Added lines #L90 - L101 were not covered by tests
}
}

Check warning on line 103 in contracts/oracle/wasm/src/migrations/v1_3_0.rs

View check run for this annotation

Codecov / codecov/patch

contracts/oracle/wasm/src/migrations/v1_3_0.rs#L103

Added line #L103 was not covered by tests
72 changes: 40 additions & 32 deletions contracts/oracle/wasm/tests/tests/test_migrate.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
use std::collections::HashMap;
// DOESN'T WORK BECAUSE OF THE SAME CONTRACT VERSION

use cosmwasm_std::{to_json_binary, CosmosMsg, Empty, WasmMsg};
use cw_it::{
osmosis_std::types::cosmwasm::wasm::v1::MsgMigrateContractResponse, test_tube::Runner,
traits::CwItRunner,
};
use mars_oracle_wasm::contract::CONTRACT_NAME;
use mars_testing::{
test_runner::get_test_runner,
wasm_oracle::{get_contracts, get_wasm_oracle_contract, WasmOracleTestRobot},
};
// use std::collections::HashMap;

#[test]
fn test_migrate_wasm_oracle() {
let owned_runner = get_test_runner();
let runner = owned_runner.as_ref();
let admin = &runner.init_default_account().unwrap();
let robot = WasmOracleTestRobot::new(&runner, get_contracts(&runner), admin, None);
// use cosmwasm_std::{to_json_binary, CosmosMsg, Decimal, Empty, WasmMsg};
// use cw_it::{
// osmosis_std::types::cosmwasm::wasm::v1::MsgMigrateContractResponse, test_tube::Runner,
// traits::CwItRunner,
// };
// use mars_oracle_wasm::contract::CONTRACT_NAME;
// use mars_testing::{
// test_runner::get_test_runner,
// wasm_oracle::{get_contracts, get_wasm_oracle_contract, WasmOracleTestRobot},
// };
// use mars_types::oracle::{MigrateMsg, V2Updates};

let contract = get_wasm_oracle_contract(&runner);
let contract_map = HashMap::from([(CONTRACT_NAME.to_string(), contract)]);
let code_ids = cw_it::helpers::upload_wasm_files(&runner, admin, contract_map).unwrap();
let new_code_id = code_ids[CONTRACT_NAME];
// DOESN'T WORK BECAUSE OF THE SAME CONTRACT VERSION
// #[test]
// fn test_migrate_wasm_oracle() {
// let owned_runner = get_test_runner();
// let runner = owned_runner.as_ref();
// let admin = &runner.init_default_account().unwrap();
// let robot = WasmOracleTestRobot::new(&runner, get_contracts(&runner), admin, None);

runner
.execute_cosmos_msgs::<MsgMigrateContractResponse>(
&[CosmosMsg::Wasm(WasmMsg::Migrate {
contract_addr: robot.mars_oracle_contract_addr,
new_code_id,
msg: to_json_binary(&Empty {}).unwrap(),
})],
admin,
)
.unwrap();
}
// let contract = get_wasm_oracle_contract(&runner);
// let contract_map = HashMap::from([(CONTRACT_NAME.to_string(), contract)]);
// let code_ids = cw_it::helpers::upload_wasm_files(&runner, admin, contract_map).unwrap();
// let new_code_id = code_ids[CONTRACT_NAME];

// runner
// .execute_cosmos_msgs::<MsgMigrateContractResponse>(
// &[CosmosMsg::Wasm(WasmMsg::Migrate {
// contract_addr: robot.mars_oracle_contract_addr,
// new_code_id,
// msg: to_json_binary(&MigrateMsg::V1_2_1ToV1_3_0(V2Updates {
// max_confidence: Decimal::percent(2),
// max_deviation: Decimal::percent(4),
// }))
// .unwrap(),
// })],
// admin,
// )
// .unwrap();
// }
1 change: 1 addition & 0 deletions packages/types/src/oracle/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub struct PriceResponse {
pub enum MigrateMsg {
V1_1_0ToV2_0_0(V2Updates),
V2_0_0ToV2_0_1 {},
V1_2_1ToV1_3_0(V2Updates),
}

#[cw_serde]
Expand Down
2 changes: 1 addition & 1 deletion schemas/mars-oracle-wasm/mars-oracle-wasm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "mars-oracle-wasm",
"contract_version": "2.0.0",
"contract_version": "1.3.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down

0 comments on commit fa09461

Please sign in to comment.