Skip to content

Latest commit

 

History

History

README.md

Transfer Tokens (Anchor)

Note

This is the Anchor v1 copy of this example, on Anchor 1.2.0, the current stable Anchor release. Every anchor command on this page needs the v1 CLI: avm install 1.2.0 && avm use 1.2.0. The Anchor v2 version of this example is in ../anchor.

Transfer tokens between token accounts via CPI to the token program.

See also: Transfer Tokens overview and the repository catalog.

Major concepts

  • Associated token accounts
  • transfer_checked, which carries the mint and decimals through the CPI
  • anchor_spl::token_interface types, so the same program works against both the Classic Token Program and the Token Extensions Program
  • Amounts: mint_token and transfer_tokens take amount in minor units, the raw integer the token program operates on. Clients convert from major units offchain: 1 token with 9 decimals is 1 * 10^9 minor units. The program never scales amounts onchain.

Setup

From this directory (tokens/transfer-tokens/anchor/):

anchor build

Prerequisites: Agave CLI (version in Anchor.toml [toolchain]), Anchor.

Testing

Tests run in-process with LiteSVM. No local validator.

anchor test

This runs cargo test as configured in Anchor.toml. Tests call instruction handlers and check onchain state.

Usage

Read the program programs/ source and Anchor.toml for deployed program IDs. For deployment, use anchor build && anchor deploy against your target cluster.