Transfer

import { Connection, Keypair, Transaction, SystemProgram, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
import * as bs58 from "bs58";

// connection
const connection = new Connection("https://api.devnet.solana.com");

// 5YNmS1R9nNSCDzb5a7mMJ1dwK9uHeAAF4CmPEwKgVWr8
const feePayer = Keypair.fromSecretKey(
  bs58.decode("588FU4PktJWfGfxtzpAAXywSNt74AvtroVzGfKkVN1LwRuvHwKGr851uH8czM5qm4iqLbs1kKoMKtMJG4ATR7Ld2")
);

// G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoY
const alice = Keypair.fromSecretKey(
  bs58.decode("4NMwxzmYj2uvHuq8xoqhY8RXg63KSVJM1DXkpbmkUY7YQWuoyQgFnnzn6yo3CMnqZasnNPNuAT2TLwQsCaKkUddp")
);

(async () => {
  let tx = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: alice.publicKey,
      toPubkey: new PublicKey("4MWwxzWsWmHrsbfPFwE6LDq471nqNeNMsD6DS7y8nruw"),
      lamports: 1 * LAMPORTS_PER_SOL,
    })
  );
  tx.feePayer = feePayer.publicKey;

  let txhash = await connection.sendTransaction(tx, [feePayer, alice]);
  console.log(`txhash: ${txhash}`);
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  1. this tx do alice transfer 1 SOL to 4MWwxzWsWmHrsbfPFwE6LDq471nqNeNMsD6DS7y8nruw and fee payer is feePayer
  2. tx.feePayer = feePayer.publicKey; is dispensable. If you don't make it explicit, the first signer of this tx will be treated as a fee payer.
Last Updated: 9/23/2022, 6:56:02 PM
Contributors: Yihau Chen