diff --git a/README.md b/README.md index 2d87b6c5..f0cf5967 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@
-* **[1. Introduction to the Solana Blockchain](chapters/1_intro.md)** -* **[2. Setup a Developing Environment](chapters/2_dev_env.md)** -* **[3. Mastering the Anchor Framework](chapters/3_anchor.md)** -* **[4. Master Solana Frontend](chapters/4_frontend.md)** -* **[5. In-Depth Solana Programs, SPL, CPI](chapters/5_programs_in_depth.md)** -* **[6. Transfer Hooks](chapters/6_transfer_hooks.md)** -* **[7. External Resources to Keep Sharpening Your Axe](chapters/7_sharpening_your_axes.md)** +* **[1. Introduction to the Solana Blockchain](chapters/01_intro.md)** +* **[2. Setup a Developing Environment](chapters/02_dev_env.md)** +* **[3. Mastering the Anchor Framework](chapters/03_anchor.md)** +* **[4. Master Solana Frontend](chapters/04_frontend.md)** +* **[5. In-Depth Solana Programs, SPL, CPI](chapters/05_programs_in_depth.md)** +* **[6. Transfer Hooks](chapters/06_transfer_hooks.md)** +* **[7. External Resources to Keep Sharpening Your Axe](chapters/07_sharpening_your_axes.md)** @@ -27,24 +27,24 @@
-#### Introduction +#### Beginner
-* **[1. Hello World](demos/1_hello_world)** -* **[2. Functions, Constructors, Math, Errors](demos/2_functions)** -* **[3. Anchor and the Interface Definition Language](demos/3_anchor)** -* **[4. Solidity vs. Solana](demos/4_sol_vs_sol)** -* **[5. Rust, Structs, Custom Derive Macros](demos/5_rust)** -* **[6. Block Variables, Sysvars](demos/6_blocks)** -* **[7. Logs, Events, Transactions](demos/7_logs_events_txs)** -* **[8. Transaction Fees and Compute Units](demos/8_transactions)** -* **[9. Accounts and Storage](demos/9_accounts_and_storage)** +* **[1. Hello World](demos/01_hello_world)** +* **[2. Functions, Constructors, Math, Errors](demos/02_functions)** +* **[3. Anchor and the Interface Definition Language](demos/03_anchor)** +* **[4. Solidity vs. Solana](demos/04_sol_vs_sol)** +* **[5. Rust, Structs, Custom Derive Macros](demos/05_rust)** +* **[6. Block Variables, Sysvars](demos/06_blocks)** +* **[7. Logs, Events, Transactions](demos/07_logs_events_txs)** +* **[8. Transaction Fees and Compute Units](demos/08_transactions)** +* **[9. Accounts and Storage](demos/09_accounts_and_storage)**
-#### Intermediary +#### Advanced
@@ -58,7 +58,7 @@
-#### Advanced +#### Expert
diff --git a/chapters/1_intro.md b/chapters/01_intro.md similarity index 100% rename from chapters/1_intro.md rename to chapters/01_intro.md diff --git a/chapters/2_dev_env.md b/chapters/02_dev_env.md similarity index 100% rename from chapters/2_dev_env.md rename to chapters/02_dev_env.md diff --git a/chapters/3_anchor.md b/chapters/03_anchor.md similarity index 100% rename from chapters/3_anchor.md rename to chapters/03_anchor.md diff --git a/chapters/4_frontend.md b/chapters/04_frontend.md similarity index 100% rename from chapters/4_frontend.md rename to chapters/04_frontend.md diff --git a/chapters/5_programs_in_depth.md b/chapters/05_programs_in_depth.md similarity index 100% rename from chapters/5_programs_in_depth.md rename to chapters/05_programs_in_depth.md diff --git a/chapters/6_transfer_hooks.md b/chapters/06_transfer_hooks.md similarity index 100% rename from chapters/6_transfer_hooks.md rename to chapters/06_transfer_hooks.md diff --git a/chapters/7_sharpening_your_axes.md b/chapters/07_sharpening_your_axes.md similarity index 100% rename from chapters/7_sharpening_your_axes.md rename to chapters/07_sharpening_your_axes.md diff --git a/demos/.gitignore b/demos/.gitignore deleted file mode 100644 index 6985cf1b..00000000 --- a/demos/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information -*.pdb diff --git a/demos/01_hello_world/Cargo.toml b/demos/01_hello_world/Cargo.toml new file mode 100644 index 00000000..c4df1a82 --- /dev/null +++ b/demos/01_hello_world/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "hello_world" +version = "0.1.0" + +[lib] +name = "hello_world" +crate-type = ["cdylib", "lib"] + +[dependencies] +solana-program = "=1.18.8" \ No newline at end of file diff --git a/demos/01_hello_world/README.md b/demos/01_hello_world/README.md new file mode 100644 index 00000000..43a477fd --- /dev/null +++ b/demos/01_hello_world/README.md @@ -0,0 +1,135 @@ +# 🛹 Demo 1: Hello World + + +
+ +* In a separate terminal window, start your local cluster: + +```shell +solana-test-validator +``` + +
+ +* Initialize a new Rust library via Cargo: + +```shell +cargo init hello_world --lib +cd hello_world +cargo add solana-program +``` + +
+ +* This creates a very basic Solana Rust program following this layout: + +```shell +. +├── Cargo.toml +└── src + └── lib.rs +``` + +
+ +* You should modify `Cargo.toml` to the following: + +``` +[package] +name = "hello_world" +version = "0.1.0" + +[lib] +name = "hello_world" +crate-type = ["cdylib", "lib"] + +[dependencies] +// add the right version +solana-program = "=1.1x.x" +``` + + +
+ +* We will write this program inside `src/lib.rs`. + +* At the top, we import the `solana-program` crate and bring needed items into the local namespace: + +```rust +use solana_program::{ + account_info::AccountInfo, + entrypoint, + entrypoint::ProgramResult, + pubkey::Pubkey, + msg, + }; +``` + +
+ +* Every Solana program must define an `entrypoint` that tells the runtime where to start executing the code on-chain. + - The entry point should provide a public function named `process_instruction`: + +```rust +entrypoint!(process_instruction); + +pub fn process_instruction( + _program_id: &Pubkey, + _accounts: &[AccountInfo], + _instruction_data: &[u8] + ) -> ProgramResult { + + msg!("Only possible on Solana"); + + Ok(()) +} +``` + + +
+ +* Note that every on-chain program should return the `Ok` result enum with value `()`. + - This tells the Solana runtime that the program executed successfully. + +
+ + +#### Build and Deploy your Program + +
+ +* Let's build this hello world program, by running the following command from the root of the project: + +``` +cargo build-sbf +``` + +
+ +* This command will create the compiled program's `.so` file inside a folder called `./target/deploy`: + +``` +find . -name '*.so' +``` + +
+ +* Now, let's deploy it: + + +``` +solana program deploy ./target/deploy/hello_world.so +``` + +
+ +* When this program finishes being deployed, the program's public address (`program id`) is displayed. + +* You can check your Solana wallet's balance to see how much it costs to deploy this simple program. + +```shell +solana balance +``` + +
+ diff --git a/demos/01_hello_world/src/lib.rs b/demos/01_hello_world/src/lib.rs new file mode 100644 index 00000000..a704e2e1 --- /dev/null +++ b/demos/01_hello_world/src/lib.rs @@ -0,0 +1,23 @@ +extern crate solana_program; + +use solana_program::{ + account_info::AccountInfo, + entrypoint, + entrypoint::ProgramResult, + pubkey::Pubkey, + msg, + }; + + +entrypoint!(process_instruction); + +pub fn process_instruction( + _program_id: &Pubkey, + _accounts: &[AccountInfo], + _instruction_data: &[u8] + ) -> ProgramResult { + + msg!("Only possible on Solana"); + + Ok(()) +} \ No newline at end of file diff --git a/demos/2_functions/README.md b/demos/02_functions/README.md similarity index 100% rename from demos/2_functions/README.md rename to demos/02_functions/README.md diff --git a/demos/3_anchor/README.md b/demos/03_anchor/README.md similarity index 100% rename from demos/3_anchor/README.md rename to demos/03_anchor/README.md diff --git a/demos/4_sol_vs_sol/README.md b/demos/04_sol_vs_sol/README.md similarity index 100% rename from demos/4_sol_vs_sol/README.md rename to demos/04_sol_vs_sol/README.md diff --git a/demos/5_rust/README.md b/demos/05_rust/README.md similarity index 100% rename from demos/5_rust/README.md rename to demos/05_rust/README.md diff --git a/demos/6_blocks/README.md b/demos/06_blocks/README.md similarity index 100% rename from demos/6_blocks/README.md rename to demos/06_blocks/README.md diff --git a/demos/7_logs_events_txs/README.md b/demos/07_logs_events_txs/README.md similarity index 100% rename from demos/7_logs_events_txs/README.md rename to demos/07_logs_events_txs/README.md diff --git a/demos/8_transactions/README.md b/demos/08_transactions/README.md similarity index 100% rename from demos/8_transactions/README.md rename to demos/08_transactions/README.md diff --git a/demos/9_accounts_and_storage/README.md b/demos/09_accounts_and_storage/README.md similarity index 100% rename from demos/9_accounts_and_storage/README.md rename to demos/09_accounts_and_storage/README.md diff --git a/demos/1_hello_world/Cargo.toml b/demos/1_hello_world/Cargo.toml deleted file mode 100644 index e90bbfc3..00000000 --- a/demos/1_hello_world/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "hello_world" -version = "0.1.0" -edition = "2021" - - -[lib] -crate-type = ["cdylib", "lib"] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -solana-program = "1.17.25" - - - diff --git a/demos/1_hello_world/README.md b/demos/1_hello_world/README.md deleted file mode 100644 index 23623912..00000000 --- a/demos/1_hello_world/README.md +++ /dev/null @@ -1 +0,0 @@ -# 🛹 Hello World [IN CONSTRUCTION] \ No newline at end of file diff --git a/demos/1_hello_world/src/lib.rs b/demos/1_hello_world/src/lib.rs deleted file mode 100644 index 405ea444..00000000 --- a/demos/1_hello_world/src/lib.rs +++ /dev/null @@ -1,23 +0,0 @@ -use solana_program::{ - account_info::AccountInfo, - entrypoint, - entrypoint::ProgramResult, - pubkey::Pubkey, - msg, -}; - -// declare and export the program's entrypoint -entrypoint!(process_instruction); - -// program entrypoint's implementation -pub fn process_instruction( - program_id: &Pubkey, - accounts: &[AccountInfo], - instruction_data: &[u8] -) -> ProgramResult { - // log a message to the blockchain - msg!("Hello, world!"); - - // gracefully exit the program - Ok(()) -} \ No newline at end of file