LoginSignup
1
2

More than 1 year has passed since last update.

【Solana, TypeScript】自分のウォレットでSPLトークンを作成する

Last updated at Posted at 2022-03-21

内容

Solana Token Programの公式ドキュメントを見ると、新しく作成したキーペアを使ってSPLトークンを発行する方法は書いてあったのだが、自分のPhantomウォレットなどを使って発行する方法が分からなかったので、メモとして書き残しておく。

動作確認環境

  • M1 Mac (macOS Monterey 12.3)
  • @solana/web3.js : 1.36.0
  • @solana/spl-token : 0.2.0

コード

connection変数およびwallet変数は@solana/wallet-adapterで取得したもの。
トークンのnameやsymbolといったメタデータは登録していないので、Phantomウォレットで見ると表示名がmintアドレスになっているはず。

sample.ts
import * as spl from '@solana/spl-token';
import * as web3 from '@solana/web3.js';

const createSplToken = async (decimals: number, amount: number) => {
  if (!wallet.publicKey) {
    console.log('Please connect a wallet before.');
    return;
  }

  // Generate/Get keys
  const mint = web3.Keypair.generate();
  const associatedTokenAddress = await spl.getAssociatedTokenAddress(
    mint.publicKey,
    wallet.publicKey
  );

  // Transaction for creating NFT
  const transaction = new web3.Transaction().add(
    // create mint account
    web3.SystemProgram.createAccount({
      fromPubkey: wallet.publicKey,
      newAccountPubkey: mint.publicKey,
      space: spl.MINT_SIZE,
      lamports: await spl.getMinimumBalanceForRentExemptMint(connection),
      programId: spl.TOKEN_PROGRAM_ID,
    }),
    // init mint account
    spl.createInitializeMintInstruction(
      mint.publicKey, // mint pubkey
      decimals, // decimals
      wallet.publicKey, // mint authority
      wallet.publicKey // freeze authority
    ),
    // create associated-token-account
    spl.createAssociatedTokenAccountInstruction(
      wallet.publicKey, // payer
      associatedTokenAddress, // associated-token-address
      wallet.publicKey, // owner
      mint.publicKey // mint
    ),
    // mint to
    spl.createMintToInstruction(
      mint.publicKey, // mint
      associatedTokenAddress, // receiver
      wallet.publicKey, // mint authority
      amount // amount
    )
  );

  // Transaction実行
  console.log('sending a transaction...');
  const signature = await wallet.sendTransaction(transaction, connection, {
    signers: [mint],
  });
  await connection.confirmTransaction(signature, 'processed');
  console.log('successfully create SPL tokens');
};

// How to use (Create and mint 100 new tokens)
const decimals = 9;
const amount = 100 * 10 ** decimals;
await createSplToken(decimals, amount);

メタデータを登録するには

発行したSPLトークンのnameやsymbol等がPhantomウォレットで表示されるようになるには、"Solana Token Registry"というものに登録される必要があるらしい。以下のGitHubリポジトリにプルリクを送ってマージされないといけないようだが、実際に試したことはないので詳しいことは分からない。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2