-
Notifications
You must be signed in to change notification settings - Fork 49
/
transactionHistory.tsx
286 lines (264 loc) · 8.34 KB
/
transactionHistory.tsx
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import {h, Fragment} from 'preact'
import printAda from '../../../helpers/printAda'
import {getActiveAccountInfo, State} from '../../../state'
import {ADALITE_CONFIG} from '../../../config'
import actions from '../../../actions'
import {connect} from '../../../libs/unistore/preact'
import toLocalDate from '../../../helpers/toLocalDate'
import {
RewardWithdrawal,
StakingHistoryItemType,
StakingHistoryObject,
StakingReward,
HexString,
Lovelace,
TxSummaryEntry,
} from '../../../types'
import {StarIcon} from '../../common/svg'
import moment = require('moment')
const FormattedAmount = ({amount}: {amount: Lovelace}): h.JSX.Element => {
const value = printAda(Math.abs(amount) as Lovelace)
const number = `${value}`.indexOf('.') === -1 ? `${value}.0` : `${value}`
return (
<div className={`transaction-amount ${amount > 0 ? 'credit' : 'debit'}`}>
{`${number}`.padEnd(10)}
</div>
)
}
const FormattedFee = ({fee}: {fee: Lovelace}): h.JSX.Element => {
const value = printAda(fee)
return <div className="transaction-fee nowrap">{`Fee: ${value}`}</div>
}
const FormattedTransaction = ({txid}: {txid: HexString}): h.JSX.Element => (
<div className="blockexplorer-link">
<span>View on </span>
{ADALITE_CONFIG.ADALITE_CARDANO_VERSION === 'byron' && (
<span>
<a
className="transaction-address"
href={`https://blockchair.com/cardano/transaction/${txid}`}
target="_blank"
rel="noopener"
>
Blockchair
</a>
<span> | </span>
<a
className="transaction-address"
href={`https://adascan.net/transaction/${txid}`}
target="_blank"
rel="noopener"
>
AdaScan
</a>
</span>
)}
{ADALITE_CONFIG.ADALITE_CARDANO_VERSION === 'shelley' && (
<span>
<a
className="transaction-address"
href={`https://cardanoscan.io/transaction/${txid}`}
target="_blank"
rel="noopener"
>
CardanoScan
</a>
{/* <span> | </span> */}
{/* <a
className="transaction-address"
href={`hhttps://explorer.cardano.org/en/transaction?id=${txid}`}
target="_blank"
rel="noopener"
>
CardanoExplorer
</a> */}
<span> | </span>
<a
className="transaction-address"
href={`https://adaex.org/${txid}`}
target="_blank"
rel="noopener"
>
ADAex
</a>
</span>
)}
</div>
)
type MultiAssetProps = {
star: boolean
name: string
hash: string
amount: number
}
const MultiAsset = ({star, name, hash, amount}: MultiAssetProps) => (
<Fragment>
<div className="row">
<div className="multi-asset-name">
{star && <StarIcon />}
{name}
</div>
<div className={`multi-asset-amount ${amount > 0 ? 'credit' : 'debit'}`}>
{printAda(Math.abs(amount) as Lovelace)}
</div>
</div>
<div className="multi-asset-hash">
<span className="ellipsis">{hash.slice(0, -6)}</span>
{hash.slice(-6)}
</div>
</Fragment>
)
interface Props {
transactionHistory: Array<TxSummaryEntry>
stakingHistory: Array<StakingHistoryObject>
}
const ExportCSV = ({transactionHistory, stakingHistory}: Props): h.JSX.Element => {
const withdrawalHistory: {[key: string]: Lovelace} = stakingHistory
.filter((item) => item.type === StakingHistoryItemType.REWARD_WITHDRAWAL)
.reduce((acc, withdrawal: RewardWithdrawal) => {
acc[withdrawal.txHash] = withdrawal.amount
return acc
}, {})
const stakingRewards = stakingHistory.filter(
(item) => item.type === StakingHistoryItemType.STAKING_REWARD
) as Array<StakingReward>
const delimiter = ','
const rowsDelimiter = '\n'
const headers = [
'Type',
'Received amount',
'Received currency',
'Sent amount',
'Sent currency',
'Fee amount',
'Fee currency',
'Transaction ID',
'Date',
]
.map((header) => `${header}${delimiter}`)
.join('')
const enum TxSummaryType {
RECEIVED = 'Received',
SENT = 'Sent',
REWARD_AWARDED = 'Reward awarded',
}
type TxEntry = {
type: TxSummaryType
txHash?: string
dateTime: moment.Moment
sent?: Lovelace
received?: Lovelace
fee?: Lovelace
}
const transactionsEntries: Array<TxEntry> = transactionHistory.map(
(transaction: TxSummaryEntry) => {
const common = {
txHash: transaction.ctbId,
dateTime: moment.utc(new Date(transaction.ctbTimeIssued * 1000)),
}
if (withdrawalHistory.hasOwnProperty(transaction.ctbId)) {
return {
...common,
type: TxSummaryType.SENT,
sent: (Math.abs(transaction.effect - withdrawalHistory[transaction.ctbId]) -
transaction.fee) as Lovelace,
fee: transaction.fee,
}
} else if (transaction.effect > 0) {
return {
...common,
type: TxSummaryType.RECEIVED,
received: transaction.effect,
}
} else {
return {
...common,
type: TxSummaryType.SENT,
sent: (Math.abs(transaction.effect) - transaction.fee) as Lovelace,
fee: transaction.fee,
}
}
}
)
const rewardsEntries: Array<TxEntry> = stakingRewards.map((stakingReward: StakingReward) => ({
type: TxSummaryType.REWARD_AWARDED,
dateTime: moment(stakingReward.dateTime),
received: stakingReward.reward,
}))
const entries: Array<TxEntry> = [...transactionsEntries, ...rewardsEntries].sort(
(a, b) => b.dateTime.unix() - a.dateTime.unix()
)
const rows: Array<string> = entries.map((entry) =>
[
entry.type,
entry.received && printAda(entry.received as Lovelace),
entry.received !== undefined ? 'ADA' : undefined,
entry.sent && printAda(entry.sent as Lovelace),
entry.sent !== undefined ? 'ADA' : undefined,
entry.fee && printAda(entry.fee as Lovelace),
entry.fee !== undefined ? 'ADA' : undefined,
entry.txHash,
entry.dateTime.format('MM/DD/YYYY hh:mm A [UTC]'),
]
.map((value) => (value === undefined ? delimiter : `${value}${delimiter}`))
.join('')
)
const fileContents = `${headers}${rowsDelimiter}${rows.join(rowsDelimiter)}`
const filename = 'transactions.csv'
const filetype = 'text/plain'
const dataURI = `data:${filetype};base64,${btoa(fileContents)}`
return (
<a href={dataURI} download={filename} className="download-transactions-text">
Export to CSV
</a>
)
}
const TransactionHistory = ({transactionHistory, stakingHistory}: Props): h.JSX.Element => (
<div className="transactions card">
<div className="transactions-header">
<h2 className="card-title">Transaction History</h2>
<div className="download-transaction">
<ExportCSV transactionHistory={transactionHistory} stakingHistory={stakingHistory} />
</div>
</div>
{transactionHistory.length === 0 ? (
<div className="transactions-empty">No transactions found</div>
) : (
<ul className="transactions-content">
{transactionHistory.map((transaction: TxSummaryEntry) => (
<li key={transaction.ctbId} className="transaction-item">
<div className="row">
<div className="transaction-date">
{toLocalDate(new Date(transaction.ctbTimeIssued * 1000))}
</div>
<FormattedAmount amount={transaction.effect} />
</div>
<div className="row">
<FormattedTransaction txid={transaction.ctbId} />
<FormattedFee fee={transaction.fee} />
</div>
<MultiAsset
star
name="Testcoin"
hash="95a292ffee938be03e9bae5657982a74e9014eb4960108c9e23a5b39"
amount={transaction.effect} // mock, just to show something
/>
<MultiAsset
star={false}
name="Testcoin 2"
hash="95a292ffee938be03e9bae5657982a74e9014eb4960108c9e23a5b39"
amount={transaction.effect} // mock, just to show something
/>
</li>
))}
</ul>
)}
</div>
)
export default connect(
(state: State) => ({
transactionHistory: getActiveAccountInfo(state).transactionHistory,
stakingHistory: getActiveAccountInfo(state).stakingHistory,
}),
actions
)(TransactionHistory)