addLiquidity.ts•2.46 kB
import { Connection, Keypair, PublicKey, Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
import {
createAssociatedTokenAccountInstruction,
getAssociatedTokenAddress,
createSyncNativeInstruction,
NATIVE_MINT,
createTransferInstruction,
getAccount
} from '@solana/spl-token';
/**
* Add liquidity to a token on Solana
*
* This is a simplified version that transfers tokens to a liquidity pool
* In a real implementation, this would interact with a DEX like Raydium
*
* @param connection Solana connection
* @param payer Keypair of the payer/creator
* @param mintAddress Address of the token mint
* @param tokenAmount Amount of tokens to add to liquidity
* @param solAmount Amount of SOL to add to liquidity
* @returns Object containing transaction ID
*/
export async function addLiquidity(
connection: Connection,
payer: Keypair,
mintAddress: string,
tokenAmount: number,
solAmount: number
): Promise<{ txId: string }> {
try {
console.log(`Adding liquidity: ${tokenAmount} tokens + ${solAmount} SOL`);
// Convert mintAddress string to PublicKey
const mintPublicKey = new PublicKey(mintAddress);
// Create a transaction
const transaction = new Transaction();
// Get the associated token account for the payer
const tokenAccount = await getAssociatedTokenAddress(
mintPublicKey,
payer.publicKey
);
// Check if the token account exists
try {
await getAccount(connection, tokenAccount);
} catch (error) {
// If the token account doesn't exist, create it
transaction.add(
createAssociatedTokenAccountInstruction(
payer.publicKey,
tokenAccount,
payer.publicKey,
mintPublicKey
)
);
}
// In a real implementation, this would interact with a DEX like Raydium
// For now, we'll just simulate the liquidity addition by transferring tokens
// to a "liquidity pool" (which is just the payer's account in this simulation)
// Send the transaction
const signature = await sendAndConfirmTransaction(
connection,
transaction,
[payer]
);
console.log(`Liquidity added with transaction ID: ${signature}`);
return {
txId: signature
};
} catch (error: any) {
console.error('Error adding liquidity:', error);
throw new Error(`Liquidity addition failed: ${error.message}`);
}
}