-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathuseCollateralSwap.tsx
More file actions
151 lines (138 loc) · 3.89 KB
/
useCollateralSwap.tsx
File metadata and controls
151 lines (138 loc) · 3.89 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
import { normalize } from '@aave/math-utils';
import { OptimalRate } from '@paraswap/sdk';
import { useCallback, useEffect, useMemo, useState } from 'react';
import {
convertParaswapErrorMessage,
fetchExactInRate,
fetchExactInTxParams,
SwapData,
SwapTransactionParams,
UseSwapProps,
} from './common';
interface UseSwapResponse {
outputAmount: string;
outputAmountUSD: string;
inputAmount: string;
inputAmountUSD: string;
loading: boolean;
error: string;
buildTxFn: () => Promise<SwapTransactionParams>;
}
export const useCollateralSwap = ({
chainId,
max,
maxSlippage,
swapIn,
swapOut,
userAddress,
skip,
}: UseSwapProps): UseSwapResponse => {
const [inputAmount, setInputAmount] = useState<string>('0');
const [inputAmountUSD, setInputAmountUSD] = useState<string>('0');
const [outputAmount, setOutputAmount] = useState<string>('0');
const [outputAmountUSD, setOutputAmountUSD] = useState<string>('0');
const [route, setRoute] = useState<OptimalRate>();
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const swapInData = useMemo(() => {
const swapData: SwapData = {
underlyingAsset: swapIn.underlyingAsset,
decimals: swapIn.decimals,
supplyAPY: swapIn.supplyAPY,
amount: swapIn.amount,
variableBorrowAPY: swapIn.variableBorrowAPY,
};
return swapData;
}, [
swapIn.amount,
swapIn.decimals,
swapIn.supplyAPY,
swapIn.underlyingAsset,
swapIn.variableBorrowAPY,
]);
const swapOutData = useMemo(() => {
const swapData: SwapData = {
underlyingAsset: swapOut.underlyingAsset,
decimals: swapOut.decimals,
supplyAPY: swapOut.supplyAPY,
amount: swapOut.amount,
variableBorrowAPY: swapOut.variableBorrowAPY,
};
return swapData;
}, [
swapOut.amount,
swapOut.decimals,
swapOut.supplyAPY,
swapOut.underlyingAsset,
swapOut.variableBorrowAPY,
]);
const exactInRate = useCallback(() => {
return fetchExactInRate(swapInData, swapOutData, chainId, userAddress, max);
}, [chainId, swapInData, swapOutData, userAddress, max]);
useEffect(() => {
if (skip) return;
const fetchRoute = async () => {
if (
!swapInData.underlyingAsset ||
!swapOutData.underlyingAsset ||
!swapInData.amount ||
swapInData.amount === '0' ||
isNaN(+swapInData.amount)
) {
setInputAmount('0');
setOutputAmount('0');
setOutputAmountUSD('0');
setInputAmountUSD('0');
setRoute(undefined);
return;
}
setLoading(true);
try {
const route = await exactInRate();
setError('');
setRoute(route);
setInputAmount(normalize(route.srcAmount, route.srcDecimals));
setInputAmountUSD(route.srcUSD);
setOutputAmount(normalize(route.destAmount, route.destDecimals));
setOutputAmountUSD(route.destUSD);
} catch (e) {
console.error(e);
const message =
convertParaswapErrorMessage(e.message) || 'There was an issue fetching data from Velora';
setError(message);
} finally {
setLoading(false);
}
};
// Update the transaction on any dependency change
const timeout = setTimeout(() => {
fetchRoute();
}, 400);
return () => {
clearTimeout(timeout);
};
}, [
error,
skip,
swapInData.underlyingAsset,
swapOutData.underlyingAsset,
exactInRate,
maxSlippage,
swapInData.amount,
userAddress,
max,
]);
return {
outputAmount,
outputAmountUSD,
inputAmount,
inputAmountUSD,
loading,
error,
// Used for calling paraswap buildTx as very last step in transaction
buildTxFn: async () => {
if (!route) throw new Error('Route required to build transaction');
return fetchExactInTxParams(route, swapIn, swapOut, chainId, userAddress, maxSlippage);
},
};
};