@jetthai/cuora 1.1.360 → 1.1.361-dev-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,493 +0,0 @@
1
- import { assign, createMachine } from 'xstate'
2
- import findLastIndex from 'lodash/findLastIndex.js'
3
- import payment from '../../api/payment.js'
4
- import common from '../../api/common.js'
5
- import user from '../../api/user.js'
6
- import api from '../../api/payment.js'
7
-
8
- export default function (request) {
9
- const { appSecret } = request.config
10
- return createMachine(
11
- {
12
- id: 'withdraw_crypto',
13
- initial: 'loading',
14
- context: {
15
- siteConfig: null,
16
- userInfo: null,
17
- balance: null,
18
-
19
- methods: [],
20
- paymentDetail: [],
21
-
22
- withdrawLimit: null,
23
- targetWithdrawLimit: null,
24
-
25
- maxWithdrawAmount: null,
26
- enableWithdraw: false,
27
- withdrawRules: [],
28
-
29
- attachmentType: null, // null | 'withdraw' | 'pool'
30
-
31
- networks: [],
32
- selectedNetwork: null,
33
- address: '',
34
- amount: null,
35
-
36
- charge: null,
37
- realAmount: null,
38
-
39
- // otp
40
- requestId: null,
41
- status: null,
42
- target: null,
43
- ttl: null,
44
- },
45
- states: {
46
- loading: {
47
- invoke: {
48
- src: 'fetchInitData',
49
- onDone: {
50
- target: 'mode',
51
- actions: ['setInitData', 'preset', 'updateWithdrawCondition'],
52
- },
53
- onError: {
54
- target: 'fail',
55
- },
56
- },
57
- },
58
- mode: {
59
- always: [{ target: 'free', cond: 'isFree' }, { target: 'cash' }],
60
- },
61
- free: {
62
- initial: 'checking',
63
- states: {
64
- checking: {
65
- always: [{ target: 'convertible', cond: 'isConvertible' }, { target: 'notConvertible' }],
66
- },
67
- notConvertible: {
68
- on: {
69
- GIVE_UP: {
70
- target: 'givingUp',
71
- },
72
- },
73
- },
74
- givingUp: {
75
- invoke: {
76
- src: 'giveUp',
77
- onDone: {
78
- target: 'gaveUp',
79
- },
80
- onError: {
81
- target: 'convertible',
82
- },
83
- },
84
- },
85
- gaveUp: {},
86
- convertible: {
87
- on: {
88
- SUBMIT: {
89
- target: 'converting',
90
- actions: 'setAttachmentType',
91
- },
92
- },
93
- },
94
- converting: {
95
- invoke: {
96
- src: 'convertFreeCredit',
97
- onDone: [
98
- {
99
- target: '#withdraw_crypto.otp',
100
- actions: 'setOtp',
101
- cond: 'isOtpRequired',
102
- },
103
- {
104
- target: 'converted',
105
- },
106
- ],
107
- onError: {
108
- target: 'convertible',
109
- },
110
- },
111
- },
112
- converted: {
113
- always: [{ target: '#withdraw_crypto.cash.withdrawable', cond: 'hasAttachmentType' }],
114
- },
115
- },
116
- },
117
- cash: {
118
- initial: 'checking',
119
- states: {
120
- checking: {
121
- always: [
122
- { target: 'inProcessing', cond: 'haveProcessing' },
123
- { target: 'notWithdrawable', cond: 'isNotWithdrawable' },
124
- { target: 'withdrawable' },
125
- ],
126
- },
127
- notWithdrawable: {},
128
- inProcessing: {},
129
- withdrawable: {
130
- on: {
131
- SET_NETWORK: {
132
- actions: ['resetCharge', 'setNetwork'],
133
- },
134
- SET_ADDRESS: {
135
- actions: 'setAddress',
136
- },
137
- SET_AMOUNT: {
138
- actions: ['resetCharge', 'setAmount'],
139
- },
140
- CALCULATE: {
141
- target: 'calculating',
142
- cond: 'isCalculable',
143
- },
144
- PREVIEW: {
145
- target: 'submittable',
146
- cond: 'isSubmittable',
147
- },
148
- SET_ATTACHMENT_TYPE: {
149
- actions: 'setAttachmentType',
150
- },
151
- POOL: {
152
- target: 'pooling',
153
- cond: (context, event) => {
154
- const { amount, attachmentType } = context
155
- return attachmentType === 'pool' && amount > 0
156
- },
157
- },
158
- },
159
- },
160
- calculating: {
161
- entry: 'resetCharge',
162
- invoke: {
163
- src: 'fetchCalculation',
164
- onDone: {
165
- target: 'withdrawable',
166
- actions: 'setCalculatedResult',
167
- },
168
- onError: {
169
- target: 'withdrawable',
170
- },
171
- },
172
- },
173
- submittable: {
174
- on: {
175
- SUBMIT: [
176
- {
177
- target: 'pooling',
178
- cond: (context, event) => {
179
- const { amount, attachmentType } = context
180
- return attachmentType === 'pool' && amount > 0
181
- },
182
- },
183
- {
184
- target: 'submitting',
185
- },
186
- ],
187
- BACK: {
188
- target: 'withdrawable',
189
- },
190
- },
191
- },
192
- submitting: {
193
- invoke: {
194
- src: 'submitWithdraw',
195
- onDone: [
196
- {
197
- target: '#withdraw_crypto.otp',
198
- actions: 'setOtp',
199
- cond: 'isOtpRequired',
200
- },
201
- {
202
- target: 'submitted',
203
- },
204
- ],
205
- onError: {
206
- target: 'withdrawable',
207
- },
208
- },
209
- },
210
- submitted: {},
211
- pooling: {
212
- invoke: {
213
- src: 'pool',
214
- onDone: { target: 'pooled' },
215
- onError: {
216
- target: 'withdrawable',
217
- },
218
- },
219
- },
220
- pooled: {},
221
- },
222
- },
223
- otp: {
224
- type: 'parallel',
225
- states: {
226
- countdown: {
227
- after: {
228
- 1000: {
229
- target: 'countdown',
230
- actions: 'decreaseTtl',
231
- cond: (context) => context.ttl > 0,
232
- },
233
- },
234
- },
235
- verification: {
236
- initial: 'idle',
237
- states: {
238
- idle: {
239
- on: {
240
- ACKNOWLEDGE: {
241
- target: 'acknowledging',
242
- },
243
- RESET_OTP: {
244
- target: 'reset',
245
- },
246
- BACK: [
247
- {
248
- target: '#withdraw_crypto.free.convertible',
249
- cond: 'isFree',
250
- },
251
- {
252
- target: '#withdraw_crypto.cash.submittable',
253
- },
254
- ],
255
- },
256
- },
257
- acknowledging: {
258
- invoke: {
259
- src: 'acknowledge',
260
- onDone: [
261
- {
262
- target: '#withdraw_crypto.free.converted',
263
- cond: 'isFree',
264
- },
265
- {
266
- target: '#withdraw_crypto.cash.submitted',
267
- },
268
- ],
269
- onError: {
270
- target: 'idle',
271
- },
272
- },
273
- },
274
- reset: {
275
- invoke: {
276
- src: 'resetOtp',
277
- onDone: {
278
- target: 'idle',
279
- actions: 'setVerificationData',
280
- },
281
- onError: { target: 'idle' },
282
- },
283
- },
284
- },
285
- },
286
- },
287
- },
288
- fail: {},
289
- },
290
- on: {
291
- REFRESH: {
292
- target: 'loading',
293
- },
294
- },
295
- },
296
- {
297
- actions: {
298
- setInitData: assign((context, event) => {
299
- const method = event.data.paymentDetail?.[0]
300
- return {
301
- paymentDetail: event.data.paymentDetail,
302
- crypto: method,
303
- networks:
304
- method?.networkName?.map?.((value, i) => {
305
- return {
306
- label: method?.networkOptional?.[i],
307
- value,
308
- icon: value,
309
- }
310
- }) || [],
311
- }
312
- }),
313
- preset: assign((context, event) => {
314
- const { userInfo, siteConfig } = event.data
315
- const { withdraw_limit, target_withdraw_limit } = userInfo || {}
316
- const balance = parseFloat(userInfo?.balance)
317
- const methods = siteConfig?.paymentMethods?.withdraw || []
318
- return {
319
- withdrawLimit: Math.abs(withdraw_limit),
320
- targetWithdrawLimit: Math.abs(target_withdraw_limit),
321
- methods,
322
- balance,
323
- siteConfig,
324
- userInfo,
325
- attachmentType: 'withdraw',
326
- }
327
- }),
328
- updateWithdrawCondition: assign((context, event) => {
329
- const { enableWithdraw, enableWithdrawAmount } = event.data?.withdrawRule
330
- const withdrawRules = event.data?.withdrawRule?.rules || []
331
- const rules = withdrawRules.filter((rule) => rule.maxWithdrawAmount > 0)
332
- const { balance } = context
333
- const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
334
-
335
- let maxWithdrawAmount = 0
336
-
337
- if (lastMatchedIndex > -1) {
338
- rules[lastMatchedIndex].match = true
339
- maxWithdrawAmount = rules[lastMatchedIndex].maxWithdrawAmount
340
- } else if (rules.length > 0) {
341
- maxWithdrawAmount = rules[0].maxWithdrawAmount
342
- } else {
343
- maxWithdrawAmount = balance
344
- }
345
-
346
- return {
347
- maxWithdrawAmount,
348
- enableWithdrawAmount: +enableWithdrawAmount,
349
- enableWithdraw,
350
- withdrawRules,
351
- }
352
- }),
353
- setNetwork: assign((context, event) => {
354
- return {
355
- selectedNetwork: event.data,
356
- }
357
- }),
358
- setAddress: assign((context, event) => {
359
- return {
360
- address: event.data,
361
- }
362
- }),
363
- setAmount: assign((context, event) => {
364
- return {
365
- amount: event.data,
366
- }
367
- }),
368
- setCalculatedResult: assign((context, event) => {
369
- return {
370
- charge: event.data.charge,
371
- }
372
- }),
373
- resetCharge: assign((context, event) => {
374
- return {
375
- charge: null,
376
- }
377
- }),
378
- setOtp: assign((context, event) => {
379
- const { requestId, status, target, ttl } = event.data
380
- return {
381
- requestId,
382
- status,
383
- target,
384
- ttl,
385
- }
386
- }),
387
- decreaseTtl: assign((context, evnet) => {
388
- const second = context.ttl
389
- return {
390
- ttl: Math.max(second - 1, 0),
391
- }
392
- }),
393
- setVerificationData: assign((context, event) => {
394
- const { requestId, ttl, target } = event.data
395
- return {
396
- requestId,
397
- ttl,
398
- target,
399
- }
400
- }),
401
- setAttachmentType: assign((context, event) => {
402
- return {
403
- attachmentType: event.data,
404
- }
405
- }),
406
- },
407
- guards: {
408
- isFree: (context, event) => {
409
- return context.userInfo?.withdraw_rule !== 'DEFAULT'
410
- },
411
- hasAttachmentType: (context, event) => {
412
- return ['withdraw', 'pool'].includes(context.attachmentType)
413
- },
414
- haveProcessing: (context, event) => {
415
- return (context.paymentDetail?.[0]?.processing || []).length > 0
416
- },
417
- isSubmittable: (context, event) => {
418
- const { selectedNetwork, address, amount, charge } = context
419
- return selectedNetwork && address && amount && charge
420
- },
421
- isConvertible: (context, event) => {
422
- const { enableWithdraw } = context
423
- return enableWithdraw
424
- },
425
- isNotWithdrawable: (context, event) => {
426
- const { enableWithdraw } = context
427
- return !enableWithdraw
428
- },
429
- isCalculable: (context, event) => {
430
- const { selectedNetwork, amount } = context
431
- return selectedNetwork && amount
432
- },
433
- isOtpRequired: (context, event) => {
434
- return event.data?.status === 'otp-verifying'
435
- },
436
- },
437
- services: {
438
- fetchInitData: async (context, event) => {
439
- const [{ config }, userInfo, paymentDetail = [], withdrawRule] = await Promise.all([
440
- request.send(common.getConfig({ appSecret })),
441
- request.send(user.getUserInfo()),
442
- request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' })),
443
- request.send(payment.getCurrentWithdrawRule()),
444
- ])
445
- const siteConfig = JSON.parse(window.atob(config))
446
- return { siteConfig, userInfo, paymentDetail, withdrawRule }
447
- },
448
- submitWithdraw: async (context, event) => {
449
- const { crypto, selectedNetwork, address, amount } = context
450
- return await request.send(
451
- api.submitWithdrawTransactionCrypto({
452
- method: crypto.method,
453
- amount,
454
- cryptoCurrency: crypto.channel,
455
- cryptoNetwork: selectedNetwork.value,
456
- targetAddress: address,
457
- })
458
- )
459
- },
460
- convertFreeCredit: async (context, event) => {
461
- return await request.send(payment.convertFreeCredit({ exchangeType: 1 }))
462
- },
463
- fetchCalculation: async (context, event) => {
464
- const { crypto, selectedNetwork, amount } = context
465
- const method = crypto.method
466
- return await request.send(
467
- api.getWithdrawExpectedResult({
468
- method,
469
- amount,
470
- cryptoNetwork: selectedNetwork.value,
471
- })
472
- )
473
- },
474
- resetOtp: async (context, event) => {
475
- const { requestId, target } = context
476
- return await request.send(user.otpReset({ requestId, target }))
477
- },
478
- acknowledge: async (context, event) => {
479
- const { requestId, target } = context
480
- const code = event.data
481
- return await request.send(user.otpAcknowledge({ requestId, target, code }))
482
- },
483
- pool: async (context, event) => {
484
- const { amount } = context
485
- return await request.send(payment.restore({ amount }))
486
- },
487
- giveUp: async (context, event) => {
488
- return await request.send(payment.giveUp())
489
- },
490
- },
491
- }
492
- )
493
- }