1

I want to fund another user's wallet (let's say I have their public wallet address in my code) from my phantom wallet using a Flutter app.

Now I can use the /connect deeplink of Phantom in a Flutter app, but how do I create a transaction for transferring funds to another wallet in Flutter and then execute it through Phantom's /signAndSendTransaction? I know I need to use the Cryptoplease package, but I don't know how to do it exactly.

2 Answers 2

2

You can use my package for flutter flutter_phantom

    Future<web3.Transaction> createTransactionTransfer() async {
  final transaction = web3.Transaction(
      feePayer: phantom.phantomWalletPublicKey,
      recentBlockhash: (await connection.getLatestBlockhash()).blockhash);
  transaction.add(
    SystemProgram.transfer(
      fromPublicKey: phantom.phantomWalletPublicKey,
      toPublicKey: phantom.phantomWalletPublicKey,
      lamports: web3.solToLamports(1),
    ),
  );
  return transaction;
}

 void signAndSendTransaction() async {
  try { 
    web3.Transaction transaction = await createTransactionTransfer();

    web3.Buffer transactionSerialize = transaction
        .serialize(const web3.SerializeConfig(requireAllSignatures: false));

    final url = phantom.generateSignAndSendTransactionUri(
        transaction: transactionSerialize,
        redirect: "onSignAndSendTransaction");
  
    launchUrl(
      url,
      mode: LaunchMode.externalNonBrowserApplication,
    );
  } catch (e) {
  print(e)
  }
}

full example of how to use this package (Android,Ios) flutter_phantom-demo.

1

I would recommend following the merigolabs's Solana DART API.

Here's an example how to create a transaction to transfer SOL from 1 wallet to another.

// Create a System Program instruction to transfer 1 SOL from [address1] to [address2].
final transaction = web3.Transaction();
transaction.add(
    SystemProgram.transfer(
        fromPublicKey: address1, 
        toPublicKey: address2, 
        lamports: web3.solToLamports(1),
    ),
);

You will additionally have to serialise this transaction and provide it as payload to the Phantom deeplink as a parameter, as mentioned here.

Hope this helps.

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.