// for external wallets only, use ITurnkeySolanaSigner for Solana embedded wallets
import { ISolana } from '@dynamic-labs/solana';

const { primaryWallet } = useDynamicContext();

import { Connection } from '@solana/web3.js';

const connection: Connection = await (
    primaryWallet as any
).connector.getConnection();

if (!connection) return;

const fromKey = new PublicKey(primaryWallet.address);
const toKey = new PublicKey(address);

const amountInLamports = Number(amount) * 1000000000;
const transferTransaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: fromKey,
    lamports: amountInLamports,
    toPubkey: toKey,
  }),
);
const blockhash = await connection.getLatestBlockhash();
transferTransaction.recentBlockhash = blockhash.blockhash;
transferTransaction.feePayer = fromKey;

const signer = await (primaryWallet as any).connector.getSigner<ISolana>();

await signer
  .signAndSendTransaction({ transaction: transferTransaction })
  .then((res: any) => {
    console.log(
      `Transaction successful: https://solscan.io/tx/${res}?cluster=devnet`,
    );
  })
  .catch((reason: any) => {
    console.error(reason);
  });