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.

interface SuccessStructInContract {
  Cool: string;
}

interface SomeErrorEnumInContract {
  NotCool: string;
}

interface Response = { Ok?: SuccessStructInContract, Err?: SomeErrorEnumInContract }

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

pickResultOk(get)?.Cool // returns a SuccessStructInContract object or `undefined`

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.

interface SuccessStructInContract {
  Cool: string;
}

interface SomeErrorEnumInContract {
  NotCool: string;
}

interface Response = { Ok?: SuccessStructInContract, Err?: SomeErrorEnumInContract }

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

pickResultErr(get)?.NotCool // returns a SomeErrorEnumInContract object or `undefined`

pickCallInfo

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

const get = useCall(flipper, 'get')

pickCallInfo(get.result)

// Returns undefined or CallInfo
interface CallInfo {
  gasRequired: Weight;
  gasConsumed: Weight;
  storageDeposit: StorageDeposit;
}

pickTxInfo

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

const flip = useTx(flipper, 'flip')

pickTxInfo(flip.result)

// Returns undefined or Info
interface TxInfo {
  gasRequired: Weight;
  gasConsumed: Weight;
  storageDeposit: StorageDeposit;
  partialFee: Balance;
}

Last updated

Was this helpful?