Solana でトークンのロジックをテストするには、本当にテストしたい転送ロジックに取りかかる前に、mint、token account、authority をセットアップする必要があります。@solana-program/token の tokenProgram() プラグインを使えば、このセットアップを数回のメソッド呼び出しにまとめられ、ATA の作成も自動で処理されます。
npm で @solana-program/token を見る
インストール
npm install @solana/kit @solana/kit-plugin-litesvm @solana/kit-plugin-signer @solana-program/token
Mint の作成
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 が存在しない場合は、同じトランザクション内で作成されます。
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 は、必要に応じて自動的に作成されます。
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 アドレスを導出し、デコード済みのアカウントを取得します。
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 です。
// USDC has 6 decimals
const oneUSDC = 10n ** 6n; // 1_000_000n
// SOL has 9 decimals
const oneSOL = 10n ** 9n; // 1_000_000_000n
トークンの数量には必ず BigInt を使用してください。JavaScript の Number は 2^53 を超えると精度が失われます。
プラグインリファレンス
よくあるエラー