React Hooks
React hooks for subscription management, merchant operations, and event monitoring.
Installation
pnpm add @nft-sub/sdk wagmi viemuseSubscriptionSDK
Initialize and manage SDK instance
Signature
useSubscriptionSDK(config: SDKConfig)Returns
| sdk | SubscriptionSDK | null |
| isInitialized | boolean |
| error | Error | null |
| isConnected | boolean |
| address | Address | undefined |
useSubscription
Manage subscription state and operations
Signature
useSubscription(sdk: SubscriptionSDK, merchantId: bigint, userAddress?: Address)Returns
| isActive | boolean |
| status | SubscriptionStatus | null |
| isLoading | boolean |
| error | Error | null |
| subscribe | (token: Address | "ETH") => Promise<Hash | undefined> |
| checkStatus | () => Promise<void> |
useMerchant
Manage merchant data and operations
Signature
useMerchant(sdk: SubscriptionSDK, merchantId: bigint)Returns
| merchant | MerchantPlan | null |
| balance | bigint |
| isLoading | boolean |
| error | Error | null |
| withdraw | (token: Address | "ETH") => Promise<Hash | undefined> |
| setPrice | (token: Address | "ETH", price: string) => Promise<Hash | undefined> |
| refresh | () => Promise<void> |
useSubscriptionEvents
Monitor blockchain events with history
Signature
useSubscriptionEvents(sdk: SubscriptionSDK, listeners: EventListeners)Returns
| isMonitoring | boolean |
| events | Array<{type: string, data: any, timestamp: number}> |
| clearEvents | () => void |
Quick Example
import { useSubscriptionSDK, useSubscription } from '@nft-sub/sdk/hooks';
function App() {
const { sdk } = useSubscriptionSDK({ chain: 'sepolia' });
const { isActive, subscribe } = useSubscription(sdk, 1n);
return (
<button onClick={() => subscribe('ETH')}>
{isActive ? 'Active' : 'Subscribe'}
</button>
);
}