fireblocks / fireblocks-sdk-js

Typescript & Javascript SDK for developers using Fireblocks API
https://docs.fireblocks.com/api/swagger-ui/
MIT License
72 stars 69 forks source link

[Feature Request] Expose substatuses in Typescript #178

Open cyrilchapon opened 1 year ago

cyrilchapon commented 1 year ago

Is your feature request related to a problem? Please describe. I've been surprised not to find substatuses Enum or Union in Typescript; and found myself to recode it.

The trouble is now I'll have to follow Fireblocks releases and enrich it over the months

Describe the solution you'd like Some types and runtime exposed to help with substatutes.

Additional context

Here is a proposal :

export const fireblocksSubstatusesPending3rdParty = [
  '3RD_PARTY_PROCESSING',
] as const

export type FireblocksSubstatusPending3rdParty =
  (typeof fireblocksSubstatusesPending3rdParty)[number]

export const isFireblocksSubstatusPending3rdParty = (
  substatus: string,
): substatus is FireblocksSubstatusPending3rdParty =>
  fireblocksSubstatusesPending3rdParty.includes(
    substatus as FireblocksSubstatusPending3rdParty,
  )

export const fireblocksSubstatusesConfirming = [
  '3RD_PARTY_CONFIRMING',
  'PENDING_BLOCKCHAIN_CONFIRMATIONS',
] as const

export type FireblocksSubstatusConfirming =
  (typeof fireblocksSubstatusesConfirming)[number]

export const isFireblocksSubstatusConfirming = (
  substatus: string,
): substatus is FireblocksSubstatusConfirming =>
  fireblocksSubstatusesConfirming.includes(
    substatus as FireblocksSubstatusConfirming,
  )

export type FireblocksSubstatusPending =
  | FireblocksSubstatusPending3rdParty
  | FireblocksSubstatusConfirming

export const isFireblocksSubstatusPending = (
  substatus: string,
): substatus is FireblocksSubstatusPending =>
  isFireblocksSubstatusPending3rdParty(substatus) ||
  isFireblocksSubstatusConfirming(substatus)

export const fireblocksSubstatusesCompleted = [
  '3RD_PARTY_COMPLETED',
  'COMPLETED_BUT_3RD_PARTY_FAILED',
  'COMPLETED_BUT_3RD_PARTY_REJECTED',
  'CONFIRMED',
] as const

export type FireblocksSubstatusCompleted =
  (typeof fireblocksSubstatusesCompleted)[number]

export const isFireblocksSubstatusCompleted = (
  substatus: string,
): substatus is FireblocksSubstatusCompleted =>
  fireblocksSubstatusesCompleted.includes(
    substatus as FireblocksSubstatusCompleted,
  )

export const fireblocksSubstatusesBlocked = ['BLOCKED_BY_POLICY'] as const

export type FireblocksSubstatusBlocked =
  (typeof fireblocksSubstatusesBlocked)[number]

export const isFireblocksSubstatusBlocked = (
  substatus: string,
): substatus is FireblocksSubstatusBlocked =>
  fireblocksSubstatusesBlocked.includes(substatus as FireblocksSubstatusBlocked)

export const fireblocksSubstatusesCancelled = [
  '3RD_PARTY_CANCELLED',
  '3RD_PARTY_REJECTED',
  'CANCELLED_BY_USER',
  'CANCELLED_BY_USER_REQUEST',
  'REJECTED_BY_USER',
] as const

export type FireblocksSubstatusCancelled =
  (typeof fireblocksSubstatusesCancelled)[number]

export const isFireblocksSubstatusCancelled = (
  substatus: string,
): substatus is FireblocksSubstatusCancelled =>
  fireblocksSubstatusesCancelled.includes(
    substatus as FireblocksSubstatusCancelled,
  )

export const fireblocksSubstatusesRejected = [
  'AUTO_FREEZE',
  'FROZEN_MANUALLY',
  'REJECTED_AML_SCREENING',
] as const

export type FireblocksSubstatusRejected =
  (typeof fireblocksSubstatusesRejected)[number]

export const isFireblocksSubstatusRejected = (
  substatus: string,
): substatus is FireblocksSubstatusRejected =>
  fireblocksSubstatusesRejected.includes(
    substatus as FireblocksSubstatusRejected,
  )

export const fireblocksSubstatusesFailedUserInput = [
  'ACTUAL_FEE_TOO_HIGH',
  'ADDRESS_WHITELISTING_SUSPENDED',
  'AMOUNT_TOO_SMALL',
  'AUTHORIZATION_FAILED',
  'AUTHORIZER_NOT_FOUND',
  'ENV_UNSUPPORTED_ASSET',
  'ERROR_UNSUPPORTED_TRANSACTION_TYPE',
  'FAIL_ON_LOW_FEE',
  'GAS_LIMIT_TOO_LOW',
  'GAS_PRICE_TOO_LOW_FOR_RBF',
  'INCOMPLETE_USER_SETUP',
  'INSUFFICIENT_FUNDS',
  'INSUFFICIENT_FUNDS_FOR_FEE',
  'INTEGRATION_SUSPENDED',
  'INVALID_ADDRESS',
  'INVALID_CONTRACT_CALL_DATA',
  'INVALID_FEE_PARAMS',
  'INVALID_NONCE_FOR_RBF',
  'INVALID_TAG_OR_MEMO',
  'INVALID_UNMANAGED_WALLET',
  'MAX_FEE_EXCEEDED',
  'MISSING_TAG_OR_MEMO',
  'NEED_MORE_TO_CREATE_DESTINATION',
  'NO_MORE_PREPROCESSED_INDEXES',
  'NON_EXISTING_ACCOUNT_NAME',
  'RAW_MSG_EMPTY_OR_INVALID',
  'RAW_MSG_LEN_INVALID',
  'TOO_MANY_INPUTS',
  'UNAUTHORISED_DEVICE',
  'UNAUTHORISED_USER',
  'UNALLOWED_RAW_PARAM_COMBINATION',
  'UNSUPPORTED_OPERATION',
  'UNSUPPORTED_TRANSACTION_TYPE',
  'ZERO_BALANCE_IN_PERMANENT_ADDRESS',
] as const

export type FireblocksSubstatusFailedUserInput =
  (typeof fireblocksSubstatusesFailedUserInput)[number]

export const isFireblocksSubstatusFailedUserInput = (
  substatus: string,
): substatus is FireblocksSubstatusFailedUserInput =>
  fireblocksSubstatusesFailedUserInput.includes(
    substatus as FireblocksSubstatusFailedUserInput,
  )

export const fireblocksSubstatusesFailedSystemIssue = [
  'CONNECTIVITY_ERROR',
  'ERROR_ASYNC_TX_IN_FLIGHT',
  'INTERNAL_ERROR',
  'INVALID_NONCE_TOO_HIGH',
  'INVALID_NONCE_TOO_LOW',
  'INVALID_ROUTING_DESTINATION',
  'LOCKING_NONCE_ACCOUNT_TIMEOUT',
  'NETWORK_ROUTING_MISMATCH',
  'NONCE_ALLOCATION_FAILED',
  'RESOURCE_ALREADY_EXISTS',
  'SIGNER_NOT_FOUND',
  'SIGNING_ERROR',
  'TIMEOUT',
  'TX_OUTDATED',
  'UNKNOWN_ERROR',
  'UNSUPPORTED_MEDIA_TYPE',
  'VAULT_WALLET_NOT_READY',
] as const

export type FireblocksSubstatusFailedSystemIssue =
  (typeof fireblocksSubstatusesFailedSystemIssue)[number]

export const isFireblocksSubstatusFailedSystemIssue = (
  substatus: string,
): substatus is FireblocksSubstatusFailedSystemIssue =>
  fireblocksSubstatusesFailedSystemIssue.includes(
    substatus as FireblocksSubstatusFailedSystemIssue,
  )

export const fireblocksSubstatusesFailedThirdParty = [
  'ADDRESS_NOT_WHITELISTED',
  'API_KEY_MISMATCH',
  'ASSET_NOT_ENABLED_ON_DESTINATION',
  'DEST_TYPE_NOT_SUPPORTED',
  'EXCEEDED_DECIMAL_PRECISION',
  'EXCHANGE_CONFIGURATION_MISMATCH',
  'EXCHANGE_VERSION_INCOMPATIBLE',
  'INVALID_EXCHANGE_ACCOUNT',
  'METHOD_NOT_ALLOWED',
  'NON_EXISTENT_AUTO_ACCOUNT',
  'ON_PREMISE_CONNECTIVITY_ERROR',
  'PEER_ACCOUNT_DOES_NOT_EXIST',
  'THIRD_PARTY_MISSING_ACCOUNT',
  'UNAUTHORISED_IP_WHITELISTING',
  'UNAUTHORISED_MISSING_CREDENTIALS',
  'UNAUTHORISED_MISSING_PERMISSION',
  'UNAUTHORISED_OTP_FAILED',
  'WITHDRAW_LIMIT',
] as const

export type FireblocksSubstatusFailedThirdParty =
  (typeof fireblocksSubstatusesFailedThirdParty)[number]

export const isFireblocksSubstatusFailedThirdParty = (
  substatus: string,
): substatus is FireblocksSubstatusFailedThirdParty =>
  fireblocksSubstatusesFailedThirdParty.includes(
    substatus as FireblocksSubstatusFailedThirdParty,
  )

export const fireblocksSubstatusesFailedBlockchain = [
  'DOUBLE_SPENDING',
  'DROPPED_BY_BLOCKCHAIN',
  'INSUFFICIENT_RESERVED_FUNDING',
  'INVALID_SIGNATURE',
  'PARTIALLY_FAILED',
  'POWERUP_SUGGESTION_FAILURE',
  'REACHED_MEMPOOL_LIMIT_FOR_ACCOUNT',
  'REJECTED_BY_BLOCKCHAIN',
  'TOO_LONG_MEMPOOL_CHAIN',
] as const

export type FireblocksSubstatusFailedBlockchain =
  (typeof fireblocksSubstatusesFailedBlockchain)[number]

export const isFireblocksSubstatusFailedBlockchain = (
  substatus: string,
): substatus is FireblocksSubstatusFailedBlockchain =>
  fireblocksSubstatusesFailedBlockchain.includes(
    substatus as FireblocksSubstatusFailedBlockchain,
  )

export type FireblocksSubstatusFailed =
  | FireblocksSubstatusFailedUserInput
  | FireblocksSubstatusFailedSystemIssue
  | FireblocksSubstatusFailedThirdParty
  | FireblocksSubstatusFailedBlockchain

export const isFireblocksSubstatusFailed = (
  substatus: string,
): substatus is FireblocksSubstatusFailed =>
  isFireblocksSubstatusFailedUserInput(substatus) ||
  isFireblocksSubstatusFailedSystemIssue(substatus) ||
  isFireblocksSubstatusFailedThirdParty(substatus) ||
  isFireblocksSubstatusFailedBlockchain(substatus)

export type FireblocksSubstatusNotCompleted =
  | FireblocksSubstatusBlocked
  | FireblocksSubstatusCancelled
  | FireblocksSubstatusRejected
  | FireblocksSubstatusFailed

export const isFireblocksSubstatusNotCompleted = (
  substatus: string,
): substatus is FireblocksSubstatusNotCompleted =>
  isFireblocksSubstatusBlocked(substatus) ||
  isFireblocksSubstatusCancelled(substatus) ||
  isFireblocksSubstatusRejected(substatus) ||
  isFireblocksSubstatusFailed(substatus)

export type FireblocksSubstatus =
  | FireblocksSubstatusPending
  | FireblocksSubstatusCompleted
  | FireblocksSubstatusNotCompleted

export const isFireblocksSubstatus = (
  substatus: string,
): substatus is FireblocksSubstatus =>
  isFireblocksSubstatusPending(substatus) ||
  isFireblocksSubstatusCompleted(substatus) ||
  isFireblocksSubstatusNotCompleted(substatus)

Types, arrays and typeguards