To use the default Connect Wallet button, simply add this in your UI:
{/* @ts-ignore */}
<appkit-button />
π Customizing the button: You can pass additional props to modify its behavior.
π Reference:AppKit Button Docs
Updating API Calls: Using TypeScript Enums and Services
To ensure type safety, better organization, and easier maintenance, we will follow a structure similar to the Hyperliquid TypeScript SDK when handling API requests.
1. Define Constants for API Calls
To make API calls more structured, we define enums for the available request types.
Now, weβll structure API calls in lib/services/ and use our enums to ensure better type safety.
π lib/services/info/spot.ts
Handles Spot Market data requests.
tsCopierModifierimport axios from "@/lib/config/axios";
import { InfoType } from "@/lib/types/constants";
import { SpotClearinghouseStateResponse } from "@/lib/types/dashboard";
/**
* Fetches the Spot Clearinghouse State for a given user.
* @param user - The user's unique identifier.
* @returns A promise resolving to the spot clearinghouse state data.
*/
export async function getSpotClearinghouseState(user: string) {
try {
const response = await axios.post(`/info`, {
type: InfoType.SPOT_CLEARINGHOUSE_STATE, // Using Enum for safety
user,
});
if (!response.data) {
throw new Error("No data received from server");
}
return response.data as SpotClearinghouseStateResponse;
} catch (error) {
return Promise.reject(error);
}
}
Do the same for:
π lib/services/info/perp.ts
π lib/services/info/general.ts
π lib/services/exchange.ts
3. Using API Calls in Your Application
Instead of manually calling axios.post(), you can now simply use the helper functions we defined.
Example usage in a React component with useQuery:
tsCopierModifierimport { useQuery } from "@tanstack/react-query";
import { getSpotClearinghouseState } from "@/lib/services/info/spot";
const { data: spotBalanceData, isLoading: isLoadingBalances, error: balanceError } = useQuery({
queryKey: ["balances", address],
queryFn: () => (address ? getSpotClearinghouseState(address) : null),
enabled: !!address, // Only run if an address is available
});