-
Notifications
You must be signed in to change notification settings - Fork 44
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
AppBuilder::new().with_api(): how to override default api? #95
Comments
There is already |
There is even |
Searched and didn't find |
Actually I'm trying to test My Api implementation for So, is it possible to mock instantiate2 as well? This way I can just create another CanonicalAddr with string binary. |
Hi all, It uses These two files show how to replace But for generating contract addresses in Bech32 format, the AddressGenerator is needed. Lines 185 to 230 in beb8ad1
I hope this helps ;-) |
Absolute awsome, that helps! I've basically copied ´MockAddressGenerator let salt = "bad kids".as_bytes();
let canonical_creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
let CodeInfoResponse { checksum, .. } = deps
.querier
.query_wasm_code_info(CW721_CODE_ID.load(deps.storage)?)?;
let canonical_cw721_addr = instantiate2_address(&checksum, &canonical_creator, salt)
.map_err(|_| StdError::generic_err("Could not calculate addr"))?; So for "bad kids", the bytes array is: fn predictable_contract_address(
&self,
api: &dyn Api,
_storage: &mut dyn Storage,
_code_id: u64,
_instance_id: u64,
checksum: &[u8],
creator: &CanonicalAddr,
salt: &[u8],
) -> AnyResult<Addr> {
let canonical_addr = instantiate2_address(checksum, creator, salt)?;
Ok(Addr::unchecked(api.addr_humanize(&canonical_addr)?))
} But interestingly, when contract is instantiated, the salt here is: Which is the string representation for So I had to adjust
fn predictable_contract_address(
&self,
api: &dyn Api,
_storage: &mut dyn Storage,
_code_id: u64,
_instance_id: u64,
checksum: &[u8],
creator: &CanonicalAddr,
salt: &[u8],
) -> Result<Addr> {
// string representation of the salt
let salt_to_string = std::str::from_utf8(salt)?;
// Remove the square brackets and split the string by commas
let parts: Vec<&str> = salt_to_string.trim_matches(|c| c == '[' || c == ']').split(',').collect();
// Convert each part to a u8 and collect them into a Vec<u8>
let salt: Vec<u8> = parts.iter().map(|part| part.trim().parse().unwrap()).collect();
let canonical_addr = instantiate2_address(checksum, creator, &salt)?;
Ok(Addr::unchecked(api.addr_humanize(&canonical_addr)?))
} Tests are now running, but not sure whether above really solves the problem or it is just a hack for root of cause - which I haven't found yet?!? |
Hi @taitruong, unfortunately I do not get the same effect as you with the salt. Could you take a look at this PR #96, maybe I am missing something that is relevant in your test case? Thanks in advance! |
I will - as soon as I'm done with some other tasks. In the meanwhile you can check my code here: https://github.com/public-awesome/cw-ics721/pull/71/files#diff-4aa2f03efbf149b4f455e1d534278edc33135c23cd29d2ad7f8e46b74eaaa39d In |
Hi @taitruong, I guess I have spotted the issue. There is a conversion from [u8] into JSON binary for a salt:
which converts to binary representation of the text, so you get the effect you have described before. Please change it to:
It is enough to convert byte array to After applying the changes, calling: $ cargo clean
$ cargo build
$ cargo clippy
$ cargo test worked fine for me. If this solves the issue, please let me know if I can close it. Thanks! Git diff:
|
YOU r fantastic. Saved my day! Thank u so much, ser! |
Hi, couldn't find an example of how default API could be overridden. Reason is because of this:
MockApi's addr_humanize incompatible with instantiate2_address
CosmWasm/cosmwasm#1648
Problem is in MockApi: https://github.com/CosmWasm/cosmwasm/blob/fde26bd6293aaacb901c3fb5a20de0d89c339b38/packages/vm/src/testing/mock.rs#L132-L139
I know how to override wasm executor using
AppBuilder::new().with_wasm()
- maybewith_api()
is similar to this:The text was updated successfully, but these errors were encountered: