terra/token_bridge: fixed wrapped asset ids

The wrapped assets on terra have been migrated to code id 767, but the
config wasn't updated to store this new code id, so newly created
wrapped assets all use an older code id. This patch upgrades all wrapped
assets to 767 and changes the config so future wrapped assets also use
that code id.

Also added a new function `migrate_wrapped_assets` so this task can be
done in the future in a less error-prone way.
This commit is contained in:
Csongor Kiss 2022-03-29 13:59:37 +02:00 committed by Csongor Kiss
parent aea76be327
commit 9c9f5b4b23
1 changed files with 57 additions and 2 deletions

View File

@ -63,6 +63,7 @@ use cosmwasm_std::{
Empty,
Env,
MessageInfo,
Order,
QueryRequest,
Reply,
Response,
@ -122,9 +123,63 @@ pub enum TransferType<A> {
WithPayload { payload: A },
}
/// Migration code that runs the next time the contract is upgraded.
/// This function will contain ephemeral code that we want to run once, and thus
/// can (and should be) safely deleted after the upgrade happened successfully.
///
/// For example, when the code id of the wrapped assets is updated, this
/// function will take care of upgrading all the deployed wrapped asset
/// contracts. See [`migrate_wrapped_assets`].
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
Ok(Response::new())
pub fn migrate(deps: DepsMut, env: Env, _msg: MigrateMsg) -> StdResult<Response> {
// On a previous deployment, the wrapped assets have been migrated to code
// id 767:
// https://finder.terra.money/classic/tx/67e8fcff48eefe11bf6a975e621b6866ba930f9d2a85bc9ac5a70f009ee354c7
// However, that upgrade didn't change the [`wrapped_asset_code_id`] field
// of the config state of this contract (the token bridge), so every wrapped
// asset that's been deployed since by the token bridge still uses the old
// code id. We thus run [`migrate_wrapped_assets`] to upgrade all the
// wrapped assets to 767, and the function also takes care of changing the
// state accordingly. Many of these assets are already running 767, for
// those, this will be a no-op.
let messages = migrate_wrapped_assets(deps, env, 767)?;
let count = messages.len();
// NOTE: After this migrate is done, make sure to change the migrate function to
// ```
// Ok(Response::new())
// ```
Ok(Response::new()
.add_messages(messages)
.add_attribute("migrate", "upgrade cw20 wrappers")
.add_attribute("count", count.to_string()))
}
/// Migrate all wrapped assets to a new code id.
/// This function should be called in [`migrate`].
fn migrate_wrapped_assets(deps: DepsMut, _env: Env, new_code_id: u64) -> StdResult<Vec<CosmosMsg>> {
let bucket = wrapped_asset_address(deps.storage);
// Produce a migrate message for each wrapped asset.
let mut messages = vec![];
for item in bucket.range(None, None, Order::Ascending) {
let contract_address = item?.0;
messages.push(CosmosMsg::Wasm(WasmMsg::Migrate {
contract_addr: deps
.api
.addr_humanize(&contract_address.into())?
.to_string(),
new_code_id,
msg: to_binary(&MigrateMsg {})?,
}));
}
// Update config so future wrapped assets will be deployed with new code id
let mut c = config(deps.storage).load()?;
c.wrapped_asset_code_id = new_code_id;
config(deps.storage).save(&c)?;
Ok(messages)
}
#[cfg_attr(not(feature = "library"), entry_point)]