0

Getting error:failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0xd

Code is:

import {
  Connection,
  Transaction,
  clusterApiUrl,
  sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
  ExtensionType,
  TOKEN_2022_PROGRAM_ID,
  createAccount,
  createMint,
  createReallocateInstruction,
  createEnableRequiredMemoTransfersInstruction,
  createInitializeTransferFeeConfigInstruction,
} from "@solana/spl-token";

// Playground wallet
const payer = pg.wallet.keypair;

// Connection to devnet cluster
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

// Transaction signature returned from sent transaction
let transactionSignature: string;

// Authority that can mint new tokens
const mintAuthority = pg.wallet.publicKey;
// Decimals for Mint Account
const decimals = 2;

// Create Mint Account
const mint = await createMint(
  connection,
  payer, // Payer of the transaction and initialization fees
  mintAuthority, // Mint Authority
  null, // Optional Freeze Authority
  decimals, // Decimals of Mint
  undefined, // Optional keypair
  undefined, // Options for confirming the transaction
  TOKEN_2022_PROGRAM_ID // Token Extension Program ID
);

// Create Token Account for Playground wallet
const tokenAccount = await createAccount(
  connection,
  payer, // Payer to create Token Account
  mint, // Mint Account address
  payer.publicKey, // Token Account owner
  undefined, // Optional keypair, default to Associated Token Account
  undefined, // Confirmation options
  TOKEN_2022_PROGRAM_ID // Token Extension Program ID
);

// Extensions to reallocate data for
const extensions = [
  ExtensionType.MemoTransfer,
  ExtensionType.TransferFeeConfig,
];
// Instruction to reallocate Token Account data
const reallocateInstruction = createReallocateInstruction(
  tokenAccount, // Token Account address
  payer.publicKey, // Payer to reallocate data
  extensions, // Extensions to reallocate
  payer.publicKey, // Token Account owner
  undefined, // Additional signers
  TOKEN_2022_PROGRAM_ID // Token Extension Program ID
);

// Instruction to initialize the MemoTransfer Extension
const enableRequiredMemoTransfersInstruction =
  createEnableRequiredMemoTransfersInstruction(
    tokenAccount, // Token Account address
    payer.publicKey, // Token Account Owner
    undefined, // Additional signers
    TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  );
const feeBasisPoints = 100; // 1%
const maxFee = BigInt(2 ** 228) - BigInt(1);
// Instruction to initialize Mint Account data

const initTransferFeeConfig = createInitializeTransferFeeConfigInstruction(
  mint, // Mint Account address
  payer.publicKey, // Authority to update fees
  payer.publicKey, // Authority to withdraw fees
  feeBasisPoints, // Basis points for transfer fee calculation
  maxFee, // Maximum fee per transfer
  TOKEN_2022_PROGRAM_ID // Token Extension Program ID
);

// Add instructions to new transaction
const transaction = new Transaction().add(
  reallocateInstruction,
  initTransferFeeConfig,
  enableRequiredMemoTransfersInstruction
);

// Send Transactoin
transactionSignature = await sendAndConfirmTransaction(
  connection,
  transaction,
  [payer]
);

console.log(
  "\nReallocate:",
  `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`
);
1

1 Answer 1

0

Most mint extensions cannot be added after the mint is initialized because they fundamentally alter how the token operates. If people are already using a token, and all of a sudden the mint creator adds a 100% transfer fee, that would be a bad experience for the user!

So you must initialize all extensions before initializing the base mint.

1
  • Feel free to vote to close the message as a duplicate with a link to the question next time instead of copying an existing answer. Commented Jul 12, 2024 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.