@0xtorch/core 0.0.29 → 0.0.31

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 (53) hide show
  1. package/_cjs/actions/index.js +17 -13
  2. package/_cjs/actions/index.js.map +1 -1
  3. package/_cjs/actions/utils/createEvidenceNoneAccountActions.js +323 -0
  4. package/_cjs/actions/utils/createEvidenceNoneAccountActions.js.map +1 -0
  5. package/_cjs/actions/utils/parseBaseAccountActionToAccountAction.js +680 -0
  6. package/_cjs/actions/utils/parseBaseAccountActionToAccountAction.js.map +1 -0
  7. package/_cjs/actions/utils/parseBaseNormalActionToNormalAction.js +609 -0
  8. package/_cjs/actions/utils/parseBaseNormalActionToNormalAction.js.map +1 -0
  9. package/_cjs/actions/utils/transfer.js +22 -0
  10. package/_cjs/actions/utils/transfer.js.map +1 -0
  11. package/_cjs/index.js +4 -3
  12. package/_cjs/index.js.map +1 -1
  13. package/_cjs/setAccountActionPrices.js +14 -3
  14. package/_cjs/setAccountActionPrices.js.map +1 -1
  15. package/_esm/actions/index.js +4 -1
  16. package/_esm/actions/index.js.map +1 -1
  17. package/_esm/actions/utils/createEvidenceNoneAccountActions.js +336 -0
  18. package/_esm/actions/utils/createEvidenceNoneAccountActions.js.map +1 -0
  19. package/_esm/actions/{utils.js → utils/parseBaseAccountActionToAccountAction.js} +2 -613
  20. package/_esm/actions/utils/parseBaseAccountActionToAccountAction.js.map +1 -0
  21. package/_esm/actions/utils/parseBaseNormalActionToNormalAction.js +605 -0
  22. package/_esm/actions/utils/parseBaseNormalActionToNormalAction.js.map +1 -0
  23. package/_esm/actions/utils/transfer.js +10 -0
  24. package/_esm/actions/utils/transfer.js.map +1 -0
  25. package/_esm/index.js +1 -1
  26. package/_esm/index.js.map +1 -1
  27. package/_esm/setAccountActionPrices.js +14 -3
  28. package/_esm/setAccountActionPrices.js.map +1 -1
  29. package/_types/actions/index.d.ts +4 -1
  30. package/_types/actions/index.d.ts.map +1 -1
  31. package/_types/actions/utils/createEvidenceNoneAccountActions.d.ts +10 -0
  32. package/_types/actions/utils/createEvidenceNoneAccountActions.d.ts.map +1 -0
  33. package/_types/actions/utils/parseBaseAccountActionToAccountAction.d.ts +3 -0
  34. package/_types/actions/utils/parseBaseAccountActionToAccountAction.d.ts.map +1 -0
  35. package/_types/actions/utils/parseBaseNormalActionToNormalAction.d.ts +3 -0
  36. package/_types/actions/utils/parseBaseNormalActionToNormalAction.d.ts.map +1 -0
  37. package/_types/actions/{utils.d.ts → utils/transfer.d.ts} +2 -5
  38. package/_types/actions/utils/transfer.d.ts.map +1 -0
  39. package/_types/index.d.ts +1 -1
  40. package/_types/index.d.ts.map +1 -1
  41. package/_types/setAccountActionPrices.d.ts.map +1 -1
  42. package/actions/index.ts +4 -3
  43. package/actions/utils/createEvidenceNoneAccountActions.ts +470 -0
  44. package/actions/{utils.ts → utils/parseBaseAccountActionToAccountAction.ts} +10 -913
  45. package/actions/utils/parseBaseNormalActionToNormalAction.ts +874 -0
  46. package/actions/utils/transfer.ts +57 -0
  47. package/index.ts +1 -0
  48. package/package.json +1 -1
  49. package/setAccountActionPrices.ts +14 -2
  50. package/_cjs/actions/utils.js +0 -1301
  51. package/_cjs/actions/utils.js.map +0 -1
  52. package/_esm/actions/utils.js.map +0 -1
  53. package/_types/actions/utils.d.ts.map +0 -1
@@ -0,0 +1,470 @@
1
+ import { absoluteValue, minus, plus } from '@0xtorch/big-decimal'
2
+ import type {
3
+ AccountAction,
4
+ AccountActionBuyCrypto,
5
+ AccountActionBuyNft,
6
+ AccountActionSellCrypto,
7
+ AccountActionSellNft,
8
+ AccountActionSwapNft,
9
+ AccountActionTrade,
10
+ NormalActionTransfer,
11
+ Transfer,
12
+ TransferCryptoCurrencyIn,
13
+ TransferCryptoCurrencyOut,
14
+ TransferFiatCurrencyIn,
15
+ TransferFiatCurrencyOut,
16
+ TransferIn,
17
+ TransferNftIn,
18
+ TransferNftOut,
19
+ TransferOut,
20
+ } from '../types'
21
+
22
+ type CreateEvidenceNoneAccountActionsParameters = {
23
+ readonly source: string
24
+ readonly timestamp: number
25
+ readonly actionList: readonly NormalActionTransfer[]
26
+ readonly accountIdSet: Set<string>
27
+ }
28
+
29
+ export const createEvidenceNoneAccountActions = ({
30
+ source,
31
+ timestamp,
32
+ actionList,
33
+ accountIdSet,
34
+ }: CreateEvidenceNoneAccountActionsParameters): readonly AccountAction[] => {
35
+ // transfer を asset , account id 毎に merge
36
+ const transferList = createTransferList({ actionList, accountIdSet })
37
+
38
+ // direction = none の transfer を move action に個別変換
39
+ const moveActionList = transferList
40
+ .filter((transfer) => transfer.direction === 'none')
41
+ .map(
42
+ (transfer): AccountAction => ({
43
+ type: 'NormalAccountAction',
44
+ action: 'move',
45
+ source,
46
+ order: 0,
47
+ comment: undefined,
48
+ app: undefined,
49
+ evidence: 'none',
50
+ timestamp,
51
+ transfers: [transfer],
52
+ }),
53
+ )
54
+
55
+ const inOutTransferList = transferList.filter(
56
+ (transfer): transfer is TransferIn | TransferOut =>
57
+ transfer.direction !== 'none',
58
+ )
59
+
60
+ if (inOutTransferList.length === 0) {
61
+ return moveActionList.map((action, order) => ({
62
+ ...action,
63
+ order,
64
+ }))
65
+ }
66
+ // transfer 全てが in のみの場合は個別に income action に変換
67
+ else if (inOutTransferList.every((transfer) => transfer.direction === 'in')) {
68
+ const incomeActionList = inOutTransferList
69
+ .filter((transfer): transfer is TransferIn => transfer.direction === 'in')
70
+ .map(
71
+ (transfer): AccountAction => ({
72
+ type: 'NormalAccountAction',
73
+ action: 'income',
74
+ source,
75
+ order: 0,
76
+ comment: undefined,
77
+ app: undefined,
78
+ evidence: 'none',
79
+ timestamp,
80
+ transfers: [transfer],
81
+ }),
82
+ )
83
+ return [...moveActionList, ...incomeActionList].map((action, order) => ({
84
+ ...action,
85
+ order,
86
+ }))
87
+ }
88
+ // transfer 全てが out のみの場合は個別に transfer action に変換
89
+ else if (
90
+ inOutTransferList.every((transfer) => transfer.direction === 'out')
91
+ ) {
92
+ const transferActionList = inOutTransferList
93
+ .filter(
94
+ (transfer): transfer is TransferOut => transfer.direction === 'out',
95
+ )
96
+ .map(
97
+ (transfer): AccountAction => ({
98
+ type: 'NormalAccountAction',
99
+ action: 'transfer',
100
+ source,
101
+ order: 0,
102
+ comment: undefined,
103
+ app: undefined,
104
+ evidence: 'none',
105
+ timestamp,
106
+ transfers: [transfer],
107
+ }),
108
+ )
109
+ return [...moveActionList, ...transferActionList].map((action, order) => ({
110
+ ...action,
111
+ order,
112
+ }))
113
+ }
114
+ // transfer asset 全てが NFT である場合、 swap-nft action に変換
115
+ else if (
116
+ inOutTransferList.every((transfer) => transfer.asset.type === 'Nft')
117
+ ) {
118
+ return [
119
+ ...moveActionList,
120
+ {
121
+ type: 'NormalAccountAction',
122
+ action: 'swap-nft',
123
+ source,
124
+ order: 0,
125
+ comment: undefined,
126
+ app: undefined,
127
+ evidence: 'none',
128
+ timestamp,
129
+ transfers: inOutTransferList.filter(
130
+ (transfer): transfer is TransferNftIn | TransferNftOut =>
131
+ transfer.asset.type === 'Nft',
132
+ ),
133
+ } satisfies AccountActionSwapNft,
134
+ ].map((action, order) => ({
135
+ ...action,
136
+ order,
137
+ }))
138
+ }
139
+ // in transfer asset 全て NFT である場合、 buy-nft action に変換
140
+ else if (
141
+ inOutTransferList
142
+ .filter((transfer) => transfer.direction === 'in')
143
+ .every((transfer) => transfer.asset.type === 'Nft')
144
+ ) {
145
+ return [
146
+ ...moveActionList,
147
+ {
148
+ type: 'NormalAccountAction',
149
+ action: 'buy-nft',
150
+ source,
151
+ order: 0,
152
+ comment: undefined,
153
+ app: undefined,
154
+ evidence: 'none',
155
+ timestamp,
156
+ transfers: [
157
+ ...inOutTransferList.filter(
158
+ (transfer): transfer is TransferNftIn =>
159
+ transfer.direction === 'in',
160
+ ),
161
+ ...inOutTransferList.filter(
162
+ (
163
+ transfer,
164
+ ): transfer is
165
+ | TransferCryptoCurrencyOut
166
+ | TransferFiatCurrencyOut => transfer.direction === 'out',
167
+ ),
168
+ ],
169
+ } satisfies AccountActionBuyNft,
170
+ ].map((action, order) => ({
171
+ ...action,
172
+ order,
173
+ }))
174
+ }
175
+ // out transfer 全て NFT である場合、 sell-nft action に変換
176
+ else if (
177
+ inOutTransferList
178
+ .filter((transfer) => transfer.direction === 'out')
179
+ .every((transfer) => transfer.asset.type === 'Nft')
180
+ ) {
181
+ return [
182
+ ...moveActionList,
183
+ {
184
+ type: 'NormalAccountAction',
185
+ action: 'sell-nft',
186
+ source,
187
+ order: 0,
188
+ comment: undefined,
189
+ app: undefined,
190
+ evidence: 'none',
191
+ timestamp,
192
+ transfers: [
193
+ ...inOutTransferList.filter(
194
+ (
195
+ transfer,
196
+ ): transfer is TransferCryptoCurrencyIn | TransferFiatCurrencyIn =>
197
+ transfer.direction === 'in',
198
+ ),
199
+ ...inOutTransferList.filter(
200
+ (transfer): transfer is TransferNftOut =>
201
+ transfer.direction === 'out',
202
+ ),
203
+ ],
204
+ } satisfies AccountActionSellNft,
205
+ ].map((action, order) => ({
206
+ ...action,
207
+ order,
208
+ }))
209
+ }
210
+ // in transfer asset 全て Crypto , out transfer asset 全て Fiat の場合、 buy-crypto action に変換
211
+ else if (
212
+ inOutTransferList
213
+ .filter((transfer) => transfer.direction === 'in')
214
+ .every((transfer) => transfer.asset.type === 'CryptoCurrency') &&
215
+ inOutTransferList
216
+ .filter((transfer) => transfer.direction === 'out')
217
+ .every((transfer) => transfer.asset.type === 'FiatCurrency')
218
+ ) {
219
+ return [
220
+ ...moveActionList,
221
+ {
222
+ type: 'NormalAccountAction',
223
+ action: 'buy-crypto',
224
+ source,
225
+ order: 0,
226
+ comment: undefined,
227
+ app: undefined,
228
+ evidence: 'none',
229
+ timestamp,
230
+ transfers: [
231
+ ...inOutTransferList.filter(
232
+ (transfer): transfer is TransferCryptoCurrencyIn =>
233
+ transfer.direction === 'in',
234
+ ),
235
+ ...inOutTransferList.filter(
236
+ (transfer): transfer is TransferFiatCurrencyOut =>
237
+ transfer.direction === 'out',
238
+ ),
239
+ ],
240
+ } satisfies AccountActionBuyCrypto,
241
+ ].map((action, order) => ({
242
+ ...action,
243
+ order,
244
+ }))
245
+ }
246
+ // in transfer asset 全て Fiat , out transfer asset 全て Crypto の場合、 sell-crypto action に変換
247
+ else if (
248
+ inOutTransferList
249
+ .filter((transfer) => transfer.direction === 'in')
250
+ .every((transfer) => transfer.asset.type === 'FiatCurrency') &&
251
+ inOutTransferList
252
+ .filter((transfer) => transfer.direction === 'out')
253
+ .every((transfer) => transfer.asset.type === 'CryptoCurrency')
254
+ ) {
255
+ return [
256
+ ...moveActionList,
257
+ {
258
+ type: 'NormalAccountAction',
259
+ action: 'sell-crypto',
260
+ source,
261
+ order: 0,
262
+ comment: undefined,
263
+ app: undefined,
264
+ evidence: 'none',
265
+ timestamp,
266
+ transfers: [
267
+ ...inOutTransferList.filter(
268
+ (transfer): transfer is TransferFiatCurrencyIn =>
269
+ transfer.direction === 'in',
270
+ ),
271
+ ...inOutTransferList.filter(
272
+ (transfer): transfer is TransferCryptoCurrencyOut =>
273
+ transfer.direction === 'out',
274
+ ),
275
+ ],
276
+ } satisfies AccountActionSellCrypto,
277
+ ].map((action, order) => ({
278
+ ...action,
279
+ order,
280
+ }))
281
+ }
282
+ // それ以外の場合、 trade action に変換
283
+ else {
284
+ return [
285
+ ...moveActionList,
286
+ {
287
+ type: 'NormalAccountAction',
288
+ action: 'trade',
289
+ source,
290
+ order: 0,
291
+ comment: undefined,
292
+ app: undefined,
293
+ evidence: 'none',
294
+ timestamp,
295
+ transfers: [
296
+ ...inOutTransferList.filter(
297
+ (transfer) => transfer.direction === 'in',
298
+ ),
299
+ ...inOutTransferList.filter(
300
+ (transfer) => transfer.direction === 'out',
301
+ ),
302
+ ],
303
+ } satisfies AccountActionTrade,
304
+ ].map((action, order) => ({
305
+ ...action,
306
+ order,
307
+ }))
308
+ }
309
+ }
310
+
311
+ const createTransferList = ({
312
+ actionList,
313
+ accountIdSet,
314
+ }: Pick<
315
+ CreateEvidenceNoneAccountActionsParameters,
316
+ 'accountIdSet' | 'actionList'
317
+ >): readonly Transfer[] => {
318
+ const mut_transfers: (Transfer & { readonly key: string })[] = []
319
+
320
+ for (const transfer of actionList.flatMap((action) => action.transfers)) {
321
+ if (
322
+ (transfer.from === undefined || !accountIdSet.has(transfer.from)) &&
323
+ (transfer.to === undefined || !accountIdSet.has(transfer.to))
324
+ ) {
325
+ continue
326
+ }
327
+
328
+ const transferKey = createTransferKey(transfer, accountIdSet)
329
+ const transferIndex = mut_transfers.findIndex(
330
+ ({ key }) => key === transferKey,
331
+ )
332
+
333
+ if (transferIndex === -1) {
334
+ mut_transfers.push({
335
+ ...transfer,
336
+ direction: createTransferDirection(transfer, accountIdSet),
337
+ key: transferKey,
338
+ })
339
+ } else {
340
+ const baseTransfer = mut_transfers[transferIndex]
341
+ // from=new-from/to=new-to の場合は加算
342
+ if (
343
+ baseTransfer.from === transfer.from &&
344
+ baseTransfer.to === transfer.to
345
+ ) {
346
+ mut_transfers[transferIndex] = {
347
+ ...baseTransfer,
348
+ amount: plus(baseTransfer.amount, transfer.amount),
349
+ }
350
+ }
351
+ // from=new-to/to=new-from の場合は減算
352
+ else if (
353
+ baseTransfer.from === transfer.to &&
354
+ baseTransfer.to === transfer.from
355
+ ) {
356
+ mut_transfers[transferIndex] = {
357
+ ...baseTransfer,
358
+ amount: minus(baseTransfer.amount, transfer.amount),
359
+ }
360
+ }
361
+ // from=new-from/to!=new-to の場合は to を undefined にして加算
362
+ else if (
363
+ baseTransfer.from === transfer.from &&
364
+ baseTransfer.to !== transfer.to
365
+ ) {
366
+ mut_transfers[transferIndex] = {
367
+ ...baseTransfer,
368
+ to: undefined,
369
+ amount: plus(baseTransfer.amount, transfer.amount),
370
+ }
371
+ }
372
+ // from!=new-from/to=new-to の場合は from を undefined にして加算
373
+ else if (
374
+ baseTransfer.from !== transfer.from &&
375
+ baseTransfer.to === transfer.to
376
+ ) {
377
+ mut_transfers[transferIndex] = {
378
+ ...baseTransfer,
379
+ from: undefined,
380
+ amount: plus(baseTransfer.amount, transfer.amount),
381
+ }
382
+ }
383
+ // from=new-to/to!=new-from の場合は to を undefined にして減算
384
+ else if (
385
+ baseTransfer.from === transfer.to &&
386
+ baseTransfer.to !== transfer.from
387
+ ) {
388
+ mut_transfers[transferIndex] = {
389
+ ...baseTransfer,
390
+ to: undefined,
391
+ amount: minus(baseTransfer.amount, transfer.amount),
392
+ }
393
+ }
394
+ // from!=new-to/to=new-from の場合は from を undefined にして減算
395
+ else if (
396
+ baseTransfer.from !== transfer.to &&
397
+ baseTransfer.to === transfer.from
398
+ ) {
399
+ mut_transfers[transferIndex] = {
400
+ ...baseTransfer,
401
+ from: undefined,
402
+ amount: minus(baseTransfer.amount, transfer.amount),
403
+ }
404
+ }
405
+ }
406
+ }
407
+
408
+ // amount がマイナスの場合は from/to , direction を逆にして amount をプラスにする
409
+ return mut_transfers.map((transfer) =>
410
+ transfer.amount.value < 0
411
+ ? {
412
+ ...transfer,
413
+ from: transfer.to,
414
+ to: transfer.from,
415
+ direction: reverseTransferDirection(transfer.direction),
416
+ amount: absoluteValue(transfer.amount),
417
+ }
418
+ : transfer,
419
+ )
420
+ }
421
+
422
+ const createTransferDirection = (
423
+ transfer: Transfer,
424
+ accountIdSet: Set<string>,
425
+ ) => {
426
+ if (
427
+ transfer.from !== undefined &&
428
+ accountIdSet.has(transfer.from) &&
429
+ transfer.to !== undefined &&
430
+ accountIdSet.has(transfer.to)
431
+ ) {
432
+ return 'none'
433
+ } else if (transfer.from !== undefined && accountIdSet.has(transfer.from)) {
434
+ return 'out'
435
+ } else if (transfer.to !== undefined && accountIdSet.has(transfer.to)) {
436
+ return 'in'
437
+ } else {
438
+ return 'none'
439
+ }
440
+ }
441
+
442
+ const reverseTransferDirection = (
443
+ direction: Transfer['direction'],
444
+ ): Transfer['direction'] => {
445
+ switch (direction) {
446
+ case 'in': {
447
+ return 'out'
448
+ }
449
+ case 'none': {
450
+ return 'none'
451
+ }
452
+ case 'out': {
453
+ return 'in'
454
+ }
455
+ }
456
+ }
457
+
458
+ const createTransferKey = (
459
+ transfer: Transfer,
460
+ accountIdSet: Set<string>,
461
+ ): string => {
462
+ const addressKey = [transfer.from, transfer.to]
463
+ .filter(
464
+ (address): address is string =>
465
+ address !== undefined && accountIdSet.has(address),
466
+ )
467
+ .sort()
468
+ .join('/')
469
+ return `${transfer.asset.type}/${transfer.asset.id}/${addressKey}`
470
+ }