@jetthai/cuora 1.1.488-dev.1 → 1.2.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 (221) hide show
  1. package/.husky/pre-commit +3 -1
  2. package/.prettierignore +1 -2
  3. package/CHANGELOG.md +0 -746
  4. package/README.md +15 -299
  5. package/package.json +3 -6
  6. package/resetGitBranch.sh +101 -0
  7. package/src/api/activity.js +39 -24
  8. package/src/api/auth.js +35 -4
  9. package/src/api/common.js +1 -8
  10. package/src/api/game.js +10 -0
  11. package/src/api/index.js +1 -3
  12. package/src/api/payment.js +55 -18
  13. package/src/api/user.js +16 -11
  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 +209 -0
  17. package/src/machine/activity/activity_cash.js +18 -71
  18. package/src/machine/activity/diamond_shop_activity_cash.js +1 -0
  19. package/src/machine/activity/diamond_shop_activity_fc.js +196 -0
  20. package/src/machine/activity/diamond_shop_activity_vn.js +178 -0
  21. package/src/machine/activity/fafafa_activity.js +10 -0
  22. package/src/machine/activity/index.js +12 -0
  23. package/src/machine/activity/invite_friends_activity.js +114 -0
  24. package/src/machine/activity/mini_game_activity.js +68 -0
  25. package/src/machine/activity/return_bonus_activity.js +153 -0
  26. package/src/machine/activity/roulette_activity_v2.js +0 -5
  27. package/src/machine/auth.js +140 -192
  28. package/src/machine/auth_otp_v2.js +177 -0
  29. package/src/machine/auth_otp_v3.js +32 -111
  30. package/src/machine/bank_binding.js +2 -2
  31. package/src/machine/bank_binding_amb.js +145 -0
  32. package/src/machine/bonus.js +75 -0
  33. package/src/machine/common.js +91 -27
  34. package/src/machine/deposit/amb.js +92 -0
  35. package/src/machine/deposit/crypto_bank.js +325 -0
  36. package/src/machine/deposit/crypto_bank_cash.js +8 -19
  37. package/src/machine/deposit/crypto_qrcode.js +148 -0
  38. package/src/machine/deposit/crypto_qrcode_cash.js +41 -26
  39. package/src/machine/deposit/crypto_way.js +247 -0
  40. package/src/machine/deposit/crypto_way_cash.js +32 -52
  41. package/src/machine/deposit/deposit_fc.js +271 -0
  42. package/src/machine/deposit/direct.js +310 -0
  43. package/src/machine/deposit/direct_cash.js +9 -8
  44. package/src/machine/deposit/float.js +374 -0
  45. package/src/machine/deposit/index.js +28 -0
  46. package/src/machine/deposit/krway.js +151 -0
  47. package/src/machine/deposit/loadcard_cash.js +31 -14
  48. package/src/machine/deposit/loadcard_vn.js +351 -0
  49. package/src/machine/deposit/manual_cash.js +10 -8
  50. package/src/machine/deposit/manual_vn.js +458 -0
  51. package/src/machine/deposit/qrcode_cash.js +30 -14
  52. package/src/machine/deposit/qrcode_v2.js +338 -0
  53. package/src/machine/deposit/usdt.js +144 -0
  54. package/src/machine/deposit/virtual_bank_cash.js +2 -5
  55. package/src/machine/deposit/virtual_bank_vn.js +269 -0
  56. package/src/machine/deposit/vpay.js +367 -0
  57. package/src/machine/error.js +0 -7
  58. package/src/machine/game_hot_new.js +79 -0
  59. package/src/machine/game_info.js +12 -5
  60. package/src/machine/game_replay.js +46 -0
  61. package/src/machine/game_search_v2.js +401 -0
  62. package/src/machine/game_v2_1.js +88 -58
  63. package/src/machine/honor_board.js +132 -0
  64. package/src/machine/honor_board_cash.js +18 -15
  65. package/src/machine/inbox.js +12 -0
  66. package/src/machine/index.js +32 -0
  67. package/src/machine/leaderboard.js +11 -8
  68. package/src/machine/merge.js +57 -0
  69. package/src/machine/payment_data.js +86 -0
  70. package/src/machine/promotions.js +170 -0
  71. package/src/machine/ranking.js +25 -43
  72. package/src/machine/reward.js +255 -0
  73. package/src/machine/reward_cash.js +41 -2
  74. package/src/machine/reward_fc.js +98 -0
  75. package/src/machine/roulette_v2.js +25 -20
  76. package/src/machine/side.js +154 -0
  77. package/src/machine/side_cash.js +2 -3
  78. package/src/machine/side_v2.js +209 -0
  79. package/src/machine/user_reset_password.js +15 -4
  80. package/src/machine/user_sign_up.js +60 -49
  81. package/src/machine/vip_table.js +57 -0
  82. package/src/machine/websocket.js +325 -378
  83. package/src/machine/welcome.js +110 -0
  84. package/src/machine/withdraw/index.js +6 -0
  85. package/src/machine/withdraw/withdraw_crypto_cash.js +327 -0
  86. package/src/machine/withdraw/withdraw_krway.js +222 -0
  87. package/src/machine/withdraw/withdraw_manual.js +533 -0
  88. package/src/machine/withdraw/withdraw_manual_cash.js +43 -74
  89. package/src/request/config.js +0 -2
  90. package/src/request/request.js +64 -231
  91. package/src/util/constant.js +2 -12
  92. package/src/util/deviceToken.js +59 -0
  93. package/src/util/index.js +4 -2
  94. package/src/util/math.js +4 -0
  95. package/.claude/commands/sync-ai-prompts.md +0 -180
  96. package/.claude/settings.local.json +0 -25
  97. package/.github/copilot-instructions.md +0 -459
  98. package/.husky/post-checkout +0 -32
  99. package/.husky/pre-push +0 -36
  100. package/AGENTS.md +0 -459
  101. package/AI_PROMPT_SOURCE.md +0 -457
  102. package/CLAUDE.md +0 -459
  103. package/GEMINI.md +0 -459
  104. package/docs/kibana/crypto-way-deposit-amount-bug-analysis.md +0 -1003
  105. package/docs/kibana/play-providerId-error.md +0 -202
  106. package/docs/kibana/play-selectedWalletType.md +0 -1
  107. package/docs/kibana/play-selectedWalletType_2.md +0 -1
  108. package/docs/kibana/play-selectedWalletType_analysis.md +0 -565
  109. package/docs/my-claude/feat-sharon-JT-1790247240-socket/branch-transfer.md +0 -14
  110. package/docs/my-claude/feat-sharon-JT-1790247240-socket/cash-module-preparation.json +0 -40
  111. package/docs/my-claude/feat-sharon-JT-1790247240-socket/cash-module-verification.md +0 -41
  112. package/docs/my-claude/feat-sharon-JT-1790247240-socket/implementation-ledger.md +0 -87
  113. package/docs/my-claude/feat-sharon-JT-1790247240-socket/module-report.md +0 -73
  114. package/docs/my-claude/feat-sharon-JT-1790247240-socket/plan.md +0 -309
  115. package/docs/my-claude/feat-sharon-JT-1790247240-socket/test-cases.md +0 -245
  116. package/docs/my-claude/feat-sharon-JT-1791180540-onesignal/transfer.md +0 -9
  117. package/e2e/feature-map.md +0 -19
  118. package/prepare-for-pr/JT-1776243420.md +0 -85
  119. package/prepare-for-pr/TI-16026/STATE.md +0 -18
  120. package/prepare-for-pr/TI-16026/after-prepare-authorization.md +0 -3
  121. package/prepare-for-pr/TI-16026/analysis.md +0 -38
  122. package/prepare-for-pr/TI-16026/answer-analyze-01.md +0 -1
  123. package/prepare-for-pr/TI-16026/answer-commit-01.md +0 -2
  124. package/prepare-for-pr/TI-16026/answer-commit-02.md +0 -1
  125. package/prepare-for-pr/TI-16026/bundle.json +0 -18
  126. package/prepare-for-pr/TI-16026/checklist.md +0 -196
  127. package/prepare-for-pr/TI-16026/checklist.unit-test-original.md +0 -39
  128. package/prepare-for-pr/TI-16026/claude-after-model +0 -1
  129. package/prepare-for-pr/TI-16026/claude-commit-session +0 -1
  130. package/prepare-for-pr/TI-16026/claude-harvest-session +0 -1
  131. package/prepare-for-pr/TI-16026/claude-hops/01-analyze.err +0 -1
  132. package/prepare-for-pr/TI-16026/claude-hops/01-analyze.out +0 -2
  133. package/prepare-for-pr/TI-16026/claude-hops/02-analyze.err +0 -1
  134. package/prepare-for-pr/TI-16026/claude-hops/02-analyze.out +0 -4
  135. package/prepare-for-pr/TI-16026/claude-hops/03-dev.err +0 -1
  136. package/prepare-for-pr/TI-16026/claude-hops/03-dev.out +0 -4
  137. package/prepare-for-pr/TI-16026/claude-hops/04-dev.err +0 -1
  138. package/prepare-for-pr/TI-16026/claude-hops/04-dev.out +0 -4
  139. package/prepare-for-pr/TI-16026/claude-hops/05-dev.err +0 -1
  140. package/prepare-for-pr/TI-16026/claude-hops/05-dev.out +0 -3
  141. package/prepare-for-pr/TI-16026/claude-hops/06-after.err +0 -1
  142. package/prepare-for-pr/TI-16026/claude-hops/06-after.out +0 -2
  143. package/prepare-for-pr/TI-16026/claude-hops/07-commit.err +0 -1
  144. package/prepare-for-pr/TI-16026/claude-hops/07-commit.out +0 -2
  145. package/prepare-for-pr/TI-16026/claude-hops/08-commit.err +0 -1
  146. package/prepare-for-pr/TI-16026/claude-hops/08-commit.out +0 -2
  147. package/prepare-for-pr/TI-16026/claude-hops/09-commit.err +0 -1
  148. package/prepare-for-pr/TI-16026/claude-hops/09-commit.out +0 -8
  149. package/prepare-for-pr/TI-16026/claude-hops/10-harvest.err +0 -1
  150. package/prepare-for-pr/TI-16026/claude-hops/10-harvest.out +0 -3
  151. package/prepare-for-pr/TI-16026/claude-hops/11-harvest.err +0 -1
  152. package/prepare-for-pr/TI-16026/claude-hops/11-harvest.out +0 -3
  153. package/prepare-for-pr/TI-16026/commit-note.md +0 -5
  154. package/prepare-for-pr/TI-16026/evidence-index.json +0 -215
  155. package/prepare-for-pr/TI-16026/feature-map.before.md +0 -61
  156. package/prepare-for-pr/TI-16026/harvest-redaction.json +0 -34
  157. package/prepare-for-pr/TI-16026/harvest-redaction.out +0 -37
  158. package/prepare-for-pr/TI-16026/harvest.md +0 -46
  159. package/prepare-for-pr/TI-16026/lessons.md +0 -5
  160. package/prepare-for-pr/TI-16026/note-harvest-redaction.md +0 -1
  161. package/prepare-for-pr/TI-16026/open-questions.md +0 -6
  162. package/prepare-for-pr/TI-16026/publish.md +0 -32
  163. package/prepare-for-pr/TI-16026/review-convergence-response.md +0 -3
  164. package/prepare-for-pr/TI-16026/review-input/scope.md +0 -18
  165. package/prepare-for-pr/TI-16026/review-log.md +0 -57
  166. package/prepare-for-pr/TI-16026/review-notes.md +0 -97
  167. package/prepare-for-pr/TI-16026/review-response-r1.md +0 -7
  168. package/prepare-for-pr/TI-16026/self-test-report.md +0 -67
  169. package/prepare-for-pr/TI-16026/spec-proposal.md +0 -34
  170. package/prepare-for-pr/TI-16026/spec-snapshot.md +0 -291
  171. package/prepare-for-pr/TI-16026/state-log.md +0 -57
  172. package/prepare-for-pr/TI-16026/user-messages/01.md +0 -12
  173. package/prepare-for-pr/TI-16026/user-messages/02.md +0 -20
  174. package/prepare-for-pr/TI-16026/user-messages/03.md +0 -1
  175. package/prepare-for-pr/TI-16026/user-messages/04.md +0 -1
  176. package/prepare-for-pr/TI-16026/user-messages/05.md +0 -1
  177. package/prepare-for-pr/TI-16026/user-messages/06.md +0 -1
  178. package/prepare-for-pr/TI-16026/user-messages/07.md +0 -1
  179. package/prepare-for-pr/TI-16026/user-messages/08.md +0 -1
  180. package/prepare-for-pr/TI-16026/user-messages/09.md +0 -1
  181. package/prepare-for-pr/TI-16026/user-messages/10.md +0 -1
  182. package/prepare-for-pr/TI-16026/user-messages/11.md +0 -1
  183. package/prepare-for-pr/TI-16026/user-messages/12.md +0 -1
  184. package/prepare-for-pr/TI-16026/user-messages/13.md +0 -1
  185. package/prepare-for-pr/TI-16026/user-messages/14.md +0 -1
  186. package/prepare-for-pr/TI-16026/user-messages/15.md +0 -1
  187. package/prepare-for-pr/TI-16026/user-messages/16.md +0 -1
  188. package/prepare-for-pr/TI-16026/user-messages/17.md +0 -1
  189. package/prepare-for-pr/TI-16026/user-messages/18.md +0 -10
  190. package/prepare-for-pr/TI-16026/user-messages/19.md +0 -1
  191. package/prepare-for-pr/TI-16026/user-messages/20.md +0 -1
  192. package/prepare-for-pr/TI-16026/user-messages/21.md +0 -1
  193. package/prepare-for-pr/TI-16026/user-messages/22.md +0 -1
  194. package/prepare-for-pr/TI-16026/user-messages/23.md +0 -8
  195. package/prepare-for-pr/TI-16026/user-messages/24.md +0 -24
  196. package/prepare-for-pr/TI-16026/user-question.md +0 -24
  197. package/prepare-for-pr/TI-16026/verify/metrics-check.json +0 -13
  198. package/prepare-for-pr/TI-16026/verify/redaction.json +0 -38
  199. package/prepare-for-pr/TI-16026/verify/row-closure.json +0 -45
  200. package/prepare-for-pr/TI-16026/verify/s04-ws-capture.md +0 -23
  201. package/prepare-for-pr/TI-16026/verify-after.md +0 -23
  202. package/prepare-for-pr/TI-16026/verify.json +0 -80
  203. package/prepare-for-pr/TI-16026.md +0 -408
  204. package/scripts/auto-version.js +0 -69
  205. package/scripts/auto-version.sh +0 -33
  206. package/scripts/branch-auto-npm-publish.js +0 -36
  207. package/scripts/check-version.sh +0 -36
  208. package/scripts/clean-tags.sh +0 -84
  209. package/src/api/account.js +0 -7
  210. package/src/api/otp.js +0 -5
  211. package/src/util/amount.js +0 -8
  212. package/src/util/err998SentryExtra.js +0 -26
  213. package/src/util/paramStore.js +0 -139
  214. package/src/util/socketPolicy.js +0 -57
  215. package/src/util/tokenStore.js +0 -44
  216. package/test/auth_otp_v3.test.js +0 -323
  217. package/test/auth_self_timeout.test.js +0 -171
  218. package/test/request_self.test.js +0 -288
  219. package/test/withdraw_manual_cash.test.js +0 -149
  220. package/tests/session.test.mjs +0 -177
  221. package/tests/websocket.test.mjs +0 -779
@@ -0,0 +1,148 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import payment from '@jetthai/cuora/src/api/payment.js'
3
+
4
+ export default function (request) {
5
+ return createMachine(
6
+ {
7
+ id: 'crypto_qrcode',
8
+ initial: 'loading',
9
+ context: {
10
+ paymentDetails: [],
11
+ currentCategory: 'cryptoQrcode',
12
+ currentPayment: null,
13
+ cryptos: [],
14
+ selectedCrypto: null,
15
+
16
+ resultMap: {},
17
+ result: {
18
+ action: '',
19
+ cdtime: null,
20
+ accountNumber: '', // crypto address
21
+ currency: '',
22
+ network: '',
23
+ name: '',
24
+ qrcode: '',
25
+ },
26
+ },
27
+ states: {
28
+ loading: {
29
+ invoke: {
30
+ src: 'fetchPaymentDetail',
31
+ onDone: {
32
+ target: 'fetching',
33
+ actions: 'setInitData',
34
+ },
35
+ onError: {
36
+ target: 'fail',
37
+ },
38
+ },
39
+ },
40
+ finding: {
41
+ always: [
42
+ {
43
+ target: 'idle',
44
+ actions: 'setResultFromLocal',
45
+ cond: 'isFound',
46
+ },
47
+ {
48
+ target: 'fetching',
49
+ },
50
+ ],
51
+ },
52
+ fetching: {
53
+ invoke: {
54
+ src: 'submitDeposit',
55
+ onDone: {
56
+ target: 'idle',
57
+ actions: 'setResult',
58
+ },
59
+ onError: {
60
+ target: 'idle',
61
+ },
62
+ },
63
+ },
64
+ idle: {},
65
+ fail: {},
66
+ },
67
+ on: {
68
+ SET_CRYPTO: {
69
+ target: '.finding',
70
+ actions: 'setCrypto',
71
+ },
72
+ },
73
+ },
74
+ {
75
+ actions: {
76
+ setInitData: assign((context, event) => {
77
+ const paymentDetail = event.data || []
78
+ const payments = paymentDetail.filter((item) => item.transferType === 'deposit' && item.category === context.currentCategory)
79
+ const cryptos = payments?.map((value, i) => {
80
+ return {
81
+ ...value,
82
+ label: value?.cryptoOptional[0],
83
+ network: value?.networkName[0],
84
+ icon: value?.channel,
85
+ }
86
+ })
87
+ return {
88
+ cryptos,
89
+ selectedCrypto: cryptos[0],
90
+ currentPayment: cryptos[0],
91
+ }
92
+ }),
93
+ setCrypto: assign((context, event) => {
94
+ const { cryptos } = context
95
+ const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
96
+ return { selectedCrypto }
97
+ }),
98
+ setResultFromLocal: assign((context, event) => {
99
+ const { resultMap, selectedCrypto } = context
100
+ return { result: resultMap[selectedCrypto.network] }
101
+ }),
102
+ setResult: assign((context, event) => {
103
+ const { resultMap, selectedCrypto } = context
104
+ const { action, cdtime, accountNumber, currency, network, name, qrcode, redirect } = event.data
105
+ const result = {
106
+ action,
107
+ cdtime,
108
+ accountNumber,
109
+ currency,
110
+ network,
111
+ name,
112
+ qrcode,
113
+ redirect,
114
+ }
115
+
116
+ return {
117
+ result,
118
+ resultMap: {
119
+ ...resultMap,
120
+ [selectedCrypto.network]: result,
121
+ },
122
+ }
123
+ }),
124
+ },
125
+ guards: {
126
+ isFound: (context, event) => {
127
+ const { resultMap, selectedCrypto } = context
128
+ return resultMap[selectedCrypto.network]
129
+ },
130
+ },
131
+ services: {
132
+ fetchPaymentDetail: async (context, event) => {
133
+ return await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
134
+ },
135
+ submitDeposit: async (context, event) => {
136
+ const { currentPayment, selectedCrypto } = context
137
+ return await request.send(
138
+ payment.submitDepositTransactionCrypto({
139
+ method: currentPayment.method,
140
+ cryptoCurrency: currentPayment.channel,
141
+ cryptoNetwork: selectedCrypto.network,
142
+ })
143
+ )
144
+ },
145
+ },
146
+ }
147
+ )
148
+ }
@@ -10,6 +10,7 @@ export default function (request) {
10
10
  currentCategory: 'cryptoQrcode',
11
11
  cryptos: [],
12
12
  selectedCrypto: null,
13
+ resultMap: {},
13
14
  result: {},
14
15
  },
15
16
  states: {
@@ -25,8 +26,19 @@ export default function (request) {
25
26
  },
26
27
  },
27
28
  },
29
+ finding: {
30
+ always: [
31
+ {
32
+ target: 'idle',
33
+ actions: 'setResultFromLocal',
34
+ cond: 'isFound',
35
+ },
36
+ {
37
+ target: 'submitting',
38
+ },
39
+ ],
40
+ },
28
41
  submitting: {
29
- entry: 'clearResult',
30
42
  invoke: {
31
43
  src: 'submitDeposit',
32
44
  onDone: {
@@ -43,11 +55,11 @@ export default function (request) {
43
55
  },
44
56
  on: {
45
57
  SET_CRYPTO: {
46
- target: '.submitting',
58
+ target: '.finding',
47
59
  actions: 'setCrypto',
48
60
  },
49
61
  SET_NETWORK: {
50
- target: '.submitting',
62
+ target: '.finding',
51
63
  actions: 'setNetwork',
52
64
  },
53
65
  },
@@ -57,45 +69,48 @@ export default function (request) {
57
69
  setInitData: assign((context, event) => {
58
70
  const paymentDetail = event.data || []
59
71
  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
- })
72
+ const cryptos = payments.map((payment) => ({
73
+ ...payment,
74
+ label: payment.cryptoOptional?.[0],
75
+ network: payment.networkName?.[0],
76
+ value: payment.method,
77
+ icon: payment.channel,
78
+ }))
72
79
 
73
80
  return {
74
81
  cryptos,
75
82
  selectedCrypto: cryptos[0],
76
83
  }
77
84
  }),
78
- clearResult: assign({
79
- result: {},
80
- }),
81
85
  setCrypto: assign((context, event) => {
82
86
  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)
87
+ const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
87
88
  return { selectedCrypto }
88
89
  }),
89
- setNetwork: assign((context, event) => {
90
- return { selectedCrypto: event.data }
90
+ setResultFromLocal: assign((context, event) => {
91
+ const { resultMap, selectedCrypto } = context
92
+ return { result: resultMap[`${selectedCrypto.channel}_${selectedCrypto.network}`] }
91
93
  }),
92
94
  setResult: assign((context, event) => {
93
- const { accountNumber, action, currency, network, qrcode, forexRate, redirect, cryptoTag } = event.data
95
+ const { resultMap, selectedCrypto } = context
96
+ const { accountNumber, action, currency, network, qrcode, forexRate, redirect } = event.data
97
+ const result = { accountNumber, action, currency, network, qrcode, forexRate, redirect }
98
+
94
99
  return {
95
- result: { accountNumber, action, currency, network, qrcode, forexRate, redirect, cryptoTag },
100
+ result,
101
+ resultMap: {
102
+ ...resultMap,
103
+ [`${selectedCrypto.channel}_${selectedCrypto.network}`]: result,
104
+ },
96
105
  }
97
106
  }),
98
107
  },
108
+ guards: {
109
+ isFound: (context, event) => {
110
+ const { resultMap, selectedCrypto } = context
111
+ return resultMap[`${selectedCrypto.channel}_${selectedCrypto.network}`]
112
+ },
113
+ },
99
114
  services: {
100
115
  fetchPaymentDetail: async (context, event) => {
101
116
  return await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
@@ -0,0 +1,247 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import padStart from 'lodash/padStart.js'
3
+ import payment from '@jetthai/cuora/src/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
+ initial: 'loading',
12
+ context: {
13
+ paymentDetails: [],
14
+
15
+ cryptos: [],
16
+ sourceAddress: '',
17
+
18
+ amount: null,
19
+ selectedCrypto: null,
20
+
21
+ currentCategory: 'cryptoWay',
22
+ currentPayment: null,
23
+
24
+ calculation: null,
25
+ result: null,
26
+
27
+ // use for wallet mode 4
28
+ promotion: null,
29
+ },
30
+ states: {
31
+ loading: {
32
+ invoke: {
33
+ src: 'fetchPayments',
34
+ onDone: {
35
+ target: 'loaded',
36
+ actions: 'setPayment',
37
+ },
38
+ onError: {
39
+ target: '#crypto_way.fail',
40
+ },
41
+ },
42
+ },
43
+ loaded: {
44
+ always: [{ target: 'debouncing', cond: 'isCalculable' }],
45
+ },
46
+ debouncing: {
47
+ after: {
48
+ 1000: {
49
+ target: 'calculating',
50
+ },
51
+ },
52
+ },
53
+ calculating: {
54
+ invoke: {
55
+ src: 'fetchCalculation',
56
+ onDone: {
57
+ target: 'calculated',
58
+ actions: 'setCalculation',
59
+ },
60
+ onError: {
61
+ target: 'loaded',
62
+ },
63
+ },
64
+ },
65
+ calculated: {
66
+ on: {
67
+ SUBMIT: [
68
+ {
69
+ target: 'pending',
70
+ },
71
+ {
72
+ target: 'submitting',
73
+ cond: 'isSubmittable',
74
+ },
75
+ ],
76
+ },
77
+ },
78
+ pending: {
79
+ on: {
80
+ SUBMIT: {
81
+ target: 'submitting',
82
+ cond: 'isSubmittable',
83
+ },
84
+ },
85
+ },
86
+ submitting: {
87
+ invoke: {
88
+ src: 'submitDeposit',
89
+ onDone: {
90
+ target: 'submitted',
91
+ actions: 'setResult',
92
+ },
93
+ onError: {
94
+ target: 'loaded',
95
+ },
96
+ },
97
+ },
98
+ submitted: {},
99
+ fail: {},
100
+ },
101
+ on: {
102
+ SET_CRYPTO: {
103
+ target: '.loaded',
104
+ actions: 'setCrypto',
105
+ },
106
+ SET_ADDRESS: {
107
+ actions: 'setAddress',
108
+ },
109
+ SET_AMOUNT: {
110
+ target: '.loaded',
111
+ actions: ['setAmount', 'clearCalculation'],
112
+ },
113
+ INPUT_AMOUNT: {
114
+ target: '.loaded',
115
+ actions: ['inputAmount', 'clearCalculation'],
116
+ },
117
+ CALCULATE: {
118
+ target: '.debouncing',
119
+ actions: 'clearCalculation',
120
+ },
121
+ SET_PROMOTION: {
122
+ actions: 'setPromotion',
123
+ },
124
+ },
125
+ },
126
+ {
127
+ actions: {
128
+ setPayment: assign((context, event) => {
129
+ const payments = event.data.filter((item) => item.category === context.currentCategory)
130
+ const cryptos = payments?.map((value, i) => {
131
+ return {
132
+ ...value,
133
+ label: value?.cryptoOptional[0],
134
+ network: value?.networkName[0],
135
+ icon: value?.channel,
136
+ }
137
+ })
138
+ return {
139
+ cryptos,
140
+ selectedCrypto: cryptos[0],
141
+ currentPayment: cryptos[0],
142
+ }
143
+ }),
144
+ setCrypto: assign((context, event) => {
145
+ const { cryptos } = context
146
+ const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
147
+ return {
148
+ selectedCrypto: selectedCrypto,
149
+ amount: null,
150
+ calculation: null,
151
+ }
152
+ }),
153
+ setAmount: assign((context, event) => {
154
+ return {
155
+ amount: event.data,
156
+ }
157
+ }),
158
+ inputAmount: assign((context, event) => {
159
+ return { amount: preciseAdd(`${parseFloat(context.amount || 0)}`, `${parseFloat(event.data)}`) }
160
+ }),
161
+ setAddress: assign((context, event) => {
162
+ return {
163
+ sourceAddress: event.data,
164
+ }
165
+ }),
166
+ setCalculation: assign((context, event) => {
167
+ // V2接口轉換中回傳的資料結構不同
168
+ const { data } = event.data
169
+ if (data) {
170
+ return {
171
+ calculation: data,
172
+ }
173
+ } else {
174
+ return {
175
+ calculation: event.data,
176
+ }
177
+ }
178
+ }),
179
+ clearCalculation: assign((context, event) => {
180
+ return {
181
+ calculation: null,
182
+ }
183
+ }),
184
+ setResult: assign((context, event) => {
185
+ return {
186
+ result: event.data,
187
+ }
188
+ }),
189
+ setPromotion: assign((context, event) => {
190
+ const promotion = event.data
191
+ return {
192
+ promotion,
193
+ }
194
+ }),
195
+ },
196
+ guards: {
197
+ isCalculable: (context, event) => {
198
+ const { selectedCrypto, amount } = context
199
+ return selectedCrypto && amount && selectedCrypto?.minimum <= +amount && +amount <= selectedCrypto?.maximum
200
+ },
201
+ isSubmittable: (context, event) => {
202
+ const { selectedCrypto, amount } = context
203
+ return selectedCrypto && amount
204
+ },
205
+ },
206
+ services: {
207
+ fetchPayments: async (context, event) => {
208
+ return await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
209
+ },
210
+ fetchCalculation: async (context, event) => {
211
+ const { selectedCrypto, amount, promotion } = context
212
+ return await request.send(
213
+ payment.getCryptoDepositExpectedResult({
214
+ method: selectedCrypto.method,
215
+ amount,
216
+ cryptoCurrency: selectedCrypto.channel,
217
+ promotionsNo: promotion?.activityNo,
218
+ })
219
+ )
220
+ },
221
+ submitDeposit: async (context, event) => {
222
+ const { selectedCrypto, amount } = context
223
+ return await request.send(
224
+ payment.submitDepositTransactionCrypto({
225
+ method: selectedCrypto.method,
226
+ amount: amount,
227
+ cryptoCurrency: selectedCrypto.channel,
228
+ cryptoNetwork: selectedCrypto.network,
229
+ })
230
+ )
231
+ },
232
+ },
233
+ }
234
+ )
235
+ }
236
+
237
+ function preciseAdd(a, b) {
238
+ let aStr = a.toString()
239
+ let bStr = b.toString()
240
+
241
+ let aDecimalPlaces = aStr.split('.')[1] ? aStr.split('.')[1].length : 0
242
+ let bDecimalPlaces = bStr.split('.')[1] ? bStr.split('.')[1].length : 0
243
+
244
+ let factor = Math.pow(10, Math.max(aDecimalPlaces, bDecimalPlaces))
245
+
246
+ return (Math.round(a * factor) + Math.round(b * factor)) / factor
247
+ }
@@ -1,5 +1,6 @@
1
1
  import { assign, createMachine } from 'xstate'
2
2
  import payment from '@jetthai/cuora/src/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 || {}
@@ -13,6 +14,7 @@ export default function (request, context) {
13
14
  selectedAmount,
14
15
 
15
16
  cryptos: [],
17
+ sourceAddress: '',
16
18
  options: [],
17
19
 
18
20
  selectedCrypto: null,
@@ -54,9 +56,8 @@ export default function (request, context) {
54
56
  target: 'calculated',
55
57
  actions: 'setCalculation',
56
58
  },
57
- // 計算失敗回到 loaded,避免進入 calculated 造成 UI 與 state 不一致
58
59
  onError: {
59
- target: 'loaded',
60
+ target: 'calculated',
60
61
  },
61
62
  },
62
63
  },
@@ -78,15 +79,10 @@ export default function (request, context) {
78
79
  actions: ['setResult', 'setTtl'],
79
80
  },
80
81
  onError: {
81
- target: 'fail',
82
+ target: 'calculated',
82
83
  },
83
84
  },
84
85
  },
85
- fail: {
86
- after: {
87
- 0: { target: 'calculated' },
88
- },
89
- },
90
86
  submitted: {
91
87
  after: {
92
88
  1000: {
@@ -102,13 +98,16 @@ export default function (request, context) {
102
98
  target: '.debouncing',
103
99
  actions: 'setPromotion',
104
100
  internal: false,
105
- cond: 'isPromotionChanged',
106
101
  },
107
102
  SET_CRYPTO: {
108
103
  target: '.debouncing',
109
104
  actions: 'setCrypto',
110
105
  internal: false,
111
106
  },
107
+ SET_ADDRESS: {
108
+ actions: 'setAddress',
109
+ internal: false,
110
+ },
112
111
  SET_AMOUNT: {
113
112
  target: '.debouncing',
114
113
  actions: 'setAmount',
@@ -126,28 +125,18 @@ export default function (request, context) {
126
125
  setInitData: assign((context, event) => {
127
126
  const { paymentDetail } = context
128
127
  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
128
+ const cryptos = payments.map((payment) => ({
129
+ ...payment,
130
+ label: payment.cryptoOptional?.[0],
131
+ network: payment.networkName?.[0],
132
+ value: payment.method,
133
+ icon: payment.channel,
134
+ }))
146
135
 
147
136
  return {
148
137
  cryptos,
149
- selectedCrypto: defaultCrypto,
150
- options: defaultCrypto?.optional || [],
138
+ selectedCrypto: cryptos[0],
139
+ options: cryptos[0].optional,
151
140
  }
152
141
  }),
153
142
  setCrypto: assign((context, event) => {
@@ -155,17 +144,16 @@ export default function (request, context) {
155
144
  const selectedCrypto = cryptos.find((crypto) => crypto.label === event.data.label)
156
145
  return {
157
146
  selectedCrypto: selectedCrypto,
147
+ selectedAmount: null,
158
148
  calculation: null,
159
- options: selectedCrypto?.optional || [],
149
+ options: selectedCrypto.optional,
160
150
  }
161
151
  }),
162
152
  setAmount: assign((context, event) => {
163
153
  // 現金網可自由填額度,grandTotalEnable 欄位感覺不再需要?
164
154
  if (context.selectedCrypto?.grandTotalEnable === 1) {
165
- // 累加模式:用 Math.max 確保累加結果不低於 0
166
- const result = preciseAdd(`${parseFloat(context.selectedAmount || 0)}`, `${parseFloat(event.data || '0')}`)
167
155
  return {
168
- selectedAmount: Math.max(result, 0),
156
+ selectedAmount: preciseAdd(`${parseFloat(context.selectedAmount || 0)}`, `${parseFloat(event.data)}`),
169
157
  }
170
158
  } else {
171
159
  return { selectedAmount: event.data }
@@ -174,6 +162,11 @@ export default function (request, context) {
174
162
  inputAmount: assign((context, event) => {
175
163
  return { selectedAmount: event.data }
176
164
  }),
165
+ setAddress: assign((context, event) => {
166
+ return {
167
+ sourceAddress: event.data,
168
+ }
169
+ }),
177
170
  setCalculation: assign((context, event) => {
178
171
  // V2接口轉換中回傳的資料結構不同
179
172
  const { data } = event.data
@@ -198,9 +191,11 @@ export default function (request, context) {
198
191
  }
199
192
  }),
200
193
  setPromotion: assign((context, event) => {
194
+ const { selectedCrypto, selectedAmount } = context
201
195
  const promotion = event.data
202
196
  return {
203
197
  promotion,
198
+ selectedAmount: max(promotion.minimumAmount, selectedCrypto.minimum, selectedAmount, 0),
204
199
  calculation: null,
205
200
  }
206
201
  }),
@@ -217,24 +212,13 @@ export default function (request, context) {
217
212
  }),
218
213
  },
219
214
  guards: {
220
- isPromotionChanged: (context, event) => {
221
- return event.data?.promotionsNo !== context.promotion?.promotionsNo
222
- },
223
215
  isCalculable: (context, event) => {
224
216
  const { selectedCrypto, selectedAmount } = context
225
217
  return selectedCrypto && selectedAmount && selectedCrypto.minimum <= +selectedAmount && +selectedAmount <= selectedCrypto.maximum
226
218
  },
227
- // 提交前最後一道 guard:重新驗證 amount 的合法性(與 isCalculable 同等嚴格)
228
219
  isSubmittable: (context, event) => {
229
220
  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
- )
221
+ return selectedCrypto && selectedAmount && calculation
238
222
  },
239
223
  },
240
224
  services: {
@@ -250,18 +234,14 @@ export default function (request, context) {
250
234
  )
251
235
  },
252
236
  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
- }
237
+ const { selectedCrypto, selectedAmount, sourceAddress, promotion } = context
259
238
  return await request.send(
260
239
  payment.submitDepositTransactionCrypto({
261
240
  method: selectedCrypto.method,
262
- amount: amount,
241
+ amount: selectedAmount,
263
242
  cryptoCurrency: selectedCrypto.channel,
264
- cryptoNetwork: selectedCrypto.networkCode,
243
+ cryptoNetwork: selectedCrypto.network,
244
+ sourceAddress: sourceAddress,
265
245
  promotionsNo: promotion?.promotionsNo,
266
246
  })
267
247
  )