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. 這個交易是alice轉帳 1 SOL 給4MWwxzWsWmHrsbfPFwE6LDq471nqNeNMsD6DS7y8nruw,feePayer是feePayer
  2. tx.feePayer = feePayer.publicKey; 是可以省略的。如果沒有特別指定feePayer是誰,這個交易的第一個簽名的人會被當作是feePayer。
Last Updated: 2022/9/23 下午6:56:02
Contributors: Yihau Chen