forked from solana-developers/pirate-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.complexTransaction.ts
111 lines (90 loc) · 3.75 KB
/
2.complexTransaction.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
* Demonstrates how to build a more complex transaction, with multiple instructions
*/
// import custom helpers for demos
import { payer, testWallet, connection, STATIC_PUBLICKEY } from "@/lib/vars";
import { explorerURL, printConsoleSeparator } from "@/lib/helpers";
import { SystemProgram, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
(async () => {
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
console.log("Payer address:", payer.publicKey.toBase58());
console.log("Test wallet address:", testWallet.publicKey.toBase58());
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* create a simple instruction (using web3.js) to create an account
*/
const space = 0; // on-chain space to allocated (in number of bytes)
// request the cost (in lamports) to allocate `space` number of bytes on chain
const balanceForRentExemption = await connection.getMinimumBalanceForRentExemption(space);
// create this simple instruction using web3.js helper function
const createTestAccountIx = SystemProgram.createAccount({
// `fromPubkey` - this account will need to sign the transaction
fromPubkey: payer.publicKey,
// `newAccountPubkey` - the account address to create on chain
newAccountPubkey: testWallet.publicKey,
// lamports to store in this account
lamports: balanceForRentExemption + 2_000_000,
// total space to allocate
space,
// the owning program for this account
programId: SystemProgram.programId,
});
// create an instruction to transfer lamports
const transferToTestWalletIx = SystemProgram.transfer({
lamports: balanceForRentExemption + 100_000,
// `fromPubkey` - from MUST sign the transaction
fromPubkey: payer.publicKey,
// `toPubkey` - does NOT have to sign the transaction
toPubkey: testWallet.publicKey,
programId: SystemProgram.programId,
});
// create an other instruction to transfer lamports
const transferToStaticWalletIx = SystemProgram.transfer({
lamports: 100_000,
// `fromPubkey` - from MUST sign the transaction
fromPubkey: payer.publicKey,
// `toPubkey` - does NOT have to sign the transaction
toPubkey: STATIC_PUBLICKEY,
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 transaction message
const message = new TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash,
instructions: [
// create the test wallet's account on chain
createTestAccountIx,
// transfer lamports to the static wallet
transferToStaticWalletIx,
// transfer lamports to the test wallet
transferToTestWalletIx,
// transfer lamports to the static wallet
transferToStaticWalletIx,
],
}).compileToV0Message();
/**
* try changing the order of the instructions inside of the message above...
* see what happens :)
*/
// 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, testWallet]);
// actually send the transaction
const sig = await connection.sendTransaction(tx);
/**
* display some helper text
*/
printConsoleSeparator();
console.log("Transaction completed.");
console.log(explorerURL({ txSignature: sig }));
})();