Skip to content

Commit ac75d48

Browse files
feat(cosmwasm): introduce cw-account
1 parent 4dbbfb1 commit ac75d48

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

cosmwasm/cw-account/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "cw-account"
3+
version = "0.0.0"
4+
5+
authors = { workspace = true }
6+
edition = { workspace = true }
7+
license-file = { workspace = true }
8+
publish = { workspace = true }
9+
repository = { workspace = true }
10+
11+
[lib]
12+
crate-type = ["cdylib", "rlib"]
13+
14+
[dependencies]
15+
cosmwasm-schema = { workspace = true }
16+
cosmwasm-std = { workspace = true, features = ["cosmwasm_1_3"] }
17+
cw-storage-plus = { workspace = true }
18+
embed-commit = { workspace = true }
19+
frissitheto = { workspace = true }
20+
serde = { workspace = true, features = ["derive"] }
21+
thiserror = { workspace = true }
22+
unionlabs = { workspace = true, features = ["schemars"] }
23+
24+
[dev-dependencies]
25+
hex-literal = { workspace = true }
26+
27+
[lints]
28+
workspace = true

cosmwasm/cw-account/src/contract.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use cosmwasm_schema::cw_serde;
2+
use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response, StdResult};
3+
use frissitheto::UpgradeMsg;
4+
5+
use crate::{
6+
error::Error,
7+
msg::{ExecuteMsg, InstantiateMsg},
8+
state::{Config, CONFIG},
9+
};
10+
11+
fn init(deps: DepsMut, _: Env, msg: InstantiateMsg) -> Result<Response, Error> {
12+
CONFIG.save(deps.storage, &Config { owner: msg.owner })?;
13+
Ok(Response::default())
14+
}
15+
16+
#[entry_point]
17+
pub fn instantiate(_: DepsMut, _: Env, _: MessageInfo, _: ()) -> StdResult<Response> {
18+
panic!("this contract cannot be instantiated directly, but must be migrated from an existing instantiated contract.");
19+
}
20+
21+
#[cw_serde]
22+
pub struct MigrateMsg {}
23+
24+
#[entry_point]
25+
pub fn migrate(
26+
deps: DepsMut,
27+
env: Env,
28+
msg: UpgradeMsg<InstantiateMsg, MigrateMsg>,
29+
) -> Result<Response, Error> {
30+
msg.run(
31+
deps,
32+
|deps, init_msg| {
33+
let res = init(deps, env, init_msg)?;
34+
Ok((res, None))
35+
},
36+
|_, _, _| Ok((Response::default(), None)),
37+
)
38+
}
39+
40+
#[entry_point]
41+
pub fn execute(
42+
deps: DepsMut,
43+
_: Env,
44+
info: MessageInfo,
45+
msg: ExecuteMsg,
46+
) -> Result<Response, Error> {
47+
let config = CONFIG.load(deps.storage)?;
48+
if info.sender != config.owner {
49+
return Err(Error::OnlyOwner);
50+
}
51+
Ok(Response::new().add_messages(msg.messages))
52+
}

cosmwasm/cw-account/src/error.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use cosmwasm_std::StdError;
2+
use frissitheto::UpgradeError;
3+
4+
#[derive(Debug, thiserror::Error)]
5+
pub enum Error {
6+
#[error(transparent)]
7+
StdError(#[from] StdError),
8+
9+
#[error("migration error: {0}")]
10+
Migrate(#[from] UpgradeError),
11+
12+
#[error("only the owner can dispatch messages to be executed")]
13+
OnlyOwner,
14+
}

cosmwasm/cw-account/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod contract;
2+
pub mod error;
3+
pub mod msg;
4+
pub mod state;

cosmwasm/cw-account/src/msg.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use cosmwasm_schema::cw_serde;
2+
use cosmwasm_std::{Addr, CosmosMsg};
3+
4+
#[cw_serde]
5+
pub struct InstantiateMsg {
6+
pub owner: Addr,
7+
}
8+
9+
#[cw_serde]
10+
pub struct ExecuteMsg {
11+
pub messages: Vec<CosmosMsg>,
12+
}

cosmwasm/cw-account/src/state.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use cosmwasm_schema::cw_serde;
2+
use cosmwasm_std::Addr;
3+
use cw_storage_plus::Item;
4+
5+
#[cw_serde]
6+
pub struct Config {
7+
pub owner: Addr,
8+
}
9+
10+
pub const CONFIG: Item<Config> = Item::new("config");

0 commit comments

Comments
 (0)