@jetthai/cuora 1.1.359-dev-6 → 1.1.359-dev-7

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,251 +0,0 @@
1
- import { assign, createMachine } from 'xstate'
2
- import api from '../../api/payment.js'
3
-
4
- export default function (request) {
5
- return createMachine(
6
- {
7
- id: 'withdraw',
8
- initial: 'loading',
9
- context: {
10
- crypto: null,
11
- networks: [],
12
- currentNetwork: null,
13
- currentAddress: '',
14
- withdrawMethods: [],
15
- withdrawRules: [],
16
- withdrawLimit: null,
17
- withdrawAmount: null,
18
- differAmountLimit: null,
19
- tutorialUrls: [],
20
- enableWithdraw: false,
21
- description: {},
22
- policy: '',
23
- ruleName: '',
24
- balance: null,
25
- charge: null,
26
- isFree: false,
27
- },
28
- states: {
29
- loading: {
30
- invoke: {
31
- src: 'preset',
32
- onDone: {
33
- target: 'checking',
34
- actions: ['setCrypto', 'setWithdrawCondition', 'checkMinimumWithdraw'],
35
- },
36
- onError: {
37
- target: 'fail',
38
- },
39
- },
40
- },
41
- fail: {},
42
- checking: {
43
- always: [{ target: 'transferable', cond: 'isTransferable' }, { target: 'notTransferable' }],
44
- },
45
- notTransferable: {},
46
- transferable: {
47
- initial: 'operation',
48
- states: {
49
- operation: {
50
- on: {
51
- SUBMIT: [
52
- {
53
- target: '#withdraw.exchanging',
54
- cond: 'isFree',
55
- },
56
- {
57
- target: '#withdraw.transferring',
58
- },
59
- ],
60
- },
61
- },
62
- calculating: {
63
- entry: 'resetCharge',
64
- invoke: {
65
- src: 'fetchCalculation',
66
- onDone: {
67
- target: 'operation',
68
- actions: 'setCalculatedResult',
69
- },
70
- onError: {
71
- target: 'operation',
72
- },
73
- },
74
- },
75
- },
76
- on: {
77
- SET_NETWORK: {
78
- actions: 'setNetwork',
79
- },
80
- SET_ADDRESS: {
81
- actions: 'setAddress',
82
- },
83
- CALCULATE: {
84
- target: '.calculating',
85
- cond: 'isCalculable',
86
- },
87
- },
88
- },
89
- exchanging: {
90
- invoke: {
91
- src: 'submitWithdrawFree',
92
- onDone: {
93
- target: 'transferred',
94
- actions: 'setOtp',
95
- },
96
- onError: {
97
- target: 'transferable.operation',
98
- },
99
- },
100
- },
101
- transferring: {
102
- invoke: {
103
- src: 'submitWithdraw',
104
- onDone: {
105
- target: 'transferred',
106
- actions: 'setOtp',
107
- },
108
- onError: {
109
- target: 'transferable.operation',
110
- },
111
- },
112
- },
113
- transferred: {},
114
- },
115
- },
116
- {
117
- actions: {
118
- setWithdrawCondition: assign((context, event) => {
119
- const { adUrl, balance, description, differAmountLimit, enableWithdraw, enableWithdrawAmount, policy, ruleName, rules } =
120
- event.data.withdraw
121
-
122
- return {
123
- differAmountLimit: differAmountLimit,
124
- withdrawAmount: enableWithdrawAmount,
125
- withdrawLimit: enableWithdraw
126
- ? 0
127
- : rules.find((rule) => balance < rule.greaterEqualThen)?.greaterEqualThen || parseFloat(balance) + parseFloat(differAmountLimit),
128
- withdrawRules: rules.filter((rule) => rule.maxWithdrawAmount > 0),
129
- tutorialUrls: adUrl.map((item) => item.url),
130
- enableWithdraw,
131
- description,
132
- policy,
133
- ruleName,
134
- balance,
135
- isFree: policy === 'FREE',
136
- }
137
- }),
138
- setCrypto: assign((context, event) => {
139
- const method = event.data.payment?.[0]
140
- return {
141
- crypto: method,
142
- networks:
143
- method?.networkName?.map?.((value, i) => {
144
- return {
145
- label: method?.networkOptional?.[i],
146
- value,
147
- icon: value,
148
- }
149
- }) || [],
150
- }
151
- }),
152
- setNetwork: assign((context, event) => {
153
- return {
154
- currentNetwork: event.data,
155
- }
156
- }),
157
- setAddress: assign((context, event) => {
158
- return {
159
- currentAddress: event.data,
160
- }
161
- }),
162
- setCalculatedResult: assign((context, event) => {
163
- return {
164
- charge: event.data.charge,
165
- }
166
- }),
167
- resetCharge: assign((context, event) => {
168
- return {
169
- charge: null,
170
- }
171
- }),
172
- checkMinimumWithdraw: assign((context, event) => {
173
- const { balance, withdrawLimit, crypto, isFree } = context
174
- if (!isFree && crypto?.minimum > withdrawLimit) {
175
- const limit = crypto?.minimum
176
- const distance = Math.max(limit - balance, 0)
177
- return {
178
- differAmountLimit: distance,
179
- withdrawLimit: limit,
180
- }
181
- }
182
- }),
183
- setOtp: assign((context, event) => {
184
- const { requestId, status, target, ttl } = event.data
185
- return {
186
- requestId,
187
- status,
188
- target,
189
- ttl,
190
- }
191
- }),
192
- },
193
- guards: {
194
- isFree: (context, event) => {
195
- return context.isFree
196
- },
197
- isTransferable: (context, event) => {
198
- const { enableWithdraw, balance, withdrawLimit } = context
199
- return enableWithdraw && balance >= withdrawLimit
200
- },
201
- isCalculable: (context, event) => {
202
- const { currentNetwork, balance } = context
203
- return currentNetwork && balance
204
- },
205
- },
206
- services: {
207
- preset: async (context, event) => {
208
- const [withdraw, payment] = await Promise.all([
209
- request.send(api.getCurrentWithdrawRule()),
210
- request.send(api.getPaymentDetail({ transferType: 'WITHDRAW' })),
211
- ])
212
- return { withdraw, payment }
213
- },
214
- submitWithdraw: async (context, event) => {
215
- const { crypto, currentNetwork, currentAddress, withdrawAmount } = context
216
- const method = crypto.method
217
- return await request.send(
218
- api.submitWithdrawTransactionCrypto({
219
- method,
220
- amount: withdrawAmount,
221
- cryptoCurrency: crypto.channel,
222
- cryptoNetwork: currentNetwork.value,
223
- targetAddress: currentAddress,
224
- })
225
- )
226
- },
227
- submitWithdrawFree: async (context, event) => {
228
- const { currentNetwork, currentAddress } = context
229
- return await request.send(
230
- api.convertFreeCredit({
231
- exchangeType: 1,
232
- cryptoNetwork: currentNetwork.value,
233
- targetAddress: currentAddress,
234
- })
235
- )
236
- },
237
- fetchCalculation: async (context, event) => {
238
- const { crypto, currentNetwork, withdrawAmount } = context
239
- const method = crypto.method
240
- return await request.send(
241
- api.getWithdrawExpectedResult({
242
- method,
243
- amount: withdrawAmount,
244
- cryptoNetwork: currentNetwork.value,
245
- })
246
- )
247
- },
248
- },
249
- }
250
- )
251
- }