|
| 1 | +import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; |
| 2 | +import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types'; |
| 3 | +import { UnsupportedNetworkError } from './address-based'; |
| 4 | +import AnyToNativeTokenPaymentNetwork from './any-to-native'; |
| 5 | + |
| 6 | +const CURRENT_VERSION = '0.2.0'; |
| 7 | +const supportedNetworks = ['aurora', 'aurora-testnet']; |
| 8 | + |
| 9 | +export default class AnyToNearPaymentNetwork extends AnyToNativeTokenPaymentNetwork { |
| 10 | + public constructor( |
| 11 | + private currencyManager: ICurrencyManager, |
| 12 | + extensionId: ExtensionTypes.ID = ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_NATIVE_TOKEN, |
| 13 | + currentVersion: string = CURRENT_VERSION, |
| 14 | + ) { |
| 15 | + super(extensionId, currentVersion, supportedNetworks); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Check if a near address is valid |
| 20 | + * |
| 21 | + * @param {string} address address to check |
| 22 | + * @returns {boolean} true if address is valid |
| 23 | + */ |
| 24 | + protected isValidAddress(address: string, networkName?: string): boolean { |
| 25 | + switch (networkName) { |
| 26 | + case 'aurora': |
| 27 | + return this.isValidMainnetAddress(address); |
| 28 | + case 'aurora-testnet': |
| 29 | + return this.isValidTestnetAddress(address); |
| 30 | + case undefined: |
| 31 | + return this.isValidMainnetAddress(address) || this.isValidTestnetAddress(address); |
| 32 | + default: |
| 33 | + throw new UnsupportedNetworkError(networkName, this.supportedNetworks); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private isValidMainnetAddress(address: string): boolean { |
| 38 | + return this.isValidAddressForSymbolAndNetwork(address, 'NEAR', 'aurora'); |
| 39 | + } |
| 40 | + |
| 41 | + private isValidTestnetAddress(address: string): boolean { |
| 42 | + return this.isValidAddressForSymbolAndNetwork(address, 'NEAR-testnet', 'aurora-testnet'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Applies a creation extension action |
| 47 | + * |
| 48 | + * @param extensionAction action to apply |
| 49 | + * @param timestamp action timestamp |
| 50 | + * |
| 51 | + * @returns state of the extension created |
| 52 | + */ |
| 53 | + protected applyCreation( |
| 54 | + extensionAction: ExtensionTypes.IAction, |
| 55 | + timestamp: number, |
| 56 | + ): ExtensionTypes.IState { |
| 57 | + if (!extensionAction.parameters.network || extensionAction.parameters.network.length === 0) { |
| 58 | + throw Error('network is required'); |
| 59 | + } |
| 60 | + |
| 61 | + if ( |
| 62 | + extensionAction.parameters.paymentAddress && |
| 63 | + !this.isValidAddress( |
| 64 | + extensionAction.parameters.paymentAddress, |
| 65 | + extensionAction.parameters.network, |
| 66 | + ) |
| 67 | + ) { |
| 68 | + throw Error( |
| 69 | + `paymentAddress ${extensionAction.parameters.paymentAddress} is not a valid address`, |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if ( |
| 74 | + extensionAction.parameters.feeAddress && |
| 75 | + !this.isValidAddress( |
| 76 | + extensionAction.parameters.feeAddress, |
| 77 | + extensionAction.parameters.network, |
| 78 | + ) |
| 79 | + ) { |
| 80 | + throw Error(`feeAddress ${extensionAction.parameters.feeAddress} is not a valid address`); |
| 81 | + } |
| 82 | + |
| 83 | + if ( |
| 84 | + extensionAction.parameters.refundAddress && |
| 85 | + !this.isValidAddress( |
| 86 | + extensionAction.parameters.refundAddress, |
| 87 | + extensionAction.parameters.network, |
| 88 | + ) |
| 89 | + ) { |
| 90 | + throw Error( |
| 91 | + `refundAddress ${extensionAction.parameters.refundAddress} is not a valid address`, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + const feePNCreationAction = super.applyCreation(extensionAction, timestamp); |
| 96 | + |
| 97 | + return { |
| 98 | + ...feePNCreationAction, |
| 99 | + events: [ |
| 100 | + { |
| 101 | + name: 'create', |
| 102 | + parameters: { |
| 103 | + feeAddress: extensionAction.parameters.feeAddress, |
| 104 | + feeAmount: extensionAction.parameters.feeAmount, |
| 105 | + paymentAddress: extensionAction.parameters.paymentAddress, |
| 106 | + refundAddress: extensionAction.parameters.refundAddress, |
| 107 | + salt: extensionAction.parameters.salt, |
| 108 | + network: extensionAction.parameters.network, |
| 109 | + maxRateTimespan: extensionAction.parameters.maxRateTimespan, |
| 110 | + }, |
| 111 | + timestamp, |
| 112 | + }, |
| 113 | + ], |
| 114 | + values: { |
| 115 | + ...feePNCreationAction.values, |
| 116 | + network: extensionAction.parameters.network, |
| 117 | + maxRateTimespan: extensionAction.parameters.maxRateTimespan, |
| 118 | + }, |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Applies add payment address |
| 124 | + * |
| 125 | + * @param extensionState previous state of the extension |
| 126 | + * @param extensionAction action to apply |
| 127 | + * @param requestState request state read-only |
| 128 | + * @param actionSigner identity of the signer |
| 129 | + * |
| 130 | + * @returns state of the extension updated |
| 131 | + */ |
| 132 | + protected applyAddPaymentAddress( |
| 133 | + extensionState: ExtensionTypes.IState, |
| 134 | + extensionAction: ExtensionTypes.IAction, |
| 135 | + requestState: RequestLogicTypes.IRequest, |
| 136 | + actionSigner: IdentityTypes.IIdentity, |
| 137 | + timestamp: number, |
| 138 | + ): ExtensionTypes.IState { |
| 139 | + const paymentAddress = extensionAction.parameters.paymentAddress; |
| 140 | + if (!this.isValidAddress(paymentAddress, extensionState.values.network)) { |
| 141 | + throw new Error(`paymentAddress '${paymentAddress}' is not a valid address`); |
| 142 | + } |
| 143 | + return super.applyAddPaymentAddress( |
| 144 | + extensionState, |
| 145 | + extensionAction, |
| 146 | + requestState, |
| 147 | + actionSigner, |
| 148 | + timestamp, |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + protected applyAddFee( |
| 153 | + extensionState: ExtensionTypes.IState, |
| 154 | + extensionAction: ExtensionTypes.IAction, |
| 155 | + requestState: RequestLogicTypes.IRequest, |
| 156 | + actionSigner: IdentityTypes.IIdentity, |
| 157 | + timestamp: number, |
| 158 | + ): ExtensionTypes.IState { |
| 159 | + const network = |
| 160 | + requestState.extensions[ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_NATIVE_TOKEN].values.network; |
| 161 | + if ( |
| 162 | + extensionAction.parameters.feeAddress && |
| 163 | + !this.isValidAddress(extensionAction.parameters.feeAddress, network) |
| 164 | + ) { |
| 165 | + throw Error('feeAddress is not a valid address'); |
| 166 | + } |
| 167 | + return super.applyAddFee( |
| 168 | + extensionState, |
| 169 | + extensionAction, |
| 170 | + requestState, |
| 171 | + actionSigner, |
| 172 | + timestamp, |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Validate that the network and currency coming from the extension and/or action are valid and supported. |
| 178 | + * It must throw in case of error. |
| 179 | + */ |
| 180 | + protected validate( |
| 181 | + request: RequestLogicTypes.IRequest, |
| 182 | + extensionAction: ExtensionTypes.IAction, |
| 183 | + ): void { |
| 184 | + const network = |
| 185 | + extensionAction.action === ExtensionTypes.PnFeeReferenceBased.ACTION.CREATE |
| 186 | + ? extensionAction.parameters.network |
| 187 | + : request.extensions[this.extensionId]?.values.network; |
| 188 | + |
| 189 | + if (!network) { |
| 190 | + throw new Error( |
| 191 | + `The network must be provided by the creation action or by the extension state`, |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + if (!supportedNetworks.includes(network)) { |
| 196 | + throw new Error(`The network (${network}) is not supported for this payment network.`); |
| 197 | + } |
| 198 | + |
| 199 | + const currency = this.currencyManager.fromStorageCurrency(request.currency); |
| 200 | + if (!currency) { |
| 201 | + throw new UnsupportedCurrencyError(request.currency); |
| 202 | + } |
| 203 | + console.log(network); |
| 204 | + console.log(currency); |
| 205 | + if (!this.currencyManager.supportsConversion(currency, network)) { |
| 206 | + throw new Error( |
| 207 | + `The currency (${request.currency.value}) of the request is not supported for this payment network.`, |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments