Skip to content

Commit d3a4f54

Browse files
ash-burntBurntValjustinbarry
authored
allow a contract to migrate itself, Require params (#64)
* allow a contract to migrate itself * expose migrate fn in contract.rs * Require params as part of the init message (#66) * Require params as part of the init message * fix JSON parsing issue on metadata params validation --------- Co-authored-by: Burnt Val <val@burnt.com> Co-authored-by: Justin <328965+justinbarry@users.noreply.github.com>
1 parent 363ad9a commit d3a4f54

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

contracts/treasury/src/contract.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::{ContractError, ContractResult};
22
use crate::execute::{revoke_allowance, update_fee_config, update_params, withdraw_coins};
3-
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
3+
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
44
use crate::{execute, query, CONTRACT_NAME, CONTRACT_VERSION};
55
use cosmwasm_std::{
66
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
@@ -27,6 +27,7 @@ pub fn instantiate(
2727
msg.type_urls,
2828
msg.grant_configs,
2929
msg.fee_config,
30+
msg.params,
3031
)
3132
}
3233

@@ -58,6 +59,10 @@ pub fn execute(
5859
ExecuteMsg::RevokeAllowance { grantee } => revoke_allowance(deps, env, info, grantee),
5960
ExecuteMsg::UpdateParams { params } => update_params(deps, info, params),
6061
ExecuteMsg::Withdraw { coins } => withdraw_coins(deps, info, coins),
62+
ExecuteMsg::Migrate {
63+
new_code_id,
64+
migrate_msg,
65+
} => execute::migrate(deps, env, info, new_code_id, migrate_msg),
6166
}
6267
}
6368

@@ -76,3 +81,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
7681
QueryMsg::Params {} => to_json_binary(&query::params(deps.storage)?),
7782
}
7883
}
84+
85+
#[entry_point]
86+
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
87+
// No state migrations performed, just returned a Response
88+
Ok(Response::default())
89+
}

contracts/treasury/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub enum ContractError {
3535

3636
#[error("grant config for {type_url} not found")]
3737
GrantConfigNotFound { type_url: String },
38+
39+
#[error(transparent)]
40+
JsonError(#[from] serde_json::Error),
3841
}
3942

4043
pub type ContractResult<T> = Result<T, ContractError>;

contracts/treasury/src/execute.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cosmos_sdk_proto::Timestamp;
1414
use cosmwasm_std::BankMsg::Send;
1515
use cosmwasm_std::{
1616
Addr, AnyMsg, Binary, Coin, CosmosMsg, DepsMut, Env, Event, MessageInfo, Order, Response,
17+
WasmMsg,
1718
};
1819
use url::Url;
1920

@@ -24,6 +25,7 @@ pub fn init(
2425
type_urls: Vec<String>,
2526
grant_configs: Vec<GrantConfig>,
2627
fee_config: FeeConfig,
28+
params: Params,
2729
) -> ContractResult<Response> {
2830
let treasury_admin = match admin {
2931
None => info.sender,
@@ -41,6 +43,9 @@ pub fn init(
4143

4244
FEE_CONFIG.save(deps.storage, &fee_config)?;
4345

46+
validate_params(&params)?;
47+
PARAMS.save(deps.storage, &params)?;
48+
4449
Ok(Response::new().add_event(
4550
Event::new("create_treasury_instance")
4651
.add_attributes(vec![("admin", treasury_admin.into_string())]),
@@ -112,6 +117,36 @@ pub fn cancel_proposed_admin(deps: DepsMut, info: MessageInfo) -> ContractResult
112117
))
113118
}
114119

120+
pub fn migrate(
121+
deps: DepsMut,
122+
env: Env,
123+
info: MessageInfo,
124+
new_code_id: u64,
125+
migrate_msg: Binary,
126+
) -> ContractResult<Response> {
127+
// Load the current admin
128+
let admin = ADMIN.load(deps.storage)?;
129+
130+
// Check if the caller is the current admin
131+
if admin != info.sender {
132+
return Err(Unauthorized);
133+
}
134+
135+
// this assumes that the contract's wasmd admin is itself
136+
let migrate_msg = CosmosMsg::Wasm(WasmMsg::Migrate {
137+
contract_addr: env.contract.address.into_string(),
138+
new_code_id,
139+
msg: migrate_msg,
140+
});
141+
142+
Ok(Response::new()
143+
.add_event(Event::new("migrate_treasury_instance").add_attributes(vec![
144+
("new_code_id", new_code_id.to_string()),
145+
("admin", admin.to_string()),
146+
]))
147+
.add_message(migrate_msg))
148+
}
149+
115150
pub fn update_grant_config(
116151
deps: DepsMut,
117152
info: MessageInfo,
@@ -177,15 +212,21 @@ pub fn update_fee_config(
177212
Ok(Response::new().add_event(Event::new("updated_treasury_fee_config")))
178213
}
179214

215+
pub fn validate_params(params: &Params) -> ContractResult<()> {
216+
Url::parse(params.redirect_url.as_str())?;
217+
Url::parse(params.icon_url.as_str())?;
218+
serde_json::from_str::<serde_json::Value>(&params.metadata)?;
219+
220+
Ok(())
221+
}
222+
180223
pub fn update_params(deps: DepsMut, info: MessageInfo, params: Params) -> ContractResult<Response> {
181224
let admin = ADMIN.load(deps.storage)?;
182225
if admin != info.sender {
183226
return Err(Unauthorized);
184227
}
185228

186-
Url::parse(params.display_url.as_str())?;
187-
Url::parse(params.redirect_url.as_str())?;
188-
Url::parse(params.icon_url.as_str())?;
229+
validate_params(&params)?;
189230

190231
PARAMS.save(deps.storage, &params)?;
191232

contracts/treasury/src/msg.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct InstantiateMsg {
99
pub type_urls: Vec<String>,
1010
pub grant_configs: Vec<GrantConfig>,
1111
pub fee_config: FeeConfig,
12+
pub params: Params,
1213
}
1314

1415
#[cw_serde]
@@ -41,6 +42,10 @@ pub enum ExecuteMsg {
4142
Withdraw {
4243
coins: Vec<Coin>,
4344
},
45+
Migrate {
46+
new_code_id: u64,
47+
migrate_msg: Binary,
48+
},
4449
}
4550

4651
#[cw_serde]
@@ -65,3 +70,6 @@ pub enum QueryMsg {
6570
#[returns(Binary)]
6671
Params {},
6772
}
73+
74+
#[cw_serde]
75+
pub struct MigrateMsg {}

contracts/treasury/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub const PENDING_ADMIN: Item<Addr> = Item::new("pending_admin");
1414

1515
#[cw_serde]
1616
pub struct Params {
17-
pub display_url: String,
1817
pub redirect_url: String,
1918
pub icon_url: String,
19+
pub metadata: String,
2020
}
2121

2222
pub const PARAMS: Item<Params> = Item::new("params");

0 commit comments

Comments
 (0)