forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.ts
More file actions
89 lines (78 loc) · 2.34 KB
/
Copy pathexport.ts
File metadata and controls
89 lines (78 loc) · 2.34 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type { StreamData } from '@/types/stream'
import { formatTokenAmount, getStreamStatus, SECONDS_PER_DAY } from '@/lib/stream-utils'
const SECONDS_PER_DAY_BIGINT = BigInt(SECONDS_PER_DAY)
const DAYS_PER_MONTH = BigInt(30)
function unixToISO(seconds: bigint): string {
if (seconds === BigInt(0)) return ''
return new Date(Number(seconds) * 1000).toISOString()
}
function escapeCSV(value: string): string {
if (/[",\n\r]/.test(value)) return `"${value.replace(/"/g, '""')}"`
return value
}
function row(values: string[]): string {
return values.map(escapeCSV).join(',')
}
const HEADERS = [
'Stream ID',
'Status',
'Sender',
'Recipient',
'Token',
'Token Address',
'Total Amount',
'Withdrawn Amount',
'Remaining Amount',
'Start Date',
'End Date',
'Cliff Date',
'Cliff Amount',
'Rate (per day)',
'Rate (per month)',
]
export function streamsToCSV(
streams: StreamData[],
nowSeconds: number = Math.floor(Date.now() / 1000),
): string {
const lines: string[] = [HEADERS.join(',')]
for (const s of streams) {
const { decimals, symbol } = s.token
const status = getStreamStatus(s, nowSeconds)
const remaining = s.depositedAmount - s.withdrawnAmount
const ratePerDay = formatTokenAmount(s.amountPerSecond * SECONDS_PER_DAY_BIGINT, decimals, 6)
const ratePerMonth = formatTokenAmount(
s.amountPerSecond * SECONDS_PER_DAY_BIGINT * DAYS_PER_MONTH,
decimals,
6,
)
lines.push(
row([
s.id,
status,
s.sender,
s.recipient,
symbol,
s.token.address,
formatTokenAmount(s.depositedAmount, decimals, 6),
formatTokenAmount(s.withdrawnAmount, decimals, 6),
formatTokenAmount(remaining > BigInt(0) ? remaining : BigInt(0), decimals, 6),
unixToISO(s.startTime),
unixToISO(s.endTime),
s.cliffTime > s.startTime ? unixToISO(s.cliffTime) : '',
s.cliffAmount > BigInt(0) ? formatTokenAmount(s.cliffAmount, decimals, 6) : '',
ratePerDay,
ratePerMonth,
]),
)
}
return lines.join('\n')
}
export function downloadCSV(csv: string, filename: string): void {
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = filename
a.click()
URL.revokeObjectURL(url)
}