forked from solana-developers/pirate-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.simpleTransaction.ts
111 lines (84 loc) · 3.46 KB
/
1.simpleTransaction.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Introduction to the Solana web3.js package
* Demonstrating how to build and send simple transactions to the blockchain
*/
// import custom helpers for demos
import { payer, connection } from "@/lib/vars";
import { explorerURL, printConsoleSeparator } from "@/lib/helpers";
//
import {
Keypair,
LAMPORTS_PER_SOL,
SystemProgram,
TransactionMessage,
VersionedTransaction,
} from "@solana/web3.js";
(async () => {
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
console.log("Payer address:", payer.publicKey.toBase58());
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// get the current balance of the `payer` account on chain
const currentBalance = await connection.getBalance(payer.publicKey);
console.log("Current balance of 'payer' (in lamports):", currentBalance);
console.log("Current balance of 'payer' (in SOL):", currentBalance / LAMPORTS_PER_SOL);
// airdrop on low balance
if (currentBalance <= LAMPORTS_PER_SOL) {
console.log("Low balance, requesting an airdrop...");
await connection.requestAirdrop(payer.publicKey, LAMPORTS_PER_SOL);
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// generate a new, random address to create on chain
const keypair = Keypair.generate();
console.log("New keypair generated:", keypair.publicKey.toBase58());
/**
* create a simple instruction (using web3.js) to create an account
*/
// on-chain space to allocated (in number of bytes)
const space = 0;
// request the cost (in lamports) to allocate `space` number of bytes on chain
const lamports = await connection.getMinimumBalanceForRentExemption(space);
console.log("Total lamports:", lamports);
// create this simple instruction using web3.js helper function
const createAccountIx = SystemProgram.createAccount({
// `fromPubkey` - this account will need to sign the transaction
fromPubkey: payer.publicKey,
// `newAccountPubkey` - the account address to create on chain
newAccountPubkey: keypair.publicKey,
// lamports to store in this account
lamports,
// total space to allocate
space,
// the owning program for this account
programId: SystemProgram.programId,
});
/**
* build the transaction to send to the blockchain
*/
// get the latest recent blockhash
let recentBlockhash = await connection.getLatestBlockhash().then(res => res.blockhash);
// create a message (v0)
const message = new TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash,
instructions: [createAccountIx],
}).compileToV0Message();
// create a versioned transaction using the message
const tx = new VersionedTransaction(message);
// console.log("tx before signing:", tx);
// sign the transaction with our needed Signers (e.g. `payer` and `keypair`)
tx.sign([payer, keypair]);
console.log("tx after signing:", tx);
// tx.signatures.toString("base58")
// console.log(tx.signatures);
// actually send the transaction
const sig = await connection.sendTransaction(tx);
/**
* display some helper text
*/
printConsoleSeparator();
console.log("Transaction completed.");
console.log(explorerURL({ txSignature: sig }));
})();