wsol_to_claim
This is the pseudo-code for swapping WSOL into the claim token using @solana /web3.js in pure JavaScript. If the creator has configured the claim token to be WSOL itself, this instruction cannot be called.
This instruction is public, and you can invoke it on any token that you did not create yourself.
import { ComputeBudgetProgram, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js';
import { Buffer } from 'buffer';
import { JUPITER_PROGRAM_ID, PROGRAM_ID } from '../utils/constants';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
getAssociatedTokenAddressSync,
NATIVE_MINT,
TOKEN_PROGRAM_ID
} from '@solana/spl-token';
import { sendLookupAndConfirmTx } from '../utils/conn';
import { getJupiterSwapInstruction } from '../https/getJupiter';
import { getPythFeedId } from '../https/getPyth';
import { getClaimAuthAddress, getConfigAddress, getFeedAddress, getPriceFeedAccountAddress } from '../utils/pda';
const discriminator = Buffer.from([117, 15, 22, 43, 44, 157, 250, 151]);
export const swapWsolToClaim = async (connection, wallet, mintAddress, claimMint, claimSymbol, claimTokenProgram) => {
const mint = new PublicKey(mintAddress);
const claimAuthority = getClaimAuthAddress(mint);
// fetch wsol amount
const wsolVault = getAssociatedTokenAddressSync(NATIVE_MINT, claimAuthority, true, TOKEN_PROGRAM_ID);
const accountBalance = await connection.getTokenAccountBalance(wsolVault);
let wsolAmount = accountBalance.value.amount;
if (Number(wsolAmount) === 0) {
throw new Error('No SOL available for swap. Please run Harvest Fees and Swap To SOL in sequence first.');
}
if (Number(wsolAmount) > 10000000000) {
wsolAmount = "10000000000"
}
const claimVault = getAssociatedTokenAddressSync(claimMint, claimAuthority, true, claimTokenProgram);
// fetch solPriceUpdate
const solPriceUpdate = new PublicKey("7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE");
const solFeed = getFeedAddress(NATIVE_MINT);
// fetch claimPriceUpdate
const claimFeedId = await getPythFeedId(claimSymbol);
const claimPriceUpdate = getPriceFeedAccountAddress(0, claimFeedId);
const claimFeed = getFeedAddress(claimMint);
// fetch jupiter
const { swapInstruction, lookupTableAddresses } = await getJupiterSwapInstruction(
NATIVE_MINT.toString(),
claimMint.toString(),
// "jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL", // replace claimMint to test
wsolAmount,
claimAuthority.toString(),
);
const jupiterInstructionData = Buffer.from(swapInstruction.data, 'base64');
const lenBuffer = Buffer.alloc(4);
lenBuffer.writeUInt32LE(jupiterInstructionData.length, 0);
// @ts-ignore
const instructionData = Buffer.concat([discriminator, lenBuffer, jupiterInstructionData]);
const instructionAccounts = swapInstruction.accounts.map((acc) => ({
pubkey: new PublicKey(acc.pubkey),
isSigner: false,
isWritable: acc.isWritable,
}));
const accounts = [
{ pubkey: wallet.publicKey, isSigner: true, isWritable: true },
{ pubkey: getConfigAddress(), isSigner: false, isWritable: false },
{ pubkey: claimAuthority, isSigner: false, isWritable: true },
{ pubkey: mint, isSigner: false, isWritable: false },
{ pubkey: NATIVE_MINT, isSigner: false, isWritable: false },
{ pubkey: claimMint, isSigner: false, isWritable: false },
{ pubkey: wsolVault, isSigner: false, isWritable: true },
{ pubkey: claimVault, isSigner: false, isWritable: true },
{ pubkey: solFeed, isSigner: false, isWritable: false },
{ pubkey: claimFeed, isSigner: false, isWritable: false },
{ pubkey: solPriceUpdate, isSigner: false, isWritable: false },
{ pubkey: claimPriceUpdate, isSigner: false, isWritable: false },
{ pubkey: claimTokenProgram, isSigner: false, isWritable: false },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: JUPITER_PROGRAM_ID, isSigner: false, isWritable: false },
...instructionAccounts,
];
// wsol_to_claim instruction
const instruction = new TransactionInstruction({
keys: accounts,
programId: PROGRAM_ID,
data: instructionData,
});
const limitInstruction = ComputeBudgetProgram.setComputeUnitLimit({
units: 1_000_000
});
const priceInstruction = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 10000
});
const instructions = [limitInstruction, priceInstruction, instruction]
// send and confirm transaction
return sendLookupAndConfirmTx(connection, wallet, instructions, [], lookupTableAddresses);
};Last updated