How It Works
A payment moves stablecoins from the sender’s token account to the recipient’s token account. If the recipient is receiving this token for the first time, their token account can be created as part of the same transaction.See How Payments Work on Solana for core
payment concepts.
token2022Program() plugin from @solana-program/token-2022 extends your
@solana/kit client with a client.token2022 helper.
Its transferToATA method handles ATA derivation and transaction building
automatically, which is ideal for single payment transfers.
The steps below show the core flow. See the Demo for complete runnable
code.
1
Create the Token Helper
Add the
token2022Program() plugin to your client with
.use(token2022Program()). This exposes client.token2022, whose
instructions provide methods for common token operations.Use token2022Program() for mints owned by the Token-2022 program, or
tokenProgram() from @solana-program/token for mints owned by the original
Token program.2
Send the Payment
Use
transferToATA() to transfer tokens between wallets. The method handles:- ATA Resolution: Automatically derives Associated Token Accounts (ATAs) for the sender and recipient. If the recipient’s ATA doesn’t exist, the instruction to create the account is automatically added to the same transaction.
- Checked Amounts: Pass
amountin base units along with the mint’sdecimals. The transfer is checked against the mint’s decimals onchain (e.g. 250000 base units -> 0.25 tokens, if the mint has 6 decimals) - Transaction Building:
sendTransaction()builds, signs, and sends the transaction, resolving to a result whosecontext.signatureholds the transaction signature
3
Verify Balances
After the transfer completes, read the token balances from the RPC. Derive each
wallet’s ATA with
findAssociatedTokenPda(), then call
getTokenAccountBalance() to fetch the balance for that account.Demo
To run this demo, create a project and install its packages."type": "module"
lets the demo use top-level await:
127.0.0.1:8899 with the
Surfpool CLI and leave it running:
demo.ts and run it with npx tsx demo.ts.
Demo