-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
73 lines (64 loc) · 2.04 KB
/
index.ts
File metadata and controls
73 lines (64 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { parseArgs } from 'util'
import { resolve } from 'path'
import { parse } from 'csv-parse/sync'
import { stringify } from 'csv-stringify/sync'
import { BigNumber } from 'bignumber.js'
async function getInputFile(): Promise<string> {
const {
values: { input }
} = parseArgs({
args: Bun.argv,
options: {
input: {
type: 'string',
short: 'i'
}
},
strict: true,
allowPositionals: true
})
if (!input) throw new Error('No file path provided')
const resolvedPath = resolve(__dirname, input)
const csvFile = Bun.file(resolvedPath)
const fileExists = await csvFile.exists()
if (!fileExists) throw new Error('File does not exist')
return csvFile.text()
}
try {
const fileContent = await getInputFile()
const bybitOrders = parse(fileContent, {
columns: true,
skip_empty_lines: true
})
const snowballOrders = bybitOrders.map((bybitOrder: any) => {
if (bybitOrder.Direction !== 'BUY' && bybitOrder.Direction !== 'SELL')
throw new Error('Invalid direction')
const pair = bybitOrder['Spot Pairs']
if (!pair.endsWith('USDT')) throw new Error('Unsupported pair')
const eventDate = bybitOrder['Timestamp (UTC)'].split(' ')[1]
let eventSymbol = pair.replace('USDT', '-USD')
if (eventSymbol === 'TON-USD') eventSymbol = 'TON11419-USD'
const fees = new BigNumber(bybitOrder['Fees'], 10)
return {
Event: bybitOrder.Direction === 'BUY' ? 'Buy' : 'Sell',
Date: eventDate,
Symbol: eventSymbol,
Price: bybitOrder['Filled Price'],
Quantity: bybitOrder['Filled Quantity'],
Currency: 'USD',
FeeTax: fees.multipliedBy(bybitOrder['Filled Price'], 10).toString(10),
Exchange: 'BYBIT',
NKD: '',
FeeCurrency: '',
DoNotAdjustCash: '',
Note: `Bybit order ${bybitOrder['Order No.'].trim()}`
}
})
const snowballCSV = stringify(snowballOrders, {
header: true
})
await Bun.file(`snowball-${Date.now()}.csv`).write(snowballCSV)
} catch (err) {
console.error((err as Error).message)
process.exit(1)
}