0

I'm trying to process a result from a contract call using what's here. It works totally fine when I'm trying to get a u32 result from an ink_e2e test. E.g.

// -- Snip --
let number_of_items = client
    .call(&ink_e2e::alice(), &number_of_items_builder)
    .dry_run()
    .await
    .unwrap();
log!("item count is {}", number_of_items.return_value()); // displays correct item count of 1

The data produced from the call is [0, 1, 0, 0, 0] as seen here:

 CallDryRunResult { exec_result: ContractResult { gas_consumed: Weight { ref_time: 1516764649, proof_size: 41800 }, gas_required: Weight { ref_time: 1516764649, proof_size: 58086 }, storage_deposit: StorageDeposit::Charge(0), debug_message: [], result: Ok(ExecReturnValue { flags: (empty), data: [0, 1, 0, 0, 0] }), events: Some([(), (), (), (), (), (), (), (), (), (), ()]) }, _marker: PhantomData<u32> }

and that data is decoded to 1u32. The issue now is, using [0, 1, 0, 0, 0] directly in the scale::Decode method prints 256 and not 1:

let data: Vec<u8> = vec![0, 1, 0, 0, 0];
let num: u32 = scale::Decode::decode(&mut data.as_ref()).unwrap();
log::info!("num {}", num); // displays 256

The question now, why am I getting inconsistent results? What is the return_value method doing differently? And how can I get the above example to produce the expected results?

3
  • are you sure your return type is u32 and not Result<u32>? Result is prepended with 0, so 0, 1, 0, 0, 0 -> Ok(u32) would explain that
    – dastansam
    Commented May 3 at 16:08
  • @dastansam the return type from the contract is u32. Removing the first 0 in the data produces the correct result, as you stated. So I think whether or not the actual contract call returns a Result, the data is always converted to an encoded Result. Thanks
    – Kofi
    Commented May 4 at 7:50
  • great! I wrote an answer with detailed links if someone stumbles upon similar issue in the future.
    – dastansam
    Commented May 4 at 21:16

1 Answer 1

1

return_value() returns the value wrapped in Result and this is why it is prepended with 0 - Ok(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.