mango-v4-ui/components/swap/Swap.tsx

161 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-07-12 19:02:36 -07:00
import { useState, ChangeEvent, useCallback } from 'react'
2022-07-05 20:37:49 -07:00
import { TransactionInstruction } from '@solana/web3.js'
2022-07-12 19:02:36 -07:00
import { ArrowDownIcon } from '@heroicons/react/solid'
2022-07-05 20:37:49 -07:00
import mangoStore from '../../store/state'
import ContentBox from '../shared/ContentBox'
import { notify } from '../../utils/notifications'
import JupiterRoutes from './JupiterRoutes'
import TokenSelect from '../TokenSelect'
import useDebounce from '../shared/useDebounce'
2022-07-10 19:01:16 -07:00
import { numberFormat } from '../../utils/numbers'
2022-07-12 19:02:36 -07:00
import LeverageSlider from './LeverageSlider'
2022-07-15 05:50:29 -07:00
import Input from '../forms/Input'
import { useTranslation } from 'next-i18next'
2022-07-05 20:37:49 -07:00
const Swap = () => {
2022-07-15 05:50:29 -07:00
const { t } = useTranslation('common')
2022-07-05 20:37:49 -07:00
const [amountIn, setAmountIn] = useState('')
const [amountOut, setAmountOut] = useState<number>()
const [inputToken, setInputToken] = useState('SOL')
const [outputToken, setOutputToken] = useState('USDC')
const [submitting, setSubmitting] = useState(false)
const [slippage, setSlippage] = useState(0.1)
2022-07-12 19:02:36 -07:00
const debouncedAmountIn = useDebounce(amountIn, 400)
2022-07-10 19:01:16 -07:00
const set = mangoStore.getState().set
const tokens = mangoStore((s) => s.jupiterTokens)
2022-07-05 20:37:49 -07:00
2022-07-12 19:02:36 -07:00
const handleAmountInChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setAmountIn(e.target.value)
},
[]
)
2022-07-05 20:37:49 -07:00
const handleTokenInSelect = (symbol: string) => {
const inputTokenInfo = tokens.find((t: any) => t.symbol === symbol)
2022-07-10 19:01:16 -07:00
set((s) => {
s.inputTokenInfo = inputTokenInfo
})
setInputToken(symbol)
2022-07-05 20:37:49 -07:00
}
const handleTokenOutSelect = (symbol: string) => {
const outputTokenInfo = tokens.find((t: any) => t.symbol === symbol)
2022-07-10 19:01:16 -07:00
set((s) => {
s.outputTokenInfo = outputTokenInfo
})
setOutputToken(symbol)
2022-07-05 20:37:49 -07:00
}
2022-07-10 19:01:16 -07:00
const handleSwitchTokens = () => {
const inputTokenInfo = tokens.find((t: any) => t.symbol === inputToken)
const outputTokenInfo = tokens.find((t: any) => t.symbol === outputToken)
set((s) => {
s.inputTokenInfo = outputTokenInfo
s.outputTokenInfo = inputTokenInfo
})
2022-07-10 19:01:16 -07:00
setInputToken(outputToken)
setOutputToken(inputToken)
}
2022-07-05 20:37:49 -07:00
const handleSwap = async (
userDefinedInstructions: TransactionInstruction[]
) => {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const actions = mangoStore.getState().actions
const mangoAccount = mangoStore.getState().mangoAccount
if (!mangoAccount || !group) return
try {
setSubmitting(true)
const tx = await client.marginTrade3({
group,
mangoAccount,
inputToken,
amountIn: parseFloat(amountIn),
2022-07-10 19:01:16 -07:00
outputToken,
2022-07-05 20:37:49 -07:00
userDefinedInstructions,
})
console.log('Success swapping:', tx)
2022-07-10 19:01:16 -07:00
notify({
title: 'Transaction confirmed',
type: 'success',
txid: tx,
})
2022-07-05 20:37:49 -07:00
await actions.reloadAccount()
} catch (e: any) {
console.log('Error swapping:', e)
notify({
title: 'Transaction failed',
description: e.message,
txid: e?.txid,
type: 'error',
})
} finally {
setSubmitting(false)
}
}
return (
2022-07-15 05:50:29 -07:00
<ContentBox showBackground>
<p className="mb-2 text-th-fgd-3">{t('short')}</p>
<div className="mb-3 grid grid-cols-2">
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-bkg-3 bg-th-bkg-1 px-4">
<TokenSelect token={inputToken} onChange={handleTokenInSelect} />
2022-07-05 20:37:49 -07:00
</div>
2022-07-15 05:50:29 -07:00
<div className="col-span-1">
<Input
type="text"
name="amountIn"
id="amountIn"
className="w-full rounded-lg rounded-l-none border border-th-bkg-3 bg-th-bkg-1 py-3 px-4 text-right text-xl font-bold tracking-wider text-th-fgd-1 focus:outline-none"
placeholder="0.00"
value={amountIn}
onChange={handleAmountInChange}
/>
2022-07-10 19:01:16 -07:00
</div>
2022-07-15 05:50:29 -07:00
</div>
<div className="flex justify-center">
<button
className="rounded-full border border-th-bkg-4 p-1.5 text-th-fgd-3 md:hover:text-th-primary"
onClick={handleSwitchTokens}
>
<ArrowDownIcon className="h-5 w-5" />
</button>
</div>
<p className="mb-2 text-th-fgd-3">{t('long')}</p>
<div className="mb-3 grid grid-cols-2">
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-bkg-3 bg-th-bkg-1 px-4">
2022-07-05 20:37:49 -07:00
<TokenSelect token={outputToken} onChange={handleTokenOutSelect} />
2022-07-15 05:50:29 -07:00
</div>
<div className="w-full rounded-lg rounded-l-none border border-th-bkg-3 bg-th-bkg-3 py-3 px-4 text-right text-xl font-bold tracking-wider text-th-fgd-3">
{amountOut ? numberFormat.format(amountOut) : 0}
2022-07-05 20:37:49 -07:00
</div>
</div>
2022-07-15 05:50:29 -07:00
<div className="mb-2 flex items-center justify-between">
<p className="text-th-fgd-3">{t('leverage')}</p>
<p className="font-bold text-th-fgd-1">0.00x</p>
</div>
<LeverageSlider
inputToken={inputToken}
outputToken={outputToken}
onChange={(x) => setAmountIn(x)}
/>
2022-07-05 20:37:49 -07:00
<JupiterRoutes
inputToken={inputToken}
outputToken={outputToken}
amountIn={parseFloat(debouncedAmountIn)}
slippage={slippage}
handleSwap={handleSwap}
submitting={submitting}
setAmountOut={setAmountOut}
/>
</ContentBox>
)
}
export default Swap