A Frontend TypeScript SDK for interacting with the staked HYPE (stHYPE) native Hyperliquid liquid staking protocol.
View a live demo at https://sdk-sthype.vercel.app/
pnpm add @sthype/sdkpnpm add viemimport { StHYPESDK, hyperliquidMainnet, hyperliquidTestnet } from '@sthype/sdk';
// Initialize the SDK
const sdk = new StHYPESDK({
network: 'mainnet', // or 'testnet'
});
// Get staking information
const stHypeBalance = await sdk.getBalancesStHype('0x...');
const wrappedStHypeBalance = await sdk.getBalancesWrappedStHype('0x...');
const apy = await sdk.getAPY();
const totalSupply = await sdk.getTotalSupply();
// Get quotes
const stakeQuote = await sdk.getStakeQuote(BigInt(1e18));
const unstakeQuote = await sdk.getUnstakeQuote(BigInt(1e18));
// Stake HYPE
const stakeResult = await sdk.stake('0x...', BigInt(1e18), 'optional-community-code');
// Unstake stHYPE
const approveResult = await sdk.approveUnstake(BigInt(1e18));
const unstakeResult = await sdk.unstake('0x...', BigInt(1e18), 'optional-community-code');The SDK can be initialized with the following configuration:
interface SDKConfig {
network?: 'mainnet' | 'testnet'; // Optional, defaults to 'mainnet'
}The SDK will automatically use the correct contract addresses based on the selected network:
- Mainnet: Uses predefined mainnet addresses
- Testnet: Uses predefined testnet addresses
Call setWalletClient when the wallet client is available, for example with React applications using wagmi.
import { StHYPESDK } from '@sthype/sdk';
import { useWalletClient } from 'wagmi';
import { useMemo, useEffect, useState } from 'react';
function useStHypeSDK(): { sdk: StHYPESDK | null; error: string | null } {
const { data: walletClient } = useWalletClient();
const sdk = useMemo(() => {
try {
return new StHYPESDK({
network: 'mainnet',
});
} catch (err) {
console.error('SDK initialization error:', err);
return null;
}
}, []);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!sdk) {
setError('Failed to initialize SDK. Please check your configuration.');
return;
}
try {
if (walletClient) {
sdk.setWalletClient(walletClient);
setError(null);
}
} catch (err) {
console.error('Error setting wallet client:', err);
setError('Failed to connect wallet to SDK.');
}
}, [sdk, walletClient]);
return { sdk, error };
}
// In your React component
function App() {
const { sdk, error } = useStHypeSDK();
// Use sdk methods and handle errors...
}Note: The wallet client must be set before calling any methods that require wallet interaction (stake, approveUnstake, unstake).
All transaction methods in the SDK (stake, unstake, approveUnstake, redeem) return a TransactionReceipt object from viem. This object contains:
interface TransactionReceipt {
blockHash: `0x${string}`
blockNumber: bigint
contractAddress: `0x${string}` | null
cumulativeGasUsed: bigint
effectiveGasPrice: bigint
from: `0x${string}`
gasUsed: bigint
logs: Log[]
logsBloom: `0x${string}`
status: 'success' | 'reverted'
to: `0x${string}` | null
transactionHash: `0x${string}`
transactionIndex: number
type: TransactionType
}You can access properties like transactionHash to get the transaction hash, or status to check if the transaction was successful.
- Get staking information (balance, APY, total supply)
- Get staking and unstaking quotes
- Stake HYPE tokens
- Unstake stHYPE tokens
- Support for both mainnet and testnet
- TypeScript support
- Error handling with detailed error messages
Returns the stHYPE balance for a wallet address.
Returns the wrapped stHYPE (wstHYPE) balance for a wallet address.
Returns the current APY for staking.
interface APYInfo {
apy: number;
}Returns the total supply of stHYPE tokens.
Returns a quote for staking HYPE tokens.
interface StakeQuote {
inputAmount: bigint;
outputAmount: bigint;
ratio: number;
maxRedeemable: bigint;
}Stakes HYPE tokens. Requires a connected wallet client.
Returns a quote for unstaking stHYPE tokens.
interface UnstakeQuote {
amount: bigint;
instantAmount: bigint;
deferredAmount: bigint;
maxInstantUnstake: bigint;
}Returns the maximum amount of stHYPE tokens that can be instantly unstaked at the current time. This represents the protocol's available liquidity for instant unstaking.
Approves unstaking of stHYPE tokens. Must be called before unstaking. Requires a connected wallet client.
Unstakes stHYPE tokens. Requires a connected wallet client and prior approval via approveUnstake.
Redeems a specific burn operation when it becomes available for redemption. Requires a connected wallet client.
Checks if a specific burn is available for redemption.
Returns detailed information about user's burn (unstaking) operations with data formatted for easy use.
interface GetBurnsResponse {
burns: DetailedBurn[]; // All burns formatted with additional data
timestamps: bigint[]; // Original timestamp data
completionStatus: boolean[]; // Original completion status data
}
interface DetailedBurn {
amount: bigint; // Amount of the burn
user: string; // Address that will receive the funds
completed: boolean; // Whether the burn is completed
sum: bigint; // Sum value from the contract
id: bigint; // Timestamp of the burn
redeemable: boolean; // Whether the burn is redeemable
address: string; // Address that requested the burn
vestedBurn: boolean; // If true, burn was performed by someone else
}The SDK uses a custom error class SDKError that includes:
- Error message
- Error code
Example error handling:
try {
const balance = await sdk.getBalancesStHype('0x...');
} catch (error) {
if (error instanceof SDKError) {
console.error(`Error code: ${error.code}`);
console.error(`Error message: ${error.message}`);
}
}Unstaking stHYPE tokens involves a process that may not be immediate for all cases:
- Instant Unstaking: A portion of your unstaking request (up to
maxInstantUnstakeorgetMaxRedeemable()) can be processed immediately. - Deferred Unstaking: Any amount beyond the instant limit enters a queue as a "burn request".
- Burn Requests: These are tracked on-chain and become redeemable when sufficient liquidity is available.
- Redeeming: Once a burn request becomes redeemable, you must explicitly call the
redeemfunction to receive your HYPE.
Each burn request has a unique ID and status that can be tracked using the getBurns() and isRedeemable() methods. The typical workflow for tracking unstaking is:
- Call
getBurns(address)to retrieve all burn requests for an address - Filter burns that are not completed
- For each uncompleted burn, check if it's redeemable
- Redeem burns that are available
- Periodically check the status of remaining burns
# Install dependencies
npm install
# Build
npm run build
# Test
npm test
# Lint
npm run lint
# Format
npm run formatMIT