forked from SoulSwapFinance/soul-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
482 lines (458 loc) · 23.9 KB
/
index.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
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import Card from 'components/Card'
import Dots from 'components/Dots'
import GradientDot from 'components/GradientDot'
import Image from 'components/Image'
import { Feature } from 'enums/Feature'
import ListHeaderWithSort from 'features/lending/components/ListHeaderWithSort'
import { useUnderworldPair, useUnderworldPairAddresses, useUnderworldPairs } from 'features/lending/hooks'
import { formatNumber, formatPercent } from 'functions/format'
import NetworkGuard from 'guards/Network'
import { useInfiniteScroll } from 'hooks/useInfiniteScroll'
import useSearchAndSort from 'hooks/useSearchAndSort'
import Layout from 'layouts/Underworld'
import Head from 'next/head'
import Link from 'next/link'
import React from 'react'
import InfiniteScroll from 'react-infinite-scroll-component'
import { RecoilRoot } from 'recoil'
import MarketHeader from 'features/lending/components/MarketHeader'
import { useUnderworldBorrowPositions } from 'features/portfolio/AssetBalances/underworld/hooks'
import { e10 } from 'functions/math'
import router from 'next/router'
import { useActiveWeb3React } from 'services/web3'
import { ChainId, ACTIVE_UNDERWORLD_PAIRS } from 'sdk'
import { useUnderworldPairInfo, useUnderworldUserInfo } from 'hooks/useAPI'
import { formatAddress } from 'utils/wallet'
// const BORROW_IMG = "https://media.giphy.com/media/GgyKe2YYi3UR8HltC6/giphy.gif"
export default function Borrow() {
const { i18n } = useLingui()
const { chainId } = useActiveWeb3React()
// const addresses = useUnderworldPairAddresses()
const addresses = ACTIVE_UNDERWORLD_PAIRS[chainId]
const pairs = useUnderworldPairs(addresses)
const positions = useUnderworldBorrowPositions(pairs)
const data = useSearchAndSort(
pairs,
{ keys: ['search'], threshold: 0.1 },
{ key: 'totalAssetAmount.usdValue', direction: 'descending' }
)
const [numDisplayed, setNumDisplayed] = useInfiniteScroll(data.items)
return (
<BorrowLayout>
<Head>
<title>{i18n._(t`Borrow`)} | Soul</title>
<meta
key="description"
name="description"
content="Underworld is a lending and margin trading platform, built upon CoffinBox, which allows for anyone to create customized and gas-efficient markets for lending, borrowing, and collateralizing a variety of DeFi tokens, stable coins, and synthetic assets."
/>
<meta
key="twitter:description"
name="twitter:description"
content="Underworld is a lending and margin trading platform, built upon CoffinBox, which allows for anyone to create customized and gas-efficient markets for lending, borrowing, and collateralizing a variety of DeFi tokens, stable coins, and synthetic assets."
/>
<meta
key="og:description"
property="og:description"
content="Underworld is a lending and margin trading platform, built upon CoffinBox, which allows for anyone to create customized and gas-efficient markets for lending, borrowing, and collateralizing a variety of DeFi tokens, stable coins, and synthetic assets."
/>
</Head>
<Card className="h-full bg-dark-900" header={<MarketHeader type="Borrow" lists={[pairs, positions]} />}>
{positions.items && positions.items.length > 0 && (
<div className="pb-4">
<div>
<div className="grid grid-cols-4 gap-2 px-2 pb-4 text-sm md:grid-cols-5 lg:grid-cols-5 text-secondary">
<ListHeaderWithSort className="justify-center" sort={data} sortKey="search">
{i18n._(t`Positions`)}
</ListHeaderWithSort>
<ListHeaderWithSort
className="justify-center"
sort={positions}
sortKey="currentUserBorrowAmount.usdValue"
direction="descending"
>
{i18n._(t`Borrowed`)}
</ListHeaderWithSort>
<ListHeaderWithSort
className="justify-center"
sort={positions}
sortKey="userCollateralAmount.usdValue"
direction="descending"
>
{i18n._(t`Collateral`)}
</ListHeaderWithSort>
{/* <ListHeaderWithSort
className="justify-center lg:flex"
sort={positions}
sortKey="health.value"
direction="descending"
>
<>
{i18n._(t`Limit`)} <span className="hidden md:inline-block">{i18n._(t` Used`)}</span>
</>
</ListHeaderWithSort> */}
<ListHeaderWithSort
className="justify-center"
sort={positions}
sortKey="interestPerYear.value"
direction="descending"
>
{i18n._(t`Utilized`)}
</ListHeaderWithSort>
<ListHeaderWithSort
className="hidden md:flex md:justify-center"
sort={positions}
sortKey="interestPerYear.value"
direction="descending"
>
{i18n._(t`APR`)}
</ListHeaderWithSort>
</div>
<div className="flex-col space-y-2">
{positions.items.map((pair: any) => {
const userCollateralBalance = pair?.userCollateralShare // √
// const userBorrowBalance = Number(pair?.currentUserBorrowAmount.string / 1e18) // √
const collateralPrice = pair?.collateral.usd / (10**pair.collateral.tokenInfo.decimals) * 1e12
const userCollateralValue
= userCollateralBalance
* collateralPrice
/ 10**pair.collateral.tokenInfo.decimals
// const userBorrowValue = userBorrowBalance * borrowPrice
// const userCollateralBalance = Number(pair?.userCollateralShare / 1e18) // √
const userBorrowBalance = Number(pair?.currentUserBorrowAmount.string / 1e18) // √
const borrowPrice = pair?.asset.usd
// const userCollateralValue = userCollateralBalance * collateralPrice
const userBorrowValue = userBorrowBalance * borrowPrice
const pairUtilization = userBorrowValue * 10**(pair?.collateral.tokenInfo.decimals) / Number(userCollateralValue) * 100
const pairHealth = pairUtilization / 1e6
const assetAddress = pair?.asset.tokenInfo.address
const collateralAddress = pair?.collateral.tokenInfo.address
const blockchain = chainId == ChainId.FANTOM ? 'fantom' : 'avalanche'
const assetLogoURI = `https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${assetAddress}/logo.png`
const collateralLogoURI = `https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${collateralAddress}/logo.png`
return (
<div key={pair.address}>
<Link href={'/borrow/' + pair.address}>
<a className="block text-high-emphesis">
<div className="grid items-center grid-cols-4 gap-2 px-2 py-4 text-sm md:grid-cols-5 lg:grid-cols-5 align-center bg-dark-800 hover:bg-dark-purple">
<div className="flex justify-center flex-col md:flex-row items-center">
<div className="hidden justify-center text-center items-center space-x-2 sm:flex">
<Image
height={48}
width={48}
src={assetLogoURI}
className="w-6 h-6 rounded-lg md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.asset.tokenInfo.symbol}
/>
<Image
height={48}
width={48}
src={collateralLogoURI}
className="w-6 h-6 rounded-lg md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.collateral.tokenInfo.symbol}
/>
</div>
<div className="hidden sm:items-center md:hidden">
<div>
<strong>{pair.asset.tokenInfo.symbol}</strong> / {pair.collateral.tokenInfo.symbol}
</div>
<div className="block mt-0 text-xs text-left text-white-500 hidden">{pair.oracle.name}</div>
</div>
<div className="text-center justify-center items-center sm:hidden">
<div className="grid items-center grid-cols-2">
{/* <strong>{pair.collateral.tokenInfo.symbol}</strong> */}
<Image
height={36}
width={36}
src={assetLogoURI}
className="w-2 h-2 p-2 rounded-lg md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.asset.tokenInfo.symbol}
/>
<Image
height={36}
width={36}
src={collateralLogoURI}
className="w-2 h-2 p-2 rounded-sm md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.collateral.tokenInfo.symbol}
/>
</div>
<div className="block mt-0 text-xs text-left text-white-500 hidden">{pair.oracle.name}</div>
</div>
</div>
<div className="hidden text-white">
<div>
<strong>{pair.asset.tokenInfo.symbol}</strong> / {pair.collateral.tokenInfo.symbol}
</div>
<div>{pair.oracle.name}</div>
</div>
<div className="text-center">
<div>
{formatNumber(pair.currentUserBorrowAmount.string, false)} {pair.asset.tokenInfo.symbol}
</div>
<div className="text-sm text-secondary">
{formatNumber(pair.currentUserBorrowAmount.usd / 1e12, true)}
</div>
</div>
<div className="text-center md:block">
<div>
{formatNumber(Number(pair?.userCollateralShare / 1e18), false)} {pair.collateral.tokenInfo.symbol}
</div>
<div className="text-sm text-secondary">
{formatNumber(
userCollateralValue / 1e12,
true
)}
</div>
{/* <div className="text-center text-sm text-secondary"> */}
{/* {formatNumber(Number(pair?.userCollateralShare) * Number(pair?.collateralPrice / 1e18), true)} */}
{/* {formatNumber(pair?.userCollateralShare / 1e18 * (Number(usePrice(pair?.collateral.address))), true) } */}
{/* </div> */}
</div>
<div className="flex items-center justify-center">
{formatPercent(pairHealth)}
<GradientDot percent={pairHealth} />
</div>
{/* <div className="items-center text-center flex justify-center text-md sm:text-lg text-high-emphesis"> */}
{/* <div className="text-center">{
formatPercent(
pair?.currentUserBorrowAmount.usd
/ userCollateralValue /// userCollateralValue
* 100
)}</div> */}
<div className="hidden text-center md:block">{formatPercent(pair.interestPerYear.string)}</div>
</div>
</a>
</Link>
</div>
)
})}
</div>
</div>
</div>
)}
<div className="grid grid-flow-col grid-cols-4 gap-4 px-2 pb-4 text-sm md:grid-cols-5 lg:grid-cols-5 text-secondary">
<ListHeaderWithSort className="justify-center" sort={data} sortKey="search">
{i18n._(t`Markets`)}
</ListHeaderWithSort>
{/* <ListHeaderWithSort className="hidden justify-center md:flex" sort={data} sortKey="asset.tokenInfo.symbol">
{i18n._(t`Borrow`)}
</ListHeaderWithSort> */}
<ListHeaderWithSort className="hidden justify-center md:flex" sort={data} sortKey="collateral.tokenInfo.symbol">
{i18n._(t`Collateral`)}
</ListHeaderWithSort>
{/* <ListHeaderWithSort className="hidden justify-center lg:flex" sort={data} sortKey="oracle.name">
{i18n._(t`Oracle`)}
</ListHeaderWithSort> */}
<ListHeaderWithSort
className="justify-center"
sort={data}
sortKey="currentBorrowAmount.usdValue"
direction="descending"
>
{i18n._(t`Borrowed`)}
</ListHeaderWithSort>
<ListHeaderWithSort
className="justify-center"
sort={data}
sortKey="totalAssetAmount.usdValue"
direction="descending"
>
{i18n._(t`Available`)}
</ListHeaderWithSort>
<ListHeaderWithSort
className="justify-center"
sort={data}
sortKey="currentInterestPerYear.value"
direction="descending"
>
{i18n._(t`APR`)}
</ListHeaderWithSort>
</div>
<InfiniteScroll
dataLength={numDisplayed}
next={() => setNumDisplayed(numDisplayed + 5)}
hasMore={true}
loader={
<div className="mt-8 text-center">
<Dots>Loading</Dots>
</div>
}
>
<BorrowRow data={data} numDisplayed={numDisplayed} />
</InfiniteScroll>
</Card>
</BorrowLayout>
)
}
const BorrowRow = ({ data, numDisplayed }) => {
const { chainId } = useActiveWeb3React()
const blockchain = chainId == ChainId.AVALANCHE ? 'avalanche' : 'fantom'
// const { underworldUserInfo } = useUnderworldUserInfo(pair.address)
// const collateralDecimals = Number(underworldPairInfo.collateralDecimals)
// // const assetBalance = Number(underworldUserInfo.userAssetBalance) // 10**assetDecimals
// const borrowedAmount = Number(underworldUserInfo.userBorrowPart) / 10**assetDecimals
// const suppliedAmount = Number(underworldUserInfo.userBalance) // 10**lpDecimals
// const collateralAmount = Number(underworldUserInfo.userCollateralShare) / 10**collateralDecimals
// const assetInfo = (pair) => {
// // let collateralAddress = pair?.collateral.tokenInfo.address
// let assetAddress = pair?.asset.tokenInfo.address
// let pairAddress = pair?.address
// let logo = `https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${assetAddress}/logo.png`
// // let symbol = useUnderworldPairInfo(pairAddress).underworldPairInfo.assetTicker
// let decimals = Number(useUnderworldPairInfo(pair.address).underworldPairInfo.assetDecimals)
// let price = Number(useUnderworldPairInfo(pair.address).underworldPairInfo.assetPrice)
// return { logo, decimals, price }
// }
// const collateralInfo = (pairAddress) => {
// // let collateralAddress = pair?.collateral.tokenInfo.address
// let symbol = useUnderworldPairInfo(pairAddress).underworldPairInfo?.collateralTicker
// let decimals = Number(useUnderworldPairInfo(pairAddress).underworldPairInfo?.collateralDecimals)
// let price = Number(useUnderworldPairInfo(pairAddress).underworldPairInfo?.collateralPrice)
// return { symbol, decimals, price }
// }
// const borrowedAmount = (pair) => {
// // let collateralAddress = pair?.collateral.tokenInfo.address
// let bAmount = Number(useUnderworldPairInfo(pair.address).underworldPairInfo.assetPricePrice)
// console.log('bAmount:%s', bAmount)
// return bAmount
// }
// const assetPrice = (pair) => {
// // let collateralAddress = pair?.collateral.tokenInfo.address
// let aPrice = Number(useUnderworldPairInfo(pair.address).underworldPairInfo.assetPricePrice)
// console.log('aPrice:%s', aPrice)
// return aPrice
// }
// const collateralPrice = (pair) => {
// // let collateralAddress = pair?.collateral.tokenInfo.address
// let cPrice = Number(useUnderworldPairInfo(pair.address).underworldPairInfo.collateralPrice)
// console.log('cPrice:%s', cPrice)
// return cPrice
// }
// const borrowedValue = borrowedAmount * assetPrice
// const collateralValue = collateralAmount * collateralPrice
// const userDepositedValue = suppliedAmount * assetPrice
return (
<div className="flex-col space-y-2">
{data.items.slice(0, numDisplayed).map((pair) => (
<div key={pair.address}>
<Link href={'/borrow/' + String(pair.address).toLowerCase()}>
<a className="block text-high-emphesis">
<div className="grid items-center grid-cols-4 gap-2 px-2 py-4 text-sm md:grid-cols-5 lg:grid-cols-5 align-center bg-dark-800 hover:bg-dark-purple">
<div className="flex justify-center flex-col md:flex-row items-center">
<div className="hidden justify-center text-center items-center space-x-2 sm:flex">
<Image
height={48}
width={48}
src={`https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${pair?.asset.tokenInfo.address}/logo.png`}
className="w-5 h-5 rounded-lg md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.asset.tokenInfo.symbol}
/>
<Image
height={48}
width={48}
src={`https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${pair?.collateral.tokenInfo.address}/logo.png`}
className="w-5 h-5 rounded-lg md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.collateral.tokenInfo.symbol}
/>
</div>
<div className="hidden sm:items-end md:hidden">
<div>
<strong>{pair.asset.tokenInfo.symbol}</strong> / {pair.collateral.tokenInfo.symbol}
</div>
<div className="block mt-0 text-xs text-left text-white-500 hidden">{pair.oracle.name}</div>
</div>
<div className="text-center justify-center items-center sm:hidden">
<div className="grid items-center grid-cols-2">
{/* <strong>{collateralInfo(pair).symbol}</strong> */}
<Image
height={36}
width={36}
src={`https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${pair?.asset.tokenInfo.address}/logo.png`}
className="w-2 h-2 p-2 rounded-lg md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.asset.tokenInfo.symbol}
/>
<Image
height={36}
width={36}
src={`https://raw.githubusercontent.com/SoulSwapFinance/assets/master/blockchains/${blockchain}/assets/${pair?.collateral.tokenInfo.address}/logo.png`}
className="w-2 h-2 p-2 rounded-sm md:w-10 md:h-10 lg:w-12 lg:h-12"
alt={pair.collateral.tokenInfo.symbol}
/>
</div>
<div className="block mt-0 text-xs text-left text-white-500 hidden">{pair.oracle.name}</div>
</div>
</div>
{/* <div className="hidden text-white md:block">
<strong>{pair.asset.tokenInfo.symbol}</strong>
</div> */}
<div className="hidden text-center md:block">
{formatNumber(pair?.totalCollateralShare.div(e10(18)), false)}{' '}
{pair.collateral.tokenInfo.symbol}
<div className="text-secondary">{formatNumber(pair.totalCollateralShare / 1e18 * pair.collateral.usd / 1e18, true)}
</div>
</div>
{/* <div className="hidden lg:block">{pair.oracle.name}</div> */}
<div className="text-center md:text-right">
{/* <div className="md:hidden"> */}
{/* <div className="flex flex-col"> */}
{/* <div>{formatNumber(pair.currentAllAssets)}</div> */}
{/* <div>{formatNumber(pair.currentAllAssets)}</div> */}
{/* </div> */}
{/* </div> */}
<div className="text-center md:block">
{formatNumber(pair.currentBorrowAmount.string)} {pair.asset.tokenInfo.symbol}
<div className="text-secondary">{formatNumber(pair.currentBorrowAmount.usdValue.div(e10(18)), true)}</div>
</div>
</div>
<div className="text-center">
<div className="md:hidden">
<div className="text-green flex flex-col">
{/* <div>{formatNumber(pair.totalAssetAmount.string)}</div> */}
{formatPercent(100 -
((pair?.totalAsset.base.div(e10(18))) -
(pair?.totalAsset.base.sub(pair?.totalBorrow.base).div(e10(18))))
/ (pair?.totalAsset.base.div(e10(18))) * 100
)}
{/* <div>{pair.asset.tokenInfo.symbol}</div> */}
</div>
<div className="text-secondary">{formatNumber(pair.asset.usd / 1e18 * Number(pair?.totalAsset.base) / 1e18, true)}</div>
</div>
<div className="hidden md:block">
{formatNumber(pair.totalAsset.base.div(e10(18)))} {pair.asset.tokenInfo.symbol}
<div className="text-secondary">{formatNumber(Number(pair.asset.usd) / 1e18 * Number(pair.totalAsset.base / 1e18), true)}</div>
</div>
</div>
<div className="text-center">{formatPercent(pair.currentInterestPerYear.value / 1e16)}</div>
{/* <div className="text-right">{formatPercent(pair.currentInterestPerYear.value)}</div> */}
</div>
</a>
</Link>
</div>
))}
</div>
)
}
Borrow.Provider = RecoilRoot
// @ts-ignore TYPE NEEDS FIXING
const BorrowLayout = ({ children }) => {
const { i18n } = useLingui()
return (
<Layout
left={
<Card
className="h-full bg-dark-900"
// backgroundImage={BORROW_IMG}
title={i18n._(t`Borrow assets and leverage up`)}
description={i18n._(
t`Borrowing allows you to obtain liquidity without selling. Your borrow limit depends on the amount of deposited collateral. You will be able to borrow up to 75% of your collateral and repay at any time with accrued interest.`
)}
/>
}
>
{children}
</Layout>
)
}
Borrow.Guard = NetworkGuard(Feature.UNDERWORLD)