1

I am using @solana/spl-token to create a minting account on a react typescript application. When the following code work to send a normal transaction, the instruction SystemProgram.createAccount, once send to Phantom wallet, and validated send back an error to the app:

   at StandardWalletAdapter.sendTransaction (chunk-NDJPXGNU.js?v=e601c535:1838:15)
   at async chunk-NDJPXGNU.js?v=e601c535:2448:12
   at async createMintAddress (MintSolana.tsx:43:31)

Here is my React typescript component code:

import { useWallet } from "@solana/wallet-adapter-react";
import SolanaWalletButton from "../../elements/SolanaWalletButton/SolanaWalletButton";
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { clusterApiUrl, Connection, Transaction, SystemProgram, Keypair } from '@solana/web3.js';
import { useState } from "react";

const MintSolana = () => {

    const { publicKey, sendTransaction, connected, signTransaction } = useWallet();
    const [error, setError] = useState('');

    const createMintAddress = async () => {

        if (!connected || !publicKey || !signTransaction) {
            console.log('Wallet is not connected');
            return;
        }

        const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
        const MINT_SIZE = 82;

        // Get the minimum balance required for the mint account to be rent-exempt
        const rentExempt = await connection.getMinimumBalanceForRentExemption(MINT_SIZE);

        // Create a transaction to fund the mint account and create the mint
        let transaction = new Transaction().add(
            SystemProgram.createAccount({
                fromPubkey: publicKey,
                newAccountPubkey: Keypair.generate().publicKey,  // Create a new keypair for the mint account
                lamports: rentExempt,
                space: MINT_SIZE,
                programId: TOKEN_PROGRAM_ID,
            })
        );

        const {
            value: { blockhash, lastValidBlockHeight }
        } = await connection.getLatestBlockhashAndContext();

        try {
            const signature = await sendTransaction(transaction, connection)
            await connection.confirmTransaction({ blockhash, lastValidBlockHeight, signature });
            console.log("TX CONFIRMED!", signature);
        } catch (error: any) {
            setError(error);
            console.error(error);
        };
    };


    return <>
        <div>Mint on Solana</div>
        {connected ? (
            <>
                {error && <div>Error! {error.toString()}</div>}
                <div><button onClick={createMintAddress}>Create Mint Address</button></div>
            </>
        ) : (
            <SolanaWalletButton></SolanaWalletButton>
        )}

    </>;
}

export default MintSolana;

Thanks for your time.

Regards, Alex

1 Answer 1

1

You need to sign with the keypair of the new account, but this code just throws away the new keypair. You can instead do:

        let newAccount = Keypair.generate();
        // Create a transaction to fund the mint account and create the mint
        let transaction = new Transaction().add(
            SystemProgram.createAccount({
                fromPubkey: publicKey,
                newAccountPubkey: newAccount.publicKey,  // Create a new keypair for the mint account
                lamports: rentExempt,
                space: MINT_SIZE,
                programId: TOKEN_PROGRAM_ID,
            })
        );

And then later use that keypair to sign:

            const signature = await sendTransaction(transaction, connection, { signers: [newAccount] })

Assuming that I've understood how this version of sendTransaction works, using these props: https://github.com/anza-xyz/wallet-adapter/blob/78c74624e497780864533a00d6e07e965a6d8f4d/packages/core/base/src/adapter.ts#L23

1
  • Thanks a lot! I will try this and give some feedback.
    – Asalvatore
    Commented Apr 2 at 19:40

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.