Pick Helpers

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

pickDecoded picks a decoded value or undefined from DecodedContractResult returned by useCallarrow-up-right, and similar hooks.

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

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

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

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

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

pickDecodedErrorarrow-up-right

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 decodeErrorarrow-up-right.

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 {

pickResultOkarrow-up-right

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

pickResultErrarrow-up-right

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

pickCallInfoarrow-up-right

pickCallInfo picks the CallInfo or undefined from a message result. Can be used with useCallarrow-up-right and similar hooks.

pickTxInfo picks the TxInfo or undefined from a tx or dry run result. Can be used with useTxarrow-up-right, useDryRunarrow-up-right, and similar hooks.

Last updated