import { useQuote } from '0xtrails'
import { useWalletClient, useAccount } from 'wagmi'
export const CustomSwap = () => {
const { data: walletClient } = useWalletClient()
const { address } = useAccount()
const { quote, send, isLoadingQuote, quoteError, refetchQuote } = useQuote({
walletClient,
from: {
token: 'USDC',
chain: 'ethereum',
amount: '1', // human-readable USDC amount
},
to: {
token: 'USDC',
chain: 'base',
recipient: address,
},
slippageTolerance: '0.005', // 0.5%
onStatusUpdate: (states) => {
console.log('Transaction status:', states)
},
})
// Refresh quotes every 30 seconds
useEffect(() => {
const interval = setInterval(() => {
refetchQuote?.()
}, 30000)
return () => clearInterval(interval)
}, [refetchQuote])
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 (!quote) return null
return (
<div>
<p>From: {quote.originAmountFormatted} {quote.originToken.symbol}</p>
<p>To: {quote.destinationAmountFormatted} {quote.destinationToken.symbol}</p>
<button onClick={() => send?.()}>Execute Swap</button>
</div>
)
}