2

I am doing some rust tests in the elrond blockchain.

When I print a token identifier outside of execute_query, my token is well printed. Whereas, it throws an error when I try to print it in the execute_query.

#[test]
fn test_foo() {
    let mut setup = utils::setup(equip_penguin::contract_obj);

    let token = TokenIdentifier::<DebugApi>::from_esdt_bytes(b"ITEM-a1a1a1");
    // works
    println!("{:x?}", token);

    let b_wrapper = &mut setup.blockchain_wrapper;

    let _ = b_wrapper.execute_query(&setup.cf_wrapper, |sc| {
        // throw errors
        println!("{:x?}", token);
    });
}

The error is

thread 'build_url_with_one_item' panicked at 'called `Option::unwrap()` on a `None` value', /home/username/.cargo/registry/src/github.com-1ecc6299db9ec823/elrond-wasm-debug-0.27.4/src/tx_mock/tx_managed_types.rs:38:31

The utils::setup used in the above snippet code from this doc https://docs.elrond.com/developers/developer-reference/rust-testing-framework/

How does this error happens?

Fargerik
  • 210
  • 2
  • 6

1 Answers1

2

Okay, managed types must be declared inside execute_query.

The above code works:

#[test]
fn test_foo() {
    let mut setup = utils::setup(equip_penguin::contract_obj);

    let b_wrapper = &mut setup.blockchain_wrapper;

    let _ = b_wrapper.execute_query(&setup.cf_wrapper, |sc| {
        let token = TokenIdentifier::<DebugApi>::from_esdt_bytes(b"ITEM-a1a1a1");
        println!("{:x?}", token);
    });
}
Fargerik
  • 210
  • 2
  • 6
  • 1
    Have you found the part about being declared inside `execute_query` in some docs, or just by trial & error? I'm asking because I need that `ManagedBuffer` as an `out_val` of a `TxEpectMandos` so I don't have access to a closure (I'm generating a Mandos test using Rust, similar to [this](https://docs.elrond.com/developers/developer-reference/rust-testing-framework/#testing-queries)). I'm new to Elrond so I'm not sure if it's a problem in my mental model of how tests should look, or a bug in `elrond-wasm`. – mccuna Feb 15 '22 at 20:21
  • 1
    Ok, after some more digging, I found the docs part mentioning [this](https://docs.elrond.com/developers/developer-reference/rust-testing-framework/#writing-your-first-test) (it's close to the end of this section). It seems that indeed, a managed type should be created either by using `blockchain_wrapper.execute_in_managed_environment()` or by calling `let _ = DebugApi::dummy();` to initialize the `DebugApi`. P.s.: thank you Martin! – mccuna Feb 15 '22 at 21:23