Przegląd
Transfery kanałowe to standardowe transfery tokenów SPL przesyłane do bramy — nie istnieje dedykowana instrukcja transferu Private Channels. Brama kieruje transakcję do węzła zapisu, który wykonuje ją na warstwie kont off-chain kanału. Transfery są potwierdzane w momencie przetworzenia ich przez sekwencer, a nie po potwierdzeniu bloku Solana, i nie pojawiają się w sieci głównej.
Zmień RPC portfela
Przed wysłaniem transferu zmień punkt końcowy RPC swojego portfela na adres URL bramy zamiast Solana Devnet:
http://localhost:8899
Transakcje wysłane do publicznego RPC Solana trafią do sieci głównej, a nie do kanału.
Uwierzytelnianie
Jeśli uruchomiłeś stos bez --profile auth (domyślna konfiguracja devnet),
brama działa w trybie otwartym i token nie jest wymagany —
pomiń tę sekcję.
Gdy usługa Auth jest włączona (JWT_SECRET jest ustawiony na bramie), wszystkie
żądania wymagają tokenu Bearer JWT w nagłówku Authorization:
Authorization: Bearer <JWT_TOKEN>
Zobacz Uwierzytelnianie i role, aby dowiedzieć się, jak zarejestrować się, zalogować i uzyskać token.
Zbuduj i wyślij transfer
Jeśli associated token account odbiorcy nie istnieje jeszcze w kanale,
dodaj instrukcję createAssociatedTokenAccountIdempotent przed transferem.
Poniższy kod obsługuje oba przypadki:
import {address,createSolanaRpc,pipe,createTransactionMessage,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,appendTransactionMessageInstruction,signAndSendTransactionMessageWithSigners,assertIsTransactionMessageWithSingleSendingSigner,getBase58Decoder} from "@solana/kit";import {findAssociatedTokenPda,getCreateAssociatedTokenIdempotentInstruction,getTransferInstruction} from "@solana-program/token";const TOKEN_PROGRAM_ADDRESS = address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");// Point to the gateway, not a public Solana RPCconst privateChannelRpc = createSolanaRpc("http://localhost:8899");// Derive source and destination associated token accountsconst [sourceAta] = await findAssociatedTokenPda({mint: address(mintAddress),owner: address(senderAddress),tokenProgram: TOKEN_PROGRAM_ADDRESS});const [destinationAta] = await findAssociatedTokenPda({mint: address(mintAddress),owner: address(recipientAddress),tokenProgram: TOKEN_PROGRAM_ADDRESS});const transferInstruction = getTransferInstruction({source: sourceAta,destination: destinationAta,authority: transactionSigner,amount: 1_000_000n // 1 USDC (6 decimals)});// Create the destination ATA on the channel if it does not exist yetconst destinationAtaInfo = await privateChannelRpc.getAccountInfo(destinationAta, { encoding: "base64" }).send();const { value: latestBlockhash } = await privateChannelRpc.getLatestBlockhash({ commitment: "confirmed" }).send();// Sent to the Private Channels gateway (not Solana RPC directly), which// expects the legacy transaction format - do not switch this to version 0.const transactionMessage = !destinationAtaInfo.value? pipe(createTransactionMessage({ version: "legacy" }),(m) => setTransactionMessageFeePayerSigner(transactionSigner, m),(m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m),(m) =>appendTransactionMessageInstruction(getCreateAssociatedTokenIdempotentInstruction({payer: transactionSigner,ata: destinationAta,owner: address(recipientAddress),mint: address(mintAddress)}),m),(m) => appendTransactionMessageInstruction(transferInstruction, m)): pipe(createTransactionMessage({ version: "legacy" }),(m) => setTransactionMessageFeePayerSigner(transactionSigner, m),(m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m),(m) => appendTransactionMessageInstruction(transferInstruction, m));assertIsTransactionMessageWithSingleSendingSigner(transactionMessage);const signatureBytes =await signAndSendTransactionMessageWithSigners(transactionMessage);const signature = getBase58Decoder().decode(signatureBytes);console.log("Transfer signature:", signature);
Zweryfikuj transfer
Zapytaj o saldo kanałowe odbiorcy przez bramę, aby potwierdzić, że transfer dotarł:
const [recipientAta] = await findAssociatedTokenPda({mint: address(mintAddress),owner: address(recipientAddress),tokenProgram: TOKEN_PROGRAM_ADDRESS});const balance = await privateChannelRpc.getTokenAccountBalance(recipientAta).send();console.log(balance.value.uiAmountString);
Kolejne kroki
Wypłać środki — wypłać tokeny z powrotem do swojego portfela Solana
Is this page helpful?