import { useQuote } from '0xtrails'
import { useWalletClient, useAccount } from 'wagmi'
import { useState } from 'react'
export const CustomSwapComponent = () => {
const { data: walletClient } = useWalletClient()
const { address } = useAccount()
const [amount, setAmount] = useState('1') // human-readable USDC amount
const { quote, send, isLoadingQuote, quoteError } = useQuote({
walletClient,
from: {
token: 'USDC',
chain: 'ethereum',
amount,
},
to: {
token: 'USDC',
chain: 'base',
recipient: address,
},
slippageTolerance: '0.005', // 0.5%
onStatusUpdate: (states) => {
console.log('Swap progress:', states)
},
})
const handleSwap = async () => {
if (!send) return
try {
const result = await send()
console.log('Swap result:', result)
} catch (error) {
console.error('Swap failed:', error)
}
}
if (isLoadingQuote) {
return <div>Loading quote...</div>
}
if (quoteError) {
return <div>Error getting quote: {String(quoteError)}</div>
}
if (!quote) {
return <div>No quote available</div>
}
return (
<div>
<p>From: {quote.originAmountFormatted} {quote.originToken.symbol} on {quote.originChain.name}</p>
<p>To: {quote.destinationAmountFormatted} {quote.destinationToken.symbol} on {quote.destinationChain.name}</p>
<p>Fee: {quote.totalFeeAmountUsdDisplay}</p>
<p>Estimated time: {quote.completionEstimateSeconds}s</p>
<button onClick={() => swap?.()}>Execute Swap</button>
</div>
)
}