CosmWasm / book

CosmWasm book
https://book.cosmwasm.com/
32 stars 23 forks source link

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

Open maxmousse opened 2 months ago

maxmousse commented 2 months ago

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: https://github.com/CosmWasm/cw-multi-test/issues/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 commented 2 months ago

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 commented 2 months ago

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

I am a little bit confused to be honest:

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 commented 2 months ago

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 commented 2 months ago

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: https://github.com/CosmWasm/cw-multi-test/issues/169

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