0
  • I am implementing a smart contract with ink!
  • I have defined my own errors in the smart contract like the example below.
  • I don't know how to get the error information in my frontend app using polkadot.js when this error occurs.
  • Can anyone tell me who knows?

smart contract sample:

-- snip --
    #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
    #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
    pub enum OwnErrors {
        /// The Token Does Not Exists.
        OwnErrorIsOccured,
    }
-- snip --
        #[ink(message)]
        pub fn own_error_test(&mut self, account_id:AccountId, token_type:u8) -> OwnResult<()> {
            if self.value == false {
                return Err(OwnErrors::OwnErrorIsOccured);
            }
            self.token_list_for_id.insert(&self.next_id, &TokenInfo{token_address:account_id,token_type:TokenType::GovernanceToken});
            self.next_id = self.next_id + 1;
            Ok(())
        }
-- snip --

frontend sample:

-- snip --
  const own_error_test = async () => {
    const { web3FromSource } = await import("@polkadot/extension-dapp");
    const contract = new ContractPromise(api, abi, contractAddress);
    const performingAccount = accounts[0];
    const injector = await web3FromSource(performingAccount.meta.source);
    const flip = await contract.tx.ownErrorTest(
      { value: 0, gasLimit: gasLimit },
      actingAddress,
      0
    );
    if (injector !== undefined) {
      flip.signAndSend(actingAddress, { signer: injector.signer }, (result) => {
        if (result.status.isInBlock) {
          setResult("in a block");
        } else if (result.status.isFinalized) {
          setResult("finalized");
        }
        console.log("###result: ",result);
      });
    }
  };
-- snip -- 
s.Takahashi
  • 469
  • 4
  • 15

2 Answers2

0

I got the answer on Astar Network's Discord.

https://substrate.stackexchange.com/questions/4247/how-to-get-output-when-calling-a-contract-method-and-signing-it/4261#4261

    pub enum OwnErrors {
        /// The Token Does Not Exists.
        OwnErrorIsOccured,
    }
   if ( output?.toHuman()?.Err == "OwnErrorIsOccured"){
    alert("I can handle errors");
    return;
   }

s.Takahashi
  • 469
  • 4
  • 15
0

This is tricky with polkadotjs. You need to decode the output. What worked for me was using fn decodeOutput at:

https://github.com/scio-labs/use-inkathon/blob/2db45ce134dbabf54ea1d0398b1a79c281730c1b/src/helpers/decodeOutput.ts#L39

Also note that you have to use the new gasLimit setup for ink! 4.0. Details at:

https://github.com/Cardinal-Cryptography/ink4-migration/blob/main/frontend.md

steven c
  • 99
  • 1
  • 2