Pick Helpers

Utility functions to easily "pick" deeply nested values or undefined

pickDecoded

pickDecoded picks a decoded value or undefined from DecodedContractResult returned by useCall, and similar hooks.

const get = useCall<number>(flipper, 'get')

pickDecoded(get) // returns number or `undefined`

pickError

pickError picks a DispatchError (thrown in one of many possible pallets) or undefined from DecodedContractResult returned by useCall, and similar hooks.

const get = useCall<number>(flipper, 'get')

pickError(get) // returns a Dispatch Error or `undefined`

pickDecodedError

pickDecodedError picks a DispatchError (thrown in one of many possible pallets) or undefined from DecodedContractResult and returns a string error message. This is a wrapper around decodeError.

const get = useCall<number>(flipper, 'get')

const errMessage = pickDecodedError(
  get,
  flipper, 
  { ContractTrapped: 'This is a custom message. There was a panic in the contract!' }, 
  'Something went wrong... This is a default error message',
);

console.error(errMessage); // string or undefined

// export function pickDecodedError( // call: CallResult | undefined, // contract: Contract, // moduleMessages?: Record<RegistryErrorMethod, string>, // defaultMessage?: string, // ): string | undefined {

pickResultOk

pickResultOk picks the decoded Ok value or undefined if a contract returns a Result<T, E>. Can be used with useCall and similar hooks.

pickResultErr

pickResultErr picks the decoded Err value or undefined if a contract returns a Result<T, E>. Can be used with useCall and similar hooks.

pickCallInfo

pickCallInfo picks the CallInfo or undefined from a message result. Can be used with useCall and similar hooks.

pickTxInfo

pickTxInfo picks the TxInfo or undefined from a tx or dry run result. Can be used with useTx, useDryRun, and similar hooks.

Last updated

Was this helpful?