I'm new here.
I've been trying for a few days to use JUST @solana/web3.js in JS/NODE to make a Token Swap with Raydium V4.
The only Raydium examples I can find are in TS, which I don't really know that well and to be honest, the rest of my code is in JS.
I'm an VERY seasoned Programmer (20+ years), but my Blockchain experience is very low, mostly stumbling on wording I think.
Anyhow, I wrote a module that I believe follows the specs for a token swap, but I keep receiving the error
"Program CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C invoke [1]",
"Program log: AnchorError occurred. Error Code: InstructionFallbackNotFound. Error Number: 101. Error Message: Fallback functions are not supported.",
"Program CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C consumed 3563 of 200000 compute units",
"Program CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C failed: custom program error: 0x65"
I'm not sure if I'm allowed to post code or not, or but if anyone can point me in a direction with this, it would be great.
I'm just doing a sample swap, I don't really care what token it is, but for example, I used the following params
RAYDIUM_PROGRAM_ID: CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C
RPC_ENDPOINT: "https://api.mainnet-beta.solana.com"
TOKEN_MINT_ADDRESS: 5biLw67TV6tLfFthktGxsD9VxiyQb4wkPJAJQBhXpump
INPUT_TOKEN: So11111111111111111111111111111111111111112
OUTPUT_TOKEN: ChGksm9LaDCx9bzWuv8zUaDfCDzaJXzHY3yerKb4awxB
INPUT_TOKEN_ACCOUNT: C4DgMeEV2STC1R6UqA2wXdqGsDmC4BHD2tiYxsVA3Qsb
OUTPUT_TOKEN_ACCOUNT: 7mK5fNMwiwADabWLTJuUtLxidaWvhDHA2o4yuiTRT2Xs
AMOUNT_SOL: 0.01
SLIP: 10%
BRIBE: 0.01
PRIORITY: 0.01
I guess I'm not 100% sure if IM using the properly Program ID, but I've basically tried them all.
I also am getting the IN and OUT Token Accountings programmatically, so I'm pretty sure they are "right" for those input and output token address..
BUT I'm not sure if I'm using them correctly.
my swapInstructions I put together like this
// Construct the Raydium swap instruction
const swapInstruction = new TransactionInstruction({
programId: RAYDIUM_PROGRAM_ID,
keys: [
{ pubkey: new PublicKey(poolAddress), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(poolTokenA), isSigner: false, isWritable: true },
{ pubkey: new PublicKey(poolTokenB), isSigner: false, isWritable: true },
{
pubkey: new PublicKey(userTokenAccountA),
isSigner: false,
isWritable: true,
},
{
pubkey: new PublicKey(userTokenAccountB),
isSigner: false,
isWritable: true,
},
{ pubkey: wallet.publicKey, isSigner: true, isWritable: false },
],
data: Buffer.from(
Uint8Array.of(
0, // Swap instruction code
...new BN(amountIn).toArray("le", 8), // Input amount
...new BN(minOutput).toArray("le", 8), // Minimum output amount
...new BN(bribeAmount).toArray("le", 8) // Bribe amount
)
),
});
transaction.add(swapInstruction);
// Serialize and send the transaction
try {
// Fetch the latest blockhash and set it
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
//console.log(blockhash);
// Set the fee payer
transaction.feePayer = wallet.publicKey;
// Sign the transaction
transaction.sign(wallet);
// Serialize the transaction to raw format
const serializedTransaction = transaction.serialize();
// Send the serialized transaction using sendRawTransaction
const signature = await connection.sendRawTransaction(serializedTransaction, {
skipPreflight: false, // Set to true to skip preflight transaction simulation
preflightCommitment: "processed", // Commitment level for simulation
});
console.log(`Transaction sent. Signature: ${signature}`);
// Confirm the transaction
const confirmation = await connection.confirmTransaction(
{
signature,
blockhash,
lastValidBlockHeight: (await connection.getLatestBlockhash()).lastValidBlockHeight,
},
"confirmed" // Commitment level for confirmation
);