Skip to content

Commit e43df20

Browse files
committed
Add favorites and escrow projects from professional-education repo
Minor biome fixes
1 parent 65e979e commit e43df20

32 files changed

+913
-0
lines changed

basics/favorites/anchor/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
.anchor
3+
.DS_Store
4+
target
5+
**/*.rs.bk
6+
node_modules
7+
test-ledger
8+
.yarn
9+
.env
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
.anchor
3+
.DS_Store
4+
target
5+
node_modules
6+
dist
7+
build
8+
test-ledger

basics/favorites/anchor/Anchor.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[toolchain]
2+
3+
[features]
4+
resolution = true
5+
skip-lint = false
6+
7+
[programs.localnet]
8+
favorites = "ww9C83noARSQVBnqmCUmaVdbJjmiwcV9j2LkXYMoUCV"
9+
10+
[registry]
11+
url = "https://api.apr.dev"
12+
13+
[provider]
14+
cluster = "Localnet"
15+
wallet = "~/.config/solana/id.json"
16+
17+
[scripts]
18+
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

basics/favorites/anchor/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
resolver = "2"
6+
7+
[profile.release]
8+
overflow-checks = true
9+
lto = "fat"
10+
codegen-units = 1
11+
12+
[profile.release.build-override]
13+
opt-level = 3
14+
incremental = false
15+
codegen-units = 1

basics/favorites/anchor/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Favorites
2+
3+
This is a basic Anchor app using PDAs to store data for a user, and Anchor's account checks to ensure each user is only allowed to modify their own data.
4+
5+
It's used by the [https://github.com/solana-developers/professional-education](Solana Professional Education) course.
6+
7+
## Usage
8+
9+
`anchor test`, `anchor deploy` etc.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Migrations are an early feature. Currently, they're nothing more than this
2+
// single deploy script that's invoked from the CLI, injecting a provider
3+
// configured from the workspace's Anchor.toml.
4+
5+
const anchor = require('@coral-xyz/anchor');
6+
7+
module.exports = async (provider) => {
8+
// Configure client to use the provider.
9+
anchor.setProvider(provider);
10+
11+
// Add your deploy script here.
12+
};

basics/favorites/anchor/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"scripts": {
3+
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
4+
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
5+
},
6+
"dependencies": {
7+
"@coral-xyz/anchor": "^0.30.0",
8+
"@solana-developers/helpers": "^2.0.0"
9+
},
10+
"license": "UNLICENSED",
11+
"devDependencies": {
12+
"@types/bn.js": "^5.1.0",
13+
"@types/chai": "^4.3.0",
14+
"@types/mocha": "^9.0.0",
15+
"chai": "^4.3.4",
16+
"mocha": "^9.0.3",
17+
"prettier": "^2.6.2",
18+
"ts-mocha": "^10.0.0",
19+
"typescript": "^4.3.5"
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "favorites"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "favorites"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
no-log-ix-name = []
15+
cpi = ["no-entrypoint"]
16+
default = []
17+
idl-build = ["anchor-lang/idl-build"]
18+
19+
[dependencies]
20+
anchor-lang = {version = "0.30.0", features = ["init-if-needed"]}
21+
solana-program = "=1.18.5"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use anchor_lang::prelude::*;
2+
// Our program's address!
3+
// This matches the key in the target/deploy directory
4+
declare_id!("ww9C83noARSQVBnqmCUmaVdbJjmiwcV9j2LkXYMoUCV");
5+
6+
// Anchor programs always use 8 bits for the discriminator
7+
pub const ANCHOR_DISCRIMINATOR_SIZE: usize = 8;
8+
9+
// Our Solana program!
10+
#[program]
11+
pub mod favorites {
12+
use super::*;
13+
14+
// Our instruction handler! It sets the user's favorite number and color
15+
pub fn set_favorites(context: Context<SetFavorites>, number: u64, color: String, hobbies: Vec<String>) -> Result<()> {
16+
let user_public_key = context.accounts.user.key();
17+
msg!("Greetings from {}", context.program_id);
18+
msg!(
19+
"User {user_public_key}'s favorite number is {number}, favorite color is: {color}",
20+
);
21+
22+
msg!(
23+
"User's hobbies are: {:?}",
24+
hobbies
25+
);
26+
27+
context.accounts.favorites.set_inner(Favorites {
28+
number,
29+
color,
30+
hobbies
31+
});
32+
Ok(())
33+
}
34+
35+
// We can also add a get_favorites instruction handler to return the user's favorite number and color
36+
}
37+
38+
// What we will put inside the Favorites PDA
39+
#[account]
40+
#[derive(InitSpace)]
41+
pub struct Favorites {
42+
pub number: u64,
43+
44+
#[max_len(50)]
45+
pub color: String,
46+
47+
#[max_len(5, 50)]
48+
pub hobbies: Vec<String>
49+
}
50+
// When people call the set_favorites instruction, they will need to provide the accounts that will be modifed. This keeps Solana fast!
51+
#[derive(Accounts)]
52+
pub struct SetFavorites<'info> {
53+
#[account(mut)]
54+
pub user: Signer<'info>,
55+
56+
#[account(
57+
init_if_needed,
58+
payer = user,
59+
space = ANCHOR_DISCRIMINATOR_SIZE + Favorites::INIT_SPACE,
60+
seeds=[b"favorites", user.key().as_ref()],
61+
bump)]
62+
pub favorites: Account<'info, Favorites>,
63+
64+
pub system_program: Program<'info, System>,
65+
}

0 commit comments

Comments
 (0)