1.0.1 • Published 8 months ago

solana-presale-sdk v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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 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:

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