2

Almost all of the tutorial and materials online/guide uses keypair, which is not practical

0

1 Answer 1

2

The transaction process remains consistent with the examples you've seen in tutorials. The key distinction lies in how the transaction is signed and sent.

Here's an example in React using the WalletAdapter

import { WalletNotConnectedError } from "@solana/wallet-adapter-base";
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import { Keypair, SystemProgram, Transaction } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID, createTransferInstruction } from "@solana/spl-token";
import React, { FC, useCallback } from "react";

export const SendSPLTokenToAddress: FC = (
  fromTokenAccount,
  toTokenAccount,
  fromWallet
) => {
  const { connection } = useConnection();
  const { publicKey, sendTransaction } = useWallet();

  const onClick = useCallback(async () => {
    if (!publicKey) throw new WalletNotConnectedError();

    const transaction = new Transaction().add(
      createTransferInstruction(
        fromTokenAccount.address,
        toTokenAccount.address,
        fromWallet.publicKey,
        1,
        [],
        TOKEN_PROGRAM_ID
      )
    );

    const signature = await sendTransaction(transaction, connection);

    await connection.confirmTransaction(signature, "processed");
  }, [publicKey, sendTransaction, connection]);

  return (
    <button onClick={onClick} disabled={!publicKey}>
      Send 1 lamport to a random address!
    </button>
  );
};

If you're following any tutorial, replace sendAndConfirmTransaction or sendRawTransaction, as shown in the tutorial, with the sendTransaction from the useWallet hook.

3
  • your code is for sol transfer, im specifically looking for spl token, not able to get past creating associated token accounts Commented Apr 22 at 23:53
  • This is actually for SPL tokens. This question was for using the WalletAdapter. So you can ask another question on the issue you're having with sending SPL tokens
    – C.OG
    Commented Apr 24 at 9:15
  • You can also take a look at this: solana.stackexchange.com/questions/5149/…
    – C.OG
    Commented Apr 24 at 9:16

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.