简体   繁体   中英

TypeError: x.pubkey.toBase58 is not a function

I am using @solana/spl-token library to create and mint tokens. The createMint() function is throwing an error.

TypeError: x.pubkey.toBase58 is not a function
    at /home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+web3.js@1.66.2/node_modules/@solana/web3.js/lib/index.cjs.js:1565:23
    at Array.sort (<anonymous>)
    at Transaction.compileMessage (/home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+web3.js@1.66.2/node_modules/@solana/web3.js/lib/index.cjs.js:1553:17)
    at Transaction._compile (/home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+web3.js@1.66.2/node_modules/@solana/web3.js/lib/index.cjs.js:1660:26)
    at Transaction.sign (/home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+web3.js@1.66.2/node_modules/@solana/web3.js/lib/index.cjs.js:1765:26)
    at Connection.sendTransaction (/home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+web3.js@1.66.2/node_modules/@solana/web3.js/lib/index.cjs.js:6734:21)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async sendAndConfirmTransaction (/home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+web3.js@1.66.2/node_modules/@solana/web3.js/lib/index.cjs.js:2219:21)
    at async mintTo (file:///home/denimcodes/Projects/solpay/node_modules/.pnpm/@solana+spl-token@0.3.6_@solana+web3.js@1.66.2/node_modules/@solana/spl-token/lib/esm/actions/mintTo.js:23:12)
    at async file:///home/denimcodes/Projects/solpay/main.mjs:40:1

Here I have posted full code

import {
  getOrCreateAssociatedTokenAccount,
  mintTo,
  getAccount,
  getMint,
  createMint,
} from "@solana/spl-token";
import {
  Keypair,
  Connection,
  clusterApiUrl,
  LAMPORTS_PER_SOL,
} from "@solana/web3.js";

const payer = Keypair.generate();
const mintAuthority = payer;
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

const airdropSignature = await connection.requestAirdrop(
  payer.publicKey,
  LAMPORTS_PER_SOL
);
await connection.confirmTransaction(airdropSignature);

const mint = await createMint(
  connection,
  payer,
  mintAuthority.publicKey,
  mintAuthority.publicKey,
  9
);

const tokenAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  payer,
  mint,
  mintAuthority.publicKey
);

await mintTo(
  connection,
  payer,
  mint,
  tokenAccount,
  mintAuthority,
  100 * (10 ^ 9)
);

const mintInfo = await getMint(connection, mint);
console.log(`Mint supply: ${mintInfo.supply}`);

const tokenAccountInfo = await getAccount(connection, tokenAccount);
console.log(`Token account amount: ${tokenAccountInfo.amount}`);

x.pubkey.toBase58 is not a function generally throws when one of the PublicKeys getting passed into an instruction (as a parameter, such as with mintTo) is either not an actual PublicKey or is a malformed PublicKey, as defined in @solana/web3.js .

In this case, you are passing in the response from getOrCreateAssociatedTokenAccount to the mintTo call, as seen below:

const **tokenAccount** = await *getOrCreateAssociatedTokenAccount*(
  connection,
  payer,
  mint,
  mintAuthority.publicKey
);

await mintTo(
  connection,
  payer,
  mint,
  **tokenAccount**,
  mintAuthority,
  100 * (10 ^ 9)
);

Your issue is that the response from getOrCreateAssociatedTokenAccount does not return just a PublicKey, but rather an Account . So therefore, token account is an invalid PublicKey object.

To fix this, you just need to dereference the token account address, like so

const **tokenAccount** = await *getOrCreateAssociatedTokenAccount*(
  connection,
  payer,
  mint,
  mintAuthority.publicKey
);

await mintTo(
  connection,
  payer,
  mint,
  **tokenAccount.address**,
  mintAuthority,
  100 * (10 ^ 9)
);

Good luck on your Solana journey:) If you want an extra challenge, create a single transaction that contains all of those instructions so that it completes atomically (Mint account created, token account created, and token minted all in one). You should be able to locate that information on the links provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM