-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathpayment-network-factory.ts
More file actions
198 lines (183 loc) · 6.95 KB
/
payment-network-factory.ts
File metadata and controls
198 lines (183 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
AdvancedLogicTypes,
CurrencyTypes,
ExtensionTypes,
PaymentTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import {
ContractBasedDetector,
IPaymentNetworkModuleByType,
ISupportedPaymentNetworkByCurrency,
PaymentNetworkOptions,
} from './types';
import { BtcMainnetAddressBasedDetector, BtcTestnetAddressBasedDetector } from './btc';
import { DeclarativePaymentDetector } from './declarative';
import { MetaDetector } from './meta-payment-detector';
import {
ERC20AddressBasedPaymentDetector,
ERC20FeeProxyPaymentDetector,
ERC20ProxyPaymentDetector,
ERC20TransferableReceivablePaymentDetector,
} from './erc20';
import { SuperFluidPaymentDetector } from './erc777/superfluid-detector';
import { EthFeeProxyPaymentDetector, EthInputDataPaymentDetector } from './eth';
import { AnyToERC20PaymentDetector, AnyToEthFeeProxyPaymentDetector } from './any';
import { NearConversionNativeTokenPaymentDetector, NearNativeTokenPaymentDetector } from './near';
import { TronFeeProxyPaymentDetector } from './tron';
import { getPaymentNetworkExtension } from './utils';
import { getTheGraphClient } from './thegraph';
import { getDefaultProvider } from 'ethers';
const PN_ID = ExtensionTypes.PAYMENT_NETWORK_ID;
/** Register the payment network by currency and type */
const supportedPaymentNetwork: ISupportedPaymentNetworkByCurrency = {
BTC: {
mainnet: {
[PN_ID.BITCOIN_ADDRESS_BASED]: BtcMainnetAddressBasedDetector,
},
testnet: {
[PN_ID.TESTNET_BITCOIN_ADDRESS_BASED]: BtcTestnetAddressBasedDetector,
},
},
ERC777: {
'*': {
[PN_ID.ERC777_STREAM]: SuperFluidPaymentDetector,
},
},
ERC20: {
aurora: {
[PN_ID.ERC20_FEE_PROXY_CONTRACT]: ERC20FeeProxyPaymentDetector<CurrencyTypes.NearChainName>,
},
'aurora-testnet': {
[PN_ID.ERC20_FEE_PROXY_CONTRACT]: ERC20FeeProxyPaymentDetector<CurrencyTypes.NearChainName>,
},
'near-testnet': {
[PN_ID.ERC20_FEE_PROXY_CONTRACT]: ERC20FeeProxyPaymentDetector<CurrencyTypes.NearChainName>,
},
// TRON networks
tron: {
[PN_ID.ERC20_FEE_PROXY_CONTRACT]: TronFeeProxyPaymentDetector,
},
nile: {
[PN_ID.ERC20_FEE_PROXY_CONTRACT]: TronFeeProxyPaymentDetector,
},
'*': {
[PN_ID.ERC20_ADDRESS_BASED]: ERC20AddressBasedPaymentDetector,
[PN_ID.ERC20_PROXY_CONTRACT]: ERC20ProxyPaymentDetector,
[PN_ID.ERC20_FEE_PROXY_CONTRACT]: ERC20FeeProxyPaymentDetector,
[PN_ID.ERC20_TRANSFERABLE_RECEIVABLE]: ERC20TransferableReceivablePaymentDetector,
},
},
ETH: {
aurora: { [PN_ID.NATIVE_TOKEN]: NearNativeTokenPaymentDetector },
'aurora-testnet': {
[PN_ID.NATIVE_TOKEN]: NearNativeTokenPaymentDetector,
},
'near-testnet': {
[PN_ID.NATIVE_TOKEN]: NearNativeTokenPaymentDetector,
},
'*': {
[PN_ID.ETH_INPUT_DATA]: EthInputDataPaymentDetector,
[PN_ID.ETH_FEE_PROXY_CONTRACT]: EthFeeProxyPaymentDetector,
},
},
};
const anyCurrencyPaymentNetwork: IPaymentNetworkModuleByType = {
[PN_ID.ANY_TO_ERC20_PROXY]: AnyToERC20PaymentDetector,
[PN_ID.ANY_DECLARATIVE]: DeclarativePaymentDetector,
[PN_ID.ANY_TO_ETH_PROXY]: AnyToEthFeeProxyPaymentDetector,
[PN_ID.ANY_TO_NATIVE_TOKEN]: NearConversionNativeTokenPaymentDetector,
[PN_ID.META]: MetaDetector,
};
/** Factory to create the payment network according to the currency and payment network type */
export class PaymentNetworkFactory {
private readonly options: Readonly<PaymentNetworkOptions>;
/**
*
* @param advancedLogic the advanced-logic layer in charge of the extensions
* @param currencyManager the currency manager handling supported currencies
* @param options the payment network options
*/
constructor(
private readonly advancedLogic: AdvancedLogicTypes.IAdvancedLogic,
private readonly currencyManager: CurrencyTypes.ICurrencyManager,
options?: Partial<PaymentNetworkOptions>,
) {
this.options = this.buildOptions(options || {});
}
private buildOptions(options: Partial<PaymentNetworkOptions>): PaymentNetworkOptions {
const defaultOptions: PaymentNetworkOptions = {
getSubgraphClient: getTheGraphClient,
explorerApiKeys: {},
getRpcProvider: getDefaultProvider,
};
return { ...defaultOptions, ...options };
}
/**
* Creates a payment network interpretor according to payment network creation parameters
* It throws if the payment network given is not supported by this library
*
* @param paymentNetworkId the ID of the payment network to instantiate
* @param currencyType the currency type of the request
* @param paymentChain Different from request.currency.network for on-chain conversion payment networks (any-to-something)
* @returns the module to handle the payment network
*/
public createPaymentNetwork<PN_ID extends ExtensionTypes.PAYMENT_NETWORK_ID>(
paymentNetworkId: PN_ID,
currencyType: RequestLogicTypes.CURRENCY,
paymentChain?: CurrencyTypes.ChainName,
paymentNetworkVersion?: string,
): PaymentTypes.IPaymentNetwork<
PaymentTypes.GenericEventParameters,
Extract<PaymentTypes.PaymentNetworkCreateParameters, { id: PN_ID }>['parameters']
> {
const network = paymentChain ?? 'mainnet';
const currencyPaymentMap =
supportedPaymentNetwork[currencyType]?.[network] ||
supportedPaymentNetwork[currencyType]?.['*'] ||
{};
const paymentNetworkMap = {
...currencyPaymentMap,
...anyCurrencyPaymentNetwork,
};
const detectorClass = paymentNetworkMap[paymentNetworkId];
if (!detectorClass) {
throw new Error(
`the payment network id: ${paymentNetworkId} is not supported for the currency: ${currencyType} on network ${network}`,
);
}
const detector = new detectorClass({
network,
advancedLogic: this.advancedLogic,
currencyManager: this.currencyManager,
options: this.options,
...this.options,
});
if (detector.extension && 'getDeploymentInformation' in detectorClass) {
// this throws when the contract isn't deployed and was mandatory for payment detection
(detectorClass as ContractBasedDetector).getDeploymentInformation(
network as CurrencyTypes.VMChainName,
paymentNetworkVersion || detector.extension.currentVersion,
);
}
return detector;
}
/**
* Gets the module to the payment network of a request
* It throws if the payment network found is not supported by this library
*
* @param request the request
* @returns the module to handle the payment network or null if no payment network found
*/
public getPaymentNetworkFromRequest(
request: RequestLogicTypes.IRequest,
): PaymentTypes.IPaymentNetwork<PaymentTypes.GenericEventParameters, any> | null {
const pn = getPaymentNetworkExtension(request);
if (!pn) {
return null;
}
const detectionChain = pn.values?.network ?? request.currency.network;
const { id, version } = pn;
return this.createPaymentNetwork(id, request.currency.type, detectionChain, version);
}
}