Programmatic Usage
Complete example of using the Actions API to build custom blockchain interactions.
Complete Example
CustomSwap.tsx
import { createBlockchainActions } from 'hyperkit';
import { useWallet } from 'hyperkit';
function CustomSwap() {
const { wallet } = useWallet();
const [loading, setLoading] = useState(false);
const handleSwap = async () => {
if (!wallet.provider || !wallet.signer) return;
const actions = createBlockchainActions(wallet.provider, wallet.signer);
setLoading(true);
try {
const quote = await actions.getSwapQuote('USDT', 'USDC', '100');
await actions.swapTokens('USDT', 'USDC', '100', quote.outputAmount, wallet.account, 0.5);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
return <button onClick={handleSwap} disabled={loading}>
{loading ? Swapping... : Swap}
</button>;
}