From 3c67834c9356c1e901359662c8a8d3699094e411 Mon Sep 17 00:00:00 2001
From: nickfrosty <75431177+nickfrosty@users.noreply.github.com>
Date: Mon, 18 Nov 2024 15:23:50 -0500
Subject: [PATCH 1/4] feat: calculate rent
---
content/cookbook/accounts/calculate-rent.md | 43 +++++++++++++++------
1 file changed, 32 insertions(+), 11 deletions(-)
diff --git a/content/cookbook/accounts/calculate-rent.md b/content/cookbook/accounts/calculate-rent.md
index 0ab617489..85decaef0 100644
--- a/content/cookbook/accounts/calculate-rent.md
+++ b/content/cookbook/accounts/calculate-rent.md
@@ -10,18 +10,39 @@ Keeping accounts alive on Solana incurs a storage cost called rent. For the
calculation, you need to consider the amount of data you intend to store in the
account. Rent can be reclaimed in full if the account is closed.
+
+
+
+
+```typescript filename="calculate-rent.ts"
+import { createSolanaRpc } from "@solana/web3.js";
+
+const rpc = createSolanaRpc("https://api.devnet.solana.com");
+// 1.5k bytes
+const space = 1500n;
+
+const lamports = await rpc.getMinimumBalanceForRentExemption(space).send();
+console.log("Minimum balance for rent exception:", lamports);
+```
+
+
+
+
+
```typescript filename="calculate-rent.ts"
import { Connection, clusterApiUrl } from "@solana/web3.js";
-(async () => {
- const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
-
- // length of data in bytes in the account to calculate rent for
- const dataLength = 1500;
- const rentExemptionAmount =
- await connection.getMinimumBalanceForRentExemption(dataLength);
- console.log({
- rentExemptionAmount,
- });
-})();
+const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
+
+// length of data in bytes in the account to calculate rent for
+const dataLength = 1500;
+const rentExemptionAmount =
+ await connection.getMinimumBalanceForRentExemption(dataLength);
+console.log({
+ rentExemptionAmount,
+});
```
+
+
+
+
From ec61c649455081ca2f4cdad410015385f6bac2c9 Mon Sep 17 00:00:00 2001
From: nickfrosty <75431177+nickfrosty@users.noreply.github.com>
Date: Mon, 18 Nov 2024 15:39:14 -0500
Subject: [PATCH 2/4] feat: get balance
---
.../cookbook/accounts/get-account-balance.md | 36 ++++++++++++++-----
1 file changed, 28 insertions(+), 8 deletions(-)
diff --git a/content/cookbook/accounts/get-account-balance.md b/content/cookbook/accounts/get-account-balance.md
index 98a077947..f1c657bd1 100644
--- a/content/cookbook/accounts/get-account-balance.md
+++ b/content/cookbook/accounts/get-account-balance.md
@@ -6,7 +6,26 @@ description:
that account balance on Solana."
---
-```typescript filename="get-account-balance.ts" {13}
+
+
+
+
+```typescript filename="get-account-balance.ts"
+import { address, createSolanaRpc } from "@solana/web3.js";
+
+const rpc = createSolanaRpc("https://api.devnet.solana.com");
+const LAMPORTS_PER_SOL = 1_000_000_000; // 1 billion lamports per SOL
+
+const wallet = address("nicktrLHhYzLmoVbuZQzHUTicd2sfP571orwo9jfc8c");
+const { value: balance } = await rpc.getBalance(wallet).send();
+console.log(`Balance: ${Number(balance) / LAMPORTS_PER_SOL} SOL`);
+```
+
+
+
+
+
+```typescript filename="get-account-balance.ts"
import {
clusterApiUrl,
Connection,
@@ -14,12 +33,13 @@ import {
LAMPORTS_PER_SOL,
} from "@solana/web3.js";
-(async () => {
- const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
+const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
+const wallet = new PublicKey("nicktrLHhYzLmoVbuZQzHUTicd2sfP571orwo9jfc8c");
- let wallet = new PublicKey("G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoY");
- console.log(
- `${(await connection.getBalance(wallet)) / LAMPORTS_PER_SOL} SOL`,
- );
-})();
+const balance = await connection.getBalance(wallet);
+console.log(`Balance: ${balance / LAMPORTS_PER_SOL} SOL`);
```
+
+
+
+
From bc66d1c3f440f3c5e274bade36b0100ad9c32509 Mon Sep 17 00:00:00 2001
From: nickfrosty <75431177+nickfrosty@users.noreply.github.com>
Date: Mon, 18 Nov 2024 16:05:32 -0500
Subject: [PATCH 3/4] feat: create account
---
content/cookbook/accounts/create-account.md | 168 +++++++++++++++-----
1 file changed, 128 insertions(+), 40 deletions(-)
diff --git a/content/cookbook/accounts/create-account.md b/content/cookbook/accounts/create-account.md
index f1a570b64..9ed042bf3 100644
--- a/content/cookbook/accounts/create-account.md
+++ b/content/cookbook/accounts/create-account.md
@@ -7,10 +7,96 @@ description:
---
Creating an account requires using the System Program `createAccount`
-instruction. The Solana runtime will grant the owner of an account, access to
-write to its data or transfer lamports. When creating an account, we have to
-preallocate a fixed storage space in bytes (space) and enough lamports to cover
-the rent.
+instruction. The Solana runtime will grant the owner program of an account,
+access to write to its data or transfer lamports. When creating an account, we
+have to preallocate a fixed storage space in bytes (space) and enough lamports
+to cover the rent.
+
+
+
+
+
+```typescript filename="create-account.ts"
+import {
+ pipe,
+ createSolanaRpc,
+ appendTransactionMessageInstructions,
+ createSolanaRpcSubscriptions,
+ createTransactionMessage,
+ generateKeyPairSigner,
+ getSignatureFromTransaction,
+ sendAndConfirmTransactionFactory,
+ setTransactionMessageFeePayerSigner,
+ setTransactionMessageLifetimeUsingBlockhash,
+ signTransactionMessageWithSigners,
+} from "@solana/web3.js";
+import { getSetComputeUnitPriceInstruction } from "@solana-program/compute-budget";
+import {
+ getCreateAccountInstruction,
+ SYSTEM_PROGRAM_ADDRESS,
+} from "@solana-program/system";
+
+const rpc = createSolanaRpc("https://api.devnet.solana.com");
+const rpcSubscriptions = createSolanaRpcSubscriptions(
+ "wss://api.devnet.solana.com",
+);
+
+const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({
+ rpc,
+ rpcSubscriptions,
+});
+
+const space = 0n; // any extra space in the account
+const rentLamports = await rpc.getMinimumBalanceForRentExemption(space).send();
+console.log("Minimum balance for rent exception:", rentLamports);
+
+// todo: load your own signer with SOL
+const signer = await generateKeyPairSigner();
+
+// generate a new keypair and address to create
+const newAccountKeypair = await generateKeyPairSigner();
+console.log("New account address:", newAccountKeypair.address);
+
+const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
+
+const transactionMessage = pipe(
+ createTransactionMessage({ version: "legacy" }),
+ tx => setTransactionMessageFeePayerSigner(signer, tx),
+ tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
+ tx =>
+ appendTransactionMessageInstructions(
+ [
+ // add a priority fee
+ getSetComputeUnitPriceInstruction({
+ microLamports: 200_000,
+ }),
+ // create the new account
+ getCreateAccountInstruction({
+ lamports: rentLamports,
+ newAccount: newAccountKeypair,
+ payer: signer,
+ space: space,
+ // "wallet" accounts are owned by the system program
+ programAddress: SYSTEM_PROGRAM_ADDRESS,
+ }),
+ ],
+ tx,
+ ),
+);
+
+const signedTransaction =
+ await signTransactionMessageWithSigners(transactionMessage);
+const signature = getSignatureFromTransaction(signedTransaction);
+
+await sendAndConfirmTransaction(signedTransaction, {
+ commitment: "confirmed",
+});
+console.log("Signature:", signature);
+```
+
+
+
+
```typescript filename="create-account.ts"
import {
@@ -23,40 +109,42 @@ import {
LAMPORTS_PER_SOL,
} from "@solana/web3.js";
-(async () => {
- const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
- const fromPubkey = Keypair.generate();
-
- // Airdrop SOL for transferring lamports to the created account
- const airdropSignature = await connection.requestAirdrop(
- fromPubkey.publicKey,
- LAMPORTS_PER_SOL,
- );
- await connection.confirmTransaction(airdropSignature);
-
- // amount of space to reserve for the account
- const space = 0;
-
- // Seed the created account with lamports for rent exemption
- const rentExemptionAmount =
- await connection.getMinimumBalanceForRentExemption(space);
-
- const newAccountPubkey = Keypair.generate();
- const createAccountParams = {
- fromPubkey: fromPubkey.publicKey,
- newAccountPubkey: newAccountPubkey.publicKey,
- lamports: rentExemptionAmount,
- space,
- programId: SystemProgram.programId,
- };
-
- const createAccountTransaction = new Transaction().add(
- SystemProgram.createAccount(createAccountParams),
- );
-
- await sendAndConfirmTransaction(connection, createAccountTransaction, [
- fromPubkey,
- newAccountPubkey,
- ]);
-})();
+const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
+const fromPubkey = Keypair.generate();
+
+// Airdrop SOL for transferring lamports to the created account
+const airdropSignature = await connection.requestAirdrop(
+ fromPubkey.publicKey,
+ LAMPORTS_PER_SOL,
+);
+await connection.confirmTransaction(airdropSignature);
+
+// amount of space to reserve for the account
+const space = 0;
+
+// Seed the created account with lamports for rent exemption
+const rentExemptionAmount =
+ await connection.getMinimumBalanceForRentExemption(space);
+
+const newAccountPubkey = Keypair.generate();
+const createAccountParams = {
+ fromPubkey: fromPubkey.publicKey,
+ newAccountPubkey: newAccountPubkey.publicKey,
+ lamports: rentExemptionAmount,
+ space,
+ programId: SystemProgram.programId,
+};
+
+const createAccountTransaction = new Transaction().add(
+ SystemProgram.createAccount(createAccountParams),
+);
+
+await sendAndConfirmTransaction(connection, createAccountTransaction, [
+ fromPubkey,
+ newAccountPubkey,
+]);
```
+
+
+
+
From 00e1918165267d7a94203264c407f5267954301d Mon Sep 17 00:00:00 2001
From: nickfrosty <75431177+nickfrosty@users.noreply.github.com>
Date: Tue, 19 Nov 2024 09:39:33 -0500
Subject: [PATCH 4/4] chore: prettier
---
.../tokens-and-nfts/nfts-with-metaplex.md | 3 ++-
docs/core/clusters.md | 17 +++++++++--------
2 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/content/courses/tokens-and-nfts/nfts-with-metaplex.md b/content/courses/tokens-and-nfts/nfts-with-metaplex.md
index be6ce5a75..f5b929b5c 100644
--- a/content/courses/tokens-and-nfts/nfts-with-metaplex.md
+++ b/content/courses/tokens-and-nfts/nfts-with-metaplex.md
@@ -348,7 +348,8 @@ To begin, make a new folder and install the relevant dependencies:
npm i @solana/web3.js@1 @solana-developers/helpers@2 @metaplex-foundation/mpl-token-metadata @metaplex-foundation/umi-bundle-defaults @metaplex-foundation/umi-uploader-irys esrun
```
-Then create a file called `create-metaplex-nft-collection.ts`, and add our imports:
+Then create a file called `create-metaplex-nft-collection.ts`, and add our
+imports:
```typescript
import {
diff --git a/docs/core/clusters.md b/docs/core/clusters.md
index 100f5a489..c7b59ee60 100644
--- a/docs/core/clusters.md
+++ b/docs/core/clusters.md
@@ -42,17 +42,18 @@ An example of some of these Solana blockchain explorers include:
## On a high level
- Mainnet: Live production environment for deployed applications.
-- Devnet: Testing with public accessibility for developers experimenting with their applications.
+- Devnet: Testing with public accessibility for developers experimenting with
+ their applications.
- Testnet: Stress-testing for network upgrades and validator performance.
-**Example use cases**: You may want to debug a new program on Devnet or verify performance metrics on Testnet before Mainnet deployment.
-
-| **Cluster** | **Endpoint** | **Purpose** | **Notes** |
-|---------------|----------------------------------|---------------------------------------------|-------------------------------------|
-| Mainnet | `https://api.mainnet-beta.solana.com` | Live production environment | Requires SOL for transactions |
-| Devnet | `https://api.devnet.solana.com` | Public testing and development | Free SOL airdrop for testing |
-| Testnet | `https://api.testnet.solana.com` | Validator and stress testing | May have intermittent downtime |
+**Example use cases**: You may want to debug a new program on Devnet or verify
+performance metrics on Testnet before Mainnet deployment.
+| **Cluster** | **Endpoint** | **Purpose** | **Notes** |
+| ----------- | ------------------------------------- | ------------------------------ | ------------------------------ |
+| Mainnet | `https://api.mainnet-beta.solana.com` | Live production environment | Requires SOL for transactions |
+| Devnet | `https://api.devnet.solana.com` | Public testing and development | Free SOL airdrop for testing |
+| Testnet | `https://api.testnet.solana.com` | Validator and stress testing | May have intermittent downtime |
## Devnet