1

I'm struggling to find any working examples that are not deprecated. If anyone can direct me towards any that would be great.

For learning purposes I want to setup a button that will send 1 Solana from a connected wallet to a user-specified recipient address.

Any help appreciated, thank you.

2

2 Answers 2

2

To send SOL with a button, here's an example using the dapp-scaffold:

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

export const SendLamportsToRandomAddress: FC = () => {
  const { connection } = useConnection();
  const { publicKey, sendTransaction } = useWallet();

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

    const transaction = new Transaction().add(
      SystemProgram.transfer({
        fromPubkey: publicKey,
        toPubkey: Keypair.generate().publicKey,
        lamports: 1_000_000,
      })
    );

    const signature = await sendTransaction(transaction, connection);

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

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

Solana's Wallet Adapter for React has multiple starters on the Github Repo; you can use that and the example on the README to connect a wallet and create that button that sends a transaction.

There's also other packages for other frontend frameworks made by the community:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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