0

I want to use the below component in a send button in UI.On click of which the logic should execute.

import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import React, { useCallback } from 'react';

interface Props {
  children: (sendTransaction: OnSendTransaction) => React.ReactNode;
}

type OnSendTransaction = (toPublicKey: string, amount: number) => void;

const SendTransaction: React.FC<Props> = ({ children }) => {
  const { connection } = useConnection();
  const { publicKey, signTransaction, sendTransaction } = useWallet();

  const onSendSPLTransaction = useCallback(
    async (toPubkey: string, amount: number) => {
      // Some logic
    },
    [publicKey, sendTransaction, connection]
  );

  return <>{children(onSendSPLTransaction)}</>;
};
    
export default SendTransaction;

I am trying to use the wallet provider and send the spl token to another account on a click. I am able to do so for sol with a normal method but not for spl token. Since there are not many example with spl token transfer this is the only one i found.

I am not sure how to use the SendTransaction component to pass the toPublicKey and amount. I am not well versed in typescript and react.

This is snippet from the answer at: How to transfer custom SPL token by '@solana/web3.js' and '@solana/sol-wallet-adapter'

aroha_blue
  • 33
  • 6
  • You'll need to provide a bit more information here. The most important bit is probably the mint for the SPL token that you'd like to transfer. In the example that you link, they hardcode the SPL token mint with `const mint = new PublicKey('MINT ADDRESS');` -- you'll probably need to add that along with the rest of the example code to create the transfer instruction. – Jon C Mar 10 '22 at 14:34
  • Yes, the mint part is in the commented portion. I want to have a Send button somewhere which will effectively use this component to send the transaction. I am not sure how to use this component. – aroha_blue Mar 11 '22 at 19:07

0 Answers0