Skip to content

Commit

Permalink
Merge pull request #121 from milkyway-labs/rustcandy/new_oracle_address
Browse files Browse the repository at this point in the history
feat: new oracle address
  • Loading branch information
rustcandy authored Apr 24, 2024
2 parents af214d9 + 1f3d9fc commit bbb14ff
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = ["contracts/*", "packages/*"]

[workspace.package]
version = "0.4.10"
version = "0.4.11"
authors = ["Decento Labs"]
edition = "2021"
rust-version = "1.68.0"
Expand Down
4 changes: 4 additions & 0 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub fn instantiate(
stopped: true, // we start stopped
oracle_contract_address: None, // just for migration. This always needs to be set
oracle_contract_address_v2: None,
oracle_address: None,
};

CONFIG.save(deps.storage, &config)?;
Expand All @@ -115,6 +116,7 @@ pub fn instantiate(
Some(msg.treasury_address),
msg.oracle_contract_address,
msg.oracle_contract_address_v2,
msg.oracle_address,
)?;

// Init State
Expand Down Expand Up @@ -207,6 +209,7 @@ pub fn execute(
treasury_address,
oracle_contract_address,
oracle_contract_address_v2,
oracle_address,
} => update_config(
deps,
env,
Expand All @@ -222,6 +225,7 @@ pub fn execute(
treasury_address,
oracle_contract_address,
oracle_contract_address_v2,
oracle_address,
),
ExecuteMsg::ReceiveRewards {} => receive_rewards(deps, env, info),
ExecuteMsg::ReceiveUnstakedTokens { batch_id } => {
Expand Down
35 changes: 29 additions & 6 deletions contracts/staking/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ fn update_oracle_msgs(deps: Deps, env: Env, config: &Config) -> Result<Vec<Cosmo
funds: vec![]
}.into());

// Post rates to Milkyway Oracle contract
let post_rates_msg = Oracle::PostRates {
purchase_rate: purchase_rate.to_string(),
redemption_rate: redemption_rate.to_string(),
denom: config.liquid_stake_token_denom.clone(),
};

let post_rate_msg_json = serde_json::to_string(&post_rates_msg).unwrap();
messages.push(MsgExecuteContract {
sender: env.contract.address.to_string(),
contract: config.oracle_address.clone().unwrap().to_string(),
msg: post_rate_msg_json.as_bytes().to_vec(),
funds: vec![]
}.into());

Ok(messages)
}

Expand Down Expand Up @@ -744,15 +759,16 @@ pub fn update_config_from_migrate(
) -> ContractResult<Response> {
let mut config: Config = CONFIG.load(deps.storage)?;

// update oracle contract address v2
if msg.oracle_contract_address_v2.is_some() {
let oracle_contract_address_v2 = msg.oracle_contract_address_v2.unwrap();
let address = validate_address(&oracle_contract_address_v2, "osmo")?;
config.oracle_contract_address_v2 = Some(address);
// update oracle contract address v3
if msg.oracle_address.is_some() {
let oracle_address = msg.oracle_address.unwrap();
let address = validate_address(&oracle_address, "osmo")?;
config.oracle_address = Some(address);
}

CONFIG.save(deps.storage, &config)?;

Ok(Response::new().add_attribute("action", "update_oracle_contract_address_v2"))
Ok(Response::new().add_attribute("action", "update_oracle_address"))
}

// Update the config; callable by the owner
Expand All @@ -771,6 +787,7 @@ pub fn update_config(
treasury_address: Option<String>,
oracle_contract_address: Option<String>,
oracle_contract_address_v2: Option<String>,
oracle_address: Option<String>,
) -> ContractResult<Response> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;

Expand Down Expand Up @@ -837,6 +854,12 @@ pub fn update_config(
config.oracle_contract_address_v2 = Some(address);
}

if oracle_address.is_some() {
let oracle_address = oracle_address.unwrap();
let address = validate_address(&oracle_address, "osmo")?;
config.oracle_address = Some(address);
}

CONFIG.save(deps.storage, &config)?;

Ok(Response::new().add_attribute("action", "update_config"))
Expand Down
6 changes: 5 additions & 1 deletion contracts/staking/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct InstantiateMsg {
pub oracle_contract_address: Option<String>,
// The migratable redemption / purchase rate oracle address
pub oracle_contract_address_v2: Option<String>,
// The redemption / purchase rate oracle address
pub oracle_address: Option<String>,
}

#[cw_serde]
Expand Down Expand Up @@ -71,6 +73,7 @@ pub enum ExecuteMsg {
treasury_address: Option<String>,
oracle_contract_address: Option<String>,
oracle_contract_address_v2: Option<String>,
oracle_address: Option<String>,
},
ReceiveRewards {},
ReceiveUnstakedTokens {
Expand Down Expand Up @@ -108,6 +111,7 @@ pub struct ConfigResponse {
pub stopped: bool,
pub oracle_contract_address: String,
pub oracle_contract_address_v2: String,
pub oracle_address: String,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug, Default)]
Expand Down Expand Up @@ -198,7 +202,7 @@ pub enum QueryMsg {

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct MigrateMsg {
pub oracle_contract_address_v2: Option<String>
pub oracle_address: Option<String>
}

#[cw_serde]
Expand Down
45 changes: 5 additions & 40 deletions contracts/staking/src/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,33 @@ pub const ORACLE_PURCHASE_RATE_KEY: &str = "milkTIA_purchase_rate";

#[cw_serde]
pub enum Oracle {
/// Uploads and stores a new metric
PostMetric {
/// Key identifying the metric (e.g. `stuatom_redemption_rate`)
key: String,
/// Value for the metric (e.g. `1.1`)
value: String,
/// Category for the metric(e.g. `redemption_rate`)
/// Helps determine handling of additional context
metric_type: MetricType,
/// Unix timestamp with which the metric was updated on the source chain
update_time: u64,
/// Block height with which the metric was updated on the source chain
block_height: u64,
/// Additional metric-specific attributes
attributes: Option<Binary>,
},
PostRates {
denom: String,
purchase_rate: String,
redemption_rate: String,
},
}

/// The RedemptionRate struct represents the redemption rate of an stToken
#[cw_serde]
pub struct RedemptionRate {
/// stToken denom as an IBC hash, as it appears on the oracle chain
pub denom: String,
/// The redemption rate of the stToken
pub redemption_rate: Decimal,
/// The unix timestamp representing when the redemption rate was last updated
pub update_time: u64,
}

/// The PurchaseRate struct represents the purchase rate of an milkTia
#[cw_serde]
pub struct PurchaseRate {
/// stToken denom as an IBC hash, as it appears on the oracle chain
pub denom: String,
/// The purchase rate of the milkTia
pub purchase_rate: Decimal,
/// The unix timestamp representing when the purchase rate was last updated
pub update_time: u64,
}

/// This contract represents a generic key value store
/// A "metric" is the term for a piece of information stored
/// Each metric has a higher level category that helps inform if any other,
/// metric-specific logic needs to be run
/// i.e. For redemption rates, there is an expected format for the attributes
/// field with additional metadata
#[cw_serde]
pub enum MetricType {
RedemptionRate,
PurchaseRate,
Other(String),
}

/// For use in price oracles, the RedemptionRate metric requires the stToken denom
/// as it appears on the controller chain (e.g. `stuosmo`)
#[cw_serde]
pub struct RedemptionRateAttributes {
pub sttoken_denom: String,
}

/// For use in price oracles, the PurchaseRate metric requires the stToken denom
/// as it appears on the controller chain (e.g. `stuosmo`)
#[cw_serde]
pub struct PurchaseRateAttributes {
pub sttoken_denom: String,
Expand Down
4 changes: 4 additions & 0 deletions contracts/staking/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
.oracle_contract_address_v2
.map(|v| v.to_string())
.unwrap_or_default(),
oracle_address: config
.oracle_address
.map(|v| v.to_string())
.unwrap_or_default(),
};
Ok(res)
}
Expand Down
1 change: 1 addition & 0 deletions contracts/staking/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Config {
pub stopped: bool,
pub oracle_contract_address: Option<Addr>,
pub oracle_contract_address_v2: Option<Addr>,
pub oracle_address: Option<Addr>,
}
// TODO: PENDING - DOCS DEFINE THESE AS MAPS?
// Discuss: Do we want to add or remove any state?
Expand Down
2 changes: 1 addition & 1 deletion contracts/staking/src/tests/ibc_transfer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod ibc_transfer_tests {
]
);
assert_eq!(
result.messages[4],
result.messages[5],
SubMsg {
id: ibc_sub_msg_id.clone(),
msg: <MsgTransfer as Into<CosmosMsg>>::into(MsgTransfer {
Expand Down
5 changes: 5 additions & 0 deletions contracts/staking/src/tests/instantiate_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod tests {
ibc_channel_id: CHANNEL_ID.to_string(),
oracle_contract_address: None,
oracle_contract_address_v2: None,
oracle_address: None,
}
}

Expand Down Expand Up @@ -158,6 +159,7 @@ mod tests {
treasury_address: Some(OSMO3.to_string()),
oracle_contract_address: None,
oracle_contract_address_v2: None,
oracle_address: None,
};

let res = crate::contract::execute(
Expand Down Expand Up @@ -189,6 +191,7 @@ mod tests {
treasury_address: None,
oracle_contract_address: None,
oracle_contract_address_v2: None,
oracle_address: None,
};
let res = crate::contract::execute(
deps.as_mut(),
Expand All @@ -215,6 +218,7 @@ mod tests {
treasury_address: None,
oracle_contract_address: None,
oracle_contract_address_v2: None,
oracle_address: None,
};
let res = crate::contract::execute(
deps.as_mut(),
Expand All @@ -241,6 +245,7 @@ mod tests {
treasury_address: None,
oracle_contract_address: None,
oracle_contract_address_v2: None,
oracle_address: None,
};
let res = crate::contract::execute(
deps.as_mut(),
Expand Down
6 changes: 3 additions & 3 deletions contracts/staking/src/tests/reward_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ mod reward_tests {

assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(res.messages.len(), 4); // transfer message and redemption/purchase rate update
assert_eq!(res.messages[3].reply_on, ReplyOn::Always);
assert_eq!(res.messages.len(), 5); // transfer message and redemption/purchase rate update
assert_eq!(res.messages[4].reply_on, ReplyOn::Always);
assert_eq!(
res.messages[3].msg,
res.messages[4].msg,
CosmosMsg::from(MsgTransfer {
source_channel: CHANNEL_ID.to_string(),
source_port: "transfer".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions contracts/staking/src/tests/stake_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ mod staking_tests {
attr("mint_amount", "1000"),
]
);
assert_eq!(result.messages.len(), 5); // transfer, mint, redemption rate update
assert_eq!(result.messages.len(), 6); // transfer, mint, redemption rate update
assert_eq!(
result.messages[4],
result.messages[5],
SubMsg {
id: ibc_sub_msg_id.clone(),
msg: <MsgTransfer as Into<CosmosMsg>>::into(MsgTransfer {
Expand Down
1 change: 1 addition & 0 deletions contracts/staking/src/tests/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn init() -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
ibc_channel_id: CHANNEL_ID.to_string(),
oracle_contract_address: Some(OSMO3.to_string()),
oracle_contract_address_v2: Some(OSMO4.to_string()),
oracle_address: Some(OSMO4.to_string()),
};
let info = mock_info(OSMO3, &coins(1000, "uosmo"));

Expand Down
2 changes: 1 addition & 1 deletion contracts/staking/src/tests/unstake_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ mod staking_tests {
assert_eq!(attrs[3].value, "650");

let messages = resp.messages;
assert_eq!(messages.len(), 4); // batch submit and redemption/purchase rate update
assert_eq!(messages.len(), 5); // batch submit and redemption/purchase rate update
assert_eq!(
messages[0],
SubMsg {
Expand Down
8 changes: 4 additions & 4 deletions contracts/staking/src/tests/withdraw_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod withdraw_tests {
let res = execute(deps.as_mut(), env.clone(), info, msg.clone());
assert!(res.is_ok());
let messages = res.unwrap().messages;
assert_eq!(messages.len(), 4); // withdraw and redemption/purchase rate update
assert_eq!(messages.len(), 5); // withdraw and redemption/purchase rate update

let msg = QueryMsg::UnstakeRequests {
user: Addr::unchecked("bob"),
Expand Down Expand Up @@ -112,7 +112,7 @@ mod withdraw_tests {
let res = execute(deps.as_mut(), env.clone(), info, msg.clone());
assert!(res.is_ok());
let messages = res.unwrap().messages;
assert_eq!(messages.len(), 4); // withdraw and redemption/purchase rate update
assert_eq!(messages.len(), 5); // withdraw and redemption/purchase rate update

let msg = QueryMsg::UnstakeRequests {
user: Addr::unchecked("tom"),
Expand Down Expand Up @@ -190,7 +190,7 @@ mod withdraw_tests {
let res = execute(deps.as_mut(), env.clone(), info, msg.clone());
assert!(res.is_ok());
let messages = res.unwrap().messages;
assert_eq!(messages.len(), 4); // withdraw and redemption rate update
assert_eq!(messages.len(), 5); // withdraw and redemption rate update

let msg = QueryMsg::UnstakeRequests {
user: Addr::unchecked("bob"),
Expand Down Expand Up @@ -232,7 +232,7 @@ mod withdraw_tests {
let res = execute(deps.as_mut(), env.clone(), info, msg.clone());
assert!(res.is_ok());
let messages = res.unwrap().messages;
assert_eq!(messages.len(), 4); // withdraw and redemption/purchase rate update
assert_eq!(messages.len(), 5); // withdraw and redemption/purchase rate update

let msg = QueryMsg::UnstakeRequests {
user: Addr::unchecked("tom"),
Expand Down

0 comments on commit bbb14ff

Please sign in to comment.