import { useState, ChangeEvent } from 'react' import { getOrcaOutputAmount } from '@blockworks-foundation/mango-v4' import { useWallet } from '@solana/wallet-adapter-react' import debounce from 'lodash.debounce' import mangoStore, { connection } from '../store/state' import Button from './shared/Button' import Loading from './shared/Loading' import ContentBox from './shared/ContentBox' const Swap = () => { const [amountIn, setAmountIn] = useState('') const [submitting, setSubmitting] = useState(false) const [quoteOrcaAmountOut, setQuoteOrcaAmountOut] = useState(0) const { connected } = useWallet() const handleAmountInChange = (e: ChangeEvent) => { setAmountIn(e.target.value) debouncedGetOutAmount(e.target.value) } const debouncedGetOutAmount = debounce(async (amountIn) => { const x = await getOrcaOutputAmount( connection, 'SOL', 'ORCA', parseFloat(amountIn) ) setQuoteOrcaAmountOut(x) }, 500) const handleSwap = async () => { 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.marginTrade({ group, mangoAccount, inputToken: 'SOL', amountIn: parseFloat(amountIn), outputToken: 'ORCA', minimumAmountOut: parseFloat('0.00'), }) console.log('Success swapping:', tx) alert('Success! View browser console for tx id') await actions.reloadAccount() setSubmitting(false) } catch (e) { console.log('Error swapping:', e) } } return (
{quoteOrcaAmountOut ? (
Routes:
Orca
{quoteOrcaAmountOut}
Serum
0.00
) : null}
) } export default Swap