@jetthai/cuora 1.0.24 → 1.0.26

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/api/activity.js +46 -0
  3. package/api/auth.js +15 -0
  4. package/api/common.js +4 -0
  5. package/api/game.js +32 -0
  6. package/api/index.js +9 -0
  7. package/api/mailbox.js +20 -0
  8. package/api/payment.js +63 -0
  9. package/api/user.js +19 -0
  10. package/index.js +3 -58
  11. package/machine/activity/activity.js +73 -0
  12. package/machine/activity/continue_deposit_activity.js +132 -0
  13. package/machine/activity/deposit_activity.js +86 -0
  14. package/machine/activity/diamond_shop_activity.js +163 -0
  15. package/machine/activity/fafafa_activity.js +131 -0
  16. package/machine/activity/index.js +21 -0
  17. package/machine/activity/invite_friends_activity.js +121 -0
  18. package/machine/activity/ranking_activity.js +76 -0
  19. package/machine/activity/return_bonus_activity.js +150 -0
  20. package/machine/activity/specify_bets_activity.js +116 -0
  21. package/machine/auth.js +245 -0
  22. package/machine/auth_otp.js +191 -0
  23. package/machine/check.js +113 -0
  24. package/machine/common.js +163 -0
  25. package/machine/deposit.js +236 -0
  26. package/machine/error.js +72 -0
  27. package/machine/game.js +265 -0
  28. package/machine/game_favorite.js +123 -0
  29. package/machine/game_search.js +248 -0
  30. package/machine/inbox.js +246 -0
  31. package/machine/index.js +37 -0
  32. package/machine/merge.js +57 -0
  33. package/machine/payment_data.js +81 -0
  34. package/machine/reward.js +73 -0
  35. package/machine/setting.js +37 -0
  36. package/machine/side.js +147 -0
  37. package/machine/withdraw.js +367 -0
  38. package/package.json +3 -2
  39. package/request/config.js +18 -0
  40. package/request/cookie.js +52 -0
  41. package/request/request.js +97 -0
  42. package/plugin/activity.js +0 -289
  43. package/plugin/core.js +0 -518
  44. package/plugin/game.js +0 -474
  45. package/plugin/inbox.js +0 -216
  46. package/plugin/information.js +0 -57
  47. package/plugin/job/network.js +0 -61
  48. package/plugin/payment.js +0 -257
  49. package/plugin/request.js +0 -225
  50. package/plugin/search.js +0 -277
  51. package/plugin/user.js +0 -198
@@ -0,0 +1,367 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import api from '../api/payment'
3
+ import findLastIndex from 'lodash/findLastIndex'
4
+
5
+ export default function (request) {
6
+ return createMachine(
7
+ {
8
+ id: 'withdraw',
9
+ initial: 'idle',
10
+ context: {
11
+ isTurnoverMode: false,
12
+ siteConfig: null,
13
+ userInfo: null,
14
+ crypto: null,
15
+ networks: [],
16
+ currentNetwork: '',
17
+ currentAddress: '',
18
+ withdrawMethods: [],
19
+ withdrawRuleName: null,
20
+ withdrawRules: [],
21
+ withdrawLimit: null,
22
+ withdrawAmount: null,
23
+ withdrawLeft: null,
24
+ tutorialUrls: [],
25
+ balance: null,
26
+ charge: null,
27
+ },
28
+ states: {
29
+ idle: {
30
+ on: {
31
+ PRESET: {
32
+ target: 'mode',
33
+ actions: ['setSiteConfig', 'setUserInfo', 'preset'],
34
+ },
35
+ },
36
+ },
37
+ mode: {
38
+ always: [{ target: 'free', cond: 'isFree' }, { target: 'cash' }],
39
+ },
40
+ free: {
41
+ initial: 'preparing',
42
+ states: {
43
+ preparing: {
44
+ invoke: {
45
+ src: 'fetchWithdrawRules',
46
+ onDone: {
47
+ target: 'convertible',
48
+ actions: 'updateWithdrawCondition',
49
+ },
50
+ onError: {
51
+ target: 'notConvertible',
52
+ },
53
+ },
54
+ },
55
+ notConvertible: {},
56
+ convertible: {
57
+ initial: 'preparing',
58
+ states: {
59
+ preparing: {
60
+ invoke: {
61
+ src: 'fetchPayments',
62
+ onDone: {
63
+ target: 'operation',
64
+ actions: 'setCrypto',
65
+ },
66
+ onError: {
67
+ target: '#withdraw.free.notConvertible',
68
+ },
69
+ },
70
+ },
71
+ operation: {
72
+ on: {
73
+ SET_NETWORK: {
74
+ actions: 'setNetwork',
75
+ },
76
+ SET_ADDRESS: {
77
+ actions: 'setAddress',
78
+ },
79
+ CALCULATE: {
80
+ target: 'calculating',
81
+ cond: 'isCalculable',
82
+ },
83
+ SUBMIT: {
84
+ target: '#withdraw.free.converting',
85
+ },
86
+ },
87
+ },
88
+ calculating: {
89
+ entry: 'resetCharge',
90
+ invoke: {
91
+ src: 'fetchCalculation',
92
+ onDone: {
93
+ target: 'operation',
94
+ actions: 'setCalculatedResult',
95
+ },
96
+ onError: {
97
+ target: 'operation',
98
+ },
99
+ },
100
+ },
101
+ },
102
+ },
103
+ converting: {
104
+ invoke: {
105
+ src: 'submitWithdrawFree',
106
+ onDone: {
107
+ target: 'converted',
108
+ },
109
+ onError: {
110
+ target: 'convertible.operation',
111
+ },
112
+ },
113
+ },
114
+ converted: {},
115
+ },
116
+ },
117
+ cash: {
118
+ initial: 'checking',
119
+ states: {
120
+ checking: {
121
+ entry: 'calculateCashLeft',
122
+ always: [
123
+ { target: 'disable', cond: 'isWithdrawDisable' },
124
+ { target: 'transferable', cond: 'isTransferable' },
125
+ { target: 'notTransferable' },
126
+ ],
127
+ },
128
+ disable: {},
129
+ notTransferable: {},
130
+ transferable: {
131
+ entry: 'setBalanceToWithdrawAmount',
132
+ initial: 'preparing',
133
+ states: {
134
+ preparing: {
135
+ invoke: {
136
+ src: 'fetchPayments',
137
+ onDone: {
138
+ target: 'operation',
139
+ actions: 'setCrypto',
140
+ },
141
+ onError: {
142
+ target: '#withdraw.cash.checking',
143
+ },
144
+ },
145
+ },
146
+ operation: {
147
+ on: {
148
+ SET_NETWORK: {
149
+ actions: 'setNetwork',
150
+ },
151
+ SET_ADDRESS: {
152
+ actions: 'setAddress',
153
+ },
154
+ CALCULATE: {
155
+ target: 'calculating',
156
+ cond: 'isCalculable',
157
+ },
158
+ SUBMIT: {
159
+ target: '#withdraw.cash.transferring',
160
+ },
161
+ },
162
+ },
163
+ calculating: {
164
+ entry: 'resetCharge',
165
+ invoke: {
166
+ src: 'fetchCalculation',
167
+ onDone: {
168
+ target: 'operation',
169
+ actions: 'setCalculatedResult',
170
+ },
171
+ onError: {
172
+ target: 'operation',
173
+ },
174
+ },
175
+ },
176
+ },
177
+ },
178
+ transferring: {
179
+ invoke: {
180
+ src: 'submitWithdraw',
181
+ onDone: {
182
+ target: 'transferred',
183
+ },
184
+ onError: {
185
+ target: 'transferable.operation',
186
+ },
187
+ },
188
+ },
189
+ transferred: {},
190
+ },
191
+ },
192
+ },
193
+ },
194
+ {
195
+ actions: {
196
+ setUserInfo: assign((context, event) => {
197
+ return {
198
+ userInfo: event.data.userInfo,
199
+ }
200
+ }),
201
+ setSiteConfig: assign((context, event) => {
202
+ return {
203
+ siteConfig: event.data.siteConfig,
204
+ }
205
+ }),
206
+ preset: assign((context, event) => {
207
+ const { userInfo, siteConfig } = event.data
208
+ const { withdraw_rule, withdraw_limit } = userInfo || {}
209
+ const balance = parseFloat(userInfo?.balance)
210
+ const methods = siteConfig?.paymentMethods?.withdraw || []
211
+ let withdrawLimit
212
+ if (methods.includes('amb')) {
213
+ withdrawLimit = (withdraw_limit + balance) * -1
214
+ } else {
215
+ withdrawLimit = withdraw_limit * -1
216
+ }
217
+ return {
218
+ balance,
219
+ withdrawRuleName: withdraw_rule,
220
+ withdrawLimit,
221
+ }
222
+ }),
223
+ updateWithdrawCondition: assign((context, event) => {
224
+ const rules = event.data.rules.filter((rule) => rule.maxWithdrawAmount > 0)
225
+ const tutorialUrls = event.data.adUrl.map((item) => item.url)
226
+
227
+ const { balance, withdrawLimit } = context
228
+ const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
229
+
230
+ let left = 0,
231
+ limit,
232
+ amount = 0
233
+ if (lastMatchedIndex > -1) {
234
+ rules[lastMatchedIndex].match = true
235
+ amount = rules[lastMatchedIndex].maxWithdrawAmount
236
+ limit = rules[lastMatchedIndex].greaterEqualThen
237
+ } else if (withdrawLimit) {
238
+ const diff = withdrawLimit - balance
239
+ limit = withdrawLimit
240
+ if (diff < 0) {
241
+ amount = balance
242
+ } else {
243
+ left = diff
244
+ }
245
+ } else {
246
+ left = rules[0]?.greaterEqualThen - balance
247
+ limit = rules[0]?.greaterEqualThen
248
+ }
249
+
250
+ return {
251
+ withdrawLeft: left,
252
+ withdrawAmount: amount,
253
+ withdrawLimit: limit,
254
+ withdrawRules: rules,
255
+ tutorialUrls,
256
+ }
257
+ }),
258
+ setBalanceToWithdrawAmount: assign((context, event) => {
259
+ return {
260
+ withdrawAmount: context.balance,
261
+ }
262
+ }),
263
+ setCrypto: assign((context, event) => {
264
+ return {
265
+ crypto: event.data?.[0],
266
+ networks: event.data?.[0]?.optional || [],
267
+ }
268
+ }),
269
+ setNetwork: assign((context, event) => {
270
+ return {
271
+ currentNetwork: event.data,
272
+ }
273
+ }),
274
+ setAddress: assign((context, event) => {
275
+ return {
276
+ currentAddress: event.data,
277
+ }
278
+ }),
279
+ setCalculatedResult: assign((context, event) => {
280
+ return {
281
+ charge: event.data.charge,
282
+ }
283
+ }),
284
+ resetCharge: assign((context, event) => {
285
+ return {
286
+ charge: null,
287
+ }
288
+ }),
289
+ calculateCashLeft: assign((context, event) => {
290
+ const { balance, withdrawLimit } = context
291
+ const diff = withdrawLimit - balance
292
+ const left = Math.max(diff, 0)
293
+ return {
294
+ withdrawLeft: left,
295
+ }
296
+ }),
297
+ },
298
+ guards: {
299
+ isFree: (context, event) => {
300
+ return context.userInfo?.withdraw_policy === 'FREE'
301
+ },
302
+ isWithdrawDisable: (context, event) => {
303
+ const { siteConfig } = context
304
+ const methods = siteConfig?.paymentMethods?.withdraw || []
305
+ return methods.length === 0
306
+ },
307
+ isTransferable: (context, event) => {
308
+ const { balance, withdrawLimit, isTurnoverMode } = context
309
+ if (balance === 0) {
310
+ return false
311
+ }
312
+ if (isTurnoverMode) {
313
+ return withdrawLimit === 0
314
+ } else {
315
+ return balance - withdrawLimit >= 0
316
+ }
317
+ },
318
+ isCalculable: (context, event) => {
319
+ const { currentNetwork, balance } = context
320
+ return currentNetwork && balance
321
+ },
322
+ },
323
+ services: {
324
+ fetchWithdrawRules: async (context, event) => {
325
+ return await request.send(api.getCurrentWithdrawRule())
326
+ },
327
+ fetchPayments: async (context, event) => {
328
+ return await request.send(api.getPaymentDetail({ transferType: 'WITHDRAW' }))
329
+ },
330
+ submitWithdraw: async (context, event) => {
331
+ const { crypto, currentNetwork, currentAddress, withdrawAmount } = context
332
+ const method = crypto.method
333
+ return await request.send(
334
+ api.submitWithdrawTransaction({
335
+ method,
336
+ amount: withdrawAmount,
337
+ cryptoCurrency: crypto.channel,
338
+ cryptoNetwork: currentNetwork,
339
+ targetAddress: currentAddress,
340
+ })
341
+ )
342
+ },
343
+ submitWithdrawFree: async (context, event) => {
344
+ const { currentNetwork, currentAddress } = context
345
+ return await request.send(
346
+ api.convertFreeCredit({
347
+ exchangeType: 1,
348
+ cryptoNetwork: currentNetwork,
349
+ targetAddress: currentAddress,
350
+ })
351
+ )
352
+ },
353
+ fetchCalculation: async (context, event) => {
354
+ const { crypto, currentNetwork, withdrawAmount } = context
355
+ const method = crypto.method
356
+ return await request.send(
357
+ api.getWithdrawExpectedResult({
358
+ method,
359
+ amount: withdrawAmount,
360
+ cryptoNetwork: currentNetwork,
361
+ })
362
+ )
363
+ },
364
+ },
365
+ }
366
+ )
367
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@
20
20
  "dependencies": {
21
21
  "lodash": "^4.17.21",
22
22
  "universal-cookie": "^4.0.4",
23
- "url-join": "^4.0.1"
23
+ "url-join": "^4.0.1",
24
+ "xstate": "^4.25.0"
24
25
  }
25
26
  }
@@ -0,0 +1,18 @@
1
+ import urljoin from 'url-join'
2
+
3
+ export default class Config {
4
+ constructor({ apiURL, apiVersion, clientId, appSecret, platform, version }) {
5
+ this.apiURL = apiURL
6
+ this.apiVersion = apiVersion
7
+ this.clientId = clientId
8
+ this.appSecret = appSecret
9
+ this.platform = platform
10
+ this.version = version
11
+ this.fullURL = urljoin(this.apiURL, this.apiVersion, this.clientId)
12
+ }
13
+
14
+ setApiURL(url) {
15
+ this.apiURL = url
16
+ this.fullURL = urljoin(this.apiURL, this.apiVersion, this.clientId)
17
+ }
18
+ }
@@ -0,0 +1,52 @@
1
+ import Cookies from 'universal-cookie'
2
+
3
+ export default class Cookie {
4
+ constructor(tokenKey) {
5
+ this.cookies = new Cookies()
6
+ this.tokenKey = tokenKey
7
+ }
8
+
9
+ setToken(token) {
10
+ this.cookies.set(this.tokenKey, token, {
11
+ path: '/',
12
+ maxAge: 300,
13
+ })
14
+ }
15
+
16
+ prolongToken() {
17
+ const token = this.getToken()
18
+ if (token) {
19
+ this.setToken(token)
20
+ }
21
+ }
22
+
23
+ getToken() {
24
+ return this.cookies.get(this.tokenKey)
25
+ }
26
+
27
+ removeToken() {
28
+ this.cookies.remove(this.tokenKey, {
29
+ path: '/',
30
+ maxAge: 300,
31
+ })
32
+ }
33
+
34
+ setUser(user) {
35
+ this.cookies.set('cuora_tempuser', user, {
36
+ path: '/',
37
+ maxAge: 60 * 60 * 24,
38
+ })
39
+ }
40
+
41
+ getUser() {
42
+ return this.cookies.get('cuora_tempuser')
43
+ }
44
+
45
+ set(name, value, options) {
46
+ this.cookies.set(name, value, options)
47
+ }
48
+
49
+ get(name, options) {
50
+ return this.cookies.get(name, options)
51
+ }
52
+ }
@@ -0,0 +1,97 @@
1
+ import pickBy from 'lodash/pickBy'
2
+
3
+ function isValid(value) {
4
+ return value !== undefined && value !== null
5
+ }
6
+
7
+ export default class Request {
8
+ constructor(axios, config, cookie) {
9
+ this.axios = axios
10
+ this.source = axios.CancelToken.source()
11
+ this.config = config
12
+ this.cookie = cookie
13
+ this.errorHandler = null
14
+ this.initialize()
15
+ }
16
+
17
+ initialize() {
18
+ this.axios.interceptors.request.use((axiosConfig) => {
19
+ axiosConfig.headers['X-TH1GAMES-VERSION'] = this.config.version
20
+ axiosConfig.headers['X-TH1GAMES-PLATFORM'] = this.config.platform
21
+ if (this.cookie.getToken()) {
22
+ axiosConfig.headers.Authorization = this.cookie.getToken()
23
+ }
24
+ return axiosConfig
25
+ })
26
+
27
+ this.axios.interceptors.response.use(
28
+ function (response) {
29
+ if (response.data.error) {
30
+ return Promise.reject(response.data.error)
31
+ }
32
+ return Promise.resolve(response)
33
+ },
34
+ function (error) {
35
+ if (error.message === 'Network Error' || error.message === 'Request failed with status code 404') {
36
+ return Promise.reject({ code: 'ERR_998' })
37
+ }
38
+ return Promise.reject(error.response?.data?.error || error)
39
+ }
40
+ )
41
+ }
42
+
43
+ setErrorHandler(handler) {
44
+ this.errorHandler = handler
45
+ }
46
+
47
+ async send(req, customToken) {
48
+ try {
49
+ const cancelToken = customToken || this.source.token
50
+ this._checkToken(req, this.cookie.getToken())
51
+ const requestConfig = this._createConfig(req, cancelToken)
52
+ const { data } = await this.axios.request(requestConfig)
53
+ return data
54
+ } catch (error) {
55
+ if (error.code) {
56
+ this.errorHandler && this.errorHandler(error)
57
+ }
58
+ throw error
59
+ }
60
+ }
61
+
62
+ sourceUpdate() {
63
+ this.source.cancel('Requests Cancelled')
64
+ this.source = this.axios.CancelToken.source()
65
+ }
66
+
67
+ sourceGenerate() {
68
+ return this.axios.CancelToken.source()
69
+ }
70
+
71
+ _createConfig(req, cancelToken) {
72
+ const { method, url, params, isPublic } = req
73
+ // const config = { baseURL: this.config.fullURL, method, url, cancelToken, isPublic: req.isPublic }
74
+ // if (method.toUpperCase() === 'GET') {
75
+ // config.params = pickBy(params, isValid)
76
+ // } else {
77
+ // config.data = pickBy(params, isValid)
78
+ // }
79
+ // return config
80
+
81
+ return {
82
+ baseURL: this.config.fullURL,
83
+ method,
84
+ url,
85
+ cancelToken,
86
+ isPublic,
87
+ [method.toUpperCase() === 'GET' ? 'params' : 'data']: pickBy(params, isValid),
88
+ }
89
+ }
90
+
91
+ _checkToken(req, token) {
92
+ if (!req.isPublic && !token) {
93
+ console.error(req)
94
+ throw { code: 'ERR_A103', url: req.url }
95
+ }
96
+ }
97
+ }