2022-03-29

How to send solana via my app in vanilla js?

Trying to do a simple send and receive function in Solana with vanilla JS.

Below is my send function that works fine, and now I want a receive function. Where the provider would get Solana transferred from my treasury wallet.

I'm not sure what approach I should have, just starting out. Is there a way to just move things around in this function? Or do I have to have a totally different approach?

Thanks!

async function transferSOL() {
  // Detecing and storing the phantom wallet of the user (creator in this case)
  var provider = phantom;

  // Establishing connection
  var connection = new web3.Connection(
    web3.clusterApiUrl('devnet'),
  );

  var transaction = new web3.Transaction().add(
    web3.SystemProgram.transfer({
      fromPubkey: provider.publicKey,
      toPubkey: treasuryWallet.publicKey,
      lamports: 0.1 * web3.LAMPORTS_PER_SOL - 100  
    }),
  );
  // Setting the variables for the transaction
  transaction.feePayer = await provider.publicKey;
  let blockhashObj = await connection.getRecentBlockhash();
  transaction.recentBlockhash = await blockhashObj.blockhash;

  // Request creator to sign the transaction (allow the transaction)
  let signed = await provider.signTransaction(transaction);
  let signature = await connection.sendRawTransaction(signed.serialize());
  await connection.confirmTransaction(signature);
}


No comments:

Post a Comment