@soulsolidity/zap-widgets v0.0.1-alpha13
SoulZap SDK
A React SDK for integrating SoulZap functionality into your applications. This SDK provides a set of UI components and utilities to enable users to zap from one token to another across different chains.
Features
- DEX-like UI/UX: Interactive swapping interface
- Token Selection: Dynamically fetch supported tokens from the registry
- Customizable UI: Style components to match your branding
- Wallet Integration: Works with any wallet connection library
- Flexible Integration: Easy-to-use components for React applications
- TypeScript Support: Full type definitions for all components and utilities
Installation
npm install zap:sdk
# or
yarn add zap:sdkQuick Start
import { ZapProvider, ZapWidget } from 'zap:sdk';
import { useAccount } from 'wagmi';
function App() {
// Get wallet address from your preferred wallet library
const { address } = useAccount();
// Handle successful zap
const handleSuccess = (result) => {
console.log('Zap successful:', result);
};
// Handle zap error
const handleError = (error) => {
console.error('Zap failed:', error);
};
return (
<ZapProvider defaultUserAddress={address}>
<ZapWidget
onSuccess={handleSuccess}
onError={handleError}
/>
</ZapProvider>
);
}Components
ZapWidget
The main component that provides a complete UI for zapping tokens.
<ZapWidget
className="custom-class"
theme="light" // 'light', 'dark', or 'auto'
onSuccess={handleSuccess}
onError={handleError}
/>Props
className: Custom CSS class for stylingtheme: UI theme ('light', 'dark', or 'auto')onSuccess: Callback for successful zaponError: Callback for zap errors
ZapProvider
Context provider that manages the state and functionality for the zap components.
<ZapProvider
defaultChainId={56}
defaultFromToken="0x0000000000000000000000000000000000000000"
defaultUserAddress={walletAddress}
>
{/* Your components */}
</ZapProvider>Props
defaultChainId: Default chain ID to usedefaultFromToken: Default token address to use as the source tokendefaultUserAddress: User's wallet address from your wallet connection library
Individual Components
The SDK also exports individual components for custom integrations:
TokenSelector: Component for selecting tokensAmountInput: Input field for token amountsChainSelector: Component for selecting blockchain networksTransactionPreview: Component for previewing transactions
Wallet Integration
The SDK is designed to work with any wallet connection library. Simply pass the user's wallet address to the ZapProvider component:
// With wagmi/viem
import { useAccount } from 'wagmi';
function App() {
const { address } = useAccount();
return (
<ZapProvider defaultUserAddress={address}>
<ZapWidget />
</ZapProvider>
);
}
// With ethers.js
import { useState, useEffect } from 'react';
import { ethers } from 'ethers';
function App() {
const [address, setAddress] = useState('');
useEffect(() => {
const connectWallet = async () => {
if (window.ethereum) {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const accounts = await provider.send('eth_requestAccounts', []);
setAddress(accounts[0]);
}
};
connectWallet();
}, []);
return (
<ZapProvider defaultUserAddress={address}>
<ZapWidget />
</ZapProvider>
);
}API
Hooks
useZap(): Hook to access the zap context and functionality
Utilities
fetchRegistry(): Fetch the zap registry datatransformRegistryToTokens(): Transform registry data into a list of tokensgetSupportedChains(): Get supported chains from the registrygetTokensByChain(): Get tokens for a specific chainmakeZapRequest(): Make a zap API callformatAddress(): Format an address to a shortened formformatNumber(): Format a number to a readable stringisValidAddress(): Validate if a string is a valid Ethereum addresschainIdToName(): Format a chain ID to its network name
API Endpoint
The SDK interacts with the following API endpoint:
GET http://localhost:3001/zap?fromToken=<address>&fromAmount=<amount>&toToken=<address>&chain=<chainId>&user=<walletAddress>Registry Data
The SDK fetches supported tokens from the registry at:
https://raw.githubusercontent.com/SoulSolidity/registry/main/data/constants/zap.json
Customization
Styling
The SDK uses Tailwind CSS for styling. You can customize the appearance by:
- Providing custom classes via the
classNameprop - Setting the
themeprop to 'light', 'dark', or 'auto' - Overriding the CSS variables in your application
Development
# Install dependencies
npm install
# Start development server
npm run dev
# Build the SDK
npm run build:sdkLicense
MIT
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago