2: SDK quick start

yarn add @wendev/sdk @solana/web3.js

Basic Setup

import { WenDevClient, Connection } from '@wendev/sdk';

// Initialize connection
const connection = new Connection(
  'https://api.mainnet-beta.solana.com',
  'confirmed'
);

// Create SDK client
const wendev = new WenDevClient({
  connection,
  commitment: 'confirmed', // optional: 'processed' | 'confirmed' | 'finalized'
});

Example: Reading Pool State

import { PublicKey } from '@solana/web3.js';

const tokenMint = new PublicKey('YourTokenMintAddress');

// Get comprehensive pool state
const poolState = await wendev.getPoolState(tokenMint);

console.log('Status:', poolState.status); // 0: Funding, 1: Migrate, 2: Trade
console.log('Virtual Base:', poolState.virtual_base.toString());
console.log('Virtual Quote:', poolState.virtual_quote.toString());
console.log('Real Base:', poolState.real_base.toString());
console.log('Real Quote:', poolState.real_quote.toString());

// Calculate fundraising progress
const progress = (poolState.real_quote.toNumber() / 
                 poolState.total_quote_fund_raising.toNumber()) * 100;
console.log('Progress:', progress.toFixed(2) + '%');

Example: Buying Tokens

import { BN } from '@wendev/sdk';

// Amount to spend (1 SOL = 1,000,000,000 lamports)
const amountIn = new BN(1_000_000_000); // 1 SOL

// Calculate expected tokens received
const tokensOut = await wendev.getTokensForLamports(tokenMint, amountIn);
console.log('You will receive:', tokensOut.toString(), 'tokens');

// Create buy transaction
const buyTx = await wendev.createBuyTransaction(wallet.publicKey, {
  tokenMint,
  amountIn,
  slippage: 100, // 1% slippage (100 basis points)
});

// Sign and send
buyTx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
buyTx.feePayer = wallet.publicKey;
const signedTx = await wallet.signTransaction(buyTx);
const signature = await connection.sendRawTransaction(signedTx.serialize());
await connection.confirmTransaction(signature);

console.log('Buy successful! Signature:', signature);

Example: Selling Tokens

// Amount of tokens to sell (with token decimals)
const amountToSell = new BN(1_000_000_000); // 1 token (assuming 9 decimals)

// Calculate expected SOL received
const solOut = await wendev.getExpectedLamportsForTokens(tokenMint, amountToSell);
console.log('You will receive:', solOut.toString(), 'lamports');

// Create sell transaction
const sellTx = await wendev.createSellTransaction(wallet.publicKey, {
  tokenMint,
  amountIn: amountToSell,
  slippage: 100, // 1% slippage
});

// Sign and send
sellTx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
sellTx.feePayer = wallet.publicKey;
const signedSellTx = await wallet.signTransaction(sellTx);
const sellSignature = await connection.sendRawTransaction(signedSellTx.serialize());
await connection.confirmTransaction(sellSignature);

console.log('Sell successful! Signature:', sellSignature);

Example: Creating a Token

import { Keypair } from '@solana/web3.js';

// Create token creation transaction
const { tx, tokenKeypair } = await wendev.createTokenCreationTransaction(
  wallet.publicKey,
  {
    metadata: {
      name: 'My Token',
      symbol: 'MTK',
      image: 'https://example.com/token-image.png',
      description: 'My awesome token',
      socials: {
        twitter: 'https://twitter.com/mytoken',
        telegram: 'https://t.me/mytoken',
        website: 'https://mytoken.com',
      },
    },
  }
);

console.log('Token Mint:', tokenKeypair.publicKey.toBase58());

// Sign and send
tx.feePayer = wallet.publicKey;
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
tx.sign(wallet, tokenKeypair); // Must sign with both wallet and token keypair

const signature = await connection.sendRawTransaction(tx.serialize());
await connection.confirmTransaction(signature);

console.log('Token created! Signature:', signature);