@0xtorch/core 0.0.18 → 0.0.20

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 (39) hide show
  1. package/_cjs/assets/definitions/index.js +3 -1
  2. package/_cjs/assets/definitions/index.js.map +1 -1
  3. package/_cjs/assets/definitions/solana.js +17 -0
  4. package/_cjs/assets/definitions/solana.js.map +1 -0
  5. package/_cjs/assets/index.js +2 -1
  6. package/_cjs/assets/index.js.map +1 -1
  7. package/_cjs/index.js +4 -1
  8. package/_cjs/index.js.map +1 -1
  9. package/_cjs/setAccountActionPrices.js +73 -0
  10. package/_cjs/setAccountActionPrices.js.map +1 -0
  11. package/_esm/assets/definitions/index.js +1 -0
  12. package/_esm/assets/definitions/index.js.map +1 -1
  13. package/_esm/assets/definitions/solana.js +14 -0
  14. package/_esm/assets/definitions/solana.js.map +1 -0
  15. package/_esm/assets/index.js +1 -1
  16. package/_esm/assets/index.js.map +1 -1
  17. package/_esm/index.js +2 -1
  18. package/_esm/index.js.map +1 -1
  19. package/_esm/setAccountActionPrices.js +72 -0
  20. package/_esm/setAccountActionPrices.js.map +1 -0
  21. package/_types/assets/definitions/index.d.ts +1 -0
  22. package/_types/assets/definitions/index.d.ts.map +1 -1
  23. package/_types/assets/definitions/solana.d.ts +13 -0
  24. package/_types/assets/definitions/solana.d.ts.map +1 -0
  25. package/_types/assets/index.d.ts +1 -1
  26. package/_types/assets/index.d.ts.map +1 -1
  27. package/_types/index.d.ts +2 -1
  28. package/_types/index.d.ts.map +1 -1
  29. package/_types/setAccountActionPrices.d.ts +3631 -0
  30. package/_types/setAccountActionPrices.d.ts.map +1 -0
  31. package/assets/definitions/index.ts +1 -0
  32. package/assets/definitions/solana.ts +14 -0
  33. package/assets/index.ts +1 -0
  34. package/index.ts +2 -0
  35. package/package.json +1 -1
  36. package/setAccountActionPrices.ts +123 -0
  37. package/.DS_Store +0 -0
  38. package/actions/.DS_Store +0 -0
  39. package/assets/.DS_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ronin } from './ronin'
16
+ export { solana } from './solana'
16
17
  export { usd } from './usd'
@@ -0,0 +1,14 @@
1
+ import { createCryptoCurrency } from '../creates'
2
+
3
+ export const solana = createCryptoCurrency({
4
+ type: 'CryptoCurrency',
5
+ id: 'solana',
6
+ name: 'Solana',
7
+ symbol: 'SOL',
8
+ icon: undefined,
9
+ market: {
10
+ coingeckoId: 'solana',
11
+ marketCapUsd: undefined,
12
+ },
13
+ updatedAt: Date.now(),
14
+ })
package/assets/index.ts CHANGED
@@ -16,6 +16,7 @@ export {
16
16
  moonbeam,
17
17
  moonriver,
18
18
  ronin,
19
+ solana,
19
20
  usd,
20
21
  } from './definitions'
21
22
  export {
package/index.ts CHANGED
@@ -233,6 +233,7 @@ export {
233
233
  moonriver,
234
234
  nftSchema,
235
235
  ronin,
236
+ solana,
236
237
  usd,
237
238
  } from './assets'
238
239
  export type {
@@ -244,6 +245,7 @@ export type {
244
245
  } from './assets'
245
246
  export { createPortfolio } from './portfolios'
246
247
  export type { Portfolio } from './portfolios'
248
+ export { setAccountActionPrices } from './setAccountActionPrices'
247
249
  export type { Config, Logger } from './types'
248
250
  export { rest, stringify } from './utils'
249
251
  export type { Schema } from './utils'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xtorch/core",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "description": "Cryptorch | TypeScript Analyze Interface for Crypto Data.",
5
5
  "keywords": [
6
6
  "cryptorch",
@@ -0,0 +1,123 @@
1
+ import type { AccountAction } from './actions'
2
+ import type {
3
+ CryptoCurrency,
4
+ CryptoCurrencyDataSource,
5
+ CryptoCurrencyPrice,
6
+ FiatCurrency,
7
+ } from './assets'
8
+
9
+ type SetAccountActionPricesParameters = {
10
+ readonly actions: readonly AccountAction[]
11
+ readonly fiat: FiatCurrency
12
+ readonly cryptoCurrencyDataSource: CryptoCurrencyDataSource
13
+ }
14
+
15
+ export const setAccountActionPrices = async ({
16
+ actions,
17
+ fiat,
18
+ cryptoCurrencyDataSource,
19
+ }: SetAccountActionPricesParameters) => {
20
+ // crypto currency 毎の取得期間を作成
21
+ const targets = getCryptoCurrencyTargets(actions)
22
+
23
+ // 取得期間をもとに価格を取得
24
+ const cryptoCurrencyPricesOfTarget = await Promise.all(
25
+ targets.map(({ currency, from, to }) =>
26
+ cryptoCurrencyDataSource.getHistoricalPrices({
27
+ targetCurrencies: [currency],
28
+ vsCurrency: fiat,
29
+ from: from - 1000 * 60 * 60,
30
+ to: to + 1000 * 60 * 60,
31
+ }),
32
+ ),
33
+ )
34
+ const prices = cryptoCurrencyPricesOfTarget
35
+ .flat()
36
+ .toSorted((a, b) => a.timestamp - b.timestamp)
37
+
38
+ // 価格を設定して返す
39
+ return actions.map((action) => setPrices(action, prices))
40
+ }
41
+
42
+ type CryptoCurrencyTarget = {
43
+ readonly currency: CryptoCurrency
44
+ readonly from: number
45
+ readonly to: number
46
+ }
47
+
48
+ const getCryptoCurrencyTargets = (
49
+ actions: readonly AccountAction[],
50
+ ): readonly CryptoCurrencyTarget[] => {
51
+ const mut_targets: CryptoCurrencyTarget[] = []
52
+ for (const action of actions) {
53
+ for (const transfer of action.transfers) {
54
+ if (transfer.asset.type !== 'CryptoCurrency') {
55
+ continue
56
+ }
57
+ const targetIndex = mut_targets.findIndex(
58
+ (target) => target.currency.id === transfer.asset.id,
59
+ )
60
+ if (targetIndex === -1) {
61
+ mut_targets.push({
62
+ currency: transfer.asset,
63
+ from: action.timestamp,
64
+ to: action.timestamp,
65
+ })
66
+ } else {
67
+ const target = mut_targets[targetIndex]
68
+ mut_targets[targetIndex] = {
69
+ currency: target.currency,
70
+ from: Math.min(target.from, action.timestamp),
71
+ to: Math.max(target.to, action.timestamp),
72
+ }
73
+ }
74
+ }
75
+ }
76
+ return mut_targets
77
+ }
78
+
79
+ const setPrices = (
80
+ action: AccountAction,
81
+ prices: readonly CryptoCurrencyPrice[],
82
+ ): AccountAction =>
83
+ ({
84
+ ...action,
85
+ transfers: action.transfers.map((transfer) => {
86
+ switch (transfer.asset.type) {
87
+ case 'CryptoCurrency': {
88
+ return {
89
+ ...transfer,
90
+ price: getPrice(prices, action.timestamp, transfer.asset.id),
91
+ }
92
+ }
93
+ case 'FiatCurrency': {
94
+ return transfer
95
+ }
96
+ case 'Nft': {
97
+ return transfer
98
+ }
99
+ }
100
+ }),
101
+ }) as AccountAction
102
+
103
+ const getPrice = (
104
+ prices: readonly CryptoCurrencyPrice[],
105
+ timestamp: number,
106
+ id: string | undefined,
107
+ ): CryptoCurrencyPrice | undefined =>
108
+ prices
109
+ .filter(
110
+ ({ cryptoCurrencyId, timestamp: priceTimestamp }) =>
111
+ cryptoCurrencyId === id &&
112
+ priceTimestamp >= timestamp - 1000 * 60 * 60 &&
113
+ priceTimestamp <= timestamp + 1000 * 60 * 60,
114
+ )
115
+ .reduce(
116
+ (closestPrice: CryptoCurrencyPrice | undefined, price) =>
117
+ closestPrice === undefined ||
118
+ Math.abs(price.timestamp - timestamp) <
119
+ Math.abs(closestPrice.timestamp - timestamp)
120
+ ? price
121
+ : closestPrice,
122
+ undefined,
123
+ )
package/.DS_Store DELETED
Binary file
package/actions/.DS_Store DELETED
Binary file
package/assets/.DS_Store DELETED
Binary file