Solana React TypeError: this.wallet.signTransaction is not a function

Now I'm going to connect solana and React.js. And I tried to get wallet like this.

import { useConnection, useWallet } from '@solana/wallet-adapter-react';
....
function Header(props) {
 const wallet = useWallet();
    const initialize = async () => {
        const provider = await getProvider()
        const program = new Program(contractJson, programID, provider);
        console.log('program', program)
        try {
            /* interact with the program via rpc */
            await program.rpc.initialize({
                accounts: {
                    myOntology: provider.wallet.publicKey,
                    user: provider.wallet.publicKey,
                    systemProgram: SystemProgram.programId,
                    rent: anchor.web3.SYSVAR_RENT_PUBKEY,
                    clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
                },
            });
        } catch (err) {
            console.log("Transaction error: ", err);
        }

    }
}

when I call initialize method, I got this error. TypeError: this.wallet.signTransaction is not a function So I've logged about wallet and signTransaction is null. enter image description here

How can I solve this problem?

Upvotes: 2

Views: 4627

Answers (2)

harkl
harkl

Reputation: 901

You're using an anchor rpc program method, so you need to invoke the useAnchorWallet method.

import { useAnchorWallet } from "@solana/wallet-adapter-react";

const wallet = useAnchorWallet();

If you look at the useAnchorWallet method it contains an interface which expects a Transaction as an input:

signTransaction(transaction: Transaction): Promise<Transaction>;

The standard wallet adapter has the same method namespace but has a different interface:

signTransaction: SignerWalletAdapterProps['signTransaction'] | undefined;

Upvotes: 2

yangli-io
yangli-io

Reputation: 17334

The provider you pass into Program needs to incorporate your wallet

const wallet = useWallet();
const connection = new Connection('<devnet url here>')

const provider = new anchor.Provider(connection, wallet, anchor.Provider.defaultOptions()) 

const program = new Program(contractJson, programID, provider);

Upvotes: 2

Related Questions