Skip to content

Commit

Permalink
Add Chapter 3: Anchor and Anchor + CPI demo (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
urani-engineering authored Mar 27, 2024
1 parent 4c6134c commit 8ef807a
Show file tree
Hide file tree
Showing 32 changed files with 8,406 additions and 95 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
<br>

* **[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)**
* **[2. Anchor and CPI](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)**
Expand Down
98 changes: 66 additions & 32 deletions chapters/01_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,7 @@ Transaction fees are calculated based on two main parts:

<br>

---

### Programs

<br>

* Solana Programs are the executable code that interprets the instructions sent inside transactions on the blockchain.

* They run on top of the Sealevel runtime (Solana's parallel and high-speed processing model).
Programs are special types of accounts that are marked as "executable".

* Programs can own accounts and change the data of the accounts they own. Unlike other blockchains, they can also be upgraded by their owner.

* Programs are stateless, as the primary data stored in a program account is the compiled SBF code.

* Programs can be:
- **Native Programs**: programs built directly into the core of the Solana blockchain.
- **Chain Programs**: written by users and deployed directly to the blockchain for anyone to interact and execute. The Solana Labs also keep a library of them, the [Solana Program Library](https://spl.solana.com/).

* The instructions's `program_id` specifies which program will process the instructions.

* Programs on Solana don't store data or state between transactions: these are stored in accounts.

<br>

---

Expand All @@ -115,22 +92,26 @@ Programs are special types of accounts that are marked as "executable".

<br>

* Accounts on Solana are storage spaces that can hold data up to 10MB.
* Accounts on Solana are storage spaces that can hold data up to 10MB.

* Solana clients use an address (a 256-bit public key) to find an account.

* Accounts can hold arbitrary persistent data and hold ownership metadata for the runtime
* The metadata also includes the lifetime info and is expressed by lamports.

* Solana clients use an address (a 256-bit public key) to find an account.
* Accounts are referenced by an instruction representing the on-chain state and server as both the inputs and outputs of a program.

* Accounts can be signers if the transaction includes their addresses as a digital signature.

* Accounts can be treated as read-only by transactions. This enables parallel account processing between transactions.
* Accounts can be treated as read-only by transactions.
- This enables parallel account processing between transactions.

* An account is a program if it's marked as "executable" in its metadata.
- Accounts that store programs are owned by the `BPFLoader`, a program that can be used to deploy and upgrade other programs.
- The `BPFLoader` is owned by the **Native Loader**.

* If a program is marked as final (non-upgradeable), the runtime makes the account's data (the program) immutable.

* Accounts are referenced by an instruction representing the on-chain state and server as both the inputs and outputs of a program.

<br>

Expand All @@ -150,9 +131,54 @@ Programs are special types of accounts that are marked as "executable".

<br>



---

### Cross-Program Invocation (CPI)
### Programs

<br>

* Solana Programs are the executable code that interprets the instructions sent inside transactions on the blockchain.

* They run on top of the Sealevel runtime (Solana's parallel and high-speed processing model).
Programs are special types of accounts that are marked as "executable".

* Programs can own accounts and change the data of the accounts they own. Unlike other blockchains, they can also be upgraded by their owner.

* Programs are stateless, as the primary data stored in a program account is the compiled SBF code.

* Programs can be:
- **Native Programs**: programs built directly into the core of the Solana blockchain.
- **Chain Programs**: written by users and deployed directly to the blockchain for anyone to interact and execute. The Solana Labs also keep a library of them, the [Solana Program Library](https://spl.solana.com/).

* The instructions's `program_id` specifies which program will process the instructions.

* Programs on Solana don't store data or state between transactions: these are stored in accounts.

<br>



#### Memory on Solana

<br>


* Memory inside a Solana cluster can be thought of as a monolithic heap of data. All state lives in this heap.

* Programs each have access ot their own part of the heap.

* A memory region is "account" (and some programs own thousands of independent account)
- Each memory region has a program that manages it (the `owner`).



<br>



#### Cross-Program Invocation (CPI)

<br>

Expand All @@ -168,6 +194,12 @@ Programs are special types of accounts that are marked as "executable".

<br>


> [!IMPORTANT]
> When writing CPI, it's important to not pull in the dependent program's entrypoint symbols (because they may conflict with the program's own). To avoid this, programs should define an `no-entrypoint` feature in `Cargo.toml`.
<br>

#### Program Derived Address (PDA)

<br>
Expand Down Expand Up @@ -195,8 +227,6 @@ Programs are special types of accounts that are marked as "executable".
* Programs can deterministically derive any number of addresses by using seeds. These seeds can symbolically identify how the addresses are used.




<br>

---
Expand All @@ -205,10 +235,13 @@ Programs are special types of accounts that are marked as "executable".

<br>


* A wallet is a pair of public and private keys used to verify actions on the blockchain.

* The public key is used to identify the account, and the private key is used to sign transactions.

* You can choose your wallet from [this list](https://solana.com/ecosystem/explore?categories=wallet).

<br>

---
Expand All @@ -230,9 +263,10 @@ Programs are special types of accounts that are marked as "executable".

<br>

* [Solana Foundation dev documentation](https://solana.com/docs#start-learning)
* [Solana Transactions](https://solana.com/docs/core/transactions)
* [Solana Programs](https://solana.com/docs/core/programs#native-programs)
* [Accounts and Storing State](https://solana.com/docs/core/accounts)
* [Cross-Program Invocation](https://solana.com/docs/core/cpi)
* [Terminology](https://solana.com/docs/terminology#instruction)
* [Wallets Explained](https://solana.com/developers/guides/intro/wallets-explained)
* [Terminology](https://solana.com/docs/terminology#instruction) and [FAQ](https://solana.com/docs/programs/faq)
* [Solana Beta StackExchange](https://solana.stackexchange.com/)
71 changes: 56 additions & 15 deletions chapters/02_dev_env.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* In each environment, you will be using one of three networks:
* **Mainnet**: the production network where all the action happens. Transactions cost real money.
* **Testnet**: used for stress testing recent releases. Focused on network performance, stability, and validator behavior.
* **Devnet**: the primary network for development (tokens are not real, and you can get them from [faucets](more_resources.md)).
* **Devnet**: the primary network for development (these tokens have no financial value, and you can get them from [these faucets](https://github.com/urani-labs/solana-dev-onboarding-rs/blob/main/chapters/07_sharpening_your_axes.md#faucets).

<br>

Expand All @@ -22,20 +22,21 @@

<br>

* Setup the development environment with a local blockchain cluster.
1. Setup the development environment with a local blockchain cluster.

* Create a filesystem wallet and airdrop Solana tokens to it.
2. Create a filesystem wallet and airdrop Solana tokens to it.

* Write the program.
3. Write programs.
- Programs export a known `entrypoint` symbol which the Solana runtime looks up and calls when invoking a program.

* Compile the program (down to [Berkley Packet Filter](https://solana.com/docs/programs/faq#berkeley-packet-filter-bpf) byte-code that will then be deployed to the blockchain).
4. Compile the program (down to [Berkley Packet Filter](https://solana.com/docs/programs/faq#berkeley-packet-filter-bpf) byte-code that will then be deployed to the blockchain).

* Generate the program's public address (a new unique keypair, on which the pubkey is the `programId`).
5. Generate the program's public address (a new unique keypair, on which the pubkey is the `programId`).

* Deploy the program to the selected blockchain cluster by creating transactions containing the program's byte-code.
6. Deploy the program to the selected blockchain cluster by creating transactions containing the program's byte-code.

* Once the entire program is in the blockchain, a final transaction is sent to write all of the buffered byte-code to the program's data account.
- This either marks the new program as executable or completes upgrading an existing program.
7. Once the entire program is in the blockchain, a final transaction is sent to write all of the buffered byte-code to the program's data account.
- This either marks the new program as executable or completes upgrading an existing program.

<br>

Expand All @@ -50,7 +51,7 @@

<br>

* Install [Rust](https://rustup.rs/).
* Install [Rust](https://rustup.rs/) and [Yarn](https://yarnpkg.com/getting-started/install).


<br>
Expand All @@ -65,7 +66,7 @@
- building Solana programs
- deploying your programs to the blockchain

* Install the Anchor framework using [these instructions](https://solana.com/developers/guides/getstarted/setup-local-development#4-install-anchor-for-solana).
* Install the Anchor framework using [these instructions](https://github.com/urani-labs/solana-dev-onboarding-rs/blob/main/chapters/03_anchor.md).

<br>

Expand Down Expand Up @@ -140,7 +141,11 @@ solana balance

* Devnet endpoint: `https://api.devnet.solana.com`.

* From the CLI, one can connect with `solana config set --url https://api.devnet.solana.com`.
* From the CLI, one can connect with:

```shell
solana config set --url https://api.devnet.solana.com
```

<br>

Expand All @@ -149,14 +154,50 @@ solana balance

<br>

* Testnet serves as Solana's core contributors stress test.


* Gossip endpoint at `entrypoint.testnet.solana.com:8001`.

* Devnet endpoint: `https://api.testnet.solana.com`.

* From the CLI, one can connect with:

```shell
solana config set --url https://api.testnet.solana.com
```

<br>


#### Mainnet

<br>

* Solana's permissionless, persistent cluster.


* Gossip endpoint at `entrypoint.mainnet-beta.solana.com:8001`.

* Devnet endpoint: `https://api.mainnet-beta.solana.com`.

* From the CLI, one can connect with:

```shell
solana config set --url https://api.mainnet-beta.solana.com
```

<br>

#### Demo 1: Hello World


---

### Demo 1: Hello World

<br>

* Test your setup by running [this hello world program](https://github.com/urani-labs/solana-dev-onboarding-rs/tree/main/demos/1_hello_world).
* Test your setup by running [this hello world program](https://github.com/urani-labs/solana-dev-onboarding-rs/tree/main/demos/01_hello_world).

<br>

Expand Down Expand Up @@ -211,6 +252,6 @@ solana program deploy <PROGRAM_FILEPATH>
<br>

* [Setup local dev, by Solana Labs](https://solana.com/developers/guides/getstarted/setup-local-development)
* [Intro to Solana development (using only your browser)](https://solana.com/developers/guides/getstarted/hello-world-in-your-browser)
* [Intro to Solana development (on your browser)](https://solana.com/developers/guides/getstarted/hello-world-in-your-browser)
* [Reference for many `solana-cli` commands](https://docs.solanalabs.com/cli/examples/deploy-a-program)
* [Seahorse: Python's wrapper for Anchor framework](https://seahorse.dev/)
Loading

0 comments on commit 8ef807a

Please sign in to comment.