|
| 1 | +import { TokenAmount, TokenHolder } from './index.js'; |
| 2 | +import { EncodedTokenModuleRejectReason } from './types.js'; |
| 3 | + |
| 4 | +export enum TokenRejectReasonType { |
| 5 | + AddressNotFound = 'addressNotFound', |
| 6 | + TokenBalanceInsufficient = 'tokenBalanceInsufficient', |
| 7 | + DeserializationFailure = 'deserializationFailure', |
| 8 | + UnsupportedOperation = 'unsupportedOperation', |
| 9 | + OperationNotPermitted = 'operationNotPermitted', |
| 10 | + MintWouldOverflow = 'mintWouldOverflow', |
| 11 | +} |
| 12 | + |
| 13 | +type RejectReasonGen<T extends TokenRejectReasonType, D> = Omit<EncodedTokenModuleRejectReason, 'type' | 'details'> & { |
| 14 | + /** The type of rejection. */ |
| 15 | + type: T; |
| 16 | + /** Additional details about the rejection. */ |
| 17 | + details: D; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * The details of an "addressNotFound": an account address was not valid. |
| 22 | + */ |
| 23 | +export type AddressNotFoundDetails = { |
| 24 | + /** The index in the list of operations of the failing operation. */ |
| 25 | + index: number; |
| 26 | + /** The address that could not be resolved. */ |
| 27 | + address: TokenHolder.Type; |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * An account address was not valid. |
| 32 | + */ |
| 33 | +export type AddressNotFoundRejectReason = RejectReasonGen< |
| 34 | + TokenRejectReasonType.AddressNotFound, |
| 35 | + AddressNotFoundDetails |
| 36 | +>; |
| 37 | + |
| 38 | +/** |
| 39 | + * Details for a reject reason where the account's token balance is insufficient |
| 40 | + * for the attempted operation. |
| 41 | + * |
| 42 | + * See CIS-7: reject-reasons/tokenBalanceInsufficient |
| 43 | + */ |
| 44 | +export type TokenBalanceInsufficientDetails = { |
| 45 | + /** The index in the list of operations of the failing operation. */ |
| 46 | + index: number; |
| 47 | + /** The available balance for the sender at the time of the operation. */ |
| 48 | + availableBalance: TokenAmount.Type; |
| 49 | + /** The minimum required balance to perform the operation. */ |
| 50 | + requiredBalance: TokenAmount.Type; |
| 51 | +}; |
| 52 | + |
| 53 | +/** Typed reject reason for "tokenBalanceInsufficient". */ |
| 54 | +export type TokenBalanceInsufficientRejectReason = RejectReasonGen< |
| 55 | + TokenRejectReasonType.TokenBalanceInsufficient, |
| 56 | + TokenBalanceInsufficientDetails |
| 57 | +>; |
| 58 | + |
| 59 | +/** |
| 60 | + * Details for a reject reason where the operation payload could not be deserialized. |
| 61 | + * |
| 62 | + * See CIS-7: reject-reasons/deserializationFailure |
| 63 | + */ |
| 64 | +export type DeserializationFailureDetails = { |
| 65 | + /** Text description of the failure mode. */ |
| 66 | + cause?: string; |
| 67 | +}; |
| 68 | + |
| 69 | +/** Typed reject reason for "deserializationFailure". */ |
| 70 | +export type DeserializationFailureRejectReason = RejectReasonGen< |
| 71 | + TokenRejectReasonType.DeserializationFailure, |
| 72 | + DeserializationFailureDetails |
| 73 | +>; |
| 74 | + |
| 75 | +/** |
| 76 | + * Details for a reject reason where the specified operation is not supported by the module. |
| 77 | + * |
| 78 | + * See CIS-7: reject-reasons/unsupportedOperation |
| 79 | + */ |
| 80 | +export type UnsupportedOperationDetails = { |
| 81 | + /** The index in the list of operations of the failing operation. */ |
| 82 | + index: number; |
| 83 | + /** The type of operation that was not supported. */ |
| 84 | + operationType: string; |
| 85 | + /** The reason why the operation was not supported. */ |
| 86 | + reason?: string; |
| 87 | +}; |
| 88 | + |
| 89 | +/** Typed reject reason for "unsupportedOperation". */ |
| 90 | +export type UnsupportedOperationRejectReason = RejectReasonGen< |
| 91 | + TokenRejectReasonType.UnsupportedOperation, |
| 92 | + UnsupportedOperationDetails |
| 93 | +>; |
| 94 | + |
| 95 | +/** |
| 96 | + * Details for a reject reason where the operation is recognized but not permitted |
| 97 | + * under the current state or policy (e.g., paused, allow/deny list). |
| 98 | + * |
| 99 | + * See CIS-7: reject-reasons/operationNotPermitted |
| 100 | + */ |
| 101 | +export type OperationNotPermittedDetails = { |
| 102 | + /** The index in the list of operations of the failing operation. */ |
| 103 | + index: number; |
| 104 | + /** (Optionally) the address that does not have the necessary permissions to perform the operation. */ |
| 105 | + address?: TokenHolder.Type; |
| 106 | + /** The reason why the operation is not permitted. */ |
| 107 | + reason?: string; |
| 108 | +}; |
| 109 | + |
| 110 | +/** Typed reject reason for "operationNotPermitted". */ |
| 111 | +export type OperationNotPermittedRejectReason = RejectReasonGen< |
| 112 | + TokenRejectReasonType.OperationNotPermitted, |
| 113 | + OperationNotPermittedDetails |
| 114 | +>; |
| 115 | + |
| 116 | +/** |
| 117 | + * Details for a reject reason where minting would overflow supply constraints. |
| 118 | + * |
| 119 | + * See CIS-7: reject-reasons/mintWouldOverflow |
| 120 | + */ |
| 121 | +export type MintWouldOverflowDetails = { |
| 122 | + /** The index in the list of operations of the failing operation. */ |
| 123 | + index: number; |
| 124 | + /** The requested amount to mint. */ |
| 125 | + requestedAmount: TokenAmount.Type; |
| 126 | + /** The current supply of the token. */ |
| 127 | + currentSupply: TokenAmount.Type; |
| 128 | + /** The maximum representable token amount. */ |
| 129 | + maxRepresentableAmount: TokenAmount.Type; |
| 130 | +}; |
| 131 | + |
| 132 | +/** Typed reject reason for "mintWouldOverflow". */ |
| 133 | +export type MintWouldOverflowRejectReason = RejectReasonGen< |
| 134 | + TokenRejectReasonType.MintWouldOverflow, |
| 135 | + MintWouldOverflowDetails |
| 136 | +>; |
| 137 | + |
| 138 | +/** |
| 139 | + * Union of all token module reject reasons defined by CIS-7, |
| 140 | + * with strongly-typed details per reason. |
| 141 | + * |
| 142 | + * @see https://proposals.concordium.com/CIS/cis-7.html#reject-reasons |
| 143 | + */ |
| 144 | +export type TokenModuleRejectReason = |
| 145 | + | AddressNotFoundRejectReason |
| 146 | + | TokenBalanceInsufficientRejectReason |
| 147 | + | DeserializationFailureRejectReason |
| 148 | + | UnsupportedOperationRejectReason |
| 149 | + | OperationNotPermittedRejectReason |
| 150 | + | MintWouldOverflowRejectReason; |
0 commit comments