UNPKG

solana-presale-sdk

Version:

SDK for Solana Presale Program

152 lines (121 loc) 3.32 kB
# Solana Presale SDK A TypeScript SDK for interacting with the Solana Presale Program. ## Installation ```bash npm install solana-presale-sdk ``` ## Usage First, import the necessary classes and types: ```typescript import { PresaleClient } from "solana-presale-sdk"; import { Connection, PublicKey } from "@solana/web3.js"; import { useWallet } from "@solana/wallet-adapter-react"; // Initialize the client const connection = new Connection("https://api.devnet.solana.com"); const { publicKey, signTransaction, sendTransaction } = useWallet(); const programId = new PublicKey("YOUR_PROGRAM_ID"); const client = new PresaleClient( connection, { publicKey, signTransaction, sendTransaction }, programId ); // Example: Create a presale const presaleData = { tokenMintAddress: "YOUR_TOKEN_MINT_ADDRESS", softcapAmount: 1000, hardcapAmount: 2000, maxTokenAmountPerAddress: 100, pricePerToken: 0.1, startTime: new Date(), endTime: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days from now }; try { const tx = await client.createPresale(presaleData); console.log("Presale created:", tx); } catch (error) { console.error("Error creating presale:", error); } // Example: Buy tokens const buyData = { amount: 10, // Amount in SOL }; try { const tx = await client.buyToken(buyData); console.log("Tokens purchased:", tx); } catch (error) { console.error("Error buying tokens:", error); } ``` ## Available Methods - `fetchPresaleInfo()`: Get current presale information - `fetchUserInfo(userPublicKey)`: Get user's presale participation info - `startPresale(data)`: Start a presale - `createPresale(data)`: Create a new presale - `updatePresale(data)`: Update presale parameters - `buyToken(data)`: Purchase tokens in the presale - `depositToken(data, presaleInfo)`: Deposit tokens to the presale - `withdrawTokens(data, presaleInfo)`: Withdraw tokens from the presale - `claimTokens(presaleInfo)`: Claim purchased tokens - `withdrawSOL()`: Withdraw collected SOL (admin only) ## Types The SDK includes TypeScript types for all parameters and return values. Key interfaces include: ```typescript interface PreSaleFormData { tokenMintAddress: string; softcapAmount: number; hardcapAmount: number; maxTokenAmountPerAddress: number; pricePerToken: number; startTime: Date; endTime: Date; } interface BuyTokenFormData { amount: number; } interface PresaleInfo { tokenMintAddress: PublicKey; softcapAmount: BN; hardcapAmount: BN; depositTokenAmount: BN; soldTokenAmount: BN; startTime: BN; endTime: BN; maxTokenAmountPerAddress: BN; pricePerToken: BN; isLive: boolean; authority: PublicKey; isSoftCapped: boolean; isHardCapped: boolean; } interface UserInfo { buyQuoteAmount: BN; buyTokenAmount: BN; buyTime: BN; claimTime: BN; } ``` ## Error Handling All methods throw descriptive errors that can be caught and handled appropriately: ```typescript try { await client.buyToken({ amount: 10 }); } catch (error) { if (error.message.includes("insufficient funds")) { console.error("Not enough SOL in wallet"); } else { console.error("Transaction failed:", error); } } ``` ## Development To build the SDK locally: ```bash npm install npm run build ``` To run tests: ```bash npm test ``` ## License MIT