-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathBatchTransactions.tsx
More file actions
146 lines (134 loc) · 4.44 KB
/
BatchTransactions.tsx
File metadata and controls
146 lines (134 loc) · 4.44 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
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
import {
faArrowsRotate,
faPaperPlane,
IconDefinition
} from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useState } from 'react';
import { OutputContainer } from 'components/OutputContainer';
import { TransactionsOutput } from 'components/OutputContainer/components';
import { MvxButton, useGetPendingTransactionsSessions } from 'lib';
import {
useGetAccount,
useGetNetworkConfig,
useGetPendingTransactions
} from 'lib';
import { ItemsIdentifiersEnum } from 'pages/Dashboard/dashboard.types';
import {
signAndAutoSendBatchTransactions,
swapAndLockTokens,
wrapAndMultiTransferTransactions
} from './helpers';
// prettier-ignore
const styles = {
batchTx: 'batch-tx flex flex-col gap-6',
buttonsContainer: 'buttons-container flex flex-col md:flex-row gap-2 items-start',
batchTxButton: 'batch-tx-button text-sm font-normal'
} satisfies Record<string, string>;
interface BatchTransactionsButtonsType {
dataTestId: string;
onClickFunction: () => Promise<void>;
icon: IconDefinition;
label: string;
}
export const BatchTransactions = () => {
const { address, nonce, isGuarded } = useGetAccount();
const { network } = useGetNetworkConfig();
const [currentSessionId, setCurrentSessionId] = useState('');
const pendingSession = useGetPendingTransactionsSessions();
const [sessionId] = Object.keys(pendingSession);
const transactions = useGetPendingTransactions();
const pendingBatchTransactions =
currentSessionId === sessionId ? transactions : [];
const hasPendingTransactions = pendingBatchTransactions.length > 0;
const executeSignAndAutoSendBatchTransactions = async () => {
const sessionId = await signAndAutoSendBatchTransactions({
isGuarded,
address,
nonce,
chainID: network.chainId,
transactionsDisplayInfo: {
processingMessage: 'Processing batch transactions',
errorMessage:
'An error has occurred during batch transaction execution',
successMessage: 'Batch transactions successful'
}
});
setCurrentSessionId(sessionId);
};
const executeWrapMultiTransferTransactions = async () => {
const sessionId = await wrapAndMultiTransferTransactions({
isGuarded,
address,
nonce,
chainID: network.chainId,
transactionsDisplayInfo: {
processingMessage: 'Processing wrap and multi-transfer ESDTs',
errorMessage:
'An error has occurred during wrap and multi-transfer ESDTs',
successMessage: 'Wrap and multi-transfer ESDTs successful'
}
});
setCurrentSessionId(sessionId);
};
const executeSwapAndLockTokens = async () => {
const sessionId = await swapAndLockTokens({
isGuarded,
address,
nonce,
chainID: network.chainId,
transactionsDisplayInfo: {
processingMessage: 'Processing swap and lock',
errorMessage: 'An error has occurred during swap and lock',
successMessage: 'Swap and lock successful'
}
});
setCurrentSessionId(sessionId);
};
const batchTransactionsButtons: BatchTransactionsButtonsType[] = [
{
dataTestId: 'sign-auto-send',
onClickFunction: executeSignAndAutoSendBatchTransactions,
icon: faPaperPlane,
label: 'Sign & send batch'
},
{
dataTestId: 'wrap-multi-transfer',
onClickFunction: executeWrapMultiTransferTransactions,
icon: faPaperPlane,
label: 'Wrap & Multi-Transfer'
},
{
dataTestId: 'swap-lock',
onClickFunction: executeSwapAndLockTokens,
icon: faArrowsRotate,
label: 'Swap & Lock'
}
];
return (
<div id={ItemsIdentifiersEnum.batchTransactions} className={styles.batchTx}>
{pendingBatchTransactions.length > 0 && (
<OutputContainer>
<TransactionsOutput transactions={pendingBatchTransactions} />
</OutputContainer>
)}
<div className={styles.buttonsContainer}>
{batchTransactionsButtons.map((button) => (
<MvxButton
key={button.dataTestId}
data-testid={button.dataTestId}
onClick={button.onClickFunction}
disabled={hasPendingTransactions}
size='small'
>
<FontAwesomeIcon
icon={button.icon}
className={styles.batchTxButton}
/>
<span className={styles.batchTxButton}>{button.label}</span>
</MvxButton>
))}
</div>
</div>
);
};