From b87d7bcc0ed80a61c637da4178b3acbe7b0adabf Mon Sep 17 00:00:00 2001 From: Lars Lubkoll <11710767+lubkoll@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:12:51 +0200 Subject: [PATCH] Add owner --- smart-contracts/Cargo.lock | 1 + .../contracts/babylon-vault/Cargo.toml | 1 + .../babylon-vault/schema/babylon-vault.json | 115 +++++++++++++++++- .../babylon-vault/schema/raw/execute.json | 63 +++++++++- .../babylon-vault/schema/raw/instantiate.json | 8 ++ .../babylon-vault/schema/raw/query.json | 13 ++ .../schema/raw/response_to_owner.json | 31 +++++ .../contracts/babylon-vault/src/contract.rs | 24 +++- .../contracts/babylon-vault/src/error.rs | 4 + .../contracts/babylon-vault/src/lib.rs | 1 + .../contracts/babylon-vault/src/msg.rs | 8 +- .../contracts/babylon-vault/src/state.rs | 3 + .../babylon-vault/src/tests/instantiate.rs | 10 +- .../babylon-vault/src/tests/setup.rs | 1 + 14 files changed, 272 insertions(+), 11 deletions(-) create mode 100644 smart-contracts/contracts/babylon-vault/schema/raw/response_to_owner.json create mode 100644 smart-contracts/contracts/babylon-vault/src/state.rs diff --git a/smart-contracts/Cargo.lock b/smart-contracts/Cargo.lock index 7385f1b4a..3a23c67af 100644 --- a/smart-contracts/Cargo.lock +++ b/smart-contracts/Cargo.lock @@ -1407,6 +1407,7 @@ dependencies = [ "cw-orch 0.23.0", "cw-storage-plus 1.2.0", "cw2 1.1.2", + "mars-owner", "thiserror", ] diff --git a/smart-contracts/contracts/babylon-vault/Cargo.toml b/smart-contracts/contracts/babylon-vault/Cargo.toml index 9813b9344..d0d841455 100644 --- a/smart-contracts/contracts/babylon-vault/Cargo.toml +++ b/smart-contracts/contracts/babylon-vault/Cargo.toml @@ -30,3 +30,4 @@ cw-storage-plus = { workspace = true } thiserror = { workspace = true } cw2 = { workspace = true } cw-orch = { workspace = true } +mars-owner = { workspace = true } diff --git a/smart-contracts/contracts/babylon-vault/schema/babylon-vault.json b/smart-contracts/contracts/babylon-vault/schema/babylon-vault.json index cb0a68945..bf5028774 100644 --- a/smart-contracts/contracts/babylon-vault/schema/babylon-vault.json +++ b/smart-contracts/contracts/babylon-vault/schema/babylon-vault.json @@ -6,6 +6,14 @@ "$schema": "http://json-schema.org/draft-07/schema#", "title": "InstantiateMsg", "type": "object", + "required": [ + "owner" + ], + "properties": { + "owner": { + "type": "string" + } + }, "additionalProperties": false }, "execute": { @@ -71,8 +79,69 @@ } }, "additionalProperties": false + }, + { + "type": "object", + "required": [ + "update_owner" + ], + "properties": { + "update_owner": { + "$ref": "#/definitions/OwnerUpdate" + } + }, + "additionalProperties": false } - ] + ], + "definitions": { + "OwnerUpdate": { + "oneOf": [ + { + "description": "Proposes a new owner to take role. Only current owner can execute.", + "type": "object", + "required": [ + "propose_new_owner" + ], + "properties": { + "propose_new_owner": { + "type": "object", + "required": [ + "proposed" + ], + "properties": { + "proposed": { + "type": "string" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "description": "Clears the currently proposed owner. Only current owner can execute.", + "type": "string", + "enum": [ + "clear_proposed" + ] + }, + { + "description": "Promotes the proposed owner to be the current one. Only the proposed owner can execute.", + "type": "string", + "enum": [ + "accept_proposed" + ] + }, + { + "description": "Throws away the keys to the Owner role forever. Once done, no owner can ever be set later.", + "type": "string", + "enum": [ + "abolish_owner_role" + ] + } + ] + } + } }, "query": { "$schema": "http://json-schema.org/draft-07/schema#", @@ -132,6 +201,19 @@ } }, "additionalProperties": false + }, + { + "type": "object", + "required": [ + "owner" + ], + "properties": { + "owner": { + "type": "object", + "additionalProperties": false + } + }, + "additionalProperties": false } ] }, @@ -150,6 +232,37 @@ "description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```", "type": "string" }, + "owner": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "OwnerResponse", + "description": "Returned from Owner.query()", + "type": "object", + "required": [ + "abolished", + "initialized" + ], + "properties": { + "abolished": { + "type": "boolean" + }, + "initialized": { + "type": "boolean" + }, + "owner": { + "type": [ + "string", + "null" + ] + }, + "proposed": { + "type": [ + "string", + "null" + ] + } + }, + "additionalProperties": false + }, "pending": { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Array_of_Claim", diff --git a/smart-contracts/contracts/babylon-vault/schema/raw/execute.json b/smart-contracts/contracts/babylon-vault/schema/raw/execute.json index b978c2b19..ee6d7301a 100644 --- a/smart-contracts/contracts/babylon-vault/schema/raw/execute.json +++ b/smart-contracts/contracts/babylon-vault/schema/raw/execute.json @@ -61,6 +61,67 @@ } }, "additionalProperties": false + }, + { + "type": "object", + "required": [ + "update_owner" + ], + "properties": { + "update_owner": { + "$ref": "#/definitions/OwnerUpdate" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "OwnerUpdate": { + "oneOf": [ + { + "description": "Proposes a new owner to take role. Only current owner can execute.", + "type": "object", + "required": [ + "propose_new_owner" + ], + "properties": { + "propose_new_owner": { + "type": "object", + "required": [ + "proposed" + ], + "properties": { + "proposed": { + "type": "string" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "description": "Clears the currently proposed owner. Only current owner can execute.", + "type": "string", + "enum": [ + "clear_proposed" + ] + }, + { + "description": "Promotes the proposed owner to be the current one. Only the proposed owner can execute.", + "type": "string", + "enum": [ + "accept_proposed" + ] + }, + { + "description": "Throws away the keys to the Owner role forever. Once done, no owner can ever be set later.", + "type": "string", + "enum": [ + "abolish_owner_role" + ] + } + ] } - ] + } } diff --git a/smart-contracts/contracts/babylon-vault/schema/raw/instantiate.json b/smart-contracts/contracts/babylon-vault/schema/raw/instantiate.json index 1352613d5..66e351258 100644 --- a/smart-contracts/contracts/babylon-vault/schema/raw/instantiate.json +++ b/smart-contracts/contracts/babylon-vault/schema/raw/instantiate.json @@ -2,5 +2,13 @@ "$schema": "http://json-schema.org/draft-07/schema#", "title": "InstantiateMsg", "type": "object", + "required": [ + "owner" + ], + "properties": { + "owner": { + "type": "string" + } + }, "additionalProperties": false } diff --git a/smart-contracts/contracts/babylon-vault/schema/raw/query.json b/smart-contracts/contracts/babylon-vault/schema/raw/query.json index 630717c77..65de6f167 100644 --- a/smart-contracts/contracts/babylon-vault/schema/raw/query.json +++ b/smart-contracts/contracts/babylon-vault/schema/raw/query.json @@ -56,6 +56,19 @@ } }, "additionalProperties": false + }, + { + "type": "object", + "required": [ + "owner" + ], + "properties": { + "owner": { + "type": "object", + "additionalProperties": false + } + }, + "additionalProperties": false } ] } diff --git a/smart-contracts/contracts/babylon-vault/schema/raw/response_to_owner.json b/smart-contracts/contracts/babylon-vault/schema/raw/response_to_owner.json new file mode 100644 index 000000000..06aee45c1 --- /dev/null +++ b/smart-contracts/contracts/babylon-vault/schema/raw/response_to_owner.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "OwnerResponse", + "description": "Returned from Owner.query()", + "type": "object", + "required": [ + "abolished", + "initialized" + ], + "properties": { + "abolished": { + "type": "boolean" + }, + "initialized": { + "type": "boolean" + }, + "owner": { + "type": [ + "string", + "null" + ] + }, + "proposed": { + "type": [ + "string", + "null" + ] + } + }, + "additionalProperties": false +} diff --git a/smart-contracts/contracts/babylon-vault/src/contract.rs b/smart-contracts/contracts/babylon-vault/src/contract.rs index 55981168f..52e70a16c 100644 --- a/smart-contracts/contracts/babylon-vault/src/contract.rs +++ b/smart-contracts/contracts/babylon-vault/src/contract.rs @@ -1,8 +1,9 @@ use crate::error::VaultError; use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; +use crate::state::OWNER; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response}; +use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response}; use cw2::set_contract_version; const CONTRACT_NAME: &str = "quasar:babylon-vault"; @@ -15,18 +16,29 @@ pub fn instantiate( deps: DepsMut, _env: Env, _info: MessageInfo, - _msg: InstantiateMsg, + msg: InstantiateMsg, ) -> VaultResult { + OWNER.initialize( + deps.storage, + deps.api, + mars_owner::OwnerInit::SetInitialOwner { owner: msg.owner }, + )?; set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; Ok(Response::default()) } #[cfg_attr(not(feature = "library"), entry_point)] -pub fn execute(_deps: DepsMut, _env: Env, _info: MessageInfo, _msg: ExecuteMsg) -> VaultResult { - Ok(Response::default()) +pub fn execute(deps: DepsMut, _env: Env, info: MessageInfo, msg: ExecuteMsg) -> VaultResult { + match msg { + ExecuteMsg::UpdateOwner(update) => Ok(OWNER.update(deps, info, update)?), + _ => Ok(Response::default()), + } } #[cfg_attr(not(feature = "library"), entry_point)] -pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> VaultResult { - Ok(Binary::default()) +pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> VaultResult { + match msg { + QueryMsg::Owner {} => Ok(to_json_binary(&OWNER.query(deps.storage)?)?), + _ => Ok(Binary::default()), + } } diff --git a/smart-contracts/contracts/babylon-vault/src/error.rs b/smart-contracts/contracts/babylon-vault/src/error.rs index fde081039..4bb0c41c8 100644 --- a/smart-contracts/contracts/babylon-vault/src/error.rs +++ b/smart-contracts/contracts/babylon-vault/src/error.rs @@ -1,8 +1,12 @@ use cosmwasm_std::StdError; +use mars_owner::OwnerError; use thiserror::Error; #[derive(Error, Debug, PartialEq)] pub enum VaultError { #[error("{0}")] Std(#[from] StdError), + + #[error("{0}")] + Owner(#[from] OwnerError), } diff --git a/smart-contracts/contracts/babylon-vault/src/lib.rs b/smart-contracts/contracts/babylon-vault/src/lib.rs index 575c7c3ce..56f420431 100644 --- a/smart-contracts/contracts/babylon-vault/src/lib.rs +++ b/smart-contracts/contracts/babylon-vault/src/lib.rs @@ -1,6 +1,7 @@ pub mod contract; mod error; pub mod msg; +pub mod state; #[cfg(test)] mod tests; diff --git a/smart-contracts/contracts/babylon-vault/src/msg.rs b/smart-contracts/contracts/babylon-vault/src/msg.rs index 9e04dba9f..2788af57f 100644 --- a/smart-contracts/contracts/babylon-vault/src/msg.rs +++ b/smart-contracts/contracts/babylon-vault/src/msg.rs @@ -1,8 +1,11 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::{Timestamp, Uint128}; +use mars_owner::{OwnerResponse, OwnerUpdate}; #[cw_serde] -pub struct InstantiateMsg {} +pub struct InstantiateMsg { + pub owner: String, +} #[cw_serde] #[derive(cw_orch::ExecuteFns)] @@ -16,6 +19,7 @@ pub enum ExecuteMsg { RegisterLst { denom: String, }, + UpdateOwner(OwnerUpdate), } #[cw_serde] @@ -33,4 +37,6 @@ pub enum QueryMsg { Claimable { address: String }, #[returns(Uint128)] BalanceInUnderlying {}, + #[returns(OwnerResponse)] + Owner {}, } diff --git a/smart-contracts/contracts/babylon-vault/src/state.rs b/smart-contracts/contracts/babylon-vault/src/state.rs new file mode 100644 index 000000000..0c0e31ebd --- /dev/null +++ b/smart-contracts/contracts/babylon-vault/src/state.rs @@ -0,0 +1,3 @@ +use mars_owner::Owner; + +pub const OWNER: Owner = Owner::new("owner"); diff --git a/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs b/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs index cc07ff296..4ee989bf8 100644 --- a/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs +++ b/smart-contracts/contracts/babylon-vault/src/tests/instantiate.rs @@ -1,5 +1,5 @@ use crate::msg::InstantiateMsg; -use crate::tests::setup::create_test_vault; +use crate::tests::setup::{create_test_vault, OWNER}; use cosmwasm_std::Event; use cw_orch::contract::interface_traits::CwOrchInstantiate; @@ -8,7 +8,13 @@ fn test_instantiate() { let env = create_test_vault(); let vault = env.vault; - let result = vault.instantiate(&InstantiateMsg {}, None, None); + let result = vault.instantiate( + &InstantiateMsg { + owner: OWNER.to_string(), + }, + None, + None, + ); assert!(result.is_ok()); let response = result.unwrap(); assert_eq!(response.events.len(), 1); diff --git a/smart-contracts/contracts/babylon-vault/src/tests/setup.rs b/smart-contracts/contracts/babylon-vault/src/tests/setup.rs index dcd4c4b19..d80ce0af8 100644 --- a/smart-contracts/contracts/babylon-vault/src/tests/setup.rs +++ b/smart-contracts/contracts/babylon-vault/src/tests/setup.rs @@ -9,6 +9,7 @@ use cw_orch::prelude::*; use cw_orch::{contract::interface_traits::CwOrchUpload, mock::MockBase}; pub const SENDER: &str = "sender"; +pub const OWNER: &str = "owner"; #[interface(InstantiateMsg, ExecuteMsg, QueryMsg, Empty)] pub struct Vault;