user23543214
user23543214

Reputation: 1

How to resolve "Mint needs to be signer to initialize the account" error

I'm working with the Solana blockchain using the @solana/web3.js library and encountering a transaction error when attempting to create or update metadata using the Metaplex token metadata program. The error occurs when I try to send a transaction, resulting in a "SendTransactionError" with a custom program error code 0x86. Here's the relevant error output:

SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x86
    at Connection.sendEncodedTransaction
    at async Connection.sendRawTransaction
    at async Object.sendTransaction
    at async TransactionBuilder.sendAndConfirm
Logs:
- Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s invoke [1]
- Program log: IX: Create
- Program 11111111111111111111111111111111 invoke [2]
- Program 11111111111111111111111111111111 success
- Program log: Mint needs to be signer to initialize the account
- Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s consumed 11404 of 200000 compute units
- Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s failed: custom program error: 0x86

Here's my TypeScript:

import { createV1, updateV1 ,Collection, CreateMetadataAccountV3InstructionAccounts, CreateMetadataAccountV3InstructionDataArgs, Creator, MPL_TOKEN_METADATA_PROGRAM_ID, UpdateMetadataAccountV2InstructionAccounts, UpdateMetadataAccountV2InstructionData, Uses, createMetadataAccountV3, updateMetadataAccountV2, findMetadataPda, CreateV1InstructionAccounts, CreateV1InstructionData, TokenStandard, CollectionDetails, PrintSupply, UpdateV1InstructionData, UpdateV1InstructionAccounts, Data} from "@metaplex-foundation/mpl-token-metadata";
import * as web3 from "@solana/web3.js";
import { PublicKey, createSignerFromKeypair, none, percentAmount, publicKey, signerIdentity, some } from "@metaplex-foundation/umi";
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { fromWeb3JsKeypair, fromWeb3JsPublicKey} from '@metaplex-foundation/umi-web3js-adapters';
import * as bs58 from "bs58";

const SPL_TOKEN_2022_PROGRAM_ID: PublicKey = publicKey(
    'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb'
  );

  
export function loadWalletKey(keypairFile:string): web3.Keypair {
    const fs = require("fs");
    const loaded = web3.Keypair.fromSecretKey(
      new Uint8Array(JSON.parse(fs.readFileSync(keypairFile).toString())),
    );
    return loaded;
  }

const INITIALIZE = true;

async function main(){
    console.log("let's name some token-22 tokens in 2024!");
    const myKeypair = loadWalletKey("RRTigmrq7BxQRZchiYNwh8na3dn9uvApUurBi3Fue8a.json");
    const mint = new web3.PublicKey("DANK5eskRs1TU5pJK73X9RvG8w8wjM9vHA79qEL4poN8");

    const umi = createUmi("https://api.devnet.solana.com");
    const signer = createSignerFromKeypair(umi, fromWeb3JsKeypair(myKeypair))
    umi.use(signerIdentity(signer, true))

    const ourMetadata = { // TODO change those values!
        name: "xxx", 
        symbol: "xxxx",
        uri: "xxxx",
    }
    if(INITIALIZE){
        const onChainData = {
            ...ourMetadata,
            // we don't need that
            sellerFeeBasisPoints: percentAmount(0,2),
            creators: none<Creator[]>(),
            collection: none<Collection>(),
            uses: none<Uses>(),
        }
        const accounts: CreateV1InstructionAccounts = {
            mint: fromWeb3JsPublicKey(mint),
            splTokenProgram: SPL_TOKEN_2022_PROGRAM_ID
        }
        const data: CreateV1InstructionData = {
            ...onChainData,
            isMutable: true,
            discriminator: 0,
            tokenStandard: TokenStandard.Fungible,
            collectionDetails: none<CollectionDetails>(),
            ruleSet: none<PublicKey>(),
            createV1Discriminator: 0,
            primarySaleHappened: true,
            decimals: none<number>(),
            printSupply: none<PrintSupply>(),
        }
        const txid = await createV1(umi, {...accounts, ...data}).sendAndConfirm(umi);
        console.log(bs58.encode(txid.signature))
    } else {
        const onChainData = {
            ...ourMetadata,
            sellerFeeBasisPoints: 0,
            creators: none<Creator[]>(),
            collection: none<Collection>(),
            uses: none<Uses>(),
        }
        const accounts: UpdateV1InstructionAccounts = {
            mint: fromWeb3JsPublicKey(mint),
        }
        const data = {
            discriminator: 0,
            data: some<Data>(onChainData),
            updateV1Discriminator: 0,
        }
        const txid = await updateV1(umi, {...accounts, ...data}).sendAndConfirm(umi);
        console.log(bs58.encode(txid.signature))
    }
}

main();

How do I resolve the "custom program error: 0x86" and correctly sign the transaction with the mint account using @solana/web3.js and the Metaplex library? Any insights or examples on correctly setting up the transaction with the mint account as a signer would be greatly appreciated!

Upvotes: 0

Views: 542

Answers (1)

Jon C
Jon C

Reputation: 8462

Pretty simply, you also need to sign for the mint account that you're passing in.

I'm not sure how you generated the address, but you likely have a keypair file for it somewhere. Once you have that file, you can do the same as with your signer, ie:

const mintSigner = createSignerFromKeypair(umi, fromWeb3JsKeypair(mint))
umi.use(signerIdentity(mintSigner, true))

Upvotes: 0

Related Questions