Skip to content

Commit 96587aa

Browse files
EmekaManuelOnyewuchi Emeka
andauthored
update program-state-management and serialize-instruction-data-frontend (solana-foundation#369)
* refactor(): replace deprecated findProgramAddress with findProgramAddressSync The signature '(seeds: (Buffer | Uint8Array)[], programId: PublicKey): Promise<[PublicKey, number]>' of 'web3.PublicKey.findProgramAddress' is deprecated. Updated the code to use the recommended 'findProgramAddressSync' method for better compatibility. * refactor(): - Improved efficiency by importing specific modules from @solana/web3.js instead of importing the entire package. - Replaced .then() with await for better readability and async handling. - Applied linting to maintain code consistency and clarity. - Replaced abbreviations like tx with more descriptive variable names for better code readability. - Enhanced error handling using trycatch. * refactor(): replaced .then with trycatch block * refactor(): replaced .then with trycatch block * chore(): added updated link to the new movie application frontend and included the application screenshot * chore(): implemented getExplorerLink from solana-developer/helpers * chore(): updated url for movie review frontend repo * chore(): updated program review application url and course reference link * chore(): updated serialize-instruction-data file with current implementation * fixed PR review changes * chore(): fixed all issues in the PR review * chore(): removed unnecessary imports * updated code snippet and course links * chore(): updated example project url links, course links buffer methods. also removed unused imports * chore(): removed single letter and replaced with full names * chore(): made changes as requested in the pr review * fixed pr issues * chore(): fixed pr issues * chore(): included checks for character limit * chore(): changed repo link to updated example project --------- Co-authored-by: Onyewuchi Emeka <[email protected]>
1 parent 8acc91a commit 96587aa

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

content/courses/native-onchain-development/program-state-management.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -542,19 +542,21 @@ You're now ready to build and deploy your program!
542542

543543
You can test your program by submitting a transaction with the right instruction
544544
data. For that, feel free to use
545-
[this script](https://github.com/Unboxed-Software/solana-movie-client) or
546-
[the frontend](https://github.com/Unboxed-Software/solana-movie-frontend) we
545+
[this script](https://github.com/solana-developers/movie-review-program-client)
546+
or [the frontend](https://github.com/solana-developers/movie-review-frontend) we
547547
built in the
548-
[Deserialize Custom Instruction Data lesson](deserialize-custom-data). In both
549-
cases, make sure you copy and paste the program ID for your program into the
550-
appropriate area of the source code to make sure you're testing the right
548+
[Deserialize Custom Instruction Data lesson](/content/courses/native-onchain-development/deserialize-custom-data-frontend.md).
549+
In both cases, set the program ID for your program in the appropriate file
550+
`web/components/ui/review-form.ts` to make sure you're testing the right
551551
program.
552552

553-
If you use the frontend, simply replace the `MOVIE_REVIEW_PROGRAM_ID` in both
554-
the `MovieList.tsx` and `Form.tsx` components with the address of the program
555-
you've deployed. Then run the frontend, submit a view, and refresh the browser
556-
to see the review.
553+
- If you're using the script, simply replace the value assigned to
554+
`movieProgramId` in the `index.ts` component with the public key of the
555+
program you've deployed.
556+
- If you use the frontend, simply replace the `MOVIE_REVIEW_PROGRAM_ID` in the
557+
`review-form.tsx` components with the address of the program you’ve deployed.
557558

559+
Then run the frontend, submit a view, and refresh the browser to see the review.
558560
If you need more time with this project to feel comfortable with these concepts,
559561
have a look at the
560562
[solution code](https://beta.solpg.io/66d67f31cffcf4b13384d334) before
@@ -578,7 +580,7 @@ taking a name a short message as instruction data, the program should:
578580
string in each account
579581

580582
You can test your program by building the
581-
[frontend](https://github.com/Unboxed-Software/solana-student-intros-frontend)
583+
[frontend](https://github.com/solana-developers/solana-student-intro-frontend)
582584
we created in the
583585
[Page, Order, and Filter Program Data lesson](/content/courses/native-onchain-development/paging-ordering-filtering-data-frontend).
584586
Remember to replace the program ID in the frontend code with the one you've

content/courses/native-onchain-development/serialize-instruction-data-frontend.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,30 +354,69 @@ Now that we have the buffer layout set up, let’s create a method in `Movie`
354354
called `serialize()` that will return a `Buffer` with a `Movie` object’s
355355
properties encoded into the appropriate layout.
356356

357+
Instead of allocating a fixed buffer size, we'll calculate the size dynamically
358+
using known constants for the space required by each field in the `Movie`
359+
object. Specifically, we'll use `INIT_SPACE` (to account for string length
360+
metadata) and `ANCHOR_DISCRIMINATOR` (to account for the 8-byte discriminator
361+
used by Anchor).
362+
357363
```typescript
358-
import * as borsh from '@coral-xyz/borsh'
364+
import * as borsh from "@coral-xyz/borsh";
365+
366+
// Constants for size calculations
367+
const ANCHOR_DISCRIMINATOR = 8; // 8 bytes for the account discriminator used by Anchor
368+
const STRING_LENGTH_SPACE = 4; // 4 bytes to store the length of each string
369+
370+
// Specific sizes for 'title' and 'description' strings
371+
const TITLE_SIZE = 100; // Allocate 100 bytes for the 'title'
372+
const DESCRIPTION_SIZE = 500; // Allocate 500 bytes for the 'description'
373+
374+
// Total space calculation for the Movie review structure
375+
const MOVIE_REVIEW_SPACE =
376+
ANCHOR_DISCRIMINATOR + // 8 bytes for the account discriminator
377+
STRING_LENGTH_SPACE +
378+
TITLE_SIZE + // 4 bytes for the title length + 100 bytes for the title
379+
STRING_LENGTH_SPACE +
380+
DESCRIPTION_SIZE + // 4 bytes for the description length + 500 bytes for the description
381+
1 + // 1 byte for 'variant'
382+
1; // 1 byte for 'rating'
359383

360384
export class Movie {
361385
title: string;
362386
rating: number;
363387
description: string;
364388

365-
...
389+
constructor(title: string, rating: number, description: string) {
390+
// Enforce specific sizes for title and description
391+
if (title.length > TITLE_SIZE) {
392+
throw new Error(`Title cannot exceed ${TITLE_SIZE} characters.`);
393+
}
394+
if (description.length > DESCRIPTION_SIZE) {
395+
throw new Error(
396+
`Description cannot exceed ${DESCRIPTION_SIZE} characters.`,
397+
);
398+
}
399+
400+
this.title = title;
401+
this.rating = rating;
402+
this.description = description;
403+
}
366404

367405
borshInstructionSchema = borsh.struct([
368-
borsh.u8('variant'),
369-
borsh.str('title'),
370-
borsh.u8('rating'),
371-
borsh.str('description'),
372-
])
406+
borsh.u8("variant"),
407+
borsh.str("title"),
408+
borsh.u8("rating"),
409+
borsh.str("description"),
410+
]);
373411

374412
serialize(): Buffer {
375413
try {
376-
const buffer = Buffer.alloc(1000);
414+
// Allocate a buffer with the exact space needed
415+
const buffer = Buffer.alloc(MOVIE_REVIEW_SPACE);
377416
this.borshInstructionSchema.encode({ ...this, variant: 0 }, buffer);
378417
return buffer.subarray(0, this.borshInstructionSchema.getSpan(buffer));
379418
} catch (error) {
380-
console.error('Serialization error:', error);
419+
console.error("Serialization error:", error);
381420
return Buffer.alloc(0);
382421
}
383422
}
203 KB
Loading

0 commit comments

Comments
 (0)