Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Add hello world demo (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
urani-engineering-helper authored Mar 27, 2024
1 parent 6c0d52b commit 4c6134c
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 73 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

<br>

* **[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)**



Expand All @@ -27,24 +27,24 @@

<br>

#### Introduction
#### Beginner

<br>

* **[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)**


<br>

#### Intermediary
#### Advanced

<br>

Expand All @@ -58,7 +58,7 @@

<br>

#### Advanced
#### Expert

<br>

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 0 additions & 14 deletions demos/.gitignore

This file was deleted.

10 changes: 10 additions & 0 deletions demos/01_hello_world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
135 changes: 135 additions & 0 deletions demos/01_hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# 🛹 Demo 1: Hello World


<br>

* In a separate terminal window, start your local cluster:

```shell
solana-test-validator
```

<br>

* Initialize a new Rust library via Cargo:

```shell
cargo init hello_world --lib
cd hello_world
cargo add solana-program
```

<br>

* This creates a very basic Solana Rust program following this layout:

```shell
.
├── Cargo.toml
└── src
└── lib.rs
```

<br>

* 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"
```


<br>

* 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,
};
```

<br>

* 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(())
}
```


<br>

* Note that every on-chain program should return the `Ok` result enum with value `()`.
- This tells the Solana runtime that the program executed successfully.

<br>


#### Build and Deploy your Program

<br>

* Let's build this hello world program, by running the following command from the root of the project:

```
cargo build-sbf
```

<br>

* This command will create the compiled program's `.so` file inside a folder called `./target/deploy`:

```
find . -name '*.so'
```

<br>

* Now, let's deploy it:


```
solana program deploy ./target/deploy/hello_world.so
```

<br>

* 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
```

<br>

23 changes: 23 additions & 0 deletions demos/01_hello_world/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 0 additions & 16 deletions demos/1_hello_world/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion demos/1_hello_world/README.md

This file was deleted.

23 changes: 0 additions & 23 deletions demos/1_hello_world/src/lib.rs

This file was deleted.

0 comments on commit 4c6134c

Please sign in to comment.