Skip to content

Commit

Permalink
Use new helper to simplify escrow tests (#108)
Browse files Browse the repository at this point in the history
* Use new helper to make Escrow tests faster

* Update pnpm lock

* Add transfer_tokens() to replace copypasted code

* Split up sign_seeds to be less ugly

* Avoid premature optimisation and just move make and take into their respective tests

* Format, no changes

* Make a variable for mocha slow test threshhold

* Make less ugly

* Neaten

* Log signature

* add comments in test to provide context for accounts

---------

Co-authored-by: John <[email protected]>
  • Loading branch information
mikemaccana and ZYJLiu authored Aug 9, 2024
1 parent 6d134a7 commit b992087
Show file tree
Hide file tree
Showing 10 changed files with 1,391 additions and 939 deletions.
5 changes: 3 additions & 2 deletions basics/favorites/anchor/programs/favorites/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub mod favorites {

// Our instruction handler! It sets the user's favorite number and color
pub fn set_favorites(context: Context<SetFavorites>, number: u64, color: String, hobbies: Vec<String>) -> Result<()> {
let user_public_key = context.accounts.user.key();
msg!("Greetings from {}", context.program_id);
let user_public_key = context.accounts.user.key();
msg!(
"User {user_public_key}'s favorite number is {number}, favorite color is: {color}, and their hobbies are {hobbies:?}",
);
Expand Down Expand Up @@ -53,7 +53,8 @@ pub struct SetFavorites<'info> {
payer = user,
space = ANCHOR_DISCRIMINATOR_SIZE + Favorites::INIT_SPACE,
seeds=[b"favorites", user.key().as_ref()],
bump)]
bump
)]
pub favorites: Account<'info, Favorites>,

pub system_program: Program<'info, System>,
Expand Down
4 changes: 3 additions & 1 deletion basics/favorites/anchor/tests/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ describe('Favorites', () => {
it('Updates the favorites', async () => {
const newFavoriteHobbies = ['skiing', 'skydiving', 'biking', 'swimming'];
try {
await program.methods.setFavorites(favoriteNumber, favoriteColor, newFavoriteHobbies).signers([user]).rpc();
const signature = await program.methods.setFavorites(favoriteNumber, favoriteColor, newFavoriteHobbies).signers([user]).rpc();

console.log(`Transaction signature: ${signature}`);
} catch (error) {
console.error((error as Error).message);
const customErrorMessage = getCustomErrorMessage(systemProgramErrors, error);
Expand Down
1 change: 1 addition & 0 deletions tokens/escrow/anchor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project is based on [Dean Little's Anchor Escrow,](https://github.com/deanm
One of the challenges when teaching is avoiding ambiguity — names have to be carefully chosen to be clear and not possible to confuse with other times.

- Custom instructions were replaced by `@solana-developers/helpers` for many tasks to reduce the file size.
- Shared functionality to transfer tokens is now in `instructions/shared.rs`
- The upstream project has a custom file layout. We use the 'multiple files' Anchor layout.
- Contexts are separate data structures from functions that use the contexts. There is no need for OO-like `impl` patterns here - there's no mutable state stored in the Context, and the 'methods' do not mutate that state. Besides, it's easier to type!
- The name 'deposit' was being used in multiple contexts, and `deposit` can be tough because it's a verb and a noun:
Expand Down
2 changes: 1 addition & 1 deletion tokens/escrow/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.0",
"@solana-developers/helpers": "^2.3.0",
"@solana-developers/helpers": "^2.4.0",
"@solana/spl-token": "^0.4.6"
},
"license": "MIT",
Expand Down
Loading

0 comments on commit b992087

Please sign in to comment.