dedotdev / dedot

Delightful JavaScript client for Polkadot & Substrate
https://dedot.dev
Apache License 2.0
30 stars 4 forks source link

breaking(contract): better handling nested results #165

Closed 1cedrus closed 1 month ago

1cedrus commented 2 months ago

This PR addresses a task in #110 which modifies how nested results are handled during a query call via Contract or contract deployment via ContractDeployer.

Now Contract and ContractDeployer will directly return the inner data (the result of the message) and will throw DispatchError or LangError based on the error encountered when unwrapping the result. So users don't have to unwrap 2 nested results before getting into the decoded data.

Before:

import { Contract } from 'dedot/contracts';

const contract = new Contract<FlipperContractApi>(api, flipper, contractAddress);

const result = await contract.query.get({ caller });

// Unwrap 2 nested results for handling `DispatchError` & `LangError`
if (state.isOk && state.data.isOk) {
   const state: boolean = state.data.value;
   console.log(`Flipper boolean value`, state);
}

After:

import { isContractDispatchError, isContractLangError, Contract } from 'dedot/contracts';

const contract = new Contract<FlipperContractApi>(api, flipper, contractAddress);

try {
  const result = await contract.query.get({ caller });
  const state: boolean = result.data;
  console.log(`Flipper boolean value`, state);
} catch (e: any) {
  if (isContractDispatchError(e)) {
     // handle dispatch error
     const { dispatchError } = e;
  }

  if (isContractLangError(e)) {
     // handle langError error
     const { langError } = e;
  }
}