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

Error when running unit tests: Generic error: Error decoding bech32 #66

Open
maxmousse opened this issue Apr 30, 2024 · 4 comments
Open

Comments

@maxmousse
Copy link

Hi everyone,

First, thanks a lot for writing/maintaining this book. This is an awesome learning resource!

While following the book, I found an error when running unit tests. The following block of code

        // TODO: something goes wrong when instantiating the contract with admins
        // Maybe related to this: 
        let addr = app
            .instantiate_contract(
                code_id,
                Addr::unchecked("owner"),
                &InstantiateMsg {
                    admins: vec!["admin1".to_owned()],
                    donation_denom: "eth".to_owned(),
                },
                &[],
                "Contract",
                None,
            )
            .unwrap();

leads to the next error:

--- contract::tests::instantiation stdout ----
thread 'contract::tests::instantiation' panicked at src/contract.rs:178:14:
called `Result::unwrap()` on an `Err` value: Error executing WasmMsg:
  sender: owner
  Instantiate { admin: None, code_id: 1, msg: {"admins":["admin1"],"donation_denom":"eth"}, funds: [], label: "Contract" }

Caused by:
    Generic error: Error decoding bech32

From what I understand, it seems to come from cw_multi_test. They currently have an issue opened that seems to relate to the problem: CosmWasm/cw-multi-test#165

Here is a repository containing the bug: https://github.com/maxmousse/cosmwasm-test/tree/cosmwasm-tuto. Just run cargo test and the error should show.

I'd be happy to submit a pull request when there is a consensus about how to fix this =)

@asher-gh
Copy link

try this

    #[test]
    fn instantiation() {
        let mut app = App::default();

        let code = ContractWrapper::new(execute, instantiate, query);
        let code_id = app.store_code(Box::new(code));

        let addr = app
            .instantiate_contract(
                code_id,
                Addr::unchecked("owner"),
                &InstantiateMsg { admins: vec![] },
                &[],
                "Contract",
                None,
            )
            .unwrap();

        let resp: AdminsListResp = app
            .wrap()
            .query_wasm_smart(addr, &QueryMsg::AdminsList {})
            .unwrap();

        assert_eq!(resp, AdminsListResp { admins: vec![] });

        let admin1 = app.api().addr_make("admin1").to_string();
        let admin2 = app.api().addr_make("admin2").to_string();

        let addr = app
            .instantiate_contract(
                code_id,
                Addr::unchecked("owner"),
                &(InstantiateMsg {
                    admins: vec![admin1.clone(), admin2.clone()],
                }),
                &[],
                "Contract 2",
                None,
            )
            .unwrap();

        let resp: AdminsListResp = app
            .wrap()
            .query_wasm_smart(addr, &QueryMsg::AdminsList {})
            .unwrap();

        assert_eq!(
            resp,
            AdminsListResp {
                admins: vec![Addr::unchecked(admin1), Addr::unchecked(admin2)],
            }
        );
    }

@maxmousse
Copy link
Author

Hey @asher-gh, thank your for your response!

I am a little bit confused to be honest:

  • I ran your example and it worked. If I am not mistaken, the key difference is that you use app.api().addr_make("admin1").to_string() to create the addresses right ?
  • Also I tried to re-run my bugged code sample, and it worked: the problem seems to be random, I am not able to reproduce it consistently.

If you have an explanation, I'd be very interested to understand what goes wrong with my code sample.

Anyway I'll keep using the app.api().addr_make("admin1") as it seems to fix the problem. Thanks a lot for your help!

@asher-gh
Copy link

asher-gh commented May 16, 2024

Yeah, I stumbled upon the same thing. It seems like it has something to do with bech32 address encoding in cw_multi_test.

I found the usage of addr_make in the cw-plus examples and they have this in their tests.

@maxmousse
Copy link
Author

All right I'll have a look at those examples. I also opened an issue in the cw_multi_test repository, if anyone is interested: CosmWasm/cw-multi-test#169

Tell me when there is a consensus about how it should be done, I can submit a PR to update the book examples =)

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

No branches or pull requests

2 participants