Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7961

Trying to Swap SOL for any token using node js

$
0
0

I am writing a node js code to swap SOL token to any token. I am using @solana/spl-token-swap node module for the same. First, I am creating an associated address for the token I am swapping to. I am then creating a transaction, and adding swap instructions and then trying to send transaction for the same. I fetched the raw data for the pool from this API

This is how part of my code looks like :-

const swapAuthority = new PublicKey(liquidityJson.authority);const TokenAPubkey = new PublicKey('So11111111111111111111111111111111111111112');const TokenBPubkey = new PublicKey('4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R');let tokenAAccountAddress = await splToken.getAssociatedTokenAddress(    TokenAPubkey, // mint    swapAuthority, // owner    true // allow owner off curve    );let tokenBAccountAddress = await splToken.getAssociatedTokenAddress(      TokenBPubkey, // mint      swapAuthority, // owner      true // allow owner off curve      );const feeOwner = new PublicKey("HfoTxFR1Tm6kGmWgYWD6J7YHVy1UwqSULUGVLXkJqaKN"        );let tokenFeeAccountAddress = await splToken.getAssociatedTokenAddress(          new PublicKey(lpMint), // mint          feeOwner, // owner          true // allow owner off curve          );let tokenAccountPool = await splToken.getAssociatedTokenAddress(            new PublicKey(lpMint), // mint            swapAuthority, // owner            true // allow owner off curve            );let transaction = new Transaction();const createSwapInstruction = await Swap.TokenSwap.createInitSwapInstruction(              **tokenSwapStateAccount**, // Token swap state account              swapAuthority, // Swap pool authority              tokenAAccountAddress, // Token A token account              tokenBAccountAddress, // Token B token account              new PublicKey(lpMint), // Swap pool token mint              tokenFeeAccountAddress, // Token fee account              **swapPoolTokenAccount**, // Swap pool token account              splToken.TOKEN_PROGRAM_ID, // Token Program ID              Swap.TOKEN_SWAP_PROGRAM_ID, // Token Swap Program ID              0, // Trade fee numerator              10000, // Trade fee denominator              5, // Owner trade fee numerator              10000, // Owner trade fee denominator              0, // Owner withdraw fee numerator              0, // Owner withdraw fee denominator              20, // Host fee numerator              100, // Host fee denominator              Swap.CurveType.ConstantProduct // Curve type              );

Now I am confused about, what should be the value of tokenSwapStateAccount and swapPoolTokenAccount ? Where should I get the value of these for any swap pair ? Can anyone guide me to some documentation or example for the same ? I am trying to swap SOL to RAY here.


Viewing all articles
Browse latest Browse all 7961

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>