0

I'm using vue.js, solana-wallets-vue and @solana/spl-token to create a mint for the connected user. I want to createMint and set the connected wallet as payer.

  import * as web3 from '@solana/web3.js'
  import * as token from '@solana/spl-token'
  import { useWallet } from 'solana-wallets-vue'

  const connection = new web3.Connection(
    web3.clusterApiUrl('devnet'),
    'confirmed',
  )
  
  const { publicKey } = useWallet() // public key of the logged user  

  const tokenMint = await token.createMint(
    connection,
    payer, 
    publicKey, // mintAuthority,
    null, // freezAuhtority,
    2
 )

Do I need to generate a keypair for this transaction? Or Can I set the payer as the user logged in?

2 Answers 2

1

If you're connecting to a wallet, then you won't have access to the Signer, since that's protected by the wallet. I don't know what interface solana-wallets-vue exposes, but typically it looks like:

      const connection = new web3.Connection(
        web3.clusterApiUrl('devnet'),
        'confirmed',
      )
  
      const { publicKey } = useWallet() // public key of the logged user  
      
      // generate a new keypair for the mint
      const mint = web3.Keypair.generate();
      // get the required lamports for rent-exemption on the mint
      const lamports = await connection.getMinimumBalanceForRentExemption(token.MINT_SIZE);
      // create the transaction
      const transaction = new Transaction().add(
        // first instruction allocates the mint account and assigns it to the token program
        web3.SystemProgram.createAccount({
            fromPubkey: publicKey,
            newAccountPubkey: mint.publicKey,
            space: token.MINT_SIZE,
            lamports,
            programId: token.TOKEN_PROGRAM_ID,
        }),
        // initialize the account as a mint
        token.createInitializeMintInstruction(mint, 2, publicKey)
    );
    // account creation requires a signature from the account being created
    transaction.partialSign(mint);
    // call whatever sign-and-send-transaction API on the wallet 
0

just do this :

const tokenMint = await token.createMint(
connection,
publicKey, 
publicKey, // mintAuthority,
null, // freezAuhtority,
2)

please note that if the connected wallet is the payer he needs to sign for the transaction

2
  • The second argument of token.createMint() is a Keypair not a publicKey. So I can't do this.
    – Jrb
    Nov 24, 2023 at 23:53
  • i'm pretty sure it's a publickKey, i've never seen a keypair as an argument, except for signing, try it ;)
    – Effe2
    Nov 25, 2023 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.