3

I saw how to handle error from chain extension on smart contract side https://paritytech.github.io/ink-docs/macros-attributes/chain-extension#error-code

But I can't find example and can't understand how to return error from chain extension implementation?

return Err(DispatchError::Other("Unimplemented func_id"))
or 
Ok(RetVal::Converging(1))

What I need return from chain extension call function to have possibility get this error:

#[ink(extension = 1106, returns_result = true)]
fn some_func(subject: PalletAssetBalanceRequest) -> Result([u8; 16], SomeError);

1 Answer 1

4

You need to SCALE encode the Result([u8; 16], SomeError) and write it to the output buffer:

impl ChainExtension for MyExtension {
    fn call(func_id: u32, env: Environment) -> Result<RetVal> {
        // use buffers for input and output (as opposed to primitives)
        let env = env.buf_in_buf_out();

        /// do stuff with the input ...
        let input: PalletAssetBalanceRequest = env.read_as()?;

        /// write your error to the output buffer
        let err: Result([u8; 16], SomeError) = ...;
        env.write(err.encode().as_ref(), false, None);

        // 0 usually means success which means your output buffer will be decoded by ink!.
        // depends on your `FromStatusCode` implementation, though
        Ok(RetVal::Converging(0))
    }   
}

Also, check out the chain extension documentation.

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.