Metaplex UmiでNFT発行すると「ProgramNotRecognizedError」エラー

現象

実行すると下記のエラーが出る。

metaplex/umi/node_modules/@metaplex-foundation/umi-program-repository/src/createDefaultProgramRepository.ts:51
      throw new ProgramNotRecognizedError(resolvedIdentifier, cluster);
            ^
ProgramNotRecognizedError: The provided program name [splAssociatedToken] is not recognized in the [devnet] cluster. Did you forget to register this program? If so, you may use "context.programs.add(myProgram)" to fix this.

Source: SDK

    at get (metaplex/umi/node_modules/@metaplex-foundation/umi-program-repository/src/createDefaultProgramRepository.ts:51:13)
    at Object.getPublicKey (metaplex/umi/node_modules/@metaplex-foundation/umi-program-repository/src/createDefaultProgramRepository.ts:63:14)
    at findAssociatedTokenPda (metaplex/umi/node_modules/@metaplex-foundation/mpl-toolbox/src/hooked/AssociatedToken.ts:14:22)
    at mintV1 (metaplex/umi/node_modules/@metaplex-foundation/mpl-token-metadata/src/generated/instructions/mintV1.ts:215:58)
    at createAndMint (metaplex/umi/node_modules/@metaplex-foundation/mpl-token-metadata/src/createHelpers.ts:15:16)
    at createNft (metaplex/umi/node_modules/@metaplex-foundation/mpl-token-metadata/src/createHelpers.ts:21:16)
    at createCollection (metaplex/umi/src/createCollection.ts:42:18)
    at Object.<anonymous> (metaplex/umi/src/createCollection.ts:56:1)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1618:23) {
  source: 'sdk',
  sourceDetails: undefined,
  cause: undefined,
  identifier: 'splAssociatedToken',
  cluster: 'devnet'
}

ソース

// Lib
import * as dotenv from 'dotenv';

// Metaplex
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import {
  keypairIdentity,
  generateSigner,
  percentAmount,
} from '@metaplex-foundation/umi';
import {
  createNft,
} from '@metaplex-foundation/mpl-token-metadata';

const createCollection = async () => {
  // ----------------------------------------------------
  //  Setup
  // ----------------------------------------------------
  dotenv.config();

  const endpoint = 'https://api.devnet.solana.com';
  const umi = createUmi(endpoint);

  // Set Payer
  const payerSecretKey = process.env.PAYER_SECRET_KEY;
  if (!payerSecretKey) throw new Error('payerSecretKey not found.');

  const secretKeyUInt8Array = new Uint8Array(JSON.parse(payerSecretKey));
  const payerKeypair =
    umi.eddsa.createKeypairFromSecretKey(secretKeyUInt8Array);

  umi.use(keypairIdentity(payerKeypair));

  // -------------------------------------
  //  Create Collection NFT
  // -------------------------------------
  const collectionUpdateAuthority = generateSigner(umi);
  const collectionMint = generateSigner(umi);
  await createNft(umi, {
    mint: collectionMint,
    authority: collectionUpdateAuthority,
    name: 'My Collection NFT',
    uri: 'https://arweave.net/yfVoS8kmFiM_XjfZOETgdCfrByKDyheSJ20nyam8_ag',
    sellerFeeBasisPoints: percentAmount(9.99, 2), // 9.99%
    isCollection: true,
  }).sendAndConfirm(umi);

  console.log('payer =>', payerKeypair.publicKey.toString());
  console.log(
    'collectionUpdateAuthority =>',
    collectionUpdateAuthority.publicKey.toString()
  );
  console.log('collectionMint =>', collectionMint.publicKey.toString());
};

createCollection();

原因

NFT発行に必要なプログラム(Programs)の指定が漏れている。

対応

直接NFT発行する場合

mplTokenMetadata を追加する。

import {
  mplTokenMetadata,
  createNft,
} from '@metaplex-foundation/mpl-token-metadata';

 const umi = createUmi(endpoint).use(mplTokenMetadata());

CandyMachineを使う場合

mplCandyMachine を追加する。

import {
  mplCandyMachine,
  createNft,
} from '@metaplex-foundation/mpl-token-metadata';

 const umi = createUmi(endpoint).use(mplCandyMachine());