Skip to content

Commit

Permalink
instantiate and migrate with contract_addr_length
Browse files Browse the repository at this point in the history
  • Loading branch information
taitruong committed May 30, 2024
1 parent be03613 commit 64ec72b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
4 changes: 4 additions & 0 deletions contracts/sg-ics721/src/testing/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ impl Test {
outgoing_proxy,
pauser: admin.clone(),
cw721_admin: admin,
contract_addr_length: None,
},
&[],
"sg-ics721",
Expand Down Expand Up @@ -2350,6 +2351,7 @@ fn test_pause() {
outgoing_proxy: None,
cw721_base_code_id: None,
cw721_admin: None,
contract_addr_length: None,
})
.unwrap(),
}
Expand Down Expand Up @@ -2404,6 +2406,7 @@ fn test_migration() {
outgoing_proxy: None,
cw721_base_code_id: Some(12345678),
cw721_admin: Some(admin.to_string()),
contract_addr_length: None,
})
.unwrap(),
}
Expand Down Expand Up @@ -2432,6 +2435,7 @@ fn test_migration() {
outgoing_proxy: None,
cw721_base_code_id: None,
cw721_admin: Some("".to_string()),
contract_addr_length: None,
})
.unwrap(),
}
Expand Down
34 changes: 33 additions & 1 deletion packages/ics721/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
},
state::{
ClassIdInfo, CollectionData, UniversalAllNftInfoResponse, ADMIN_USED_FOR_CW721,
CLASS_ID_AND_NFT_CONTRACT_INFO, CLASS_ID_TO_CLASS, CW721_CODE_ID,
CLASS_ID_AND_NFT_CONTRACT_INFO, CLASS_ID_TO_CLASS, CONTRACT_ADDR_LENGTH, CW721_CODE_ID,
INCOMING_CLASS_TOKEN_TO_CHANNEL, INCOMING_PROXY, OUTGOING_CLASS_TOKEN_TO_CHANNEL,
OUTGOING_PROXY, PO, TOKEN_METADATA,
},
Expand Down Expand Up @@ -76,6 +76,15 @@ where
.transpose()?,
)?;

let contract_addr_length = msg.contract_addr_length;
if let Some(contract_addr_length) = contract_addr_length {
if let Some(contract_addr_length) = contract_addr_length {
CONTRACT_ADDR_LENGTH.save(deps.storage, &contract_addr_length)?;
} else {
CONTRACT_ADDR_LENGTH.remove(deps.storage);
}
}

Ok(Response::default()
.add_submessages(proxies_instantiate)
.add_attribute("method", "instantiate")
Expand All @@ -84,6 +93,13 @@ where
"cw721_admin",
msg.cw721_admin
.map_or_else(|| "immutable".to_string(), |or| or),
)
.add_attribute(
"contract_addr_length",
contract_addr_length.map_or_else(
|| "none".to_string(),
|or| or.map_or_else(|| "deleted".to_string(), |or| or.to_string()),
),
))
}

Expand Down Expand Up @@ -711,6 +727,7 @@ where
outgoing_proxy,
cw721_base_code_id,
cw721_admin,
contract_addr_length,
} => {
// disables incoming proxy if none is provided!
INCOMING_PROXY.save(
Expand Down Expand Up @@ -741,6 +758,14 @@ where
}
}

if let Some(contract_addr_length) = contract_addr_length {
if let Some(contract_addr_length) = contract_addr_length {
CONTRACT_ADDR_LENGTH.save(deps.storage, &contract_addr_length)?;
} else {
CONTRACT_ADDR_LENGTH.remove(deps.storage);
}
}

let response = Response::default()
.add_attribute("method", "migrate")
.add_attribute("pauser", pauser.map_or_else(|| "none".to_string(), |or| or))
Expand Down Expand Up @@ -768,6 +793,13 @@ where
}
},
),
)
.add_attribute(
"contract_addr_length",
contract_addr_length.map_or_else(
|| "none".to_string(),
|or| or.map_or_else(|| "deleted".to_string(), |or| or.to_string()),
),
);

self.migrate_legacy(deps, response)
Expand Down
4 changes: 4 additions & 0 deletions packages/ics721/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct InstantiateMsg {
pub pauser: Option<String>,
/// The admin address for instantiating new cw721 contracts. In case of None, contract is immutable.
pub cw721_admin: Option<String>,
/// The optional contract address length being used for instantiate2. In case of None, default length is 32 (standard in cosmwasm).
pub contract_addr_length: Option<Option<usize>>,
}

#[cw_serde]
Expand Down Expand Up @@ -212,5 +214,7 @@ pub enum MigrateMsg {
cw721_base_code_id: Option<u64>,
/// The admin address for instantiating new cw721 contracts. In case of "", contract is immutable.
cw721_admin: Option<String>,
/// The optional contract address length being used for instantiate2. In case of None, default length is 32 (standard in cosmwasm).
contract_addr_length: Option<Option<usize>>,
},
}
14 changes: 10 additions & 4 deletions packages/ics721/src/testing/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
Ics721Query,
},
state::{
CollectionData, ADMIN_USED_FOR_CW721, CLASS_ID_TO_CLASS, CW721_CODE_ID, INCOMING_PROXY,
OUTGOING_CLASS_TOKEN_TO_CHANNEL, OUTGOING_PROXY, PO,
CollectionData, ADMIN_USED_FOR_CW721, CLASS_ID_TO_CLASS, CONTRACT_ADDR_LENGTH,
CW721_CODE_ID, INCOMING_PROXY, OUTGOING_CLASS_TOKEN_TO_CHANNEL, OUTGOING_PROXY, PO,
},
utils::get_collection_data,
};
Expand Down Expand Up @@ -504,6 +504,7 @@ fn instantiate_msg(
outgoing_proxy,
pauser: Some(PAUSER_ADDR.to_string()),
cw721_admin: Some(ADMIN_ADDR.to_string()),
contract_addr_length: None,
}
}

Expand All @@ -528,10 +529,11 @@ fn test_instantiate() {
}),
label: "outgoing".to_string(),
};
let msg = instantiate_msg(
let mut msg = instantiate_msg(
Some(incoming_proxy_init_msg.clone()),
Some(outgoing_proxy_init_msg.clone()),
);
msg.contract_addr_length = Some(Some(20));
let response = Ics721Contract {}
.instantiate(deps.as_mut(), env.clone(), info, msg.clone())
.unwrap();
Expand All @@ -550,7 +552,8 @@ fn test_instantiate() {
))
.add_attribute("method", "instantiate")
.add_attribute("cw721_code_id", msg.cw721_base_code_id.to_string())
.add_attribute("cw721_admin", ADMIN_ADDR);
.add_attribute("cw721_admin", ADMIN_ADDR)
.add_attribute("contract_addr_length", "20");
assert_eq!(response, expected_response);
assert_eq!(CW721_CODE_ID.load(&deps.storage).unwrap(), 0);
// incoming and outgoing proxy initially set to None and set later in sub msg
Expand All @@ -565,6 +568,7 @@ fn test_instantiate() {
ADMIN_USED_FOR_CW721.load(&deps.storage).unwrap(),
Some(Addr::unchecked(ADMIN_ADDR.to_string()))
);
assert_eq!(CONTRACT_ADDR_LENGTH.load(&deps.storage).unwrap(), 20);
}

#[test]
Expand All @@ -582,6 +586,7 @@ fn test_migrate() {
incoming_proxy: Some("incoming".to_string()),
cw721_base_code_id: Some(1),
cw721_admin: Some("some_other_admin".to_string()),
contract_addr_length: Some(Some(20)),
};

// before migrate, populate legacy
Expand Down Expand Up @@ -638,6 +643,7 @@ fn test_migrate() {
ADMIN_USED_FOR_CW721.load(&deps.storage).unwrap(),
Some(Addr::unchecked("some_other_admin"))
);
assert_eq!(CONTRACT_ADDR_LENGTH.load(&deps.storage).unwrap(), 20);
let nft_contract_and_class_id_list = query_nft_contracts(deps.as_ref(), None, None).unwrap();
assert_eq!(nft_contract_and_class_id_list.len(), 2);
assert_eq!(nft_contract_and_class_id_list[0].0, CLASS_ID_1);
Expand Down
1 change: 1 addition & 0 deletions packages/ics721/src/testing/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fn do_instantiate(deps: DepsMut, env: Env, sender: &str) -> StdResult<Response>
outgoing_proxy: None,
pauser: None,
cw721_admin: None,
contract_addr_length: None,
};
Ics721Contract::default().instantiate(deps, env, mock_info(sender, &[]), msg)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/ics721/src/testing/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ impl Test {
outgoing_proxy,
pauser: admin.clone(),
cw721_admin: admin.clone(),
contract_addr_length: None,
},
&[],
"ics721-base",
Expand Down Expand Up @@ -2251,6 +2252,7 @@ fn test_pause() {
outgoing_proxy: None,
cw721_base_code_id: None,
cw721_admin: None,
contract_addr_length: None,
})
.unwrap(),
}
Expand Down Expand Up @@ -2306,6 +2308,7 @@ fn test_migration() {
outgoing_proxy: None,
cw721_base_code_id: Some(12345678),
cw721_admin: Some(admin.to_string()),
contract_addr_length: None,
})
.unwrap(),
}
Expand Down Expand Up @@ -2334,6 +2337,7 @@ fn test_migration() {
outgoing_proxy: None,
cw721_base_code_id: None,
cw721_admin: Some("".to_string()),
contract_addr_length: None,
})
.unwrap(),
}
Expand Down

0 comments on commit 64ec72b

Please sign in to comment.