typescript - Solana SPL tokenlerini ana ağda nasıl transfer edebilirim?


Ana ağda SPL tokenlerini transfer etmek için bir TypeScript scripti geliştireceğim.

Yani, bu rehbere başvurdum ve bu betiği geliştirdim ama bu işe yaramadı.

Bu kodu kontrol edebilir misin ve bana neyin yanlış olduğunu söyleyebilir misin?

import bs58 from "bs58";
import {
  LAMPORTS_PER_SOL,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
  Connection,
  PublicKey,
} from "@solana/web3.js";
import {
  getOrCreateAssociatedTokenAccount,
  createTransferInstruction,
  getAssociatedTokenAddressSync,
} from "@solana/spl-token";

dotenv.config();

// Keys from .env
const fromWalletPrivateKeyString = process.env.SENDER_WALLET_PRIVATE_KEY as string;
const receiverPublicKeyString = process.env.RECEIVER_WALLET_PUBLIC_KEY as string;

const fromWallet = Keypair.fromSecretKey(bs58.decode(fromWalletPrivateKeyString));
const toWalletPublicKey = new PublicKey(receiverPublicKeyString);

// Token Mint Account Address
const mint = new PublicKey("7WphEnjwKtWWMbb7eEVYeLDNN2jodCo871tVt8jHpump"); // Mainnet

(async () => {
  const connection = new Connection("https://api.mainnet-beta.solana.com");

  // Get or create the fromTokenAccount
  const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
    connection,
    fromWallet,
    mint,
    fromWallet.publicKey
  );

  // Get or create the toTokenAccount
  const toTokenAccount = await getOrCreateAssociatedTokenAccount(
    connection,
    fromWallet,
    mint,
    toWalletPublicKey
  );

  console.log("From Token Account:", fromTokenAccount.address.toString());
  console.log("To Token Account:", toTokenAccount.address.toString());

  // Add token transfer instructions to transaction
  const transaction = new Transaction().add(
    createTransferInstruction(
      fromTokenAccount.address,
      toTokenAccount.address,
      fromWallet.publicKey,
      100 * LAMPORTS_PER_SOL // Adjust the amount based on token decimals
    )
  );

  // Sign transaction, broadcast, and confirm
  const transactionSignature = await sendAndConfirmTransaction(
    connection,
    transaction,
    [fromWallet]
  );

  console.log(
    "Transaction Signature: ",
    `https://solscan.io/tx/${transactionSignature}`
  );
})();

Cevap #1

Bu kod yanlış.

LAMPORTS_PER_SOL 1,000,000,000'dur.

const transaction = new Transaction().add(
    createTransferInstruction(
      fromTokenAccount.address,
      toTokenAccount.address,
      fromWallet.publicKey,
      100 * LAMPORTS_PER_SOL // Adjust the amount based on token decimals
    )
);

100 token göndermek için, 100'ü token ondalık sayılarıyla çarpmanız gerekir. Token ondalık sayılarını almak istiyorsanız, @solana/spl-token içindeki getMint() fonksiyonunu kullanabilirsiniz.

Umarım bu sana yardımcı olur.

Teşekkürler.