@0xtorch/core 0.0.34 → 0.0.35

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 (35) hide show
  1. package/_cjs/actions/index.js +5 -1
  2. package/_cjs/actions/index.js.map +1 -1
  3. package/_cjs/actions/utils/checkIfAccountActionsIsValid.js +60 -0
  4. package/_cjs/actions/utils/checkIfAccountActionsIsValid.js.map +1 -0
  5. package/_cjs/actions/utils/createEvidenceNoneAccountActions.js +2 -117
  6. package/_cjs/actions/utils/createEvidenceNoneAccountActions.js.map +1 -1
  7. package/_cjs/actions/utils/mergeSameAssetTransfers.js +121 -0
  8. package/_cjs/actions/utils/mergeSameAssetTransfers.js.map +1 -0
  9. package/_cjs/index.js +5 -3
  10. package/_cjs/index.js.map +1 -1
  11. package/_esm/actions/index.js +2 -0
  12. package/_esm/actions/index.js.map +1 -1
  13. package/_esm/actions/utils/checkIfAccountActionsIsValid.js +66 -0
  14. package/_esm/actions/utils/checkIfAccountActionsIsValid.js.map +1 -0
  15. package/_esm/actions/utils/createEvidenceNoneAccountActions.js +2 -124
  16. package/_esm/actions/utils/createEvidenceNoneAccountActions.js.map +1 -1
  17. package/_esm/actions/utils/mergeSameAssetTransfers.js +124 -0
  18. package/_esm/actions/utils/mergeSameAssetTransfers.js.map +1 -0
  19. package/_esm/index.js +1 -1
  20. package/_esm/index.js.map +1 -1
  21. package/_types/actions/index.d.ts +2 -0
  22. package/_types/actions/index.d.ts.map +1 -1
  23. package/_types/actions/utils/checkIfAccountActionsIsValid.d.ts +9 -0
  24. package/_types/actions/utils/checkIfAccountActionsIsValid.d.ts.map +1 -0
  25. package/_types/actions/utils/createEvidenceNoneAccountActions.d.ts.map +1 -1
  26. package/_types/actions/utils/mergeSameAssetTransfers.d.ts +3 -0
  27. package/_types/actions/utils/mergeSameAssetTransfers.d.ts.map +1 -0
  28. package/_types/index.d.ts +1 -1
  29. package/_types/index.d.ts.map +1 -1
  30. package/actions/index.ts +2 -0
  31. package/actions/utils/checkIfAccountActionsIsValid.ts +129 -0
  32. package/actions/utils/createEvidenceNoneAccountActions.ts +5 -166
  33. package/actions/utils/mergeSameAssetTransfers.ts +162 -0
  34. package/index.ts +2 -0
  35. package/package.json +1 -1
@@ -1,4 +1,3 @@
1
- import { absoluteValue, minus, plus } from '@0xtorch/big-decimal'
2
1
  import type {
3
2
  AccountAction,
4
3
  AccountActionBuyCrypto,
@@ -8,7 +7,6 @@ import type {
8
7
  AccountActionSwapNft,
9
8
  AccountActionTrade,
10
9
  NormalActionTransfer,
11
- Transfer,
12
10
  TransferCryptoCurrencyIn,
13
11
  TransferCryptoCurrencyOut,
14
12
  TransferFiatCurrencyIn,
@@ -18,6 +16,7 @@ import type {
18
16
  TransferNftOut,
19
17
  TransferOut,
20
18
  } from '../types'
19
+ import { mergeSameAssetTransfers } from './mergeSameAssetTransfers'
21
20
 
22
21
  type CreateEvidenceNoneAccountActionsParameters = {
23
22
  readonly source: string
@@ -33,7 +32,10 @@ export const createEvidenceNoneAccountActions = ({
33
32
  accountIdSet,
34
33
  }: CreateEvidenceNoneAccountActionsParameters): readonly AccountAction[] => {
35
34
  // transfer を asset , account id 毎に merge
36
- const transferList = createTransferList({ actionList, accountIdSet })
35
+ const transferList = mergeSameAssetTransfers(
36
+ actionList.flatMap((action) => action.transfers),
37
+ accountIdSet,
38
+ )
37
39
 
38
40
  // direction = none の transfer を move action に個別変換
39
41
  const moveActionList = transferList
@@ -315,166 +317,3 @@ export const createEvidenceNoneAccountActions = ({
315
317
  }))
316
318
  }
317
319
  }
318
-
319
- const createTransferList = ({
320
- actionList,
321
- accountIdSet,
322
- }: Pick<
323
- CreateEvidenceNoneAccountActionsParameters,
324
- 'accountIdSet' | 'actionList'
325
- >): readonly Transfer[] => {
326
- const mut_transfers: (Transfer & { readonly key: string })[] = []
327
-
328
- for (const transfer of actionList.flatMap((action) => action.transfers)) {
329
- if (
330
- (transfer.from === undefined || !accountIdSet.has(transfer.from)) &&
331
- (transfer.to === undefined || !accountIdSet.has(transfer.to))
332
- ) {
333
- continue
334
- }
335
-
336
- const transferKey = createTransferKey(transfer, accountIdSet)
337
- const transferIndex = mut_transfers.findIndex(
338
- ({ key }) => key === transferKey,
339
- )
340
-
341
- if (transferIndex === -1) {
342
- mut_transfers.push({
343
- ...transfer,
344
- direction: createTransferDirection(transfer, accountIdSet),
345
- key: transferKey,
346
- })
347
- } else {
348
- const baseTransfer = mut_transfers[transferIndex]
349
- // from=new-from/to=new-to の場合は加算
350
- if (
351
- baseTransfer.from === transfer.from &&
352
- baseTransfer.to === transfer.to
353
- ) {
354
- mut_transfers[transferIndex] = {
355
- ...baseTransfer,
356
- amount: plus(baseTransfer.amount, transfer.amount),
357
- }
358
- }
359
- // from=new-to/to=new-from の場合は減算
360
- else if (
361
- baseTransfer.from === transfer.to &&
362
- baseTransfer.to === transfer.from
363
- ) {
364
- mut_transfers[transferIndex] = {
365
- ...baseTransfer,
366
- amount: minus(baseTransfer.amount, transfer.amount),
367
- }
368
- }
369
- // from=new-from/to!=new-to の場合は to を undefined にして加算
370
- else if (
371
- baseTransfer.from === transfer.from &&
372
- baseTransfer.to !== transfer.to
373
- ) {
374
- mut_transfers[transferIndex] = {
375
- ...baseTransfer,
376
- to: undefined,
377
- amount: plus(baseTransfer.amount, transfer.amount),
378
- }
379
- }
380
- // from!=new-from/to=new-to の場合は from を undefined にして加算
381
- else if (
382
- baseTransfer.from !== transfer.from &&
383
- baseTransfer.to === transfer.to
384
- ) {
385
- mut_transfers[transferIndex] = {
386
- ...baseTransfer,
387
- from: undefined,
388
- amount: plus(baseTransfer.amount, transfer.amount),
389
- }
390
- }
391
- // from=new-to/to!=new-from の場合は to を undefined にして減算
392
- else if (
393
- baseTransfer.from === transfer.to &&
394
- baseTransfer.to !== transfer.from
395
- ) {
396
- mut_transfers[transferIndex] = {
397
- ...baseTransfer,
398
- to: undefined,
399
- amount: minus(baseTransfer.amount, transfer.amount),
400
- }
401
- }
402
- // from!=new-to/to=new-from の場合は from を undefined にして減算
403
- else if (
404
- baseTransfer.from !== transfer.to &&
405
- baseTransfer.to === transfer.from
406
- ) {
407
- mut_transfers[transferIndex] = {
408
- ...baseTransfer,
409
- from: undefined,
410
- amount: minus(baseTransfer.amount, transfer.amount),
411
- }
412
- }
413
- }
414
- }
415
-
416
- // amount がマイナスの場合は from/to , direction を逆にして amount をプラスにする
417
- return mut_transfers
418
- .filter((transfer) => transfer.amount.value !== 0n)
419
- .map((transfer) =>
420
- transfer.amount.value < 0
421
- ? {
422
- ...transfer,
423
- from: transfer.to,
424
- to: transfer.from,
425
- direction: reverseTransferDirection(transfer.direction),
426
- amount: absoluteValue(transfer.amount),
427
- }
428
- : transfer,
429
- )
430
- }
431
-
432
- const createTransferDirection = (
433
- transfer: Transfer,
434
- accountIdSet: Set<string>,
435
- ) => {
436
- if (
437
- transfer.from !== undefined &&
438
- accountIdSet.has(transfer.from) &&
439
- transfer.to !== undefined &&
440
- accountIdSet.has(transfer.to)
441
- ) {
442
- return 'none'
443
- } else if (transfer.from !== undefined && accountIdSet.has(transfer.from)) {
444
- return 'out'
445
- } else if (transfer.to !== undefined && accountIdSet.has(transfer.to)) {
446
- return 'in'
447
- } else {
448
- return 'none'
449
- }
450
- }
451
-
452
- const reverseTransferDirection = (
453
- direction: Transfer['direction'],
454
- ): Transfer['direction'] => {
455
- switch (direction) {
456
- case 'in': {
457
- return 'out'
458
- }
459
- case 'none': {
460
- return 'none'
461
- }
462
- case 'out': {
463
- return 'in'
464
- }
465
- }
466
- }
467
-
468
- const createTransferKey = (
469
- transfer: Transfer,
470
- accountIdSet: Set<string>,
471
- ): string => {
472
- const addressKey = [transfer.from, transfer.to]
473
- .filter(
474
- (address): address is string =>
475
- address !== undefined && accountIdSet.has(address),
476
- )
477
- .sort()
478
- .join('/')
479
- return `${transfer.asset.type}/${transfer.asset.id}/${addressKey}`
480
- }
@@ -0,0 +1,162 @@
1
+ import { absoluteValue, minus, plus } from '@0xtorch/big-decimal'
2
+ import type { Transfer } from '../types'
3
+
4
+ export const mergeSameAssetTransfers = (
5
+ transfers: readonly Transfer[],
6
+ accountIds: Set<string>,
7
+ ): readonly Transfer[] => {
8
+ const mut_transfers: (Transfer & { readonly key: string })[] = []
9
+
10
+ for (const transfer of transfers) {
11
+ if (
12
+ (transfer.from === undefined || !accountIds.has(transfer.from)) &&
13
+ (transfer.to === undefined || !accountIds.has(transfer.to))
14
+ ) {
15
+ continue
16
+ }
17
+
18
+ const transferKey = createTransferKey(transfer, accountIds)
19
+ const transferIndex = mut_transfers.findIndex(
20
+ ({ key }) => key === transferKey,
21
+ )
22
+
23
+ if (transferIndex === -1) {
24
+ mut_transfers.push({
25
+ ...transfer,
26
+ direction: createTransferDirection(transfer, accountIds),
27
+ key: transferKey,
28
+ })
29
+ } else {
30
+ const baseTransfer = mut_transfers[transferIndex]
31
+ // from=new-from/to=new-to の場合は加算
32
+ if (
33
+ baseTransfer.from === transfer.from &&
34
+ baseTransfer.to === transfer.to
35
+ ) {
36
+ mut_transfers[transferIndex] = {
37
+ ...baseTransfer,
38
+ amount: plus(baseTransfer.amount, transfer.amount),
39
+ }
40
+ }
41
+ // from=new-to/to=new-from の場合は減算
42
+ else if (
43
+ baseTransfer.from === transfer.to &&
44
+ baseTransfer.to === transfer.from
45
+ ) {
46
+ mut_transfers[transferIndex] = {
47
+ ...baseTransfer,
48
+ amount: minus(baseTransfer.amount, transfer.amount),
49
+ }
50
+ }
51
+ // from=new-from/to!=new-to の場合は to を undefined にして加算
52
+ else if (
53
+ baseTransfer.from === transfer.from &&
54
+ baseTransfer.to !== transfer.to
55
+ ) {
56
+ mut_transfers[transferIndex] = {
57
+ ...baseTransfer,
58
+ to: undefined,
59
+ amount: plus(baseTransfer.amount, transfer.amount),
60
+ }
61
+ }
62
+ // from!=new-from/to=new-to の場合は from を undefined にして加算
63
+ else if (
64
+ baseTransfer.from !== transfer.from &&
65
+ baseTransfer.to === transfer.to
66
+ ) {
67
+ mut_transfers[transferIndex] = {
68
+ ...baseTransfer,
69
+ from: undefined,
70
+ amount: plus(baseTransfer.amount, transfer.amount),
71
+ }
72
+ }
73
+ // from=new-to/to!=new-from の場合は to を undefined にして減算
74
+ else if (
75
+ baseTransfer.from === transfer.to &&
76
+ baseTransfer.to !== transfer.from
77
+ ) {
78
+ mut_transfers[transferIndex] = {
79
+ ...baseTransfer,
80
+ to: undefined,
81
+ amount: minus(baseTransfer.amount, transfer.amount),
82
+ }
83
+ }
84
+ // from!=new-to/to=new-from の場合は from を undefined にして減算
85
+ else if (
86
+ baseTransfer.from !== transfer.to &&
87
+ baseTransfer.to === transfer.from
88
+ ) {
89
+ mut_transfers[transferIndex] = {
90
+ ...baseTransfer,
91
+ from: undefined,
92
+ amount: minus(baseTransfer.amount, transfer.amount),
93
+ }
94
+ }
95
+ }
96
+ }
97
+
98
+ // amount がマイナスの場合は from/to , direction を逆にして amount をプラスにする
99
+ return mut_transfers
100
+ .filter((transfer) => transfer.amount.value !== 0n)
101
+ .map((transfer) =>
102
+ transfer.amount.value < 0
103
+ ? {
104
+ ...transfer,
105
+ from: transfer.to,
106
+ to: transfer.from,
107
+ direction: reverseTransferDirection(transfer.direction),
108
+ amount: absoluteValue(transfer.amount),
109
+ }
110
+ : transfer,
111
+ )
112
+ }
113
+
114
+ const createTransferKey = (
115
+ transfer: Transfer,
116
+ accountIds: Set<string>,
117
+ ): string => {
118
+ const addressKey = [transfer.from, transfer.to]
119
+ .filter(
120
+ (address): address is string =>
121
+ address !== undefined && accountIds.has(address),
122
+ )
123
+ .sort()
124
+ .join('/')
125
+ return `${transfer.asset.type}/${transfer.asset.id}/${addressKey}`
126
+ }
127
+
128
+ const createTransferDirection = (
129
+ transfer: Transfer,
130
+ accountIds: Set<string>,
131
+ ) => {
132
+ if (
133
+ transfer.from !== undefined &&
134
+ accountIds.has(transfer.from) &&
135
+ transfer.to !== undefined &&
136
+ accountIds.has(transfer.to)
137
+ ) {
138
+ return 'none'
139
+ } else if (transfer.from !== undefined && accountIds.has(transfer.from)) {
140
+ return 'out'
141
+ } else if (transfer.to !== undefined && accountIds.has(transfer.to)) {
142
+ return 'in'
143
+ } else {
144
+ return 'none'
145
+ }
146
+ }
147
+
148
+ const reverseTransferDirection = (
149
+ direction: Transfer['direction'],
150
+ ): Transfer['direction'] => {
151
+ switch (direction) {
152
+ case 'in': {
153
+ return 'out'
154
+ }
155
+ case 'none': {
156
+ return 'none'
157
+ }
158
+ case 'out': {
159
+ return 'in'
160
+ }
161
+ }
162
+ }
package/index.ts CHANGED
@@ -46,6 +46,7 @@ export {
46
46
  accountActionWithdrawSchema,
47
47
  accountActionWithdrawWithBondSchema,
48
48
  accountActionWrapSchema,
49
+ checkIfAccountActionsIsValid,
49
50
  createEvidenceNoneAccountActions,
50
51
  crossActionBundleSchema,
51
52
  crossTypeSchema,
@@ -58,6 +59,7 @@ export {
58
59
  isTransferNftIn,
59
60
  isTransferNftNone,
60
61
  isTransferNftOut,
62
+ mergeSameAssetTransfers,
61
63
  normalActionActionSchema,
62
64
  normalActionAddLiquiditySchema,
63
65
  normalActionApproveSchema,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xtorch/core",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "description": "Cryptorch | TypeScript Analyze Interface for Crypto Data.",
5
5
  "keywords": [
6
6
  "cryptorch",