1
- import {
2
- AdvancedLogicTypes ,
3
- ExtensionTypes ,
4
- PaymentTypes ,
5
- RequestLogicTypes ,
6
- } from '@requestnetwork/types' ;
1
+ import { AdvancedLogicTypes , PaymentTypes , RequestLogicTypes } from '@requestnetwork/types' ;
7
2
import { ICurrencyManager } from '@requestnetwork/currency' ;
8
3
import { IPaymentNetworkModuleByType , ISupportedPaymentNetworkByCurrency } from './types' ;
9
4
import { BtcMainnetAddressBasedDetector } from './btc/mainnet-address-based' ;
@@ -18,6 +13,7 @@ import { EthFeeProxyPaymentDetector } from './eth/fee-proxy-detector';
18
13
import { AnyToERC20PaymentDetector } from './any/any-to-erc20-proxy' ;
19
14
import { NearConversionNativeTokenPaymentDetector , NearNativeTokenPaymentDetector } from './near' ;
20
15
import { AnyToEthFeeProxyPaymentDetector } from './any/any-to-eth-proxy' ;
16
+ import { getPaymentNetworkExtension } from './utils' ;
21
17
22
18
const PN_ID = PaymentTypes . PAYMENT_NETWORK_ID ;
23
19
@@ -62,133 +58,84 @@ const anyCurrencyPaymentNetwork: IPaymentNetworkModuleByType = {
62
58
[ PN_ID . ANY_TO_NATIVE ] : NearConversionNativeTokenPaymentDetector ,
63
59
} ;
64
60
61
+ export type PaymentNetworkOptions = {
62
+ /** override default bitcoin detection provider */
63
+ bitcoinDetectionProvider ?: PaymentTypes . IBitcoinDetectionProvider ;
64
+ /** the explorer API (eg Etherscan) api keys, for PNs that rely on it. Record by network name */
65
+ explorerApiKeys ?: Record < string , string > ;
66
+ } ;
67
+
65
68
/** Factory to create the payment network according to the currency and payment network type */
66
- export default class PaymentNetworkFactory {
69
+ export class PaymentNetworkFactory {
70
+ /**
71
+ *
72
+ * @param advancedLogic the advanced-logic layer in charge of the extensions
73
+ * @param currencyManager the currency manager handling supported currencies
74
+ * @param options the payment network options
75
+ */
76
+ constructor (
77
+ private readonly advancedLogic : AdvancedLogicTypes . IAdvancedLogic ,
78
+ private readonly currencyManager : ICurrencyManager ,
79
+ private readonly options ?: PaymentNetworkOptions ,
80
+ ) { }
81
+
67
82
/**
68
83
* Creates a payment network according to payment network creation parameters
69
84
* It throws if the payment network given is not supported by this library
70
85
*
71
- * @param advancedLogic the advanced-logic layer in charge of the extensions
72
86
* @param currency the currency of the request
73
87
* @param paymentNetworkCreationParameters creation parameters of payment network
74
- * @param bitcoinDetectionProvider bitcoin detection provider
75
- * @param currencyManager the currency manager handling supported currencies
76
88
* @returns the module to handle the payment network
77
89
*/
78
- public static createPaymentNetwork ( {
79
- advancedLogic,
80
- currency,
81
- paymentNetworkCreationParameters,
82
- bitcoinDetectionProvider,
83
- currencyManager,
84
- } : {
85
- advancedLogic : AdvancedLogicTypes . IAdvancedLogic ;
86
- currency : RequestLogicTypes . ICurrency ;
87
- paymentNetworkCreationParameters : PaymentTypes . IPaymentNetworkCreateParameters ;
88
- bitcoinDetectionProvider ?: PaymentTypes . IBitcoinDetectionProvider ;
89
- currencyManager : ICurrencyManager ;
90
- } ) : PaymentTypes . IPaymentNetwork {
91
- const paymentNetworkForCurrency = this . supportedPaymentNetworksForCurrency ( currency ) ;
90
+ public createPaymentNetwork (
91
+ paymentNetworkId : PaymentTypes . PAYMENT_NETWORK_ID ,
92
+ currencyType : RequestLogicTypes . CURRENCY ,
93
+ currencyNetwork ?: string ,
94
+ ) : PaymentTypes . IPaymentNetwork {
95
+ const currencyPaymentMap =
96
+ supportedPaymentNetwork [ currencyType ] ?. [ currencyNetwork || 'mainnet' ] ||
97
+ supportedPaymentNetwork [ currencyType ] ?. [ '*' ] ||
98
+ { } ;
99
+ const paymentNetworkMap = {
100
+ ...currencyPaymentMap ,
101
+ ...anyCurrencyPaymentNetwork ,
102
+ } ;
92
103
93
- if ( ! paymentNetworkForCurrency [ paymentNetworkCreationParameters . id ] ) {
104
+ if ( ! paymentNetworkMap [ paymentNetworkId ] ) {
94
105
throw new Error (
95
- `the payment network id: ${
96
- paymentNetworkCreationParameters . id
97
- } is not supported for the currency: ${ currency . type } on network ${
98
- currency . network || 'mainnet'
106
+ `the payment network id: ${ paymentNetworkId } is not supported for the currency: ${ currencyType } on network ${
107
+ currencyNetwork || 'mainnet'
99
108
} `,
100
109
) ;
101
110
}
102
-
103
- return new paymentNetworkForCurrency [ paymentNetworkCreationParameters . id ] ( {
104
- advancedLogic,
105
- bitcoinDetectionProvider,
106
- currencyManager,
111
+ return new paymentNetworkMap [ paymentNetworkId ] ( {
112
+ advancedLogic : this . advancedLogic ,
113
+ currencyManager : this . currencyManager ,
114
+ ...this . options ,
107
115
} ) ;
108
116
}
109
117
110
118
/**
111
119
* Gets the module to the payment network of a request
112
120
* It throws if the payment network found is not supported by this library
113
121
*
114
- * @param advancedLogic the advanced-logic layer in charge of the extensions
115
122
* @param request the request
116
- * @param bitcoinDetectionProvider bitcoin detection provider
117
- * @param explorerApiKeys the explorer API (eg Etherscan) api keys, for PNs that rely on it. Record by network name.
118
- * @param currencyManager the currency manager handling supported currencies
119
123
* @returns the module to handle the payment network or null if no payment network found
120
124
*/
121
- public static getPaymentNetworkFromRequest ( {
122
- advancedLogic,
123
- request,
124
- bitcoinDetectionProvider,
125
- explorerApiKeys,
126
- currencyManager,
127
- } : {
128
- advancedLogic : AdvancedLogicTypes . IAdvancedLogic ;
129
- request : RequestLogicTypes . IRequest ;
130
- currencyManager : ICurrencyManager ;
131
- bitcoinDetectionProvider ?: PaymentTypes . IBitcoinDetectionProvider ;
132
- explorerApiKeys ?: Record < string , string > ;
133
- } ) : PaymentTypes . IPaymentNetwork | null {
134
- const currency = request . currency ;
135
- const extensionPaymentNetwork = Object . values ( request . extensions || { } ) . find (
136
- ( extension ) => extension . type === ExtensionTypes . TYPE . PAYMENT_NETWORK ,
137
- ) ;
125
+ public getPaymentNetworkFromRequest (
126
+ request : RequestLogicTypes . IRequest ,
127
+ ) : PaymentTypes . IPaymentNetwork | null {
128
+ const pn = getPaymentNetworkExtension ( request ) ;
138
129
139
- if ( ! extensionPaymentNetwork ) {
130
+ if ( ! pn ) {
140
131
return null ;
141
132
}
142
133
143
- const paymentNetworkId = extensionPaymentNetwork . id ;
144
- const paymentNetworkForCurrency = this . supportedPaymentNetworksForCurrency ( currency ) ;
145
-
146
- if ( ! paymentNetworkForCurrency [ paymentNetworkId ] ) {
147
- throw new Error (
148
- `the payment network id: ${ paymentNetworkId } is not supported for the currency: ${
149
- currency . type
150
- } on network ${ currency . network || 'mainnet' } `,
151
- ) ;
152
- }
153
-
154
- return new paymentNetworkForCurrency [ paymentNetworkId ] ( {
155
- advancedLogic,
156
- bitcoinDetectionProvider,
157
- explorerApiKeys,
158
- currencyManager,
159
- } ) ;
160
- }
161
-
162
- /**
163
- * Gets the payment networks supported for a Currency object
164
- *
165
- * @param currency The currency to get the supported networks for
166
- */
167
- public static supportedPaymentNetworksForCurrency (
168
- currency : RequestLogicTypes . ICurrency ,
169
- ) : IPaymentNetworkModuleByType {
170
- if ( ! supportedPaymentNetwork [ currency . type ] ) {
171
- return anyCurrencyPaymentNetwork ;
172
- }
173
-
174
- const paymentNetwork =
175
- supportedPaymentNetwork [ currency . type ] [ currency . network || 'mainnet' ] ||
176
- supportedPaymentNetwork [ currency . type ] [ '*' ] ;
177
-
178
- return { ...paymentNetwork , ...anyCurrencyPaymentNetwork } ;
179
- }
180
-
181
- /**
182
- * Checks if a networkId is part of the supported networks for given currency
183
- *
184
- * @param paymentNetworkId The networkId to check is supported by this currency
185
- * @param currency The currency to check the supported networks for
186
- */
187
- public static currencySupportsPaymentNetwork (
188
- paymentNetworkId : PaymentTypes . PAYMENT_NETWORK_ID ,
189
- currency : RequestLogicTypes . ICurrency ,
190
- ) : boolean {
191
- const paymentNetworks = this . supportedPaymentNetworksForCurrency ( currency ) ;
192
- return ! ! paymentNetworks [ paymentNetworkId ] ;
134
+ const paymentNetworkId = pn . id as unknown as PaymentTypes . PAYMENT_NETWORK_ID ;
135
+ return this . createPaymentNetwork (
136
+ paymentNetworkId ,
137
+ request . currency . type ,
138
+ request . currency . network ,
139
+ ) ;
193
140
}
194
141
}
0 commit comments