> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# トークンのテスト

> @solana-program/token と LiteSVM を使って SPL トークンをテストする

Solana でトークンのロジックをテストするには、本当にテストしたい転送ロジックに取りかかる前に、mint、token account、authority をセットアップする必要があります。`@solana-program/token` の `tokenProgram()` プラグインを使えば、このセットアップを数回のメソッド呼び出しにまとめられ、ATA の作成も自動で処理されます。

[npm で `@solana-program/token` を見る](https://www.npmjs.com/package/@solana-program/token)

## インストール

```bash
npm install @solana/kit @solana/kit-plugin-litesvm @solana/kit-plugin-signer @solana-program/token
```

## Mint の作成

```typescript
import { createClient, generateKeyPairSigner, lamports } from "@solana/kit";
import { litesvm } from "@solana/kit-plugin-litesvm";
import { signer } from "@solana/kit-plugin-signer";
import { tokenProgram } from "@solana-program/token";

const mySigner = await generateKeyPairSigner();
const client = createClient()
  .use(signer(mySigner))
  .use(litesvm())
  .use(tokenProgram());

client.svm.airdrop(client.payer.address, lamports(10_000_000_000n));

const mintAuthority = await generateKeyPairSigner();
const newMint = await generateKeyPairSigner();

await client.token.instructions
  .createMint({
    newMint,
    decimals: 9,
    mintAuthority: mintAuthority.address
  })
  .sendTransaction();

// Verify
const mintAccount = await client.token.accounts.mint.fetch(newMint.address);
console.log(mintAccount.data.supply); // 0n
console.log(mintAccount.data.decimals); // 9
```

## トークンのミント

`mintToATA` は、所有者の Associated Token Account にトークンを発行します。ATA が存在しない場合は、同じトランザクション内で作成されます。

```typescript
await client.token.instructions
  .mintToATA({
    mint: newMint.address,
    owner: client.payer.address,
    mintAuthority,
    amount: 1_000_000_000_000n, // 1000 tokens (9 decimals)
    decimals: 9
  })
  .sendTransaction();
```

`decimals` パラメータは安全のためのチェックです。mint の decimals と完全に一致していないと、Token program は `Custom program error: #18`(`0x12`、`MintDecimalsMismatch`)でトランザクションを拒否します。これにより、誤って 1000 倍ものトークンをミントしてしまう事態を防げます。

## トークンの転送

`transferToATA` はユーザー間でトークンを移動します。受取人の ATA は、必要に応じて自動的に作成されます。

```typescript
const alice = await generateKeyPairSigner();
const bob = await generateKeyPairSigner();

// Mint to Alice
await client.token.instructions
  .mintToATA({
    mint: newMint.address,
    owner: alice.address,
    mintAuthority,
    amount: 1_000_000_000_000n,
    decimals: 9
  })
  .sendTransaction();

// Transfer 400 tokens from Alice to Bob
await client.token.instructions
  .transferToATA({
    mint: newMint.address,
    authority: alice,
    recipient: bob.address,
    amount: 400_000_000_000n,
    decimals: 9
  })
  .sendTransaction();
```

`authority` は、転送元トークンを所有する署名者です。プラグインは、転送元と転送先の両方の ATA を、それぞれのアドレスと mint から導出します。

## 残高の確認

トークン残高を読み取るには、`findAssociatedTokenPda` で ATA アドレスを導出し、デコード済みのアカウントを取得します。

```typescript
import {
  findAssociatedTokenPda,
  TOKEN_PROGRAM_ADDRESS
} from "@solana-program/token";

const [aliceAta] = await findAssociatedTokenPda({
  owner: alice.address,
  mint: newMint.address,
  tokenProgram: TOKEN_PROGRAM_ADDRESS
});

const aliceAccount = await client.token.accounts.token.fetch(aliceAta);
console.log(aliceAccount.data.amount); // 600_000_000_000n
console.log(aliceAccount.data.mint); // newMint.address
console.log(aliceAccount.data.owner); // alice.address
```

## Decimals(小数桁数)

トークンの数量は常に最小単位(raw units)で表します。decimals が 9 の 1000 トークンは `1000n` ではなく `1_000_000_000_000n` です。

```typescript
// USDC has 6 decimals
const oneUSDC = 10n ** 6n; // 1_000_000n

// SOL has 9 decimals
const oneSOL = 10n ** 9n; // 1_000_000_000n
```

<Warning>
  トークンの数量には必ず `BigInt` を使用してください。JavaScript の `Number` は 2^53 を超えると精度が失われます。
</Warning>

## プラグインリファレンス

| メソッド                                      | 説明                                                  |
| ------------------------------------------- | ------------------------------------------------------------ |
| `client.token.instructions.createMint()`    | 新しい SPL トークンの mint を作成します                                  |
| `client.token.instructions.mintToATA()`     | 所有者の ATA にトークンをミントします(必要に応じて ATA を作成)        |
| `client.token.instructions.transferToATA()` | 受取人の ATA にトークンを転送します(必要に応じて ATA を作成) |
| `client.token.accounts.mint.fetch()`        | mint account を取得してデコードします                              |
| `client.token.accounts.token.fetch()`       | token account を取得してデコードします                             |

## よくあるエラー

| エラー                                                               | 原因                             | 対処方法                                                    |
| ------------------------------------------------------------------- | --------------------------------- | ------------------------------------------------------ |
| `Custom program error: #18`(`0x12`、`MintDecimalsMismatch`)        | `decimals` が mint と一致していません | `createMint` に渡したものと同じ decimals の値を使用してください |
| `Account not found at address: ...`                                 | アカウントがまだ存在しません         | 取得またはミントを行う前に、mint が作成されていることを確認してください  |
| `Attempt to debit an account but found no record of a prior credit` | 手数料を支払うための SOL を支払者が持っていません  | 先に支払者へ SOL をエアドロップしてください                         |
| `Custom program error: #4`(`OwnerMismatch`)                       | 署名している `authority` が、転送元の token account の所有者ではありません | 実際の所有者を `authority` として渡してください     |
