solana-presale-sdk
v1.0.1
Published
SDK for Solana Presale Program
Downloads
140
Readme
Solana Presale SDK
A TypeScript SDK for interacting with the Solana Presale Program.
Installation
npm install solana-presale-sdk
Usage
First, import the necessary classes and types:
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 informationfetchUserInfo(userPublicKey)
: Get user's presale participation infostartPresale(data)
: Start a presalecreatePresale(data)
: Create a new presaleupdatePresale(data)
: Update presale parametersbuyToken(data)
: Purchase tokens in the presaledepositToken(data, presaleInfo)
: Deposit tokens to the presalewithdrawTokens(data, presaleInfo)
: Withdraw tokens from the presaleclaimTokens(presaleInfo)
: Claim purchased tokenswithdrawSOL()
: Withdraw collected SOL (admin only)
Types
The SDK includes TypeScript types for all parameters and return values. Key interfaces include:
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:
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:
npm install
npm run build
To run tests:
npm test
License
MIT