Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider contract address length for instantiate2 #97

Merged
merged 6 commits into from
May 31, 2024

Conversation

taitruong
Copy link
Collaborator

@taitruong taitruong commented May 30, 2024

This PR adds new CONTRACT_ADDR_LENGTH: Item<usize> = Item::new("n") storage. In most case it can be left empty. For Injective it contains 20 as value.

By default cosmwasm considers addresses with 32 bytes as a constant. Injective on the other hand is of length 20 bytes.
See details here: CosmWasm/cosmwasm#2155

So instantiate2 fails, when transferring from a chain to Injective. Solution is simple - as suggested by Simon: slicing and taking first 20 bytes.

This has been tested on testnet, Stargaze -> Injective, and works like a charm.

Comment on lines 154 to 160
if let Some(len) = CONTRACT_ADDR_LENGTH.may_load(deps.storage)? {
Ok(deps
.api
.addr_humanize(&canonical_cw721_addr[..len].into())?)
} else {
Ok(deps.api.addr_humanize(&canonical_cw721_addr)?)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the main logic for slicing addr

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@humanalgorithm look here, for Injective CONTRACT_ADDR_LENGTH is set with 20. All other chains have no entry in CONTRACT_ADDR_LENGTH storage.

Value is then used for slicing first 20 bytes: canonical_cw721_addr[..len]

@@ -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<usize>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usize has different sizes in dev and production. Better use u32 here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and changed to u32

@taitruong taitruong requested a review from jhernandezb May 31, 2024 09:35
@taitruong taitruong requested a review from Art3miX May 31, 2024 21:15
@taitruong taitruong added this pull request to the merge queue May 31, 2024
Merged via the queue into public-awesome:main with commit 1f39e18 May 31, 2024
1 check passed
@taitruong taitruong deleted the contract_address_length branch June 10, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants