-
Notifications
You must be signed in to change notification settings - Fork 4
/
BorrowActionsPage.tsx
571 lines (532 loc) · 20.6 KB
/
BorrowActionsPage.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import { TickMath } from '@uniswap/v3-sdk';
import Big from 'big.js';
import JSBI from 'jsbi';
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import styled from 'styled-components';
import tw from 'twin.macro';
import { chain, useContract, useContractRead, useProvider } from 'wagmi';
import MarginAccountABI from '../assets/abis/MarginAccount.json';
import MarginAccountLensABI from '../assets/abis/MarginAccountLens.json';
import UniswapV3PoolABI from '../assets/abis/UniswapV3Pool.json';
import { ReactComponent as PieChartIcon } from '../assets/svg/pie_chart.svg';
import { ReactComponent as TrendingUpIcon } from '../assets/svg/trending_up.svg';
import { AccountStatsCard } from '../components/borrow/AccountStatsCard';
import BorrowSelectActionModal from '../components/borrow/BorrowSelectActionModal';
import { HypotheticalToggleButton } from '../components/borrow/HypotheticalToggleButton';
import ManageAccountWidget from '../components/borrow/ManageAccountWidget';
import MarginAccountHeader from '../components/borrow/MarginAccountHeader';
import TokenAllocationPieChartWidget from '../components/borrow/TokenAllocationPieChartWidget';
import AppPage from 'shared/lib/components/common/AppPage';
import { PreviousPageButton } from '../components/common/Buttons';
import TokenChooser from '../components/common/TokenChooser';
import { Display } from 'shared/lib/components/common/Typography';
import PnLGraph from '../components/graph/PnLGraph';
import {
Action,
ActionCardState,
calculateHypotheticalStates,
UniswapPosition,
UniswapPositionPrior,
} from '../data/Actions';
import { RESPONSIVE_BREAKPOINT_MD, RESPONSIVE_BREAKPOINT_XS } from '../data/constants/Breakpoints';
import { useDebouncedEffect } from '../data/hooks/UseDebouncedEffect';
import {
Assets,
computeLiquidationThresholds,
fetchMarginAccount,
Liabilities,
LiquidationThresholds,
MarginAccount,
sumAssetsPerToken,
} from '../data/MarginAccount';
import { formatPriceRatio, formatTokenAmount } from '../util/Numbers';
import { getAmountsForLiquidity, uniswapPositionKey } from '../util/Uniswap';
export const GENERAL_DEBOUNCE_DELAY_MS = 250;
const SECONDARY_COLOR = 'rgba(130, 160, 182, 1)';
const BodyWrapper = styled.div`
display: grid;
width: 100%;
grid-template-columns: calc(100% - 582px) 550px;
gap: 32px;
@media (max-width: ${RESPONSIVE_BREAKPOINT_MD}) {
grid-template-columns: 1fr;
}
`;
const GridExpandingDiv = styled.div`
grid-row: 1 / 4;
grid-column: 2 / span 1;
justify-self: center;
@media (max-width: ${RESPONSIVE_BREAKPOINT_MD}) {
justify-self: start;
grid-row: 2 / span 1;
grid-column: 1 / span 1;
}
`;
const EmptyStateWrapper = styled.div`
${tw`w-full`}
background-color: rgba(13, 24, 33, 1);
border-radius: 4px;
`;
const EmptyStateContainer = styled.div`
${tw`flex flex-col items-center text-center gap-2`}
width: 300px;
padding: 24px;
margin: 0 auto;
`;
const EmptyStateSvgWrapper = styled.div`
${tw`flex items-center justify-center`}
width: 32px;
height: 32px;
svg {
width: 32px;
height: 32px;
path {
/* stroke: rgb(255, 255, 255); */
stroke: ${SECONDARY_COLOR};
}
}
`;
const AccountStatsGrid = styled.div`
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
@media (max-width: ${RESPONSIVE_BREAKPOINT_XS}) {
grid-template-columns: 1fr;
}
`;
type AccountParams = {
account: string;
};
async function fetchUniswapPositions(
priors: UniswapPositionPrior[],
marginAccountAddress: string,
uniswapV3PoolContract: any,
sqrtPriceX96: Big,
token0Decimals: number,
token1Decimals: number
) {
const keys = priors.map((prior) => uniswapPositionKey(marginAccountAddress, prior.lower!, prior.upper!));
const results = await Promise.all(keys.map((key) => uniswapV3PoolContract.positions(key)));
const fetchedUniswapPositions = new Map<string, UniswapPosition>();
priors.forEach((prior, i) => {
const liquidity = JSBI.BigInt(results[i].liquidity.toString());
const amounts = getAmountsForLiquidity(
liquidity,
prior.lower!,
prior.upper!,
TickMath.getTickAtSqrtRatio(JSBI.BigInt(sqrtPriceX96.toFixed(0))),
token0Decimals,
token1Decimals
);
fetchedUniswapPositions.set(keys[i], {
...prior,
liquidity: liquidity,
amount0: amounts[0],
amount1: amounts[1],
});
});
return fetchedUniswapPositions;
}
export default function BorrowActionsPage() {
const navigate = useNavigate();
const params = useParams<AccountParams>();
const accountAddressParam = params.account;
// MARK: component state
const [isShowingHypothetical, setIsShowingHypothetical] = useState<boolean>(false);
const [marginAccount, setMarginAccount] = useState<MarginAccount | null>(null);
const [uniswapPositions, setUniswapPositions] = useState(new Map<string, UniswapPosition>());
const [actionResults, setActionResults] = useState<ActionCardState[]>([]);
const [activeActions, setActiveActions] = useState<Action[]>([]);
const [actionModalOpen, setActionModalOpen] = useState(false);
const [isToken0Selected, setIsToken0Selected] = useState(true);
// --> state that could be computed in-line, but we use React so that we can debounce liquidation threshold calcs
const [hypotheticalStates, setHypotheticalStates] = useState<
{
assets: Assets;
liabilities: Liabilities;
positions: Map<string, UniswapPosition>;
}[]
>([]);
const [displayedMarginAccount, setDisplayedMarginAccount] = useState<MarginAccount | null>(null);
const [displayedUniswapPositions, setDisplayedUniswapPositions] = useState<UniswapPosition[]>([]);
const [liquidationThresholds, setLiquidationThresholds] = useState<LiquidationThresholds | null>(null);
const [borrowInterestInputValue, setBorrowInterestInputValue] = useState<string>('');
const [swapFeesInputValue, setSwapFeesInputValue] = useState<string>('');
// MARK: wagmi hooks
const provider = useProvider({ chainId: chain.goerli.id });
const marginAccountContract = useContract({
addressOrName: accountAddressParam ?? '', // TODO better optional resolution
contractInterface: MarginAccountABI,
signerOrProvider: provider,
});
const marginAccountLensContract = useContract({
addressOrName: '0xFc9A50F2dD9348B5a9b00A21B09D9988bd9726F7',
contractInterface: MarginAccountLensABI,
signerOrProvider: provider,
});
const { data: uniswapPositionPriors } = useContractRead({
addressOrName: accountAddressParam ?? '', // TODO better optional resolution
contractInterface: MarginAccountABI,
functionName: 'getUniswapPositions',
});
const uniswapV3PoolContract = useContract({
addressOrName: marginAccount?.uniswapPool ?? '', // TODO better option resolution
contractInterface: UniswapV3PoolABI,
signerOrProvider: provider,
});
useEffect(() => {
setBorrowInterestInputValue('');
setSwapFeesInputValue('');
}, [isToken0Selected]);
// MARK: fetch margin account
useEffect(() => {
let mounted = true;
async function fetch(marginAccountAddress: string) {
const fetchedMarginAccount = await fetchMarginAccount(
marginAccountContract,
marginAccountLensContract,
provider,
marginAccountAddress
);
if (mounted) {
setMarginAccount(fetchedMarginAccount);
}
}
if (accountAddressParam) {
fetch(accountAddressParam);
}
return () => {
mounted = false;
};
}, [accountAddressParam, marginAccountContract, marginAccountLensContract, provider]);
// MARK: fetch uniswap positions
useEffect(() => {
let mounted = true;
async function fetch() {
if (!marginAccount || !accountAddressParam || !Array.isArray(uniswapPositionPriors)) return;
const fetchedUniswapPositions = await fetchUniswapPositions(
uniswapPositionPriors as UniswapPositionPrior[],
accountAddressParam,
uniswapV3PoolContract,
marginAccount.sqrtPriceX96,
marginAccount.token0.decimals,
marginAccount.token1.decimals
);
if (mounted) {
setUniswapPositions(fetchedUniswapPositions);
// there may be a slight discrepancy between Sum{uniswapPositions.amountX} and marginAccount.assets.uniX.
// this is because one is computed on-chain and cached, while the other is computed locally.
// if we've fetched both, prefer the uniswapPositions version (local & newer).
const i = { amount0: 0, amount1: 0 };
const { amount0, amount1 } = Array.from(fetchedUniswapPositions.values()).reduce((p, c) => {
return {
amount0: p.amount0 + (c.amount0 || 0),
amount1: p.amount1 + (c.amount1 || 0),
};
}, i);
setMarginAccount({
...marginAccount,
assets: { ...marginAccount.assets, uni0: amount0, uni1: amount1 },
});
}
}
fetch();
return () => {
mounted = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
accountAddressParam,
marginAccount?.sqrtPriceX96,
marginAccount?.token0,
marginAccount?.token1,
uniswapPositionPriors,
uniswapV3PoolContract,
]);
// MARK: compute hypothetical states
useEffect(() => {
if (!marginAccount) return;
// assets and liabilities after adding hypothetical actions
const _hypotheticalStates = calculateHypotheticalStates(marginAccount, uniswapPositions, actionResults);
// check whether actions seem valid on the frontend (estimating whether transaction will succeed/fail)
const numValidActions = _hypotheticalStates.length - 1;
// get the latest *valid* hypothetical state. this will be the one we display
const {
assets: assetsF,
liabilities: liabilitiesF,
positions: uniswapPositionsF,
} = _hypotheticalStates[numValidActions];
// tell React about our findings
const _marginAccount = { ...marginAccount };
if (isShowingHypothetical) {
const numericBorrowInterest = parseFloat(borrowInterestInputValue) || 0.0;
const numericSwapFees = parseFloat(swapFeesInputValue) || 0.0;
// Apply the user's inputted swap fees to the displayed margin account's assets
_marginAccount.assets = {
...assetsF,
token0Raw: assetsF.token0Raw + (isToken0Selected ? numericSwapFees : 0),
token1Raw: assetsF.token1Raw + (isToken0Selected ? 0 : numericSwapFees),
};
// Apply the user's inputted borrow interest to the displayed margin account's liabilities
_marginAccount.liabilities = {
...liabilitiesF,
amount0: liabilitiesF.amount0 - (isToken0Selected ? numericBorrowInterest : 0),
amount1: liabilitiesF.amount1 - (isToken0Selected ? 0 : numericBorrowInterest),
};
}
setHypotheticalStates(_hypotheticalStates);
setDisplayedMarginAccount(_marginAccount);
setDisplayedUniswapPositions(Array.from((isShowingHypothetical ? uniswapPositionsF : uniswapPositions).values()));
console.log('Running 1');
}, [
marginAccount,
uniswapPositions,
actionResults,
isShowingHypothetical,
isToken0Selected,
borrowInterestInputValue,
swapFeesInputValue,
]);
// compute liquidation thresholds for the currently-selected data (current or hypothetical)
useDebouncedEffect(
() => {
if (!displayedMarginAccount) return;
console.log('Running 2');
const lt: LiquidationThresholds = computeLiquidationThresholds(
displayedMarginAccount,
displayedUniswapPositions,
0.025,
120,
6
);
setLiquidationThresholds(lt);
},
GENERAL_DEBOUNCE_DELAY_MS,
[displayedMarginAccount, displayedUniswapPositions]
);
// if no account data is found, don't render the page
if (!marginAccount || !displayedMarginAccount) {
return null;
}
// flip liquidation thresholds if necessary
let displayedLiquidationThresholds = liquidationThresholds;
if (displayedLiquidationThresholds && isToken0Selected) {
displayedLiquidationThresholds = {
lower: 1.0 / displayedLiquidationThresholds.upper,
upper: 1.0 / displayedLiquidationThresholds.lower,
};
}
// check whether actions seem valid on the frontend (estimating whether transaction will succeed/fail)
const numValidActions = Math.min(hypotheticalStates.length - 1);
const problematicActionIdx = numValidActions < actionResults.length ? numValidActions : -1;
// check whether we're prepared to send a transaction (independent of whether transaction will succeed/fail)
const transactionIsReady = actionResults.findIndex((result) => result.actionArgs === undefined) === -1;
// pre-compute some values to cut down on logic in the HTML
const token0 = marginAccount.token0;
const token1 = marginAccount.token1;
const [selectedToken, unselectedToken] = isToken0Selected ? [token0, token1] : [token1, token0];
const [assetsSum0, assetsSum1] = sumAssetsPerToken(displayedMarginAccount.assets);
const isActiveAssetsEmpty = Object.values(displayedMarginAccount.assets).every((a) => a === 0);
const isActiveLiabilitiesEmpty = Object.values(displayedMarginAccount.liabilities).every((l) => l === 0);
function updateActionResults(updatedActionResults: ActionCardState[]) {
setActionResults(updatedActionResults);
}
function handleAddAction(action: Action) {
if (actionResults.length === 0) setIsShowingHypothetical(true);
updateActionResults([
...actionResults,
{
actionId: action.id,
aloeResult: null,
uniswapResult: null,
},
]);
setActiveActions([...activeActions, action]);
}
function handleAddActions(actions: Action[], defaultActionResults?: ActionCardState[]) {
if (actionResults.length === 0) setIsShowingHypothetical(true);
if (defaultActionResults && actions.length !== defaultActionResults.length) {
console.error(
'You must pass in the same number of action results as you do actions (or pass no action results in).'
);
return;
}
const newActionResults =
defaultActionResults ||
actions.map((x) => {
return {
actionId: x.id,
aloeResult: null,
uniswapResult: null,
};
});
updateActionResults([...actionResults, ...newActionResults]);
setActiveActions([...activeActions, ...actions]);
}
function clearActions() {
updateActionResults([]);
setActiveActions([]);
setIsShowingHypothetical(false);
}
return (
<AppPage>
<BodyWrapper>
<div className='flex gap-8 items-center mb-4'>
<PreviousPageButton onClick={() => navigate('../borrow')} />
<MarginAccountHeader
token0={token0}
token1={token1}
feeTier={marginAccount.feeTier}
id={accountAddressParam || ''}
/>
</div>
<GridExpandingDiv>
<ManageAccountWidget
marginAccount={marginAccount}
hypotheticalStates={hypotheticalStates}
uniswapPositions={displayedUniswapPositions}
activeActions={activeActions}
actionResults={actionResults}
updateActionResults={updateActionResults}
onAddAction={() => {
setActionModalOpen(true);
}}
onRemoveAction={(index: number) => {
let actionResultsCopy = [...actionResults];
const updatedActionResults = actionResultsCopy.filter((_, i) => i !== index);
setActionResults(updatedActionResults);
let activeActionsCopy = [...activeActions];
setActiveActions(activeActionsCopy.filter((_, i) => i !== index));
}}
problematicActionIdx={problematicActionIdx}
transactionIsViable={transactionIsReady}
clearActions={clearActions}
/>
</GridExpandingDiv>
<div className='w-full flex flex-col justify-between'>
<div className='w-full flex flex-col gap-4 mb-8'>
<div className='flex gap-4 items-center'>
<Display size='M' weight='medium'>
Summary
</Display>
<TokenChooser
token0={token0}
token1={token1}
isToken0Selected={isToken0Selected}
setIsToken0Selected={setIsToken0Selected}
/>
<div className='ml-auto'>
{actionResults.length > 0 && (
<HypotheticalToggleButton
showHypothetical={isShowingHypothetical}
setShowHypothetical={setIsShowingHypothetical}
/>
)}
</div>
</div>
<AccountStatsGrid>
<AccountStatsCard
label='Assets'
valueLine1={`${formatTokenAmount(assetsSum0, 5)} ${token0.ticker || ''}`}
valueLine2={`${formatTokenAmount(assetsSum1, 5)} ${token1.ticker || ''}`}
showAsterisk={isShowingHypothetical}
/>
<AccountStatsCard
label='Liabilities'
valueLine1={`${formatTokenAmount(displayedMarginAccount.liabilities.amount0, 5)} ${
token0.ticker || ''
}`}
valueLine2={`${formatTokenAmount(displayedMarginAccount.liabilities.amount1, 5)} ${
token1.ticker || ''
}`}
showAsterisk={isShowingHypothetical}
/>
<AccountStatsCard
label='Lower Liquidation Threshold'
valueLine1={
displayedLiquidationThresholds
? `${formatPriceRatio(displayedLiquidationThresholds.lower, 5)} ${selectedToken?.ticker || ''}/${
unselectedToken?.ticker || ''
}`
: '-'
}
showAsterisk={isShowingHypothetical}
/>
<AccountStatsCard
label='Upper Liquidation Threshold'
valueLine1={
displayedLiquidationThresholds
? `${formatPriceRatio(displayedLiquidationThresholds.upper, 5)} ${selectedToken?.ticker || ''}/${
unselectedToken?.ticker || ''
}`
: '-'
}
showAsterisk={isShowingHypothetical}
/>
</AccountStatsGrid>
</div>
<div className='w-full flex flex-col gap-4 mb-8'>
<Display size='M' weight='medium'>
P&L
</Display>
{!isActiveAssetsEmpty || !isActiveLiabilitiesEmpty ? (
<PnLGraph
marginAccount={displayedMarginAccount}
uniswapPositions={displayedUniswapPositions}
inTermsOfToken0={isToken0Selected}
liquidationThresholds={displayedLiquidationThresholds}
isShowingHypothetical={isShowingHypothetical}
borrowInterestInputValue={borrowInterestInputValue}
swapFeesInputValue={swapFeesInputValue}
setBorrowInterestInputValue={setBorrowInterestInputValue}
setSwapFeesInputValue={setSwapFeesInputValue}
/>
) : (
<EmptyStateWrapper>
<EmptyStateContainer>
<EmptyStateSvgWrapper>
<TrendingUpIcon />
</EmptyStateSvgWrapper>
<Display size='XS' color={SECONDARY_COLOR}>
A P&L graph of your open positions will appear here.
</Display>
</EmptyStateContainer>
</EmptyStateWrapper>
)}
</div>
<div className='w-full flex flex-col gap-4'>
<Display size='M' weight='medium'>
Token Allocation
</Display>
{!isActiveAssetsEmpty ? (
<TokenAllocationPieChartWidget
token0={token0}
token1={token1}
assets={displayedMarginAccount.assets}
sqrtPriceX96={marginAccount.sqrtPriceX96}
/>
) : (
<EmptyStateWrapper>
<EmptyStateContainer>
<EmptyStateSvgWrapper>
<PieChartIcon />
</EmptyStateSvgWrapper>
<Display size='XS' color={SECONDARY_COLOR}>
A breakdown of your token allocation will appear here.
</Display>
</EmptyStateContainer>
</EmptyStateWrapper>
)}
</div>
</div>
</BodyWrapper>
<BorrowSelectActionModal
isOpen={actionModalOpen}
setIsOpen={setActionModalOpen}
handleAddAction={handleAddAction}
handleAddActions={handleAddActions}
/>
</AppPage>
);
}