From 415017a293d70aac9c2e7ac49044b4cf82c8419c Mon Sep 17 00:00:00 2001 From: nickfrosty <75431177+nickfrosty@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:46:41 -0500 Subject: [PATCH 1/2] feat: wip --- .../cookbook/accounts/create-pda-account.md | 161 +++++++++++------- 1 file changed, 98 insertions(+), 63 deletions(-) diff --git a/content/cookbook/accounts/create-pda-account.md b/content/cookbook/accounts/create-pda-account.md index 9465de687..0e6be0f7c 100644 --- a/content/cookbook/accounts/create-pda-account.md +++ b/content/cookbook/accounts/create-pda-account.md @@ -11,10 +11,31 @@ Accounts found at Program Derived Addresses (PDAs) can only be created on-chain. The accounts have addresses that have an associated off-curve public key, but no secret key. -To generate a PDA, use `findProgramAddressSync` with your required seeds. +## Generating a PDA + + + +To generate a PDA, use `getProgramDerivedAddress` with your required seeds. Generating with the same seeds will always generate the same PDA. -## Generating a PDA + + +```typescript filename="generate-pda.ts" +import { getProgramDerivedAddress, address } from "@solana/web3.js"; + +const [pda, bump] = await getProgramDerivedAddress({ + programAddress: address("G1DCNUQTSGHehwdLCAmRyAG8hf51eCHrLNUqkgGKYASj"), + seeds: ["test"], +}); +console.log(`bump: ${bump}, address: ${pda}`); +``` + + + + + +To generate a PDA, use `findProgramAddressSync` with your required seeds. +Generating with the same seeds will always generate the same PDA. ```typescript filename="generate-pda.ts" import { PublicKey } from "@solana/web3.js"; @@ -25,14 +46,16 @@ let [pda, bump] = PublicKey.findProgramAddressSync( [Buffer.from("test")], programId, ); -console.log(`bump: ${bump}, pubkey: ${pda.toBase58()}`); +console.log(`bump: ${bump}, address: ${pda.toBase58()}`); // you will find the result is different from `createProgramAddress`. // It is expected because the real seed we used to calculate is ["test" + bump] ``` -## Create an Account at a PDA + -### Program + + +## Create a PDA Account in a Program (via CPI) ```rust filename="create-pda.rs" {24-37} use solana_program::{ @@ -77,7 +100,19 @@ fn process_instruction( } ``` -## Client +## Create a PDA Account in a Client + + + + + +```typescript filename="create-pda.ts" + +``` + + + + ```typescript filename="create-pda.ts" import { @@ -92,61 +127,61 @@ import { SYSVAR_RENT_PUBKEY, } from "@solana/web3.js"; -(async () => { - // program id - const programId = new PublicKey( - "7ZP42kRwUQ2zgbqXoaXzAFaiQnDyp6swNktTSv8mNQGN", - ); - - // connection - const connection = new Connection(clusterApiUrl("devnet"), "confirmed"); - - // setup fee payer - const feePayer = Keypair.generate(); - const feePayerAirdropSignature = await connection.requestAirdrop( - feePayer.publicKey, - LAMPORTS_PER_SOL, - ); - await connection.confirmTransaction(feePayerAirdropSignature); - - // setup pda - let [pda, bump] = await PublicKey.findProgramAddress( - [feePayer.publicKey.toBuffer()], - programId, - ); - console.log(`bump: ${bump}, pubkey: ${pda.toBase58()}`); - - const data_size = 0; - - let tx = new Transaction().add( - new TransactionInstruction({ - keys: [ - { - pubkey: feePayer.publicKey, - isSigner: true, - isWritable: true, - }, - { - pubkey: pda, - isSigner: false, - isWritable: true, - }, - { - pubkey: SYSVAR_RENT_PUBKEY, - isSigner: false, - isWritable: false, - }, - { - pubkey: SystemProgram.programId, - isSigner: false, - isWritable: false, - }, - ], - data: Buffer.from(new Uint8Array([data_size, bump])), - programId: programId, - }), - ); - - console.log(`txhash: ${await connection.sendTransaction(tx, [feePayer])}`); -})(); +// program id +const programId = new PublicKey("7ZP42kRwUQ2zgbqXoaXzAFaiQnDyp6swNktTSv8mNQGN"); + +// connection +const connection = new Connection(clusterApiUrl("devnet"), "confirmed"); + +// setup fee payer +const feePayer = Keypair.generate(); +const feePayerAirdropSignature = await connection.requestAirdrop( + feePayer.publicKey, + LAMPORTS_PER_SOL, +); +await connection.confirmTransaction(feePayerAirdropSignature); + +// setup pda +let [pda, bump] = await PublicKey.findProgramAddress( + [feePayer.publicKey.toBuffer()], + programId, +); +console.log(`bump: ${bump}, pubkey: ${pda.toBase58()}`); + +const data_size = 0; + +let tx = new Transaction().add( + new TransactionInstruction({ + keys: [ + { + pubkey: feePayer.publicKey, + isSigner: true, + isWritable: true, + }, + { + pubkey: pda, + isSigner: false, + isWritable: true, + }, + { + pubkey: SYSVAR_RENT_PUBKEY, + isSigner: false, + isWritable: false, + }, + { + pubkey: SystemProgram.programId, + isSigner: false, + isWritable: false, + }, + ], + data: Buffer.from(new Uint8Array([data_size, bump])), + programId: programId, + }), +); + +console.log(`txhash: ${await connection.sendTransaction(tx, [feePayer])}`); ``` + + + + From 15e3f02c520feb214aa27636fa3593f8a4f4b4c6 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 26 Nov 2024 00:39:47 +0000 Subject: [PATCH 2/2] [chore] automatic prettier formatting --- .github/label-actions.yml | 4 ++-- package.json | 2 +- tsconfig.json | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/label-actions.yml b/.github/label-actions.yml index 0226a174c..ec2760e5a 100644 --- a/.github/label-actions.yml +++ b/.github/label-actions.yml @@ -13,6 +13,6 @@ solved: # Close the discussion close: true # Set a close reason - close-reason: 'resolved' + close-reason: "resolved" # Lock the discussion - lock: true \ No newline at end of file + lock: true diff --git a/package.json b/package.json index 7b05ad619..772fc613f 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "ignore": "^5.3.1", "lint-staged": "^15.2.7", "mdast": "^3.0.0", - "prettier": "^3.2.4", + "prettier": "3.3.3", "remark": "^15.0.1", "remark-frontmatter": "^5.0.0", "remark-parse": "^11.0.0", diff --git a/tsconfig.json b/tsconfig.json index d90767bb3..5f9e81ca6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,20 +18,20 @@ "paths": { "contentlayer/generated": ["./.contentlayer/generated"], "@/*": ["./src/*"], - "@@/*": ["./*"], + "@@/*": ["./*"] }, "plugins": [ { - "name": "next", - }, - ], + "name": "next" + } + ] }, "include": [ ".contentlayer/generated", "next-env.d.ts", "**/*.ts", "**/*.tsx", - ".next/types/**/*.ts", + ".next/types/**/*.ts" ], - "exclude": ["node_modules", "code"], + "exclude": ["node_modules", "code"] }