Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cookbook] create account #261

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 49 additions & 88 deletions content/cookbook/accounts/create-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,93 +11,58 @@ 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.

<Tabs groupId="language" items={['web3.js v2', 'web3.js v1']}>
<CodeTabs storage="cookbook">

<Tab value="web3.js v2">

```typescript title="create-account.ts"
```typescript !! title="gill"
import {
pipe,
createSolanaRpc,
appendTransactionMessageInstructions,
createSolanaRpcSubscriptions,
createTransactionMessage,
generateKeyPairSigner,
getSignatureFromTransaction,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
createTransaction,
createSolanaClient,
signTransactionMessageWithSigners,
} from "@solana/web3.js";
import { getSetComputeUnitPriceInstruction } from "@solana-program/compute-budget";
getMinimumBalanceForRentExemption,
generateKeyPairSigner,
} from "gill";
import { loadKeypairSignerFromFile } from "gill/node";
import {
getCreateAccountInstruction,
SYSTEM_PROGRAM_ADDRESS,
} from "@solana-program/system";
} from "gill/programs";

const rpc = createSolanaRpc("https://api.devnet.solana.com");
const rpcSubscriptions = createSolanaRpcSubscriptions(
"wss://api.devnet.solana.com",
);

const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({
rpc,
rpcSubscriptions,
const { rpc, sendAndConfirmTransaction } = createSolanaClient({
urlOrMoniker: "devnet",
});

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 signer = await loadKeypairSignerFromFile();

const signedTransaction =
await signTransactionMessageWithSigners(transactionMessage);
const signature = getSignatureFromTransaction(signedTransaction);
const newAccount = await generateKeyPairSigner();

await sendAndConfirmTransaction(signedTransaction, {
commitment: "confirmed",
});
console.log("Signature:", signature);
```
const space = 0n; // any extra space in the account

</Tab>
const rentLamports = getMinimumBalanceForRentExemption(space);
// const rentLamports = await rpc.getMinimumBalanceForRentExemption(space).send();

const tx = createTransaction({
version: "legacy",
feePayer: signer,
instructions: [
getCreateAccountInstruction({
lamports: rentLamports,
newAccount: newAccount,
payer: signer,
space: space,
// "wallet" accounts are owned by the system program
programAddress: SYSTEM_PROGRAM_ADDRESS,
}),
],
latestBlockhash,
});

<Tab value="web3.js v1">
const signedTransaction = await signTransactionMessageWithSigners(tx);
await sendAndConfirmTransaction(signedTransaction);
```

```typescript title="create-account.ts"
```typescript !! title="web3.js"
import {
SystemProgram,
Keypair,
Expand All @@ -109,11 +74,13 @@ import {
} from "@solana/web3.js";

const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const fromPubkey = Keypair.generate();

const fromKeypair = Keypair.generate();
const newAccount = Keypair.generate();

// Airdrop SOL for transferring lamports to the created account
const airdropSignature = await connection.requestAirdrop(
fromPubkey.publicKey,
fromKeypair.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction(airdropSignature);
Expand All @@ -122,20 +89,16 @@ await connection.confirmTransaction(airdropSignature);
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 rentLamports = await connection.getMinimumBalanceForRentExemption(space);

const createAccountTransaction = new Transaction().add(
SystemProgram.createAccount(createAccountParams),
SystemProgram.createAccount({
fromPubkey: fromKeypair.publicKey,
newAccountPubkey: newAccount.publicKey,
lamports: rentLamports,
space,
programId: SystemProgram.programId,
}),
);

await sendAndConfirmTransaction(connection, createAccountTransaction, [
Expand All @@ -144,6 +107,4 @@ await sendAndConfirmTransaction(connection, createAccountTransaction, [
]);
```

</Tab>

</Tabs>
</CodeTabs>