-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathSupplyActions.tsx
More file actions
321 lines (293 loc) · 11.1 KB
/
Copy pathSupplyActions.tsx
File metadata and controls
321 lines (293 loc) · 11.1 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import {
API_ETH_MOCK_ADDRESS,
gasLimitRecommendations,
ProtocolAction,
} from '@aave/contract-helpers';
import { valueToBigNumber } from '@aave/math-utils';
import { TransactionResponse } from '@ethersproject/providers';
import { Trans } from '@lingui/macro';
import { BoxProps } from '@mui/material';
import { useQueryClient } from '@tanstack/react-query';
import { Contract, PopulatedTransaction } from 'ethers';
import { parseUnits, splitSignature } from 'ethers/lib/utils';
import React, { useEffect, useState } from 'react';
import { useAppDataContext } from 'src/hooks/app-data-provider/useAppDataProvider';
import { SignedParams, useApprovalTx } from 'src/hooks/useApprovalTx';
import { usePoolApprovedAmount } from 'src/hooks/useApprovedAmount';
import { useModalContext } from 'src/hooks/useModal';
import { useWeb3Context } from 'src/libs/hooks/useWeb3Context';
import { useRootStore } from 'src/store/root';
import { ApprovalMethod } from 'src/store/walletSlice';
import { getErrorTextFromError, TxAction } from 'src/ui-config/errorMapping';
import { useShallow } from 'zustand/shallow';
import { TxActionsWrapper } from '../TxActionsWrapper';
import { APPROVAL_GAS_LIMIT, checkRequiresApproval } from '../utils';
import { refetchSupplyPoolData } from './utils';
// Minimal ABI for Pool multicall + setUserEMode + supply
const POOL_MULTICALL_ABI = [
'function multicall(bytes[] calldata data) external returns (bytes[] memory results)',
'function setUserEMode(uint8 categoryId)',
'function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)',
'function supplyWithPermit(address asset, uint256 amount, address onBehalfOf, uint16 referralCode, uint256 deadline, uint8 v, bytes32 r, bytes32 s)',
];
export interface SupplyActionProps extends BoxProps {
amountToSupply: string;
isWrongNetwork: boolean;
customGasPrice?: string;
poolAddress: string;
symbol: string;
blocked: boolean;
decimals: number;
isWrappedBaseAsset: boolean;
setShowUSDTResetWarning?: (showUSDTResetWarning: boolean) => void;
chainId?: number;
selectedEmodeId?: number;
}
export const SupplyActions = React.memo(
({
amountToSupply,
poolAddress,
isWrongNetwork,
sx,
symbol,
blocked,
decimals,
isWrappedBaseAsset,
setShowUSDTResetWarning,
chainId,
selectedEmodeId,
...props
}: SupplyActionProps) => {
const [
tryPermit,
supply,
supplyWithPermit,
setUserEMode,
walletApprovalMethodPreference,
estimateGasLimit,
addTransaction,
currentMarketData,
account,
] = useRootStore(
useShallow((state) => [
state.tryPermit,
state.supply,
state.supplyWithPermit,
state.setUserEMode,
state.walletApprovalMethodPreference,
state.estimateGasLimit,
state.addTransaction,
state.currentMarketData,
state.account,
])
);
const { reserves } = useAppDataContext();
const {
approvalTxState,
mainTxState,
loadingTxns,
setLoadingTxns,
setApprovalTxState,
setMainTxState,
setGasLimit,
setTxError,
} = useModalContext();
const permitAvailable = tryPermit({ reserveAddress: poolAddress, isWrappedBaseAsset });
const { sendTx } = useWeb3Context();
const queryClient = useQueryClient();
const [signatureParams, setSignatureParams] = useState<SignedParams | undefined>();
const {
data: approvedAmount,
refetch: fetchApprovedAmount,
isRefetching: fetchingApprovedAmount,
isFetchedAfterMount,
} = usePoolApprovedAmount(currentMarketData, poolAddress);
setLoadingTxns(fetchingApprovedAmount);
const requiresApproval =
Number(amountToSupply) !== 0 &&
checkRequiresApproval({
approvedAmount: approvedAmount?.amount || '0',
amount: amountToSupply,
signedAmount: signatureParams ? signatureParams.amount : '0',
});
if (requiresApproval && approvalTxState?.success) {
// There was a successful approval tx, but the approval amount is not enough.
// Clear the state to prompt for another approval.
setApprovalTxState({});
}
const usePermit = permitAvailable && walletApprovalMethodPreference === ApprovalMethod.PERMIT;
const { approval, requiresApprovalReset } = useApprovalTx({
usePermit,
approvedAmount,
requiresApproval,
assetAddress: poolAddress,
symbol,
decimals,
signatureAmount: amountToSupply,
onApprovalTxConfirmed: fetchApprovedAmount,
onSignTxCompleted: (signedParams) => setSignatureParams(signedParams),
chainId,
setShowUSDTResetWarning,
});
useEffect(() => {
if (!isFetchedAfterMount) {
fetchApprovedAmount();
}
}, [fetchApprovedAmount, isFetchedAfterMount]);
// Update gas estimation
useEffect(() => {
let supplyGasLimit = 0;
if (usePermit) {
supplyGasLimit = Number(
gasLimitRecommendations[ProtocolAction.supplyWithPermit].recommended
);
} else {
supplyGasLimit = Number(gasLimitRecommendations[ProtocolAction.supply].recommended);
if (requiresApproval && !approvalTxState.success) {
supplyGasLimit += Number(APPROVAL_GAS_LIMIT);
}
}
// Add gas for setUserEMode if bundling e-mode switch
if (selectedEmodeId !== undefined) {
supplyGasLimit += 100000;
}
setGasLimit(supplyGasLimit.toString());
}, [requiresApproval, approvalTxState, usePermit, setGasLimit, selectedEmodeId]);
const action = async () => {
try {
setMainTxState({ ...mainTxState, loading: true });
let response: TransactionResponse;
let action = ProtocolAction.default;
const needsEmodeSwitch = selectedEmodeId !== undefined;
const isNativeAsset = poolAddress.toLowerCase() === API_ETH_MOCK_ADDRESS.toLowerCase();
if (needsEmodeSwitch) {
if (isNativeAsset) {
// Native ETH goes through WETH Gateway which is a separate contract —
// can't bundle with Pool.multicall. Send setUserEMode first, then supply.
const eModeTxs = await setUserEMode(selectedEmodeId);
const eModeTx = await eModeTxs[0].tx();
const eModeTxWithGas = await estimateGasLimit(eModeTx as PopulatedTransaction);
const eModeResponse = await sendTx(eModeTxWithGas);
await eModeResponse.wait(1);
action = ProtocolAction.supply;
let supplyTxData = supply({
amount: parseUnits(amountToSupply, decimals).toString(),
reserve: poolAddress,
});
supplyTxData = await estimateGasLimit(supplyTxData);
response = await sendTx(supplyTxData);
await response.wait(1);
} else {
// ERC20: Bundle setUserEMode + supply via Pool multicall
const poolContractAddress = currentMarketData.addresses.LENDING_POOL;
const poolInterface = new Contract(poolContractAddress, POOL_MULTICALL_ABI).interface;
const currentAccount = useRootStore.getState().account;
const setEModeCalldata = poolInterface.encodeFunctionData('setUserEMode', [
selectedEmodeId,
]);
let supplyCalldata: string;
if (usePermit && signatureParams) {
action = ProtocolAction.supplyWithPermit;
const sig = splitSignature(signatureParams.signature);
supplyCalldata = poolInterface.encodeFunctionData('supplyWithPermit', [
poolAddress,
parseUnits(amountToSupply, decimals).toString(),
currentAccount,
0,
signatureParams.deadline,
sig.v,
sig.r,
sig.s,
]);
} else {
action = ProtocolAction.supply;
supplyCalldata = poolInterface.encodeFunctionData('supply', [
poolAddress,
parseUnits(amountToSupply, decimals).toString(),
currentAccount,
0,
]);
}
let multicallTxData = await new Contract(
poolContractAddress,
POOL_MULTICALL_ABI
).populateTransaction.multicall([setEModeCalldata, supplyCalldata]);
multicallTxData = { ...multicallTxData, from: currentAccount };
multicallTxData = await estimateGasLimit(multicallTxData);
response = await sendTx(multicallTxData);
await response.wait(1);
}
} else if (usePermit && signatureParams) {
// Standard supply with permit (no e-mode switch)
action = ProtocolAction.supplyWithPermit;
let signedSupplyWithPermitTxData = supplyWithPermit({
signature: signatureParams.signature,
amount: parseUnits(amountToSupply, decimals).toString(),
reserve: poolAddress,
deadline: signatureParams.deadline,
});
signedSupplyWithPermitTxData = await estimateGasLimit(signedSupplyWithPermitTxData);
response = await sendTx(signedSupplyWithPermitTxData);
await response.wait(1);
} else {
// Standard supply (no e-mode switch)
action = ProtocolAction.supply;
let supplyTxData = supply({
amount: parseUnits(amountToSupply, decimals).toString(),
reserve: poolAddress,
});
supplyTxData = await estimateGasLimit(supplyTxData);
response = await sendTx(supplyTxData);
await response.wait(1);
}
addTransaction(response.hash, {
action,
txState: 'success',
asset: poolAddress,
amount: amountToSupply,
assetName: symbol,
amountUsd: (() => {
const reserve = reserves.find((r) => r.underlyingAsset === poolAddress);
return reserve
? valueToBigNumber(amountToSupply).multipliedBy(reserve.priceInUSD).toString()
: undefined;
})(),
});
await refetchSupplyPoolData(queryClient, account, currentMarketData);
setMainTxState({
txHash: response.hash,
loading: false,
success: true,
});
} catch (error) {
const parsedError = getErrorTextFromError(error, TxAction.GAS_ESTIMATION, false);
setTxError(parsedError);
setMainTxState({
txHash: undefined,
loading: false,
});
}
};
return (
<TxActionsWrapper
blocked={blocked}
mainTxState={mainTxState}
approvalTxState={approvalTxState}
isWrongNetwork={isWrongNetwork}
requiresAmount
amount={amountToSupply}
symbol={symbol}
preparingTransactions={loadingTxns || !approvedAmount}
actionText={<Trans>Supply {symbol}</Trans>}
actionInProgressText={<Trans>Supplying {symbol}</Trans>}
handleApproval={approval}
handleAction={action}
requiresApproval={requiresApproval}
tryPermit={permitAvailable}
sx={sx}
{...props}
requiresApprovalReset={requiresApprovalReset}
/>
);
}
);