使用代币程序转移 SPL 代币。为了发送 SPL 代币,您需要知道其 SPL 代币账户地址。您可以使用以下示例获取地址并发送代币。
import {
Connection,
clusterApiUrl,
Keypair,
LAMPORTS_PER_SOL,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
createMint,
getOrCreateAssociatedTokenAccount,
mintTo,
createTransferInstruction,
} from "@solana/spl-token";
(async () => {
// Connect to cluster
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
// Generate a new wallet keypair and airdrop SOL
const fromWallet = Keypair.generate();
const fromAirdropSignature = await connection.requestAirdrop(
fromWallet.publicKey,
LAMPORTS_PER_SOL,
);
// Wait for airdrop confirmation
await connection.confirmTransaction(fromAirdropSignature);
// Generate a new wallet to receive newly minted token
const toWallet = Keypair.generate();
// Create new token mint
const mint = await createMint(
connection,
fromWallet,
fromWallet.publicKey,
null,
9,
);
// Get the token account of the fromWallet Solana address, if it does not exist, create it
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWallet,
mint,
fromWallet.publicKey,
);
//get the token account of the toWallet Solana address, if it does not exist, create it
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWallet,
mint,
toWallet.publicKey,
);
// Minting 1 new token to the "fromTokenAccount" account we just returned/created
await mintTo(
connection,
fromWallet,
mint,
fromTokenAccount.address,
fromWallet.publicKey,
1000000000, // it's 1 token, but in lamports
[],
);
// Add token transfer instructions to transaction
const transaction = new Transaction().add(
createTransferInstruction(
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
1,
),
);
// Sign transaction, broadcast, and confirm
await sendAndConfirmTransaction(connection, transaction, [fromWallet]);
})();
https://solana.com/zh/developers/cookbook/transactions/send-tokens
版权属于:区块链中文技术社区 / 转载原创者
本文链接:https://www.bcskill.com/index.php/archives/2289.html
相关技术文章仅限于相关区块链底层技术研究,禁止用于非法用途,后果自负!本站严格遵守一切相关法律政策!
楼主残忍的关闭了评论