@0xtorch/csv 0.0.14 → 0.0.15

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 (213) hide show
  1. package/.DS_Store +0 -0
  2. package/_cjs/index.js +3 -3
  3. package/_cjs/index.js.map +1 -1
  4. package/_cjs/{parseCsvFileToAccountActions.js → parseCsvFileToActions.js} +11 -11
  5. package/_cjs/parseCsvFileToActions.js.map +1 -0
  6. package/_cjs/parseRowsToActions/allValue.js +54 -0
  7. package/_cjs/parseRowsToActions/allValue.js.map +1 -0
  8. package/_cjs/parseRowsToActions/asset.js +95 -0
  9. package/_cjs/parseRowsToActions/asset.js.map +1 -0
  10. package/_cjs/parseRowsToActions/cellValue.js +8 -0
  11. package/_cjs/parseRowsToActions/cellValue.js.map +1 -0
  12. package/_cjs/parseRowsToActions/index.js +74 -0
  13. package/_cjs/parseRowsToActions/index.js.map +1 -0
  14. package/_cjs/parseRowsToActions/joinValue.js +52 -0
  15. package/_cjs/parseRowsToActions/joinValue.js.map +1 -0
  16. package/_cjs/parseRowsToActions/key.js +8 -0
  17. package/_cjs/parseRowsToActions/key.js.map +1 -0
  18. package/_cjs/parseRowsToActions/mathValue.js +123 -0
  19. package/_cjs/parseRowsToActions/mathValue.js.map +1 -0
  20. package/_cjs/parseRowsToActions/parse.js +72 -0
  21. package/_cjs/parseRowsToActions/parse.js.map +1 -0
  22. package/_cjs/parseRowsToActions/pattern.js +27 -0
  23. package/_cjs/parseRowsToActions/pattern.js.map +1 -0
  24. package/_cjs/parseRowsToActions/simpleValue.js +22 -0
  25. package/_cjs/parseRowsToActions/simpleValue.js.map +1 -0
  26. package/_cjs/parseRowsToActions/sliceValue.js +35 -0
  27. package/_cjs/parseRowsToActions/sliceValue.js.map +1 -0
  28. package/_cjs/parseRowsToActions/splitValue.js +11 -0
  29. package/_cjs/parseRowsToActions/splitValue.js.map +1 -0
  30. package/_cjs/parseRowsToActions/transfer.js +110 -0
  31. package/_cjs/parseRowsToActions/transfer.js.map +1 -0
  32. package/_cjs/schemas/{schemas.js → format.js} +2 -2
  33. package/_cjs/schemas/format.js.map +1 -0
  34. package/_cjs/schemas/index.js +2 -2
  35. package/_cjs/schemas/index.js.map +1 -1
  36. package/_cjs/schemas/parser.js +117 -72
  37. package/_cjs/schemas/parser.js.map +1 -1
  38. package/_esm/index.js +1 -1
  39. package/_esm/index.js.map +1 -1
  40. package/_esm/{parseCsvFileToAccountActions.js → parseCsvFileToActions.js} +9 -9
  41. package/_esm/parseCsvFileToActions.js.map +1 -0
  42. package/_esm/parseRowsToActions/allValue.js +50 -0
  43. package/_esm/parseRowsToActions/allValue.js.map +1 -0
  44. package/_esm/parseRowsToActions/asset.js +89 -0
  45. package/_esm/parseRowsToActions/asset.js.map +1 -0
  46. package/_esm/parseRowsToActions/cellValue.js +4 -0
  47. package/_esm/parseRowsToActions/cellValue.js.map +1 -0
  48. package/_esm/parseRowsToActions/index.js +72 -0
  49. package/_esm/parseRowsToActions/index.js.map +1 -0
  50. package/_esm/parseRowsToActions/joinValue.js +48 -0
  51. package/_esm/parseRowsToActions/joinValue.js.map +1 -0
  52. package/_esm/parseRowsToActions/key.js +4 -0
  53. package/_esm/parseRowsToActions/key.js.map +1 -0
  54. package/_esm/parseRowsToActions/mathValue.js +119 -0
  55. package/_esm/parseRowsToActions/mathValue.js.map +1 -0
  56. package/_esm/parseRowsToActions/parse.js +70 -0
  57. package/_esm/parseRowsToActions/parse.js.map +1 -0
  58. package/_esm/parseRowsToActions/pattern.js +23 -0
  59. package/_esm/parseRowsToActions/pattern.js.map +1 -0
  60. package/_esm/parseRowsToActions/simpleValue.js +18 -0
  61. package/_esm/parseRowsToActions/simpleValue.js.map +1 -0
  62. package/_esm/parseRowsToActions/sliceValue.js +31 -0
  63. package/_esm/parseRowsToActions/sliceValue.js.map +1 -0
  64. package/_esm/parseRowsToActions/splitValue.js +7 -0
  65. package/_esm/parseRowsToActions/splitValue.js.map +1 -0
  66. package/_esm/parseRowsToActions/transfer.js +106 -0
  67. package/_esm/parseRowsToActions/transfer.js.map +1 -0
  68. package/_esm/schemas/{schemas.js → format.js} +3 -3
  69. package/_esm/schemas/format.js.map +1 -0
  70. package/_esm/schemas/index.js +1 -1
  71. package/_esm/schemas/index.js.map +1 -1
  72. package/_esm/schemas/parser.js +126 -93
  73. package/_esm/schemas/parser.js.map +1 -1
  74. package/_types/index.d.ts +1 -1
  75. package/_types/index.d.ts.map +1 -1
  76. package/_types/parseCsvFileToActions.d.ts +14 -0
  77. package/_types/parseCsvFileToActions.d.ts.map +1 -0
  78. package/_types/parseRowsToActions/allValue.d.ts +9 -0
  79. package/_types/parseRowsToActions/allValue.d.ts.map +1 -0
  80. package/_types/parseRowsToActions/asset.d.ts +30 -0
  81. package/_types/parseRowsToActions/asset.d.ts.map +1 -0
  82. package/_types/parseRowsToActions/cellValue.d.ts +8 -0
  83. package/_types/parseRowsToActions/cellValue.d.ts.map +1 -0
  84. package/_types/parseRowsToActions/index.d.ts +18 -0
  85. package/_types/parseRowsToActions/index.d.ts.map +1 -0
  86. package/_types/parseRowsToActions/joinValue.d.ts +9 -0
  87. package/_types/parseRowsToActions/joinValue.d.ts.map +1 -0
  88. package/_types/parseRowsToActions/key.d.ts +8 -0
  89. package/_types/parseRowsToActions/key.d.ts.map +1 -0
  90. package/_types/parseRowsToActions/mathValue.d.ts +10 -0
  91. package/_types/parseRowsToActions/mathValue.d.ts.map +1 -0
  92. package/_types/parseRowsToActions/parse.d.ts +17 -0
  93. package/_types/parseRowsToActions/parse.d.ts.map +1 -0
  94. package/_types/parseRowsToActions/pattern.d.ts +7 -0
  95. package/_types/parseRowsToActions/pattern.d.ts.map +1 -0
  96. package/_types/parseRowsToActions/simpleValue.d.ts +9 -0
  97. package/_types/parseRowsToActions/simpleValue.d.ts.map +1 -0
  98. package/_types/parseRowsToActions/sliceValue.d.ts +9 -0
  99. package/_types/parseRowsToActions/sliceValue.d.ts.map +1 -0
  100. package/_types/parseRowsToActions/splitValue.d.ts +9 -0
  101. package/_types/parseRowsToActions/splitValue.d.ts.map +1 -0
  102. package/_types/parseRowsToActions/transfer.d.ts +15 -0
  103. package/_types/parseRowsToActions/transfer.d.ts.map +1 -0
  104. package/_types/schemas/format.d.ts +35204 -0
  105. package/_types/schemas/format.d.ts.map +1 -0
  106. package/_types/schemas/index.d.ts +1 -1
  107. package/_types/schemas/index.d.ts.map +1 -1
  108. package/_types/schemas/parser.d.ts +78521 -916
  109. package/_types/schemas/parser.d.ts.map +1 -1
  110. package/_types/types.d.ts +2 -2
  111. package/_types/types.d.ts.map +1 -1
  112. package/index.ts +1 -1
  113. package/package.json +1 -1
  114. package/parseCsvFileToActions.ts +52 -0
  115. package/parseRowsToActions/allValue.ts +61 -0
  116. package/parseRowsToActions/asset.ts +152 -0
  117. package/parseRowsToActions/cellValue.ts +13 -0
  118. package/parseRowsToActions/index.ts +115 -0
  119. package/parseRowsToActions/joinValue.ts +68 -0
  120. package/parseRowsToActions/key.ts +13 -0
  121. package/parseRowsToActions/mathValue.ts +160 -0
  122. package/parseRowsToActions/parse.ts +108 -0
  123. package/parseRowsToActions/pattern.ts +35 -0
  124. package/parseRowsToActions/simpleValue.ts +29 -0
  125. package/parseRowsToActions/sliceValue.ts +51 -0
  126. package/parseRowsToActions/splitValue.ts +18 -0
  127. package/parseRowsToActions/transfer.ts +167 -0
  128. package/schemas/{schemas.ts → format.ts} +2 -2
  129. package/schemas/index.ts +1 -1
  130. package/schemas/parser.ts +143 -93
  131. package/tests/assets.ts +2 -1
  132. package/tests/coincheck-standard.csv +0 -10
  133. package/tests/coincheck.ts +196 -106
  134. package/types.ts +2 -2
  135. package/_cjs/parseCsvFileToAccountActions.js.map +0 -1
  136. package/_cjs/parseCsvRowToAction/amount.js +0 -48
  137. package/_cjs/parseCsvRowToAction/amount.js.map +0 -1
  138. package/_cjs/parseCsvRowToAction/asset.js +0 -63
  139. package/_cjs/parseCsvRowToAction/asset.js.map +0 -1
  140. package/_cjs/parseCsvRowToAction/condition.js +0 -32
  141. package/_cjs/parseCsvRowToAction/condition.js.map +0 -1
  142. package/_cjs/parseCsvRowToAction/index.js +0 -6
  143. package/_cjs/parseCsvRowToAction/index.js.map +0 -1
  144. package/_cjs/parseCsvRowToAction/nft.js +0 -16
  145. package/_cjs/parseCsvRowToAction/nft.js.map +0 -1
  146. package/_cjs/parseCsvRowToAction/parseCsvRowToAction.js +0 -79
  147. package/_cjs/parseCsvRowToAction/parseCsvRowToAction.js.map +0 -1
  148. package/_cjs/parseCsvRowToAction/text.js +0 -29
  149. package/_cjs/parseCsvRowToAction/text.js.map +0 -1
  150. package/_cjs/parseCsvRowToAction/timestamp.js +0 -12
  151. package/_cjs/parseCsvRowToAction/timestamp.js.map +0 -1
  152. package/_cjs/parseCsvRowToAction/transfer.js +0 -25
  153. package/_cjs/parseCsvRowToAction/transfer.js.map +0 -1
  154. package/_cjs/parseCsvRowsToActions.js +0 -34
  155. package/_cjs/parseCsvRowsToActions.js.map +0 -1
  156. package/_cjs/schemas/schemas.js.map +0 -1
  157. package/_esm/parseCsvFileToAccountActions.js.map +0 -1
  158. package/_esm/parseCsvRowToAction/amount.js +0 -44
  159. package/_esm/parseCsvRowToAction/amount.js.map +0 -1
  160. package/_esm/parseCsvRowToAction/asset.js +0 -59
  161. package/_esm/parseCsvRowToAction/asset.js.map +0 -1
  162. package/_esm/parseCsvRowToAction/condition.js +0 -28
  163. package/_esm/parseCsvRowToAction/condition.js.map +0 -1
  164. package/_esm/parseCsvRowToAction/index.js +0 -2
  165. package/_esm/parseCsvRowToAction/index.js.map +0 -1
  166. package/_esm/parseCsvRowToAction/nft.js +0 -12
  167. package/_esm/parseCsvRowToAction/nft.js.map +0 -1
  168. package/_esm/parseCsvRowToAction/parseCsvRowToAction.js +0 -77
  169. package/_esm/parseCsvRowToAction/parseCsvRowToAction.js.map +0 -1
  170. package/_esm/parseCsvRowToAction/text.js +0 -25
  171. package/_esm/parseCsvRowToAction/text.js.map +0 -1
  172. package/_esm/parseCsvRowToAction/timestamp.js +0 -8
  173. package/_esm/parseCsvRowToAction/timestamp.js.map +0 -1
  174. package/_esm/parseCsvRowToAction/transfer.js +0 -21
  175. package/_esm/parseCsvRowToAction/transfer.js.map +0 -1
  176. package/_esm/parseCsvRowsToActions.js +0 -32
  177. package/_esm/parseCsvRowsToActions.js.map +0 -1
  178. package/_esm/schemas/schemas.js.map +0 -1
  179. package/_types/parseCsvFileToAccountActions.d.ts +0 -14
  180. package/_types/parseCsvFileToAccountActions.d.ts.map +0 -1
  181. package/_types/parseCsvRowToAction/amount.d.ts +0 -11
  182. package/_types/parseCsvRowToAction/amount.d.ts.map +0 -1
  183. package/_types/parseCsvRowToAction/asset.d.ts +0 -17
  184. package/_types/parseCsvRowToAction/asset.d.ts.map +0 -1
  185. package/_types/parseCsvRowToAction/condition.d.ts +0 -10
  186. package/_types/parseCsvRowToAction/condition.d.ts.map +0 -1
  187. package/_types/parseCsvRowToAction/index.d.ts +0 -2
  188. package/_types/parseCsvRowToAction/index.d.ts.map +0 -1
  189. package/_types/parseCsvRowToAction/nft.d.ts +0 -12
  190. package/_types/parseCsvRowToAction/nft.d.ts.map +0 -1
  191. package/_types/parseCsvRowToAction/parseCsvRowToAction.d.ts +0 -16
  192. package/_types/parseCsvRowToAction/parseCsvRowToAction.d.ts.map +0 -1
  193. package/_types/parseCsvRowToAction/text.d.ts +0 -11
  194. package/_types/parseCsvRowToAction/text.d.ts.map +0 -1
  195. package/_types/parseCsvRowToAction/timestamp.d.ts +0 -8
  196. package/_types/parseCsvRowToAction/timestamp.d.ts.map +0 -1
  197. package/_types/parseCsvRowToAction/transfer.d.ts +0 -18
  198. package/_types/parseCsvRowToAction/transfer.d.ts.map +0 -1
  199. package/_types/parseCsvRowsToActions.d.ts +0 -16
  200. package/_types/parseCsvRowsToActions.d.ts.map +0 -1
  201. package/_types/schemas/schemas.d.ts +0 -593
  202. package/_types/schemas/schemas.d.ts.map +0 -1
  203. package/parseCsvFileToAccountActions.ts +0 -52
  204. package/parseCsvRowToAction/amount.ts +0 -66
  205. package/parseCsvRowToAction/asset.ts +0 -93
  206. package/parseCsvRowToAction/condition.ts +0 -39
  207. package/parseCsvRowToAction/index.ts +0 -1
  208. package/parseCsvRowToAction/nft.ts +0 -28
  209. package/parseCsvRowToAction/parseCsvRowToAction.ts +0 -128
  210. package/parseCsvRowToAction/text.ts +0 -38
  211. package/parseCsvRowToAction/timestamp.ts +0 -19
  212. package/parseCsvRowToAction/transfer.ts +0 -48
  213. package/parseCsvRowsToActions.ts +0 -54
@@ -0,0 +1,68 @@
1
+ import { stringify } from '@0xtorch/core'
2
+ import type { z } from 'zod'
3
+ import type { joinValueSchema } from '../schemas/parser'
4
+ import type { FormattedRow } from '../types'
5
+ import { getCellValue } from './cellValue'
6
+ import { getMathValue } from './mathValue'
7
+ import { getSliceValue } from './sliceValue'
8
+ import { getSplitValue } from './splitValue'
9
+
10
+ export const getJoinValue = ({
11
+ schema,
12
+ rows,
13
+ service,
14
+ }: {
15
+ schema: z.infer<typeof joinValueSchema>
16
+ rows: readonly FormattedRow[]
17
+ service: string
18
+ }) => {
19
+ return schema.values
20
+ .map((value) => stringify(getValue({ schema: value, rows, service })))
21
+ .join('')
22
+ }
23
+
24
+ const getValue = ({
25
+ schema,
26
+ rows,
27
+ service,
28
+ }: {
29
+ schema: z.infer<typeof joinValueSchema>['values'][0]
30
+ rows: readonly FormattedRow[]
31
+ service: string
32
+ }) => {
33
+ if (typeof schema === 'string') {
34
+ return schema
35
+ }
36
+ switch (schema.type) {
37
+ case 'cell': {
38
+ return getCellValue({
39
+ schema,
40
+ rows,
41
+ })
42
+ }
43
+ case 'math': {
44
+ return getMathValue({
45
+ schema,
46
+ rows,
47
+ service,
48
+ })
49
+ }
50
+ case 'service': {
51
+ return service
52
+ }
53
+ case 'slice': {
54
+ return getSliceValue({
55
+ schema,
56
+ rows,
57
+ service,
58
+ })
59
+ }
60
+ case 'split': {
61
+ return getSplitValue({
62
+ schema,
63
+ rows,
64
+ service,
65
+ })
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,13 @@
1
+ import type { FormattedRow } from '../types'
2
+
3
+ type CreateKeyValueParameters = {
4
+ row: FormattedRow
5
+ key: readonly string[]
6
+ }
7
+
8
+ export const createKeyValue = ({
9
+ row,
10
+ key,
11
+ }: CreateKeyValueParameters): string => {
12
+ return key.map((k) => row[k]?.toString()).join('')
13
+ }
@@ -0,0 +1,160 @@
1
+ import {
2
+ type BigDecimal,
3
+ createBigDecimal,
4
+ divide,
5
+ minus,
6
+ plus,
7
+ times,
8
+ } from '@0xtorch/big-decimal'
9
+ import type { z } from 'zod'
10
+ import type { mathValueSchema } from '../schemas/parser'
11
+ import type { FormattedRow } from '../types'
12
+ import { getCellValue } from './cellValue'
13
+ import { getSliceValue } from './sliceValue'
14
+ import { getSplitValue } from './splitValue'
15
+
16
+ export const getMathValue = ({
17
+ schema,
18
+ rows,
19
+ service,
20
+ }: {
21
+ schema: z.infer<typeof mathValueSchema>
22
+ rows: readonly FormattedRow[]
23
+ service: string
24
+ }) => {
25
+ const firstValue = schema.values[0]
26
+ let current = getBigDecimalValue({ schema: firstValue, rows, service })
27
+ let operator: '+' | '-' | '*' | '/' | undefined
28
+ let args: BigDecimal[] = []
29
+ for (const value of schema.values.slice(1)) {
30
+ if (value === '+') {
31
+ if (operator !== undefined) {
32
+ current = calculate(operator, current, args)
33
+ args = []
34
+ }
35
+ operator = '+'
36
+ continue
37
+ }
38
+ if (value === '-') {
39
+ if (operator !== undefined) {
40
+ current = calculate(operator, current, args)
41
+ args = []
42
+ }
43
+ operator = '-'
44
+ continue
45
+ }
46
+ if (value === '*') {
47
+ if (operator !== undefined) {
48
+ current = calculate(operator, current, args)
49
+ args = []
50
+ }
51
+ operator = '*'
52
+ continue
53
+ }
54
+ if (value === '/') {
55
+ if (operator !== undefined) {
56
+ current = calculate(operator, current, args)
57
+ args = []
58
+ }
59
+ operator = '/'
60
+ continue
61
+ }
62
+ args.push(getBigDecimalValue({ schema: value, rows, service }))
63
+ }
64
+ if (operator !== undefined) {
65
+ current = calculate(operator, current, args)
66
+ }
67
+ return current
68
+ }
69
+
70
+ const getBigDecimalValue = ({
71
+ schema,
72
+ rows,
73
+ service,
74
+ }: {
75
+ schema: z.infer<typeof mathValueSchema>['values'][0]
76
+ rows: readonly FormattedRow[]
77
+ service: string
78
+ }): BigDecimal => {
79
+ const value = getValue({ schema, rows, service })
80
+ if (value === undefined) {
81
+ return createBigDecimal(0n)
82
+ }
83
+ if (typeof value === 'string' || typeof value === 'number') {
84
+ return createBigDecimal(value)
85
+ }
86
+ return value
87
+ }
88
+
89
+ const getValue = ({
90
+ schema,
91
+ rows,
92
+ service,
93
+ }: {
94
+ schema: z.infer<typeof mathValueSchema>['values'][0]
95
+ rows: readonly FormattedRow[]
96
+ service: string
97
+ }) => {
98
+ if (typeof schema === 'string') {
99
+ return schema
100
+ }
101
+ switch (schema.type) {
102
+ case 'cell': {
103
+ return getCellValue({
104
+ schema,
105
+ rows,
106
+ })
107
+ }
108
+ case 'service': {
109
+ return service
110
+ }
111
+ case 'slice': {
112
+ return getSliceValue({
113
+ schema,
114
+ rows,
115
+ service,
116
+ })
117
+ }
118
+ case 'split': {
119
+ return getSplitValue({
120
+ schema,
121
+ rows,
122
+ service,
123
+ })
124
+ }
125
+ }
126
+ }
127
+
128
+ const calculate = (
129
+ operator: '+' | '-' | '*' | '/',
130
+ current: BigDecimal,
131
+ args: BigDecimal[],
132
+ ): BigDecimal => {
133
+ let newValue = current
134
+ switch (operator) {
135
+ case '+': {
136
+ for (const arg of args) {
137
+ newValue = plus(newValue, arg)
138
+ }
139
+ return newValue
140
+ }
141
+ case '-': {
142
+ for (const arg of args) {
143
+ newValue = minus(newValue, arg)
144
+ }
145
+ return newValue
146
+ }
147
+ case '*': {
148
+ for (const arg of args) {
149
+ newValue = times(newValue, arg)
150
+ }
151
+ return newValue
152
+ }
153
+ case '/': {
154
+ for (const arg of args) {
155
+ newValue = divide(times(newValue, createBigDecimal(1n), 18), arg, 18)
156
+ }
157
+ return newValue
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,108 @@
1
+ import {
2
+ type Action,
3
+ type CryptoCurrency,
4
+ type FiatCurrency,
5
+ stringify,
6
+ } from '@0xtorch/core'
7
+ import type { z } from 'zod'
8
+ import type { parserSchema } from '../schemas/parser'
9
+ import type { FormattedRow } from '../types'
10
+ import { getAllValue } from './allValue'
11
+ import { getAssetById } from './asset'
12
+ import { getCellValue } from './cellValue'
13
+ import { isMatchPattern } from './pattern'
14
+ import { createTransfers } from './transfer'
15
+
16
+ type ParseRowsToActionsByParserParameters = {
17
+ rows: readonly FormattedRow[]
18
+ parser: z.infer<typeof parserSchema>
19
+ symbolAssetMap?: { readonly [symbol: string]: string }
20
+ service: string
21
+ cryptoes: readonly CryptoCurrency[]
22
+ fiats: readonly FiatCurrency[]
23
+ }
24
+
25
+ export const parseRowsToActionsByParser = ({
26
+ rows,
27
+ parser,
28
+ symbolAssetMap,
29
+ service,
30
+ cryptoes,
31
+ fiats,
32
+ }: ParseRowsToActionsByParserParameters): readonly Action[] | undefined => {
33
+ // 合致しない condition がある場合は undefined を返す
34
+ for (const condition of parser.conditions) {
35
+ const value = getAllValue({
36
+ schema: condition.value,
37
+ rows,
38
+ service,
39
+ })
40
+ if (
41
+ !isMatchPattern({ value: stringify(value), pattern: condition.pattern })
42
+ ) {
43
+ return undefined
44
+ }
45
+ }
46
+
47
+ // generator 毎に action 生成
48
+ const actions: Action[] = []
49
+ for (const generator of parser.generators) {
50
+ const source = stringify(
51
+ getAllValue({ schema: generator.source, rows, service }),
52
+ )
53
+ const timestampValue = getCellValue({ schema: generator.timestamp, rows })
54
+ const timestamp =
55
+ typeof timestampValue === 'number'
56
+ ? timestampValue
57
+ : new Date(stringify(timestampValue)).getTime()
58
+ const comment =
59
+ generator.comment === undefined
60
+ ? undefined
61
+ : stringify(getAllValue({ schema: generator.comment, rows, service }))
62
+ const app =
63
+ generator.app === undefined
64
+ ? undefined
65
+ : stringify(getAllValue({ schema: generator.app, rows, service }))
66
+ const crossId =
67
+ generator.crossId === undefined
68
+ ? undefined
69
+ : stringify(getAllValue({ schema: generator.crossId, rows, service }))
70
+ const loanId =
71
+ generator.loanId === undefined
72
+ ? undefined
73
+ : stringify(getAllValue({ schema: generator.loanId, rows, service }))
74
+ const target =
75
+ generator.target === undefined
76
+ ? undefined
77
+ : getAssetById({
78
+ schema: generator.target,
79
+ rows,
80
+ service,
81
+ })
82
+ actions.push({
83
+ source,
84
+ order: 0,
85
+ type: generator.type,
86
+ timestamp,
87
+ evidence: 'system-rule',
88
+ comment,
89
+ app,
90
+ transfers: generator.transfers.flatMap((transfer) =>
91
+ createTransfers({
92
+ schema: transfer,
93
+ rows,
94
+ service,
95
+ symbolAssetMap,
96
+ cryptoes,
97
+ fiats,
98
+ }),
99
+ ),
100
+ crossId,
101
+ crossType: generator.crossType,
102
+ loanId,
103
+ target:
104
+ target === undefined || target.type !== 'Nft' ? undefined : target,
105
+ })
106
+ }
107
+ return actions
108
+ }
@@ -0,0 +1,35 @@
1
+ import { stringify } from '@0xtorch/core'
2
+ import type { z } from 'zod'
3
+ import type { patternSchema } from '../schemas/parser'
4
+
5
+ export const isMatchPattern = ({
6
+ value,
7
+ pattern,
8
+ }: {
9
+ value: string
10
+ pattern: z.infer<typeof patternSchema>
11
+ }): boolean => {
12
+ const trimmedValue = value.trim()
13
+ if (typeof pattern === 'string') {
14
+ return trimmedValue === pattern
15
+ }
16
+ switch (pattern.type) {
17
+ case 'defined': {
18
+ return (
19
+ trimmedValue !== stringify(undefined) && trimmedValue.trim().length > 0
20
+ )
21
+ }
22
+ case 'undefined': {
23
+ return (
24
+ trimmedValue.trim().length === 0 ||
25
+ trimmedValue === stringify(undefined)
26
+ )
27
+ }
28
+ case 'in': {
29
+ return pattern.values.includes(trimmedValue)
30
+ }
31
+ case 'not-in': {
32
+ return !pattern.values.includes(trimmedValue)
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,29 @@
1
+ import type { z } from 'zod'
2
+ import type { simpleValueSchema } from '../schemas/parser'
3
+ import type { FormattedRow } from '../types'
4
+ import { getCellValue } from './cellValue'
5
+
6
+ export const getSimpleValue = ({
7
+ schema,
8
+ rows,
9
+ service,
10
+ }: {
11
+ schema: z.infer<typeof simpleValueSchema>
12
+ rows: readonly FormattedRow[]
13
+ service: string
14
+ }) => {
15
+ if (typeof schema === 'string') {
16
+ return schema
17
+ }
18
+ switch (schema.type) {
19
+ case 'cell': {
20
+ return getCellValue({
21
+ schema,
22
+ rows,
23
+ })
24
+ }
25
+ case 'service': {
26
+ return service
27
+ }
28
+ }
29
+ }
@@ -0,0 +1,51 @@
1
+ import { stringify } from '@0xtorch/core'
2
+ import type { z } from 'zod'
3
+ import type { sliceValueSchema } from '../schemas/parser'
4
+ import type { FormattedRow } from '../types'
5
+ import { getCellValue } from './cellValue'
6
+ import { getSplitValue } from './splitValue'
7
+
8
+ export const getSliceValue = ({
9
+ schema,
10
+ rows,
11
+ service,
12
+ }: {
13
+ schema: z.infer<typeof sliceValueSchema>
14
+ rows: readonly FormattedRow[]
15
+ service: string
16
+ }) => {
17
+ const value = getValue({ schema, rows, service })
18
+ return stringify(value).slice(schema.start, schema.end)
19
+ }
20
+
21
+ const getValue = ({
22
+ schema,
23
+ rows,
24
+ service,
25
+ }: {
26
+ schema: z.infer<typeof sliceValueSchema>
27
+ rows: readonly FormattedRow[]
28
+ service: string
29
+ }) => {
30
+ if (typeof schema.value === 'string') {
31
+ return schema.value
32
+ }
33
+ switch (schema.value.type) {
34
+ case 'cell': {
35
+ return getCellValue({
36
+ schema: schema.value,
37
+ rows,
38
+ })
39
+ }
40
+ case 'service': {
41
+ return service
42
+ }
43
+ case 'split': {
44
+ return getSplitValue({
45
+ schema: schema.value,
46
+ rows,
47
+ service,
48
+ })
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,18 @@
1
+ import { stringify } from '@0xtorch/core'
2
+ import type { z } from 'zod'
3
+ import type { splitValueSchema } from '../schemas/parser'
4
+ import type { FormattedRow } from '../types'
5
+ import { getSimpleValue } from './simpleValue'
6
+
7
+ export const getSplitValue = ({
8
+ schema,
9
+ rows,
10
+ service,
11
+ }: {
12
+ schema: z.infer<typeof splitValueSchema>
13
+ rows: readonly FormattedRow[]
14
+ service: string
15
+ }) => {
16
+ const value = getSimpleValue({ schema: schema.value, rows, service })
17
+ return stringify(value).split(schema.splitter)[schema.index]
18
+ }
@@ -0,0 +1,167 @@
1
+ import { type BigDecimal, createBigDecimal } from '@0xtorch/big-decimal'
2
+ import {
3
+ type CryptoCurrency,
4
+ type FiatCurrency,
5
+ type Transfer,
6
+ stringify,
7
+ } from '@0xtorch/core'
8
+ import type { z } from 'zod'
9
+ import type {
10
+ amountSchema,
11
+ targetConditionSchema,
12
+ transferComponentSchema,
13
+ } from '../schemas/parser'
14
+ import type { FormattedRow } from '../types'
15
+ import { getAllValue } from './allValue'
16
+ import { getAsset } from './asset'
17
+ import { isMatchPattern } from './pattern'
18
+
19
+ export const createTransfers = ({
20
+ schema,
21
+ rows,
22
+ service,
23
+ symbolAssetMap,
24
+ cryptoes,
25
+ fiats,
26
+ }: {
27
+ schema: z.infer<typeof transferComponentSchema>
28
+ rows: readonly FormattedRow[]
29
+ service: string
30
+ symbolAssetMap?: { readonly [symbol: string]: string }
31
+ cryptoes: readonly CryptoCurrency[]
32
+ fiats: readonly FiatCurrency[]
33
+ }): readonly Transfer[] => {
34
+ if (schema.target === undefined) {
35
+ return [
36
+ createSingleTransfer({
37
+ schema,
38
+ rows,
39
+ service,
40
+ symbolAssetMap,
41
+ cryptoes,
42
+ fiats,
43
+ }),
44
+ ]
45
+ }
46
+ const targetRows = filterTargetRows({
47
+ conditions: schema.target,
48
+ rows,
49
+ })
50
+ return targetRows.map((targetRow) =>
51
+ createSingleTransfer({
52
+ schema,
53
+ rows: [targetRow],
54
+ service,
55
+ symbolAssetMap,
56
+ cryptoes,
57
+ fiats,
58
+ }),
59
+ )
60
+ }
61
+
62
+ const filterTargetRows = ({
63
+ conditions,
64
+ rows,
65
+ }: {
66
+ conditions: readonly z.infer<typeof targetConditionSchema>[]
67
+ rows: readonly FormattedRow[]
68
+ }): readonly FormattedRow[] => {
69
+ const filteredRows: FormattedRow[] = []
70
+ for (const row of rows) {
71
+ let isMatch = true
72
+ for (const condition of conditions) {
73
+ const value = stringify(row[condition.column])
74
+ if (!isMatchPattern({ value, pattern: condition.pattern })) {
75
+ isMatch = false
76
+ break
77
+ }
78
+ }
79
+ if (isMatch) {
80
+ filteredRows.push(row)
81
+ }
82
+ }
83
+ return filteredRows
84
+ }
85
+
86
+ const createSingleTransfer = ({
87
+ schema,
88
+ rows,
89
+ service,
90
+ symbolAssetMap,
91
+ cryptoes,
92
+ fiats,
93
+ }: {
94
+ schema: z.infer<typeof transferComponentSchema>
95
+ rows: readonly FormattedRow[]
96
+ service: string
97
+ symbolAssetMap?: { readonly [symbol: string]: string }
98
+ cryptoes: readonly CryptoCurrency[]
99
+ fiats: readonly FiatCurrency[]
100
+ }): Transfer => {
101
+ const from =
102
+ schema.from === undefined
103
+ ? undefined
104
+ : stringify(getAllValue({ schema: schema.from, rows, service }))
105
+ const to =
106
+ schema.to === undefined
107
+ ? undefined
108
+ : stringify(getAllValue({ schema: schema.to, rows, service }))
109
+ return {
110
+ direction: schema.direction,
111
+ from,
112
+ to,
113
+ asset: getAsset({
114
+ schema: schema.asset,
115
+ rows,
116
+ service,
117
+ symbolAssetMap,
118
+ cryptoes,
119
+ fiats,
120
+ }),
121
+ amount: createAmount({
122
+ schema: schema.amount,
123
+ rows,
124
+ service,
125
+ }),
126
+ }
127
+ }
128
+
129
+ const createAmount = ({
130
+ schema,
131
+ rows,
132
+ service,
133
+ }: {
134
+ schema: z.infer<typeof amountSchema>
135
+ rows: readonly FormattedRow[]
136
+ service: string
137
+ }): BigDecimal => {
138
+ switch (schema.type) {
139
+ case 'decimal': {
140
+ const baseValue = stringify(
141
+ getAllValue({ schema: schema.value, rows, service }),
142
+ )
143
+ let value: bigint
144
+ if (baseValue === stringify(undefined)) {
145
+ value = 0n
146
+ } else if (baseValue.includes('.')) {
147
+ value = BigInt(Math.floor(Number(baseValue)))
148
+ } else {
149
+ value = BigInt(baseValue)
150
+ }
151
+ return {
152
+ value,
153
+ decimals: schema.decimals,
154
+ }
155
+ }
156
+ case 'text': {
157
+ const value = getAllValue({ schema: schema.value, rows, service })
158
+ if (value === undefined) {
159
+ return createBigDecimal(0n)
160
+ }
161
+ if (typeof value === 'string' || typeof value === 'number') {
162
+ return createBigDecimal(value)
163
+ }
164
+ return value
165
+ }
166
+ }
167
+ }
@@ -1,11 +1,11 @@
1
1
  import { z } from 'zod'
2
2
  import { formatterSchema } from './formatter'
3
- import { parsersSchema } from './parser'
3
+ import { parsersWithKeySchema } from './parser'
4
4
 
5
5
  export const csvFormatSchema = z.object({
6
6
  id: z.string(),
7
7
  service: z.string(),
8
8
  formatter: formatterSchema,
9
- parsers: parsersSchema,
9
+ parser: parsersWithKeySchema,
10
10
  symbolAssetMap: z.record(z.string()).optional(),
11
11
  })
package/schemas/index.ts CHANGED
@@ -1 +1 @@
1
- export { csvFormatSchema } from './schemas'
1
+ export { csvFormatSchema } from './format'