Skip to content

Commit b992087

Browse files
mikemaccanaZYJLiu
andauthored
Use new helper to simplify escrow tests (#108)
* 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]>
1 parent 6d134a7 commit b992087

File tree

10 files changed

+1391
-939
lines changed

10 files changed

+1391
-939
lines changed

basics/favorites/anchor/programs/favorites/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub mod favorites {
1313

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

5960
pub system_program: Program<'info, System>,

basics/favorites/anchor/tests/favorites.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ describe('Favorites', () => {
5454
it('Updates the favorites', async () => {
5555
const newFavoriteHobbies = ['skiing', 'skydiving', 'biking', 'swimming'];
5656
try {
57-
await program.methods.setFavorites(favoriteNumber, favoriteColor, newFavoriteHobbies).signers([user]).rpc();
57+
const signature = await program.methods.setFavorites(favoriteNumber, favoriteColor, newFavoriteHobbies).signers([user]).rpc();
58+
59+
console.log(`Transaction signature: ${signature}`);
5860
} catch (error) {
5961
console.error((error as Error).message);
6062
const customErrorMessage = getCustomErrorMessage(systemProgramErrors, error);

tokens/escrow/anchor/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This project is based on [Dean Little's Anchor Escrow,](https://github.com/deanm
2525
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.
2626

2727
- Custom instructions were replaced by `@solana-developers/helpers` for many tasks to reduce the file size.
28+
- Shared functionality to transfer tokens is now in `instructions/shared.rs`
2829
- The upstream project has a custom file layout. We use the 'multiple files' Anchor layout.
2930
- 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!
3031
- The name 'deposit' was being used in multiple contexts, and `deposit` can be tough because it's a verb and a noun:

tokens/escrow/anchor/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"dependencies": {
77
"@coral-xyz/anchor": "^0.30.0",
8-
"@solana-developers/helpers": "^2.3.0",
8+
"@solana-developers/helpers": "^2.4.0",
99
"@solana/spl-token": "^0.4.6"
1010
},
1111
"license": "MIT",

0 commit comments

Comments
 (0)