@0xtorch/core 0.0.34 → 0.0.36
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.
- package/_cjs/actions/index.js +5 -1
- package/_cjs/actions/index.js.map +1 -1
- package/_cjs/actions/parsers/default.js +3 -5
- package/_cjs/actions/parsers/default.js.map +1 -1
- package/_cjs/actions/parsers/income/atomicArbitrage.js +4 -4
- package/_cjs/actions/parsers/income/atomicArbitrage.js.map +1 -1
- package/_cjs/actions/utils/checkIfAccountActionsIsValid.js +60 -0
- package/_cjs/actions/utils/checkIfAccountActionsIsValid.js.map +1 -0
- package/_cjs/actions/utils/createEvidenceNoneAccountActions.js +29 -146
- package/_cjs/actions/utils/createEvidenceNoneAccountActions.js.map +1 -1
- package/_cjs/actions/utils/mergeSameAssetTransfers.js +119 -0
- package/_cjs/actions/utils/mergeSameAssetTransfers.js.map +1 -0
- package/_cjs/assets/definitions/coingeckoCryptoCurrencyDataSource.js +2 -2
- package/_cjs/assets/definitions/coingeckoCryptoCurrencyDataSource.js.map +1 -1
- package/_cjs/assets/externals/coingecko/getAutoRetry.js +2 -2
- package/_cjs/assets/externals/coingecko/getAutoRetry.js.map +1 -1
- package/_cjs/index.js +5 -3
- package/_cjs/index.js.map +1 -1
- package/_cjs/utils/errors.js +4 -5
- package/_cjs/utils/errors.js.map +1 -1
- package/_cjs/utils/rest.js +4 -7
- package/_cjs/utils/rest.js.map +1 -1
- package/_cjs/utils/stringify.js.map +1 -1
- package/_esm/actions/index.js +2 -0
- package/_esm/actions/index.js.map +1 -1
- package/_esm/actions/parsers/default.js +3 -5
- package/_esm/actions/parsers/default.js.map +1 -1
- package/_esm/actions/parsers/income/atomicArbitrage.js +4 -4
- package/_esm/actions/parsers/income/atomicArbitrage.js.map +1 -1
- package/_esm/actions/utils/checkIfAccountActionsIsValid.js +66 -0
- package/_esm/actions/utils/checkIfAccountActionsIsValid.js.map +1 -0
- package/_esm/actions/utils/createEvidenceNoneAccountActions.js +29 -153
- package/_esm/actions/utils/createEvidenceNoneAccountActions.js.map +1 -1
- package/_esm/actions/utils/mergeSameAssetTransfers.js +122 -0
- package/_esm/actions/utils/mergeSameAssetTransfers.js.map +1 -0
- package/_esm/assets/definitions/coingeckoCryptoCurrencyDataSource.js +6 -3
- package/_esm/assets/definitions/coingeckoCryptoCurrencyDataSource.js.map +1 -1
- package/_esm/assets/externals/coingecko/getAutoRetry.js +2 -2
- package/_esm/assets/externals/coingecko/getAutoRetry.js.map +1 -1
- package/_esm/index.js +1 -1
- package/_esm/index.js.map +1 -1
- package/_esm/utils/errors.js +5 -7
- package/_esm/utils/errors.js.map +1 -1
- package/_esm/utils/rest.js +4 -8
- package/_esm/utils/rest.js.map +1 -1
- package/_esm/utils/stringify.js +0 -2
- package/_esm/utils/stringify.js.map +1 -1
- package/_types/actions/index.d.ts +2 -0
- package/_types/actions/index.d.ts.map +1 -1
- package/_types/actions/parsers/action.d.ts.map +1 -1
- package/_types/actions/parsers/borrow/borrow.d.ts.map +1 -1
- package/_types/actions/parsers/crossReplace/bridgeFrom.d.ts.map +1 -1
- package/_types/actions/parsers/crossReplace/bridgeTo.d.ts.map +1 -1
- package/_types/actions/parsers/crossReplace/crossReplace.d.ts.map +1 -1
- package/_types/actions/parsers/crossTrade/crossTrade.d.ts.map +1 -1
- package/_types/actions/parsers/default.d.ts.map +1 -1
- package/_types/actions/parsers/deposit/addLiquidity.d.ts.map +1 -1
- package/_types/actions/parsers/deposit/deposit.d.ts.map +1 -1
- package/_types/actions/parsers/deposit/depositWithBond.d.ts.map +1 -1
- package/_types/actions/parsers/deposit/stake.d.ts.map +1 -1
- package/_types/actions/parsers/fee/fee.d.ts.map +1 -1
- package/_types/actions/parsers/fee/transactionFee.d.ts.map +1 -1
- package/_types/actions/parsers/ignore/approve.d.ts +1 -3
- package/_types/actions/parsers/ignore/approve.d.ts.map +1 -1
- package/_types/actions/parsers/ignore/failTx.d.ts +1 -3
- package/_types/actions/parsers/ignore/failTx.d.ts.map +1 -1
- package/_types/actions/parsers/ignore/receiveFromCex.d.ts.map +1 -1
- package/_types/actions/parsers/ignore/revoke.d.ts +1 -3
- package/_types/actions/parsers/ignore/revoke.d.ts.map +1 -1
- package/_types/actions/parsers/ignore/sendToCex.d.ts.map +1 -1
- package/_types/actions/parsers/ignore/spam.d.ts +1 -3
- package/_types/actions/parsers/ignore/spam.d.ts.map +1 -1
- package/_types/actions/parsers/income/atomicArbitrage.d.ts +1 -3
- package/_types/actions/parsers/income/atomicArbitrage.d.ts.map +1 -1
- package/_types/actions/parsers/income/freeMintNft.d.ts.map +1 -1
- package/_types/actions/parsers/income/income.d.ts.map +1 -1
- package/_types/actions/parsers/repayment/repayment.d.ts.map +1 -1
- package/_types/actions/parsers/repayment/repaymentWithDebt.d.ts.map +1 -1
- package/_types/actions/parsers/replace/replace.d.ts.map +1 -1
- package/_types/actions/parsers/replace/swapNft.d.ts.map +1 -1
- package/_types/actions/parsers/replace/unwrap.d.ts.map +1 -1
- package/_types/actions/parsers/replace/wrap.d.ts.map +1 -1
- package/_types/actions/parsers/trade/buyCrypto.d.ts.map +1 -1
- package/_types/actions/parsers/trade/buyNft.d.ts.map +1 -1
- package/_types/actions/parsers/trade/mintNft.d.ts.map +1 -1
- package/_types/actions/parsers/trade/sellCrypto.d.ts.map +1 -1
- package/_types/actions/parsers/trade/sellNft.d.ts.map +1 -1
- package/_types/actions/parsers/trade/trade.d.ts +1 -3
- package/_types/actions/parsers/trade/trade.d.ts.map +1 -1
- package/_types/actions/parsers/transfer/transfer.d.ts.map +1 -1
- package/_types/actions/parsers/valuedown/valuedown.d.ts.map +1 -1
- package/_types/actions/parsers/valueup/valueup.d.ts.map +1 -1
- package/_types/actions/parsers/withdraw/removeLiquidity.d.ts.map +1 -1
- package/_types/actions/parsers/withdraw/unstake.d.ts.map +1 -1
- package/_types/actions/parsers/withdraw/withdraw.d.ts.map +1 -1
- package/_types/actions/parsers/withdraw/withdrawWithBond.d.ts.map +1 -1
- package/_types/actions/utils/checkIfAccountActionsIsValid.d.ts +9 -0
- package/_types/actions/utils/checkIfAccountActionsIsValid.d.ts.map +1 -0
- package/_types/actions/utils/createEvidenceNoneAccountActions.d.ts.map +1 -1
- package/_types/actions/utils/mergeSameAssetTransfers.d.ts +3 -0
- package/_types/actions/utils/mergeSameAssetTransfers.d.ts.map +1 -0
- package/_types/actions/utils/transfer.d.ts +10 -259
- package/_types/actions/utils/transfer.d.ts.map +1 -1
- package/_types/assets/creates/createCryptoCurrency.d.ts +2 -16
- package/_types/assets/creates/createCryptoCurrency.d.ts.map +1 -1
- package/_types/assets/creates/createFiatCurrency.d.ts +2 -7
- package/_types/assets/creates/createFiatCurrency.d.ts.map +1 -1
- package/_types/assets/definitions/coingeckoCryptoCurrencyDataSource.d.ts.map +1 -1
- package/_types/assets/externals/coingecko/getAutoRetry.d.ts.map +1 -1
- package/_types/assets/externals/coingecko/getCoinMarketChartRange.d.ts.map +1 -1
- package/_types/assets/externals/coingecko/getCoinsMarkets.d.ts.map +1 -1
- package/_types/index.d.ts +1 -1
- package/_types/index.d.ts.map +1 -1
- package/_types/utils/errors.d.ts.map +1 -1
- package/_types/utils/rest.d.ts.map +1 -1
- package/_types/utils/schema.d.ts.map +1 -1
- package/_types/utils/stringify.d.ts.map +1 -1
- package/_types/utils/types.d.ts.map +1 -1
- package/actions/index.ts +2 -0
- package/actions/parsers/default.ts +5 -4
- package/actions/utils/checkIfAccountActionsIsValid.ts +129 -0
- package/actions/utils/createEvidenceNoneAccountActions.ts +32 -203
- package/actions/utils/mergeSameAssetTransfers.ts +163 -0
- package/assets/definitions/coingeckoCryptoCurrencyDataSource.ts +2 -0
- package/index.ts +2 -0
- package/package.json +2 -5
- package/utils/errors.ts +3 -5
- package/utils/rest.ts +4 -6
- package/utils/stringify.ts +0 -2
- package/utils/types.ts +0 -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 =
|
|
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
|
|
@@ -64,7 +66,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
64
66
|
}))
|
|
65
67
|
}
|
|
66
68
|
// transfer 全てが in のみの場合は個別に income action に変換
|
|
67
|
-
|
|
69
|
+
if (inOutTransferList.every((transfer) => transfer.direction === 'in')) {
|
|
68
70
|
const incomeActionList = inOutTransferList
|
|
69
71
|
.filter((transfer): transfer is TransferIn => transfer.direction === 'in')
|
|
70
72
|
.map(
|
|
@@ -86,9 +88,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
86
88
|
}))
|
|
87
89
|
}
|
|
88
90
|
// transfer 全てが out のみの場合は個別に transfer action に変換
|
|
89
|
-
|
|
90
|
-
inOutTransferList.every((transfer) => transfer.direction === 'out')
|
|
91
|
-
) {
|
|
91
|
+
if (inOutTransferList.every((transfer) => transfer.direction === 'out')) {
|
|
92
92
|
const transferActionList = inOutTransferList
|
|
93
93
|
.filter(
|
|
94
94
|
(transfer): transfer is TransferOut => transfer.direction === 'out',
|
|
@@ -112,9 +112,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
112
112
|
}))
|
|
113
113
|
}
|
|
114
114
|
// transfer asset 全てが NFT である場合、 swap-nft action に変換
|
|
115
|
-
|
|
116
|
-
inOutTransferList.every((transfer) => transfer.asset.type === 'Nft')
|
|
117
|
-
) {
|
|
115
|
+
if (inOutTransferList.every((transfer) => transfer.asset.type === 'Nft')) {
|
|
118
116
|
return [
|
|
119
117
|
...moveActionList,
|
|
120
118
|
{
|
|
@@ -137,7 +135,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
137
135
|
}))
|
|
138
136
|
}
|
|
139
137
|
// in transfer asset 全て NFT , out transfer asset に NFT 含まない場合、 buy-nft action に変換
|
|
140
|
-
|
|
138
|
+
if (
|
|
141
139
|
inOutTransferList
|
|
142
140
|
.filter((transfer) => transfer.direction === 'in')
|
|
143
141
|
.every((transfer) => transfer.asset.type === 'Nft') &&
|
|
@@ -177,7 +175,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
177
175
|
}))
|
|
178
176
|
}
|
|
179
177
|
// out transfer 全て NFT, in transfer に NFT を含まない場合、 sell-nft action に変換
|
|
180
|
-
|
|
178
|
+
if (
|
|
181
179
|
inOutTransferList
|
|
182
180
|
.filter((transfer) => transfer.direction === 'out')
|
|
183
181
|
.every((transfer) => transfer.asset.type === 'Nft') &&
|
|
@@ -216,7 +214,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
216
214
|
}))
|
|
217
215
|
}
|
|
218
216
|
// in transfer asset 全て Crypto , out transfer asset 全て Fiat の場合、 buy-crypto action に変換
|
|
219
|
-
|
|
217
|
+
if (
|
|
220
218
|
inOutTransferList
|
|
221
219
|
.filter((transfer) => transfer.direction === 'in')
|
|
222
220
|
.every((transfer) => transfer.asset.type === 'CryptoCurrency') &&
|
|
@@ -252,7 +250,7 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
252
250
|
}))
|
|
253
251
|
}
|
|
254
252
|
// in transfer asset 全て Fiat , out transfer asset 全て Crypto の場合、 sell-crypto action に変換
|
|
255
|
-
|
|
253
|
+
if (
|
|
256
254
|
inOutTransferList
|
|
257
255
|
.filter((transfer) => transfer.direction === 'in')
|
|
258
256
|
.every((transfer) => transfer.asset.type === 'FiatCurrency') &&
|
|
@@ -288,193 +286,24 @@ export const createEvidenceNoneAccountActions = ({
|
|
|
288
286
|
}))
|
|
289
287
|
}
|
|
290
288
|
// それ以外の場合、 trade action に変換
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
} satisfies AccountActionTrade,
|
|
312
|
-
].map((action, order) => ({
|
|
313
|
-
...action,
|
|
314
|
-
order,
|
|
315
|
-
}))
|
|
316
|
-
}
|
|
317
|
-
}
|
|
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}`
|
|
289
|
+
return [
|
|
290
|
+
...moveActionList,
|
|
291
|
+
{
|
|
292
|
+
type: 'NormalAccountAction',
|
|
293
|
+
action: 'trade',
|
|
294
|
+
source,
|
|
295
|
+
order: 0,
|
|
296
|
+
comment: undefined,
|
|
297
|
+
app: undefined,
|
|
298
|
+
evidence: 'none',
|
|
299
|
+
timestamp,
|
|
300
|
+
transfers: [
|
|
301
|
+
...inOutTransferList.filter((transfer) => transfer.direction === 'in'),
|
|
302
|
+
...inOutTransferList.filter((transfer) => transfer.direction === 'out'),
|
|
303
|
+
],
|
|
304
|
+
} satisfies AccountActionTrade,
|
|
305
|
+
].map((action, order) => ({
|
|
306
|
+
...action,
|
|
307
|
+
order,
|
|
308
|
+
}))
|
|
480
309
|
}
|
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
}
|
|
140
|
+
if (transfer.from !== undefined && accountIds.has(transfer.from)) {
|
|
141
|
+
return 'out'
|
|
142
|
+
}
|
|
143
|
+
if (transfer.to !== undefined && accountIds.has(transfer.to)) {
|
|
144
|
+
return 'in'
|
|
145
|
+
}
|
|
146
|
+
return 'none'
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const reverseTransferDirection = (
|
|
150
|
+
direction: Transfer['direction'],
|
|
151
|
+
): Transfer['direction'] => {
|
|
152
|
+
switch (direction) {
|
|
153
|
+
case 'in': {
|
|
154
|
+
return 'out'
|
|
155
|
+
}
|
|
156
|
+
case 'none': {
|
|
157
|
+
return 'none'
|
|
158
|
+
}
|
|
159
|
+
case 'out': {
|
|
160
|
+
return 'in'
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -34,6 +34,7 @@ export const coingeckoCryptoCurrencyDataSource = createCryptoCurrencyDataSource(
|
|
|
34
34
|
)
|
|
35
35
|
}
|
|
36
36
|
const coingeckoIds = targetCurrencies.map(
|
|
37
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
37
38
|
(currency) => currency.market.coingeckoId!,
|
|
38
39
|
)
|
|
39
40
|
const coins = await getCoinsMarkets(
|
|
@@ -84,6 +85,7 @@ export const coingeckoCryptoCurrencyDataSource = createCryptoCurrencyDataSource(
|
|
|
84
85
|
// timestamp から from,to を生成する
|
|
85
86
|
const sortedTimestampList = timestampList.toSorted((a, b) => a - b)
|
|
86
87
|
const from = Math.floor(sortedTimestampList[0] / 1000)
|
|
88
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
87
89
|
const to = Math.ceil(sortedTimestampList.at(-1)! / 1000)
|
|
88
90
|
|
|
89
91
|
// from, to を 90 日間毎に分割する
|
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.
|
|
3
|
+
"version": "0.0.36",
|
|
4
4
|
"description": "Cryptorch | TypeScript Analyze Interface for Crypto Data.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cryptorch",
|
|
@@ -35,10 +35,7 @@
|
|
|
35
35
|
"!tsconfig.build.json"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@0xtorch/big-decimal": "^0.0.
|
|
38
|
+
"@0xtorch/big-decimal": "^0.0.10",
|
|
39
39
|
"zod": "^3.23.8"
|
|
40
|
-
},
|
|
41
|
-
"peerDependencies": {
|
|
42
|
-
"typescript": "^5.0.0"
|
|
43
40
|
}
|
|
44
41
|
}
|
package/utils/errors.ts
CHANGED
|
@@ -26,11 +26,10 @@ class BaseError extends Error {
|
|
|
26
26
|
const details =
|
|
27
27
|
arguments_.cause instanceof BaseError
|
|
28
28
|
? arguments_.cause.details
|
|
29
|
-
:
|
|
30
|
-
arguments_.cause?.message !== undefined &&
|
|
29
|
+
: arguments_.cause?.message !== undefined &&
|
|
31
30
|
arguments_.cause?.message.length > 0
|
|
32
31
|
? arguments_.cause.message
|
|
33
|
-
: arguments_.details
|
|
32
|
+
: arguments_.details ?? ''
|
|
34
33
|
|
|
35
34
|
this.message = [
|
|
36
35
|
shortMessage.length > 0 ? shortMessage : 'An error occurred.',
|
|
@@ -49,9 +48,8 @@ class BaseError extends Error {
|
|
|
49
48
|
|
|
50
49
|
walk(): Error
|
|
51
50
|
walk(function_: (error: unknown) => boolean): Error | null
|
|
52
|
-
//
|
|
51
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
53
52
|
walk(function_?: any): any {
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
55
53
|
return walk(this, function_)
|
|
56
54
|
}
|
|
57
55
|
}
|
package/utils/rest.ts
CHANGED
|
@@ -47,7 +47,6 @@ export const rest = async <TData>(
|
|
|
47
47
|
signal: true,
|
|
48
48
|
},
|
|
49
49
|
)
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
51
50
|
const data =
|
|
52
51
|
response.headers.get('Content-Type')?.startsWith('application/json') ===
|
|
53
52
|
true
|
|
@@ -57,8 +56,7 @@ export const rest = async <TData>(
|
|
|
57
56
|
if (!response.ok) {
|
|
58
57
|
const errorStringify =
|
|
59
58
|
typeof data === 'object' && data !== null && 'error' in data
|
|
60
|
-
?
|
|
61
|
-
stringify(data.error)
|
|
59
|
+
? stringify(data.error)
|
|
62
60
|
: ''
|
|
63
61
|
throw new HttpRequestError({
|
|
64
62
|
body: body === undefined ? undefined : stringify(body),
|
|
@@ -77,15 +75,15 @@ export const rest = async <TData>(
|
|
|
77
75
|
error instanceof ZodError
|
|
78
76
|
) {
|
|
79
77
|
throw error
|
|
80
|
-
}
|
|
78
|
+
}
|
|
79
|
+
if (error instanceof Error) {
|
|
81
80
|
throw new HttpRequestError({
|
|
82
81
|
body: body === undefined ? undefined : stringify(body),
|
|
83
82
|
details: error.message,
|
|
84
83
|
url,
|
|
85
84
|
})
|
|
86
|
-
} else {
|
|
87
|
-
throw error
|
|
88
85
|
}
|
|
86
|
+
throw error
|
|
89
87
|
}
|
|
90
88
|
}
|
|
91
89
|
|
package/utils/stringify.ts
CHANGED
|
@@ -2,9 +2,7 @@ export const stringify: typeof JSON.stringify = (value, replacer, space) =>
|
|
|
2
2
|
JSON.stringify(
|
|
3
3
|
value,
|
|
4
4
|
(key, value_) => {
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
6
5
|
const value = typeof value_ === 'bigint' ? value_.toString() : value_
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
8
6
|
return typeof replacer === 'function' ? replacer(key, value) : value
|
|
9
7
|
},
|
|
10
8
|
space,
|