@jetthai/cuora 1.1.488 → 2.0.0-beta.0

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 (212) hide show
  1. package/.husky/pre-commit +3 -1
  2. package/BREAKING_CHANGES.md +9 -0
  3. package/CHANGELOG.md +3 -316
  4. package/README.md +15 -299
  5. package/package.json +5 -5
  6. package/resetGitBranch.sh +101 -0
  7. package/src/api/account.js +1 -0
  8. package/src/api/activity.js +39 -24
  9. package/src/api/auth.js +36 -5
  10. package/src/api/common.js +3 -5
  11. package/src/api/game.js +10 -0
  12. package/src/api/payment.js +55 -17
  13. package/src/api/user.js +16 -10
  14. package/src/index.js +2 -2
  15. package/src/lib/fingerprint_v3_11_12.js +11433 -0
  16. package/src/machine/activity/activity.js +213 -0
  17. package/src/machine/activity/activity_cash.js +36 -84
  18. package/src/machine/activity/cash_rain_activity.js +1 -0
  19. package/src/machine/activity/continue_deposit_activity.js +1 -0
  20. package/src/machine/activity/deposit_activity.js +1 -0
  21. package/src/machine/activity/diamond_shop_activity_cash.js +8 -0
  22. package/src/machine/activity/diamond_shop_activity_fc.js +197 -0
  23. package/src/machine/activity/diamond_shop_activity_vn.js +179 -0
  24. package/src/machine/activity/fafafa_activity.js +11 -0
  25. package/src/machine/activity/friend_invite_friends_activity.js +7 -8
  26. package/src/machine/activity/index.js +12 -0
  27. package/src/machine/activity/instant_rebate_activity.js +1 -0
  28. package/src/machine/activity/invite_friends_activity.js +115 -0
  29. package/src/machine/activity/jackpot.js +1 -0
  30. package/src/machine/activity/mini_game_activity.js +69 -0
  31. package/src/machine/activity/otp_verified_activity.js +1 -0
  32. package/src/machine/activity/ranking_activity.js +1 -0
  33. package/src/machine/activity/return_bonus_activity.js +155 -0
  34. package/src/machine/activity/roulette_activity_v2.js +1 -0
  35. package/src/machine/activity/sign_in_activity.js +1 -0
  36. package/src/machine/activity/sign_in_monthly_activity.js +2 -1
  37. package/src/machine/activity/specify_bets_activity.js +1 -0
  38. package/src/machine/activity/specify_turnover_activity.js +1 -0
  39. package/src/machine/activity/time_bomb_activity.js +1 -0
  40. package/src/machine/activity/vip_card_activity.js +1 -0
  41. package/src/machine/auth.js +112 -149
  42. package/src/machine/auth_otp_v2.js +178 -0
  43. package/src/machine/auth_otp_v3.js +22 -64
  44. package/src/machine/bank_binding.js +3 -2
  45. package/src/machine/bank_binding_amb.js +146 -0
  46. package/src/machine/bonus.js +76 -0
  47. package/src/machine/common.js +98 -14
  48. package/src/machine/deposit/amb.js +93 -0
  49. package/src/machine/deposit/crypto_bank.js +340 -0
  50. package/src/machine/deposit/crypto_bank_cash.js +35 -20
  51. package/src/machine/deposit/crypto_qrcode.js +158 -0
  52. package/src/machine/deposit/crypto_qrcode_cash.js +64 -26
  53. package/src/machine/deposit/crypto_way.js +253 -0
  54. package/src/machine/deposit/crypto_way_cash.js +37 -48
  55. package/src/machine/deposit/deposit_fc.js +272 -0
  56. package/src/machine/deposit/direct.js +311 -0
  57. package/src/machine/deposit/direct_cash.js +10 -8
  58. package/src/machine/deposit/float.js +375 -0
  59. package/src/machine/deposit/index.js +28 -0
  60. package/src/machine/deposit/krway.js +152 -0
  61. package/src/machine/deposit/loadcard_cash.js +32 -14
  62. package/src/machine/deposit/loadcard_vn.js +352 -0
  63. package/src/machine/deposit/manual_cash.js +11 -8
  64. package/src/machine/deposit/manual_vn.js +459 -0
  65. package/src/machine/deposit/qrcode_cash.js +32 -15
  66. package/src/machine/deposit/qrcode_v2.js +339 -0
  67. package/src/machine/deposit/usdt.js +163 -0
  68. package/src/machine/deposit/usdt_cash.js +15 -1
  69. package/src/machine/deposit/virtual_bank_cash.js +12 -13
  70. package/src/machine/deposit/virtual_bank_vn.js +270 -0
  71. package/src/machine/deposit/vpay.js +374 -0
  72. package/src/machine/error.js +1 -7
  73. package/src/machine/game_betslip.js +1 -0
  74. package/src/machine/game_favorite_v2.js +1 -0
  75. package/src/machine/game_hot_new.js +80 -0
  76. package/src/machine/game_info.js +13 -1
  77. package/src/machine/game_replay.js +47 -0
  78. package/src/machine/game_search_cash.js +1 -0
  79. package/src/machine/game_search_v2.js +402 -0
  80. package/src/machine/game_v2_1.js +126 -39
  81. package/src/machine/honor_board.js +133 -0
  82. package/src/machine/honor_board_cash.js +28 -22
  83. package/src/machine/inbox.js +105 -43
  84. package/src/machine/index.js +32 -0
  85. package/src/machine/leaderboard.js +12 -8
  86. package/src/machine/merge.js +58 -0
  87. package/src/machine/navbar.js +3 -1
  88. package/src/machine/payment_data.js +87 -0
  89. package/src/machine/promotions.js +163 -0
  90. package/src/machine/ranking.js +30 -45
  91. package/src/machine/reward.js +256 -0
  92. package/src/machine/reward_cash.js +42 -2
  93. package/src/machine/reward_fc.js +99 -0
  94. package/src/machine/roulette_v2.js +11 -14
  95. package/src/machine/setting.js +1 -0
  96. package/src/machine/side.js +160 -0
  97. package/src/machine/side_cash.js +3 -3
  98. package/src/machine/side_v2.js +223 -0
  99. package/src/machine/user_info.js +1 -0
  100. package/src/machine/user_photo.js +1 -0
  101. package/src/machine/user_reset_password.js +14 -0
  102. package/src/machine/user_sign_up.js +61 -46
  103. package/src/machine/vip_table.js +58 -0
  104. package/src/machine/websocket.js +349 -374
  105. package/src/machine/welcome.js +111 -0
  106. package/src/machine/withdraw/index.js +6 -0
  107. package/src/machine/withdraw/withdraw_crypto_cash.js +328 -0
  108. package/src/machine/withdraw/withdraw_krway.js +223 -0
  109. package/src/machine/withdraw/withdraw_manual.js +541 -0
  110. package/src/machine/withdraw/withdraw_manual_cash.js +39 -59
  111. package/src/request/config.js +0 -2
  112. package/src/request/request.js +60 -124
  113. package/src/util/constant.js +2 -6
  114. package/src/util/deviceToken.js +60 -0
  115. package/src/util/index.js +4 -2
  116. package/src/util/math.js +4 -0
  117. package/src/util/storage.js +4 -3
  118. package/test/api.behavior.test.js +69 -0
  119. package/test/api.contract.test.js +18 -0
  120. package/test/config.test.js +27 -0
  121. package/test/deviceToken.test.js +139 -0
  122. package/test/helpers.coverage.test.js +11 -0
  123. package/test/helpers.js +91 -0
  124. package/test/index.exports.test.js +14 -0
  125. package/test/machine.activity.core.coverage.test.js +337 -0
  126. package/test/machine.activity.gap.coverage.test.js +252 -0
  127. package/test/machine.activity.more.coverage.test.js +180 -0
  128. package/test/machine.activity_plus_and_binding_amb.coverage.test.js +201 -0
  129. package/test/machine.activity_timer_bonus.coverage.test.js +136 -0
  130. package/test/machine.activity_welcome.low.coverage.test.js +211 -0
  131. package/test/machine.amb_rewardfc_websocket.more.coverage.test.js +201 -0
  132. package/test/machine.auth.coverage.test.js +215 -0
  133. package/test/machine.bank_binding.test.js +143 -0
  134. package/test/machine.binding_otp_krway.coverage.test.js +223 -0
  135. package/test/machine.contract.test.js +50 -0
  136. package/test/machine.crypto_bank.coverage.test.js +270 -0
  137. package/test/machine.crypto_way.coverage.test.js +108 -0
  138. package/test/machine.crypto_way_cash.coverage.test.js +114 -0
  139. package/test/machine.definition.test.js +137 -0
  140. package/test/machine.deposit.manual.coverage.test.js +365 -0
  141. package/test/machine.deposit_fc.coverage.test.js +126 -0
  142. package/test/machine.direct.coverage.test.js +96 -0
  143. package/test/machine.direct_cash.coverage.test.js +108 -0
  144. package/test/machine.error.coverage.test.js +37 -0
  145. package/test/machine.events.test.js +126 -0
  146. package/test/machine.favorite_jackpot.coverage.test.js +82 -0
  147. package/test/machine.float.coverage.test.js +136 -0
  148. package/test/machine.flow.test.js +226 -0
  149. package/test/machine.game_hot_new_vip_table.coverage.test.js +105 -0
  150. package/test/machine.game_search_cash.coverage.test.js +155 -0
  151. package/test/machine.game_search_v2.test.js +199 -0
  152. package/test/machine.game_user_profile.coverage.test.js +100 -0
  153. package/test/machine.honor_board.test.js +133 -0
  154. package/test/machine.inbox.common.coverage.test.js +304 -0
  155. package/test/machine.loadcard_cash.coverage.test.js +101 -0
  156. package/test/machine.loadcard_vn.coverage.test.js +122 -0
  157. package/test/machine.misc.coverage.test.js +361 -0
  158. package/test/machine.more2.coverage.test.js +261 -0
  159. package/test/machine.options.execution.test.js +272 -0
  160. package/test/machine.priority.coverage.test.js +603 -0
  161. package/test/machine.qrcode_cash.coverage.test.js +107 -0
  162. package/test/machine.qrcode_v2.coverage.test.js +121 -0
  163. package/test/machine.regression.test.js +82 -0
  164. package/test/machine.roulette_v2.test.js +142 -0
  165. package/test/machine.side_reward_reset.coverage.test.js +220 -0
  166. package/test/machine.targeted.coverage.test.js +313 -0
  167. package/test/machine.usdt.coverage.test.js +95 -0
  168. package/test/machine.usdt_cash.coverage.test.js +73 -0
  169. package/test/machine.vip_card_activity.test.js +114 -0
  170. package/test/machine.virtual_bank_cash.coverage.test.js +105 -0
  171. package/test/machine.virtual_bank_vn.coverage.test.js +105 -0
  172. package/test/machine.vpay.coverage.test.js +142 -0
  173. package/test/machine.withdraw_crypto_cash.coverage.test.js +166 -0
  174. package/test/machine.withdraw_krway.coverage.test.js +123 -0
  175. package/test/machine.withdraw_manual.coverage.test.js +185 -0
  176. package/test/machine.withdraw_manual_cash.coverage.test.js +123 -0
  177. package/test/request.test.js +325 -0
  178. package/test/util.storage.errors.test.js +47 -0
  179. package/test/util.test.js +41 -0
  180. package/.claude/commands/sync-ai-prompts.md +0 -93
  181. package/.claude/settings.local.json +0 -23
  182. package/.github/copilot-instructions.md +0 -448
  183. package/.husky/post-checkout +0 -32
  184. package/.husky/pre-push +0 -36
  185. package/AGENTS.md +0 -448
  186. package/AI_PROMPT_SOURCE.md +0 -446
  187. package/CLAUDE.md +0 -448
  188. package/GEMINI.md +0 -448
  189. package/docs/kibana/crypto-way-deposit-amount-bug-analysis.md +0 -1003
  190. package/docs/kibana/play-providerId-error.md +0 -202
  191. package/docs/kibana/play-selectedWalletType.md +0 -1
  192. package/docs/kibana/play-selectedWalletType_2.md +0 -1
  193. package/docs/kibana/play-selectedWalletType_analysis.md +0 -565
  194. package/docs/my-claude/feat-sharon-JT-1790247240-socket/branch-transfer.md +0 -14
  195. package/docs/my-claude/feat-sharon-JT-1790247240-socket/cash-module-preparation.json +0 -40
  196. package/docs/my-claude/feat-sharon-JT-1790247240-socket/cash-module-verification.md +0 -41
  197. package/docs/my-claude/feat-sharon-JT-1790247240-socket/implementation-ledger.md +0 -87
  198. package/docs/my-claude/feat-sharon-JT-1790247240-socket/module-report.md +0 -73
  199. package/docs/my-claude/feat-sharon-JT-1790247240-socket/plan.md +0 -309
  200. package/docs/my-claude/feat-sharon-JT-1790247240-socket/test-cases.md +0 -245
  201. package/docs/my-claude/feat-sharon-JT-1791180540-onesignal/transfer.md +0 -9
  202. package/scripts/auto-version.js +0 -68
  203. package/scripts/auto-version.sh +0 -33
  204. package/scripts/branch-auto-npm-publish.js +0 -29
  205. package/scripts/check-version.sh +0 -36
  206. package/scripts/clean-tags.sh +0 -84
  207. package/src/util/amount.js +0 -8
  208. package/src/util/err998SentryExtra.js +0 -26
  209. package/src/util/paramStore.js +0 -139
  210. package/src/util/socketPolicy.js +0 -57
  211. package/test/auth_otp_v3.test.js +0 -323
  212. package/tests/websocket.test.mjs +0 -779
@@ -1,15 +1,17 @@
1
1
  import { assign, createMachine } from 'xstate'
2
- import payment from '@jetthai/cuora/src/api/payment.js'
2
+ import payment from '../../api/payment.js'
3
3
 
4
4
  export default function (request) {
5
5
  return createMachine(
6
6
  {
7
7
  id: 'crypto_qrcode_cash',
8
+ predictableActionArguments: true,
8
9
  initial: 'loading',
9
10
  context: {
10
11
  currentCategory: 'cryptoQrcode',
11
12
  cryptos: [],
12
13
  selectedCrypto: null,
14
+ resultMap: {},
13
15
  result: {},
14
16
  },
15
17
  states: {
@@ -25,8 +27,19 @@ export default function (request) {
25
27
  },
26
28
  },
27
29
  },
30
+ finding: {
31
+ always: [
32
+ {
33
+ target: 'idle',
34
+ actions: 'setResultFromLocal',
35
+ cond: 'isFound',
36
+ },
37
+ {
38
+ target: 'submitting',
39
+ },
40
+ ],
41
+ },
28
42
  submitting: {
29
- entry: 'clearResult',
30
43
  invoke: {
31
44
  src: 'submitDeposit',
32
45
  onDone: {
@@ -43,11 +56,11 @@ export default function (request) {
43
56
  },
44
57
  on: {
45
58
  SET_CRYPTO: {
46
- target: '.submitting',
59
+ target: '.finding',
47
60
  actions: 'setCrypto',
48
61
  },
49
62
  SET_NETWORK: {
50
- target: '.submitting',
63
+ target: '.finding',
51
64
  actions: 'setNetwork',
52
65
  },
53
66
  },
@@ -57,45 +70,70 @@ export default function (request) {
57
70
  setInitData: assign((context, event) => {
58
71
  const paymentDetail = event.data || []
59
72
  const payments = paymentDetail.filter((item) => item.category === context.currentCategory)
60
- const cryptos = payments.flatMap((payment) => {
61
- const networks = payment.networks?.length > 0 ? payment.networks : [null]
62
- return networks.map((networkItem, index) => ({
63
- ...payment,
64
- label: payment.cryptoOptional?.[index] ?? payment.cryptoOptional?.[0],
65
- network: networkItem?.networkCode ?? payment.networkName?.[index] ?? payment.networkName?.[0],
66
- value: payment.method,
67
- icon: payment.channel,
68
- networkCode: networkItem?.networkCode ?? payment.networks?.[0]?.networkCode,
69
- networkDisplayName: networkItem?.networkDisplayName ?? payment.networks?.[0]?.networkDisplayName,
70
- }))
71
- })
73
+ const cryptos = payments.map((payment) => ({
74
+ ...payment,
75
+ label: payment.cryptoOptional?.[0],
76
+ network: payment.networkName?.[0],
77
+ value: payment.method,
78
+ icon: payment.channel,
79
+ }))
72
80
 
73
81
  return {
74
82
  cryptos,
75
83
  selectedCrypto: cryptos[0],
76
84
  }
77
85
  }),
78
- clearResult: assign({
79
- result: {},
80
- }),
81
86
  setCrypto: assign((context, event) => {
82
87
  const { cryptos } = context
83
- const data = event.data
84
- const selectedCrypto = data.networkCode
85
- ? cryptos.find((c) => c.channel === data.channel && c.networkCode === data.networkCode)
86
- : cryptos.find((c) => c.label === data.label)
88
+ const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
89
+ if (!selectedCrypto) {
90
+ return {}
91
+ }
87
92
  return { selectedCrypto }
88
93
  }),
89
94
  setNetwork: assign((context, event) => {
90
- return { selectedCrypto: event.data }
95
+ const { selectedCrypto } = context
96
+ const network = event.data?.value || event.data?.network || event.data?.label || event.data
97
+ if (!selectedCrypto || !network) {
98
+ return {}
99
+ }
100
+ return {
101
+ selectedCrypto: {
102
+ ...selectedCrypto,
103
+ network,
104
+ },
105
+ }
106
+ }),
107
+ setResultFromLocal: assign((context, event) => {
108
+ const { resultMap, selectedCrypto } = context
109
+ if (!selectedCrypto) {
110
+ return {}
111
+ }
112
+ return { result: resultMap[`${selectedCrypto.channel}_${selectedCrypto.network}`] }
91
113
  }),
92
114
  setResult: assign((context, event) => {
93
- const { accountNumber, action, currency, network, qrcode, forexRate, redirect, cryptoTag } = event.data
115
+ const { resultMap, selectedCrypto } = context
116
+ const { accountNumber, action, currency, network, qrcode, forexRate, redirect } = event.data
117
+ const result = { accountNumber, action, currency, network, qrcode, forexRate, redirect }
118
+
94
119
  return {
95
- result: { accountNumber, action, currency, network, qrcode, forexRate, redirect, cryptoTag },
120
+ result,
121
+ resultMap: {
122
+ ...resultMap,
123
+ [`${selectedCrypto.channel}_${selectedCrypto.network}`]: result,
124
+ },
96
125
  }
97
126
  }),
98
127
  },
128
+ guards: {
129
+ isFound: (context, event) => {
130
+ const { resultMap, selectedCrypto } = context
131
+ if (!selectedCrypto) {
132
+ return false
133
+ }
134
+ return resultMap[`${selectedCrypto.channel}_${selectedCrypto.network}`]
135
+ },
136
+ },
99
137
  services: {
100
138
  fetchPaymentDetail: async (context, event) => {
101
139
  return await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
@@ -0,0 +1,253 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import padStart from 'lodash/padStart.js'
3
+ import payment from '../../api/payment.js'
4
+ import common from '../../api/common.js'
5
+
6
+ export default function (request) {
7
+ const { appSecret } = request.config
8
+ return createMachine(
9
+ {
10
+ id: 'crypto_way',
11
+ predictableActionArguments: true,
12
+ initial: 'loading',
13
+ context: {
14
+ paymentDetails: [],
15
+
16
+ cryptos: [],
17
+ sourceAddress: '',
18
+
19
+ amount: null,
20
+ selectedCrypto: null,
21
+
22
+ currentCategory: 'cryptoWay',
23
+ currentPayment: null,
24
+
25
+ calculation: null,
26
+ result: null,
27
+
28
+ // use for wallet mode 4
29
+ promotion: null,
30
+ },
31
+ states: {
32
+ loading: {
33
+ invoke: {
34
+ src: 'fetchPayments',
35
+ onDone: {
36
+ target: 'loaded',
37
+ actions: 'setPayment',
38
+ },
39
+ onError: {
40
+ target: '#crypto_way.fail',
41
+ },
42
+ },
43
+ },
44
+ loaded: {
45
+ always: [{ target: 'debouncing', cond: 'isCalculable' }],
46
+ },
47
+ debouncing: {
48
+ after: {
49
+ 1000: {
50
+ target: 'calculating',
51
+ },
52
+ },
53
+ },
54
+ calculating: {
55
+ invoke: {
56
+ src: 'fetchCalculation',
57
+ onDone: {
58
+ target: 'calculated',
59
+ actions: 'setCalculation',
60
+ },
61
+ onError: {
62
+ target: 'loaded',
63
+ },
64
+ },
65
+ },
66
+ calculated: {
67
+ on: {
68
+ SUBMIT: [
69
+ {
70
+ target: 'submitting',
71
+ cond: 'isSubmittable',
72
+ },
73
+ {
74
+ target: 'pending',
75
+ },
76
+ ],
77
+ },
78
+ },
79
+ pending: {
80
+ on: {
81
+ SUBMIT: {
82
+ target: 'submitting',
83
+ cond: 'isSubmittable',
84
+ },
85
+ },
86
+ },
87
+ submitting: {
88
+ invoke: {
89
+ src: 'submitDeposit',
90
+ onDone: {
91
+ target: 'submitted',
92
+ actions: 'setResult',
93
+ },
94
+ onError: {
95
+ target: 'submitFail',
96
+ },
97
+ },
98
+ },
99
+ submitFail: {
100
+ after: {
101
+ 0: { target: 'loaded' },
102
+ },
103
+ },
104
+ submitted: {},
105
+ fail: {},
106
+ },
107
+ on: {
108
+ SET_CRYPTO: {
109
+ target: '.loaded',
110
+ actions: 'setCrypto',
111
+ },
112
+ SET_ADDRESS: {
113
+ actions: 'setAddress',
114
+ },
115
+ SET_AMOUNT: {
116
+ target: '.loaded',
117
+ actions: ['setAmount', 'clearCalculation'],
118
+ },
119
+ INPUT_AMOUNT: {
120
+ target: '.loaded',
121
+ actions: ['inputAmount', 'clearCalculation'],
122
+ },
123
+ CALCULATE: {
124
+ target: '.debouncing',
125
+ actions: 'clearCalculation',
126
+ },
127
+ SET_PROMOTION: {
128
+ actions: 'setPromotion',
129
+ },
130
+ },
131
+ },
132
+ {
133
+ actions: {
134
+ setPayment: assign((context, event) => {
135
+ const payments = event.data.filter((item) => item.category === context.currentCategory)
136
+ const cryptos = payments?.map((value, i) => {
137
+ return {
138
+ ...value,
139
+ label: value?.cryptoOptional[0],
140
+ network: value?.networkName[0],
141
+ icon: value?.channel,
142
+ }
143
+ })
144
+ return {
145
+ cryptos,
146
+ selectedCrypto: cryptos[0],
147
+ currentPayment: cryptos[0],
148
+ }
149
+ }),
150
+ setCrypto: assign((context, event) => {
151
+ const { cryptos } = context
152
+ const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
153
+ return {
154
+ selectedCrypto: selectedCrypto,
155
+ amount: null,
156
+ calculation: null,
157
+ }
158
+ }),
159
+ setAmount: assign((context, event) => {
160
+ return {
161
+ amount: event.data,
162
+ }
163
+ }),
164
+ inputAmount: assign((context, event) => {
165
+ return { amount: preciseAdd(`${parseFloat(context.amount || 0)}`, `${parseFloat(event.data)}`) }
166
+ }),
167
+ setAddress: assign((context, event) => {
168
+ return {
169
+ sourceAddress: event.data,
170
+ }
171
+ }),
172
+ setCalculation: assign((context, event) => {
173
+ // V2接口轉換中回傳的資料結構不同
174
+ const { data } = event.data
175
+ if (data) {
176
+ return {
177
+ calculation: data,
178
+ }
179
+ } else {
180
+ return {
181
+ calculation: event.data,
182
+ }
183
+ }
184
+ }),
185
+ clearCalculation: assign((context, event) => {
186
+ return {
187
+ calculation: null,
188
+ }
189
+ }),
190
+ setResult: assign((context, event) => {
191
+ return {
192
+ result: event.data,
193
+ }
194
+ }),
195
+ setPromotion: assign((context, event) => {
196
+ const promotion = event.data
197
+ return {
198
+ promotion,
199
+ }
200
+ }),
201
+ },
202
+ guards: {
203
+ isCalculable: (context, event) => {
204
+ const { selectedCrypto, amount } = context
205
+ return selectedCrypto && amount && selectedCrypto?.minimum <= +amount && +amount <= selectedCrypto?.maximum
206
+ },
207
+ isSubmittable: (context, event) => {
208
+ const { selectedCrypto, amount } = context
209
+ return selectedCrypto && amount
210
+ },
211
+ },
212
+ services: {
213
+ fetchPayments: async (context, event) => {
214
+ return await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
215
+ },
216
+ fetchCalculation: async (context, event) => {
217
+ const { selectedCrypto, amount, promotion } = context
218
+ return await request.send(
219
+ payment.getCryptoDepositExpectedResult({
220
+ method: selectedCrypto.method,
221
+ amount,
222
+ cryptoCurrency: selectedCrypto.channel,
223
+ promotionsNo: promotion?.activityNo,
224
+ })
225
+ )
226
+ },
227
+ submitDeposit: async (context, event) => {
228
+ const { selectedCrypto, amount } = context
229
+ return await request.send(
230
+ payment.submitDepositTransactionCrypto({
231
+ method: selectedCrypto.method,
232
+ amount: amount,
233
+ cryptoCurrency: selectedCrypto.channel,
234
+ cryptoNetwork: selectedCrypto.network,
235
+ })
236
+ )
237
+ },
238
+ },
239
+ }
240
+ )
241
+ }
242
+
243
+ function preciseAdd(a, b) {
244
+ let aStr = a.toString()
245
+ let bStr = b.toString()
246
+
247
+ let aDecimalPlaces = aStr.split('.')[1] ? aStr.split('.')[1].length : 0
248
+ let bDecimalPlaces = bStr.split('.')[1] ? bStr.split('.')[1].length : 0
249
+
250
+ let factor = Math.pow(10, Math.max(aDecimalPlaces, bDecimalPlaces))
251
+
252
+ return (Math.round(a * factor) + Math.round(b * factor)) / factor
253
+ }
@@ -1,11 +1,13 @@
1
1
  import { assign, createMachine } from 'xstate'
2
- import payment from '@jetthai/cuora/src/api/payment.js'
2
+ import payment from '../../api/payment.js'
3
+ import { max } from '../../util/index.js'
3
4
 
4
5
  export default function (request, context) {
5
6
  const { paymentDetail = [], promotion, selectedAmount } = context || {}
6
7
  return createMachine(
7
8
  {
8
9
  id: 'crypto_way_cash',
10
+ predictableActionArguments: true,
9
11
  initial: 'loading',
10
12
  context: {
11
13
  paymentDetail,
@@ -13,6 +15,7 @@ export default function (request, context) {
13
15
  selectedAmount,
14
16
 
15
17
  cryptos: [],
18
+ sourceAddress: '',
16
19
  options: [],
17
20
 
18
21
  selectedCrypto: null,
@@ -54,9 +57,8 @@ export default function (request, context) {
54
57
  target: 'calculated',
55
58
  actions: 'setCalculation',
56
59
  },
57
- // 計算失敗回到 loaded,避免進入 calculated 造成 UI 與 state 不一致
58
60
  onError: {
59
- target: 'loaded',
61
+ target: 'calculated',
60
62
  },
61
63
  },
62
64
  },
@@ -102,13 +104,16 @@ export default function (request, context) {
102
104
  target: '.debouncing',
103
105
  actions: 'setPromotion',
104
106
  internal: false,
105
- cond: 'isPromotionChanged',
106
107
  },
107
108
  SET_CRYPTO: {
108
109
  target: '.debouncing',
109
110
  actions: 'setCrypto',
110
111
  internal: false,
111
112
  },
113
+ SET_ADDRESS: {
114
+ actions: 'setAddress',
115
+ internal: false,
116
+ },
112
117
  SET_AMOUNT: {
113
118
  target: '.debouncing',
114
119
  actions: 'setAmount',
@@ -126,46 +131,38 @@ export default function (request, context) {
126
131
  setInitData: assign((context, event) => {
127
132
  const { paymentDetail } = context
128
133
  const payments = paymentDetail.filter((item) => item.category === context.currentCategory)
129
-
130
- // 安全地處理 networks 數組,添加空值檢查
131
- const cryptos = payments.flatMap((payment) => {
132
- if (!payment.networks || !Array.isArray(payment.networks)) {
133
- return []
134
- }
135
- return payment.networks.map((network) => ({
136
- ...payment,
137
- label: `${network.cryptoName || ''} - ${network.networkDisplayName || ''}`,
138
- value: payment.method,
139
- icon: payment.channel,
140
- networkCode: network.networkCode,
141
- }))
142
- })
143
-
144
- // 安全地處理空數組情況
145
- const defaultCrypto = cryptos.length > 0 ? cryptos[0] : null
134
+ const cryptos = payments.map((payment) => ({
135
+ ...payment,
136
+ label: payment.cryptoOptional?.[0],
137
+ network: payment.networkName?.[0],
138
+ value: payment.method,
139
+ icon: payment.channel,
140
+ }))
146
141
 
147
142
  return {
148
143
  cryptos,
149
- selectedCrypto: defaultCrypto,
150
- options: defaultCrypto?.optional || [],
144
+ selectedCrypto: cryptos[0],
145
+ options: cryptos[0]?.optional || [],
151
146
  }
152
147
  }),
153
148
  setCrypto: assign((context, event) => {
154
149
  const { cryptos } = context
155
150
  const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
151
+ if (!selectedCrypto) {
152
+ return {}
153
+ }
156
154
  return {
157
155
  selectedCrypto: selectedCrypto,
156
+ selectedAmount: null,
158
157
  calculation: null,
159
- options: selectedCrypto?.optional || [],
158
+ options: selectedCrypto.optional || [],
160
159
  }
161
160
  }),
162
161
  setAmount: assign((context, event) => {
163
162
  // 現金網可自由填額度,grandTotalEnable 欄位感覺不再需要?
164
163
  if (context.selectedCrypto?.grandTotalEnable === 1) {
165
- // 累加模式:用 Math.max 確保累加結果不低於 0
166
- const result = preciseAdd(`${parseFloat(context.selectedAmount || 0)}`, `${parseFloat(event.data || '0')}`)
167
164
  return {
168
- selectedAmount: Math.max(result, 0),
165
+ selectedAmount: preciseAdd(`${parseFloat(context.selectedAmount || 0)}`, `${parseFloat(event.data)}`),
169
166
  }
170
167
  } else {
171
168
  return { selectedAmount: event.data }
@@ -174,6 +171,11 @@ export default function (request, context) {
174
171
  inputAmount: assign((context, event) => {
175
172
  return { selectedAmount: event.data }
176
173
  }),
174
+ setAddress: assign((context, event) => {
175
+ return {
176
+ sourceAddress: event.data,
177
+ }
178
+ }),
177
179
  setCalculation: assign((context, event) => {
178
180
  // V2接口轉換中回傳的資料結構不同
179
181
  const { data } = event.data
@@ -198,9 +200,11 @@ export default function (request, context) {
198
200
  }
199
201
  }),
200
202
  setPromotion: assign((context, event) => {
201
- const promotion = event.data
203
+ const { selectedCrypto, selectedAmount } = context
204
+ const promotion = event.data || {}
202
205
  return {
203
206
  promotion,
207
+ selectedAmount: max(promotion?.minimumAmount, selectedCrypto?.minimum, selectedAmount, 0),
204
208
  calculation: null,
205
209
  }
206
210
  }),
@@ -217,24 +221,13 @@ export default function (request, context) {
217
221
  }),
218
222
  },
219
223
  guards: {
220
- isPromotionChanged: (context, event) => {
221
- return event.data?.promotionsNo !== context.promotion?.promotionsNo
222
- },
223
224
  isCalculable: (context, event) => {
224
225
  const { selectedCrypto, selectedAmount } = context
225
226
  return selectedCrypto && selectedAmount && selectedCrypto.minimum <= +selectedAmount && +selectedAmount <= selectedCrypto.maximum
226
227
  },
227
- // 提交前最後一道 guard:重新驗證 amount 的合法性(與 isCalculable 同等嚴格)
228
228
  isSubmittable: (context, event) => {
229
229
  const { selectedCrypto, selectedAmount, calculation } = context
230
- return (
231
- selectedCrypto &&
232
- selectedAmount &&
233
- +selectedAmount > 0 &&
234
- selectedCrypto.minimum <= +selectedAmount &&
235
- +selectedAmount <= selectedCrypto.maximum &&
236
- calculation
237
- )
230
+ return selectedCrypto && selectedAmount && calculation
238
231
  },
239
232
  },
240
233
  services: {
@@ -250,18 +243,14 @@ export default function (request, context) {
250
243
  )
251
244
  },
252
245
  submitDeposit: async (context, event) => {
253
- const { selectedCrypto, selectedAmount, promotion } = context
254
- // 防禦性檢查:將 amount 轉為數字型別,並驗證範圍合法性,避免非法金額送出 API
255
- const amount = +selectedAmount
256
- if (!amount || amount <= 0 || amount < selectedCrypto.minimum || amount > selectedCrypto.maximum) {
257
- throw new Error('INVALID_AMOUNT')
258
- }
246
+ const { selectedCrypto, selectedAmount, sourceAddress, promotion } = context
259
247
  return await request.send(
260
248
  payment.submitDepositTransactionCrypto({
261
249
  method: selectedCrypto.method,
262
- amount: amount,
250
+ amount: selectedAmount,
263
251
  cryptoCurrency: selectedCrypto.channel,
264
- cryptoNetwork: selectedCrypto.networkCode,
252
+ cryptoNetwork: selectedCrypto.network,
253
+ sourceAddress: sourceAddress,
265
254
  promotionsNo: promotion?.promotionsNo,
266
255
  })
267
256
  )