-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtransaction_history_section.tsx
111 lines (105 loc) · 3.13 KB
/
transaction_history_section.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
import download from '../../ui-components/images/download.svg';
import { ProofId, UserPaymentTx } from '@aztec/sdk';
import { Button, ButtonSize, ButtonTheme } from '@aztec/aztec-ui';
import { useAccountStateManager, useSdk } from '../../alt-model/top_level_context/top_level_context_hooks.js';
import { useObs } from '../../app/util/index.js';
import { Section, SectionTitle } from '../../ui-components/index.js';
import { TransactionHistory } from './transaction_history.js';
import { useUserTxs } from '../../alt-model/user_tx_hooks.js';
export function TransactionHistorySection() {
const sdk = useSdk();
const txs = useUserTxs();
const accountStateManager = useAccountStateManager();
const accountState = useObs(accountStateManager.stateObs);
async function getHistory() {
if (!sdk || !txs || !accountState || !accountState.userId) return;
const rows = [
[
'Ethereum Account',
'Aztec Account Address',
'Transaction ID',
'Created',
'Settled',
'Transaction Type',
'Asset ID',
'Value',
'Fee',
'Is Sender?',
],
];
txs.forEach(tx => {
let txType = '',
value = '',
assetId = '',
isSender = '',
fee = '';
switch (tx.proofId) {
case ProofId.DEPOSIT:
txType = 'Deposit';
break;
case ProofId.WITHDRAW:
txType = 'Withdrawal';
break;
case ProofId.SEND:
txType = 'Send';
break;
case ProofId.ACCOUNT:
txType = 'Account';
break;
case ProofId.DEFI_DEPOSIT:
txType = 'Defi Deposit';
break;
case ProofId.DEFI_CLAIM:
txType = 'Defi Claim';
break;
}
if (tx instanceof UserPaymentTx) {
value = tx.value.value.toString();
assetId = tx.value.assetId.toString();
fee = tx.fee.value.toString();
isSender = tx.isSender.toString();
}
rows.push([
accountState.ethAddressUsedForAccountKey.toString(),
tx.userId.toString(),
tx.txId ? tx.txId.toString() : '-',
tx.created ? tx.created.toISOString() : '-',
tx.settled ? tx.settled.toISOString() : '-',
txType,
assetId,
value,
fee,
isSender ? 'Yes' : 'No',
]);
});
const blob = new Blob([rows.map(e => e.join(',')).join('\n')], { type: 'csv' });
const a = document.createElement('a');
a.download = 'transaction_history.csv';
a.href = window.URL.createObjectURL(blob);
const clickEvt = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
});
a.dispatchEvent(clickEvt);
a.remove();
}
return (
<Section>
<SectionTitle
label="Transaction History"
sideComponent={
<Button
imageSrc={download}
text="Download History"
size={ButtonSize.Small}
theme={ButtonTheme.Secondary}
onClick={getHistory}
disabled={!txs || txs.length === 0}
/>
}
/>
<TransactionHistory txs={txs} />
</Section>
);
}