UNPKG

@web3auth/no-modal

Version:
33 lines (30 loc) 868 B
import { useState, useCallback } from 'react'; import { useWalletServicesPlugin } from './useWalletServicesPlugin.js'; import { WalletServicesPluginError } from '../../base/plugin/errors.js'; const useSwap = () => { const { plugin, ready } = useWalletServicesPlugin(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const showSwap = useCallback(async showSwapParams => { setLoading(true); setError(null); try { if (!plugin) throw WalletServicesPluginError.notInitialized(); if (!ready) throw WalletServicesPluginError.walletPluginNotConnected(); await plugin.showSwap(showSwapParams); } catch (error) { setError(error); } finally { setLoading(false); } }, [plugin, ready]); return { loading, error, showSwap }; }; export { useSwap };