2023-03-24 05:22:29 -07:00
|
|
|
import { memo, useMemo, useEffect, useRef } from 'react'
|
2022-07-16 04:22:59 -07:00
|
|
|
import { Token } from '../../types/jupiter'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-07-16 04:22:59 -07:00
|
|
|
import { IconButton } from '../shared/Button'
|
2022-11-18 11:11:06 -08:00
|
|
|
import { XMarkIcon } from '@heroicons/react/20/solid'
|
2022-07-17 04:51:51 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-09-14 03:37:45 -07:00
|
|
|
import Decimal from 'decimal.js'
|
2022-09-14 17:02:16 -07:00
|
|
|
import { getTokenInMax } from './useTokenMax'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2022-11-18 11:11:06 -08:00
|
|
|
import useJupiterMints from 'hooks/useJupiterMints'
|
2022-11-20 12:32:38 -08:00
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
2022-12-19 14:41:22 -08:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
2023-01-24 17:12:13 -08:00
|
|
|
import FormatNumericValue from '@components/shared/FormatNumericValue'
|
2023-04-16 18:04:13 -07:00
|
|
|
import { formatTokenSymbol } from 'utils/tokens'
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-11-19 11:20:36 -08:00
|
|
|
// const generateSearchTerm = (item: Token, searchValue: string) => {
|
|
|
|
// const normalizedSearchValue = searchValue.toLowerCase()
|
|
|
|
// const values = `${item.symbol} ${item.name}`.toLowerCase()
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-11-19 11:20:36 -08:00
|
|
|
// const isMatchingWithSymbol =
|
|
|
|
// item.symbol.toLowerCase().indexOf(normalizedSearchValue) >= 0
|
|
|
|
// const matchingSymbolPercent = isMatchingWithSymbol
|
|
|
|
// ? normalizedSearchValue.length / item.symbol.length
|
|
|
|
// : 0
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-11-19 11:20:36 -08:00
|
|
|
// return {
|
|
|
|
// token: item,
|
|
|
|
// matchingIdx: values.indexOf(normalizedSearchValue),
|
|
|
|
// matchingSymbolPercent,
|
|
|
|
// }
|
|
|
|
// }
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-11-19 11:20:36 -08:00
|
|
|
// const startSearch = (items: Token[], searchValue: string) => {
|
|
|
|
// return items
|
|
|
|
// .map((item) => generateSearchTerm(item, searchValue))
|
|
|
|
// .filter((item) => item.matchingIdx >= 0)
|
|
|
|
// .sort((i1, i2) => i1.matchingIdx - i2.matchingIdx)
|
|
|
|
// .sort((i1, i2) => i2.matchingSymbolPercent - i1.matchingSymbolPercent)
|
|
|
|
// .map((item) => item.token)
|
|
|
|
// }
|
2022-07-16 04:22:59 -07:00
|
|
|
|
|
|
|
const TokenItem = ({
|
|
|
|
token,
|
|
|
|
onSubmit,
|
2022-09-14 03:37:45 -07:00
|
|
|
useMargin,
|
|
|
|
type,
|
2022-07-16 04:22:59 -07:00
|
|
|
}: {
|
2023-02-25 16:22:39 -08:00
|
|
|
token: TokenInfoWithAmounts
|
2022-07-16 04:22:59 -07:00
|
|
|
onSubmit: (x: string) => void
|
2022-09-14 03:37:45 -07:00
|
|
|
useMargin: boolean
|
2023-02-08 16:31:53 -08:00
|
|
|
type: 'input' | 'output' | undefined
|
2022-07-16 04:22:59 -07:00
|
|
|
}) => {
|
2023-02-08 16:31:53 -08:00
|
|
|
const { t } = useTranslation('trade')
|
2022-07-16 04:22:59 -07:00
|
|
|
const { address, symbol, logoURI, name } = token
|
2022-09-14 03:37:45 -07:00
|
|
|
|
2023-02-08 16:31:53 -08:00
|
|
|
const bank = useMemo(() => {
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
if (!group) return
|
|
|
|
return group.getFirstBankByMint(new PublicKey(address))
|
|
|
|
}, [address])
|
|
|
|
|
2023-05-11 18:48:30 -07:00
|
|
|
const isReduceOnly = useMemo(() => {
|
|
|
|
if (!bank) return false
|
|
|
|
const borrowsReduceOnly = bank.areBorrowsReduceOnly()
|
|
|
|
const depositsReduceOnly = bank.areDepositsReduceOnly()
|
|
|
|
return borrowsReduceOnly && depositsReduceOnly
|
|
|
|
}, [bank])
|
|
|
|
|
2022-07-16 04:22:59 -07:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<button
|
|
|
|
key={address}
|
2023-04-19 18:12:45 -07:00
|
|
|
className={`flex w-full cursor-pointer items-center justify-between rounded-md p-2 font-normal focus:outline-none focus-visible:bg-th-bkg-3 md:hover:bg-th-bkg-2`}
|
2022-08-10 13:23:19 -07:00
|
|
|
onClick={() => onSubmit(address)}
|
2022-07-16 04:22:59 -07:00
|
|
|
>
|
|
|
|
<div className="flex items-center">
|
|
|
|
<picture>
|
|
|
|
<source srcSet={logoURI} type="image/webp" />
|
|
|
|
<img src={logoURI} width="24" height="24" alt={symbol} />
|
|
|
|
</picture>
|
|
|
|
<div className="ml-2.5">
|
2023-02-08 16:31:53 -08:00
|
|
|
<p className="text-left text-th-fgd-2">
|
2023-04-16 18:04:13 -07:00
|
|
|
{bank?.name ? formatTokenSymbol(bank.name) : symbol || 'unknown'}
|
2023-05-11 18:48:30 -07:00
|
|
|
{isReduceOnly ? (
|
2023-02-08 16:31:53 -08:00
|
|
|
<span className="ml-1.5 text-xxs text-th-warning">
|
|
|
|
{t('reduce-only')}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p className="text-left text-xs text-th-fgd-4">
|
2022-09-14 03:37:45 -07:00
|
|
|
{name || 'unknown'}
|
2023-02-08 16:31:53 -08:00
|
|
|
</p>
|
2022-07-16 04:22:59 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-09 18:18:47 -08:00
|
|
|
{type === 'input' &&
|
|
|
|
token.amount &&
|
|
|
|
token.amountWithBorrow &&
|
|
|
|
token.decimals ? (
|
|
|
|
<p className="font-mono text-sm text-th-fgd-2">
|
2023-01-24 17:12:13 -08:00
|
|
|
{useMargin ? (
|
|
|
|
<FormatNumericValue
|
|
|
|
value={token.amountWithBorrow}
|
|
|
|
decimals={token.decimals}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<FormatNumericValue
|
|
|
|
value={token.amount}
|
|
|
|
decimals={token.decimals}
|
|
|
|
/>
|
|
|
|
)}
|
2022-09-14 03:37:45 -07:00
|
|
|
</p>
|
|
|
|
) : null}
|
2022-07-16 04:22:59 -07:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-19 11:20:36 -08:00
|
|
|
// const popularTokenSymbols = ['USDC', 'SOL', 'USDT', 'MNGO', 'BTC']
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2023-02-25 16:22:39 -08:00
|
|
|
interface TokenInfoWithAmounts extends Token {
|
|
|
|
amount?: Decimal
|
|
|
|
amountWithBorrow?: Decimal
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:54:38 -07:00
|
|
|
const SwapFormTokenList = ({
|
2022-07-16 04:22:59 -07:00
|
|
|
onClose,
|
|
|
|
onTokenSelect,
|
|
|
|
type,
|
2022-09-14 03:37:45 -07:00
|
|
|
useMargin,
|
2022-07-16 04:22:59 -07:00
|
|
|
}: {
|
|
|
|
onClose: () => void
|
|
|
|
onTokenSelect: (x: string) => void
|
2023-02-08 16:31:53 -08:00
|
|
|
type: 'input' | 'output' | undefined
|
2022-09-14 03:37:45 -07:00
|
|
|
useMargin: boolean
|
2022-07-16 04:22:59 -07:00
|
|
|
}) => {
|
2022-09-16 04:37:24 -07:00
|
|
|
const { t } = useTranslation(['common', 'swap'])
|
2022-11-18 11:11:06 -08:00
|
|
|
// const [search, setSearch] = useState('')
|
|
|
|
const { mangoTokens } = useJupiterMints()
|
2022-08-20 11:54:38 -07:00
|
|
|
const inputBank = mangoStore((s) => s.swap.inputBank)
|
|
|
|
const outputBank = mangoStore((s) => s.swap.outputBank)
|
2022-11-20 12:32:38 -08:00
|
|
|
const { group } = useMangoGroup()
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2023-03-24 05:22:29 -07:00
|
|
|
const focusRef = useRef<HTMLButtonElement>(null)
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-09-29 21:21:23 -07:00
|
|
|
// const popularTokens = useMemo(() => {
|
|
|
|
// return tokens.filter((token) => {
|
|
|
|
// return !token?.name || !token?.symbol
|
|
|
|
// ? false
|
|
|
|
// : popularTokenSymbols.includes(token.symbol)
|
|
|
|
// })
|
|
|
|
// }, [tokens])
|
2022-07-16 04:22:59 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-27 23:20:11 -08:00
|
|
|
function onEscape(e: KeyboardEvent) {
|
2022-07-16 04:22:59 -07:00
|
|
|
if (e.keyCode === 27) {
|
2022-08-02 11:04:00 -07:00
|
|
|
onClose()
|
2022-07-16 04:22:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener('keydown', onEscape)
|
|
|
|
return () => window.removeEventListener('keydown', onEscape)
|
|
|
|
}, [onClose])
|
|
|
|
|
2023-02-25 16:22:39 -08:00
|
|
|
const tokenInfos: TokenInfoWithAmounts[] = useMemo(() => {
|
2022-09-14 03:37:45 -07:00
|
|
|
if (
|
2022-11-18 11:11:06 -08:00
|
|
|
mangoTokens?.length &&
|
2022-09-14 03:37:45 -07:00
|
|
|
group &&
|
|
|
|
mangoAccount &&
|
|
|
|
outputBank &&
|
2022-12-12 21:38:12 -08:00
|
|
|
inputBank &&
|
2022-09-14 03:37:45 -07:00
|
|
|
type === 'input'
|
|
|
|
) {
|
2022-11-18 11:11:06 -08:00
|
|
|
const filteredSortedTokens = mangoTokens
|
2022-09-14 03:37:45 -07:00
|
|
|
.map((token) => {
|
2022-09-22 14:35:07 -07:00
|
|
|
const max = getTokenInMax(
|
|
|
|
mangoAccount,
|
2022-12-19 14:41:22 -08:00
|
|
|
new PublicKey(token.address),
|
2022-12-12 21:38:12 -08:00
|
|
|
outputBank.mint,
|
2022-09-22 14:35:07 -07:00
|
|
|
group,
|
|
|
|
useMargin
|
|
|
|
)
|
2022-09-14 17:02:16 -07:00
|
|
|
return { ...token, ...max }
|
2022-09-14 03:37:45 -07:00
|
|
|
})
|
|
|
|
.filter((token) => (token.symbol === outputBank?.name ? false : true))
|
|
|
|
.sort((a, b) =>
|
|
|
|
useMargin
|
2022-09-14 17:02:16 -07:00
|
|
|
? Number(b.amountWithBorrow) - Number(a.amountWithBorrow)
|
|
|
|
: Number(b.amount) - Number(a.amount)
|
2022-07-16 04:22:59 -07:00
|
|
|
)
|
2022-09-14 03:37:45 -07:00
|
|
|
|
|
|
|
return filteredSortedTokens
|
2022-11-18 11:11:06 -08:00
|
|
|
} else if (mangoTokens?.length) {
|
|
|
|
const filteredTokens = mangoTokens
|
2022-09-14 03:37:45 -07:00
|
|
|
.map((token) => ({
|
|
|
|
...token,
|
2022-09-14 17:02:16 -07:00
|
|
|
amount: new Decimal(0),
|
|
|
|
amountWithBorrow: new Decimal(0),
|
2022-09-14 03:37:45 -07:00
|
|
|
}))
|
|
|
|
.filter((token) => (token.symbol === inputBank?.name ? false : true))
|
2023-01-05 20:46:36 -08:00
|
|
|
.sort((a, b) => a.symbol.localeCompare(b.symbol))
|
2022-09-14 03:37:45 -07:00
|
|
|
return filteredTokens
|
2022-07-16 04:22:59 -07:00
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
2022-11-18 11:11:06 -08:00
|
|
|
}, [mangoTokens, inputBank, outputBank, mangoAccount, group, useMargin, type])
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-09-29 21:21:23 -07:00
|
|
|
// const handleUpdateSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
// setSearch(e.target.value)
|
|
|
|
// }
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2022-09-29 21:21:23 -07:00
|
|
|
// const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos
|
|
|
|
const sortedTokens = tokenInfos
|
2022-07-16 04:22:59 -07:00
|
|
|
|
2023-03-24 05:22:29 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (focusRef?.current) {
|
|
|
|
focusRef.current.focus()
|
|
|
|
}
|
|
|
|
}, [focusRef])
|
|
|
|
|
2022-07-16 04:22:59 -07:00
|
|
|
return (
|
|
|
|
<>
|
2022-09-16 04:37:24 -07:00
|
|
|
<p className="mb-3">
|
|
|
|
{type === 'input'
|
2022-11-22 02:36:45 -08:00
|
|
|
? t('swap:pay')
|
|
|
|
: type === 'output'
|
|
|
|
? t('swap:receive')
|
|
|
|
: ''}
|
2022-09-16 04:37:24 -07:00
|
|
|
</p>
|
2022-10-08 03:15:03 -07:00
|
|
|
<IconButton
|
|
|
|
className="absolute top-2 right-2 text-th-fgd-3 hover:text-th-fgd-2"
|
|
|
|
onClick={onClose}
|
|
|
|
hideBg
|
2023-03-24 05:22:29 -07:00
|
|
|
ref={focusRef}
|
2022-10-08 03:15:03 -07:00
|
|
|
>
|
2022-09-29 21:21:23 -07:00
|
|
|
<XMarkIcon className="h-6 w-6" />
|
2022-07-16 04:22:59 -07:00
|
|
|
</IconButton>
|
2022-09-14 03:37:45 -07:00
|
|
|
{/* No need for search/popular tokens until we have more tokens */}
|
|
|
|
|
|
|
|
{/* <div className="flex items-center text-th-fgd-4">
|
2022-07-16 04:22:59 -07:00
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
placeholder="Search by token or paste address"
|
2022-09-06 21:36:35 -07:00
|
|
|
prefix={<MagnifyingGlassIcon className="h-5 w-5" />}
|
2022-07-16 04:22:59 -07:00
|
|
|
autoFocus
|
|
|
|
value={search}
|
|
|
|
onChange={handleUpdateSearch}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-08-02 11:04:00 -07:00
|
|
|
{popularTokens.length ? (
|
2022-07-16 04:22:59 -07:00
|
|
|
<div className="mt-4 flex flex-wrap">
|
2022-08-15 19:41:06 -07:00
|
|
|
{popularTokens.map((token) => {
|
|
|
|
let logoURI
|
2022-11-18 11:11:06 -08:00
|
|
|
if (mangoTokens.length) {
|
|
|
|
logoURI = mangoTokens.find(
|
2022-08-15 19:41:06 -07:00
|
|
|
(t) => t.address === token.address
|
|
|
|
)!.logoURI
|
|
|
|
}
|
2022-08-20 11:54:38 -07:00
|
|
|
const disabled =
|
|
|
|
(type === 'input' && token.symbol === outputBank?.name) ||
|
|
|
|
(type === 'output' && token.symbol === inputBank?.name)
|
2022-08-15 19:41:06 -07:00
|
|
|
return (
|
|
|
|
<button
|
2022-08-20 11:54:38 -07:00
|
|
|
className={`${
|
|
|
|
disabled ? 'opacity-20' : 'hover:border-th-fgd-3'
|
|
|
|
} mx-1 mb-2 flex items-center rounded-md border border-th-bkg-4 py-1 px-3 focus:border-th-fgd-2`}
|
2022-08-15 19:41:06 -07:00
|
|
|
onClick={() => onTokenSelect(token.address)}
|
2022-08-20 11:54:38 -07:00
|
|
|
disabled={disabled}
|
2022-08-15 19:41:06 -07:00
|
|
|
key={token.address}
|
|
|
|
>
|
|
|
|
{logoURI ? (
|
|
|
|
<Image alt="" width="16" height="16" src={logoURI} />
|
|
|
|
) : (
|
|
|
|
<QuestionMarkCircleIcon className="h-5 w-5 text-th-fgd-3" />
|
|
|
|
)}
|
|
|
|
<span className="ml-1.5 text-th-fgd-1">{token.symbol}</span>
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
})}
|
2022-07-16 04:22:59 -07:00
|
|
|
</div>
|
2022-09-14 03:37:45 -07:00
|
|
|
) : null} */}
|
|
|
|
{/* <div className="my-2 border-t border-th-bkg-4"></div> */}
|
2022-10-08 03:15:03 -07:00
|
|
|
<div className="mb-2 flex justify-between rounded bg-th-bkg-2 p-2">
|
2022-09-14 03:37:45 -07:00
|
|
|
<p className="text-xs text-th-fgd-4">{t('token')}</p>
|
|
|
|
{type === 'input' ? (
|
|
|
|
<p className="text-xs text-th-fgd-4">{t('max')}</p>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2022-11-27 15:40:40 -08:00
|
|
|
<div className="overflow-auto pb-2">
|
2022-07-16 04:22:59 -07:00
|
|
|
{sortedTokens.map((token) => (
|
2022-07-22 08:40:53 -07:00
|
|
|
<TokenItem
|
|
|
|
key={token.address}
|
|
|
|
token={token}
|
|
|
|
onSubmit={onTokenSelect}
|
2022-09-14 03:37:45 -07:00
|
|
|
useMargin={useMargin}
|
|
|
|
type={type}
|
2022-07-22 08:40:53 -07:00
|
|
|
/>
|
2022-07-16 04:22:59 -07:00
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:54:38 -07:00
|
|
|
export default memo(SwapFormTokenList)
|