creator_claim
This is the pseudo-code for creator claim using @solana /web3.js in pure JavaScript.
import { PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
import { Buffer } from 'buffer';
import { FEE_RECEIVER, PROGRAM_ID } from '../utils/constants';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
createAssociatedTokenAccountInstruction,
createCloseAccountInstruction,
createInitializeAccountInstruction,
getAssociatedTokenAddressSync,
NATIVE_MINT,
TOKEN_PROGRAM_ID
} from '@solana/spl-token';
import { sendAndConfirmTx } from '../utils/conn';
import { getClaimAuthAddress, getConfigAddress } from '../utils/pda';
import { generateRandomSeed } from '../utils/tools';
const discriminator = Buffer.from([229, 219, 40, 85, 171, 183, 45, 142]);
export const creatorClaim = async (connection, wallet, mintAddress, claimMint, claimTokenProgram) => {
const instructionData = discriminator;
const mint = new PublicKey(mintAddress);
const instructions = [];
// Instruction: Create creatorClaimAccount
let creatorClaimAccount;
if (claimMint.equals(NATIVE_MINT)) {
const seed = generateRandomSeed(24);
creatorClaimAccount = await PublicKey.createWithSeed(wallet.publicKey, seed, TOKEN_PROGRAM_ID);
const createAccountIx = SystemProgram.createAccountWithSeed({
fromPubkey: wallet.publicKey,
basePubkey: wallet.publicKey,
seed: seed,
newAccountPubkey: creatorClaimAccount,
lamports: 2039280,
space: 165,
programId: TOKEN_PROGRAM_ID,
});
instructions.push(createAccountIx);
const initAccountIx = createInitializeAccountInstruction(creatorClaimAccount, NATIVE_MINT, wallet.publicKey);
instructions.push(initAccountIx);
} else {
creatorClaimAccount = getAssociatedTokenAddressSync(claimMint, wallet.publicKey, false, claimTokenProgram);
const accountInfo = await connection.getAccountInfo(creatorClaimAccount);
if (!accountInfo) {
const createAccountIx = createAssociatedTokenAccountInstruction(
wallet.publicKey,
creatorClaimAccount,
wallet.publicKey,
claimMint,
claimTokenProgram,
);
instructions.push(createAccountIx);
}
}
// accounts
const claimAuthority = getClaimAuthAddress(mint);
const claimVault = getAssociatedTokenAddressSync(claimMint, claimAuthority, true, claimTokenProgram);
const feeAccount = getAssociatedTokenAddressSync(claimMint, FEE_RECEIVER, false, claimTokenProgram);
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: claimMint, isSigner: false, isWritable: false },
{ pubkey: claimVault, isSigner: false, isWritable: true },
{ pubkey: creatorClaimAccount, isSigner: false, isWritable: true },
{ pubkey: FEE_RECEIVER, isSigner: false, isWritable: false },
{ pubkey: feeAccount, isSigner: false, isWritable: true },
{ pubkey: claimTokenProgram, isSigner: false, isWritable: false },
{ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
];
// creator_claim instruction
const instruction = new TransactionInstruction({
keys: accounts,
programId: PROGRAM_ID,
data: instructionData,
});
instructions.push(instruction);
// Instruction: Close creatorClaimAccount
if (claimMint.equals(NATIVE_MINT)) {
const closeAccountIx = createCloseAccountInstruction(creatorClaimAccount, wallet.publicKey, wallet.publicKey);
instructions.push(closeAccountIx);
}
// send and confirm transaction
return sendAndConfirmTx(connection, wallet, instructions, []);
};Last updated