forked from solana-developers/solana-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
63 changed files
with
8,375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: Cooking with Solana | ||
head: | ||
- - meta | ||
- name: title | ||
content: Solana Cookbook | Home to Solana References | ||
- - meta | ||
- name: og:title | ||
content: Solana Cookbook | Home to Solana References | ||
- - meta | ||
- name: description | ||
content: The Solana cookbook is a collection of useful examples and references for building on Solana | ||
- - meta | ||
- name: og:description | ||
content: The Solana cookbook is a collection of useful examples and references for building on Solana | ||
- - meta | ||
- name: og:image | ||
content: https://solanacookbook.com/cookbook-sharing-card.png | ||
- - meta | ||
- name: og:image:alt | ||
content: Solana splash card | ||
- - meta | ||
- name: twitter:card | ||
content: summary | ||
- - meta | ||
- name: twitter:site | ||
content: "@solanacookbook" | ||
- - meta | ||
- name: twitter:image | ||
content: "https://solanacookbook.com/cookbook-sharing-card.png" | ||
- - meta | ||
- name: robots | ||
content: index,follow,noodp | ||
- - meta | ||
- name: googlebot | ||
content: index,follow | ||
footer: MIT Licensed | ||
--- | ||
|
||
# Cooking with Solana | ||
|
||
The *Solana Cookbook* is a developer resource that | ||
provides the essential concepts and references for | ||
building applications on Solana. Each concept and | ||
reference will focus on specific aspects of Solana | ||
development while providing additional details and usage | ||
examples. | ||
|
||
## Contributing | ||
|
||
The Cookbook is designed in a way that makes it easy for | ||
new Solana developers to contribute. Even if you | ||
don't know how to do something, contributing to the | ||
cookbook is a great way to learn! | ||
|
||
Check out all open issues [here](https://github.com/solana-developers/solana-cookbook/issues). Contribution guidelines [here](https://github.com/solana-developers/solana-cookbook#contributing). If you find the cookbook is missing a suggestion, please add an issue. | ||
|
||
## How to Read the Cookbook | ||
|
||
The Solana Cookbook is split into different sections, each aimed at a different goal. | ||
|
||
| Section | Description | | ||
|---------------|-----------------------------------------------------------------| | ||
| Core Concepts | Building blocks of Solana that are good to know for development | | ||
| Guides | Snack-sized guides about different tools for development | | ||
| References | References to commonly needed code snippets | |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
--- | ||
title: Accounts | ||
head: | ||
- - meta | ||
- name: title | ||
content: Solana Cookbook | Accounts | ||
- - meta | ||
- name: og:title | ||
content: Solana Cookbook | Accounts | ||
- - meta | ||
- name: description | ||
content: Accounts are an essential building block for developing on Solana. Learn about Accounts and more Core Concepts at The Solana cookbook. | ||
- - meta | ||
- name: og:description | ||
content: Accounts are an essential building block for developing on Solana. Learn about Accounts and more Core Concepts at The Solana cookbook. | ||
- - meta | ||
- name: og:image | ||
content: https://solanacookbook.com/cookbook-sharing-card.png | ||
- - meta | ||
- name: og:image:alt | ||
content: Solana splash card | ||
- - meta | ||
- name: twitter:card | ||
content: summary | ||
- - meta | ||
- name: twitter:site | ||
content: "@solanacookbook" | ||
- - meta | ||
- name: twitter:image | ||
content: "https://solanacookbook.com/cookbook-sharing-card.png" | ||
- - meta | ||
- name: robots | ||
content: index,follow,noodp | ||
- - meta | ||
- name: googlebot | ||
content: index,follow | ||
footer: MIT Licensed | ||
--- | ||
|
||
# Accounts | ||
|
||
Accounts within Solana are used to store state. They are an essential | ||
building block for developing on Solana. | ||
|
||
## Facts | ||
|
||
::: tip Fact Sheet | ||
|
||
- Accounts are used to store data | ||
- Each account has a unique address | ||
- Accounts have a max size of 10MB (10 Mega Bytes) | ||
- PDA accounts have a max size of 10KB (10 Kilo Bytes) | ||
- PDA accounts can be used to sign on behalf of a program | ||
- Accounts size are fixed at creation time, but can be adjusted using [realloc](https://solanacookbook.com/references/programs.html#how-to-change-account-size) | ||
- Account data storage is paid with rent | ||
- Default account owner is the System Program | ||
::: | ||
|
||
## Deep Dive | ||
|
||
### Account Model | ||
|
||
There are 3 kinds of accounts on Solana: | ||
|
||
- Data accounts store data | ||
- Program accounts store executable programs | ||
- Native accounts that indicate native programs on Solana such as System, Stake, and Vote | ||
|
||
Within data accounts, there are 2 types: | ||
|
||
- System owned accounts | ||
- PDA (Program Derived Address) accounts | ||
|
||
Each account has an address (usually a public key) and an owner | ||
(address of a program account). The full field list an account stores | ||
is found below. | ||
|
||
| Field | Description | | ||
| ---------- | ---------------------------------------------- | | ||
| lamports | The number of lamports owned by this account | | ||
| owner | The program owner of this account | | ||
| executable | Whether this account can process instructions | | ||
| data | The raw data byte array stored by this account | | ||
| rent_epoch | The next epoch that this account will owe rent | | ||
|
||
There are a few important ownership rules: | ||
|
||
- Only a data account's owner can modify its data and debit lamports | ||
- Anyone is allowed to credit lamports to a data account | ||
- The owner of an account may assign a new owner if the account's data is zeroed out | ||
|
||
Program accounts do not store state. | ||
|
||
For example, if you have a counter program that lets you increment a counter, you | ||
must create two accounts, one account to store the program's code, and one to store | ||
the counter. | ||
|
||
![](./account_example.jpeg) | ||
|
||
To prevent an account from being deleted, you must pay rent. | ||
|
||
### Rent | ||
|
||
Storing data on accounts costs SOL to maintain, and it is funded by what is called | ||
rent. If you maintain a minimum balance equivalent to 2 years of rent payments in an | ||
account, your account will be exempt from paying rent. You can retrieve rent by closing | ||
the account and sending the lamports back to your wallet. | ||
|
||
Rent is paid during two different timings: | ||
|
||
1. When referenced by a transaction | ||
2. Once an epoch | ||
|
||
A percentage of rent collected by accounts is destroyed, while the rest is distributed | ||
to vote accounts at the end of every slot. | ||
|
||
If the account does not have enough to pay rent, the account will be deallocated and the data | ||
removed. | ||
|
||
It is also important to note that new accounts must be rent exempt. | ||
|
||
## Other Resources | ||
|
||
- [Solana Account Model](https://solana.wiki/zh-cn/docs/account-model/#account-storage) | ||
- [Official Documentation](https://docs.solana.com/developing/programming-model/accounts) | ||
- [pencilflip account thread](https://twitter.com/pencilflip/status/1452402100470644739) | ||
|
||
### Credit | ||
|
||
This core concept is credited to Pencilflip. [Follow him on Twitter](https://twitter.com/intent/user?screen_name=pencilflip). |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
title: Program Derived Addresses (PDAs) | ||
head: | ||
- - meta | ||
- name: title | ||
content: Solana Cookbook | PDAs | ||
- - meta | ||
- name: og:title | ||
content: Solana Cookbook | PDAs | ||
- - meta | ||
- name: description | ||
content: PDAs are home to accounts that are designed to be controlled by a specific program. Learn about PDAs and more Core Concepts at The Solana cookbook. | ||
- - meta | ||
- name: og:description | ||
content: PDAs are home to accounts that are designed to be controlled by a specific program. Learn about PDAs and more Core Concepts at The Solana cookbook. | ||
- - meta | ||
- name: og:image | ||
content: https://solanacookbook.com/cookbook-sharing-card.png | ||
- - meta | ||
- name: og:image:alt | ||
content: Solana splash card | ||
- - meta | ||
- name: twitter:card | ||
content: summary | ||
- - meta | ||
- name: twitter:site | ||
content: "@solanacookbook" | ||
- - meta | ||
- name: twitter:image | ||
content: "https://solanacookbook.com/cookbook-sharing-card.png" | ||
- - meta | ||
- name: robots | ||
content: index,follow,noodp | ||
- - meta | ||
- name: googlebot | ||
content: index,follow | ||
footer: MIT Licensed | ||
--- | ||
|
||
# Program Derived Addresses (PDAs) | ||
|
||
Program Derived Addresses (PDAs) are home to accounts that are designed to be controlled by a specific program. With PDAs, programs can programmatically sign for certain addresses without needing a private key. PDAs serve as the foundation for [Cross-Program Invocation](https://docs.solana.com/developing/programming-model/calling-between-programs#cross-program-invocations), which allows Solana apps to be composable with one another. | ||
|
||
## Facts | ||
|
||
::: tip Fact Sheet | ||
- PDAs are 32 byte strings that look like public keys, but don’t have corresponding private keys | ||
- `findProgramAddress` will deterministically derive a PDA from a programId and seeds (collection of bytes) | ||
- A bump (one byte) is used to push a potential PDA off the ed25519 elliptic curve | ||
- Programs can sign for their PDAs by providing the seeds and bump to [invoke_signed](https://docs.solana.com/developing/programming-model/calling-between-programs#program-signed-accounts) | ||
- A PDA can only be signed by the program from which it was derived | ||
- In addition to allowing for programs to sign for different instructions, PDAs also provide a hashmap-like interface for [indexing accounts](../guides/account-maps.md) | ||
::: | ||
|
||
## Deep Dive | ||
|
||
PDAs are an essential building block for developing programs on Solana. With PDAs, programs can sign for accounts while guaranteeing that no external user could also generate a valid signature for the same account. In addition to signing for accounts, certain programs can also modify accounts held at their PDAs. | ||
|
||
![Accounts matrix](./account-matrix.png) | ||
|
||
<small style="text-align:center;display:block;">Image courtesy of <a href="https://twitter.com/pencilflip">Pencilflip</a></small> | ||
|
||
### Generating PDAs | ||
|
||
To understand the concept behind PDAs, it may be helpful to consider that PDAs are not technically created, but rather found. PDAs are generated from a combination of seeds (such as the string `“vote_account”`) and a program id. This combination of seeds and program id is then run through a sha256 hash function to see whether or not they generate a public key that lies on the ed25519 elliptic curve. | ||
|
||
In running our program id and seeds through a hash function, there is a ~50% chance that we actually end up with a valid public key that does lie on the elliptic curve. In this case, we simply add something to fudge our input a little bit and try again. The technical term for this fudge factor is a bump. In Solana, we start with bump = 255 and simply iterate down through bump = 254, bump = 253, etc. until we get an address that is not on the elliptic curve. This may seem rudimentary, but once found it gives us a deterministic way of deriving the same PDA over and over again. | ||
|
||
![PDA on the ellipitic curve](./pda-curve.png) | ||
|
||
### Interacting with PDAs | ||
|
||
When a PDA is generated, `findProgramAddress` will return both the address and the bump used to kick the address off of the elliptic curve. Armed with this bump, a program can then [sign](../references/accounts.md#sign-with-a-pda) for any instruction that requires its PDA. In order to sign, programs should pass the instruction, the list of accounts, and the seeds and bump used to derive the PDA to `invoke_signed`. In addition to signing for instructions, PDAs must also sign for their own creation via `invoke_signed`. | ||
|
||
When building with PDAs, it is common to [store the bump seed](https://github.com/solana-labs/solana-program-library/blob/78e29e9238e555967b9125799d7d420d7d12b959/token-swap/program/src/state.rs#L100) in the account data itself. This allows developers to easily validate a PDA without having to pass in the bump as an instruction argument. | ||
|
||
## Other Resources | ||
- [Official Documentation](https://docs.solana.com/developing/programming-model/calling-between-programs#program-derived-addresses) | ||
- [Understanding Program Derived Addresses](https://www.brianfriel.xyz/understanding-program-derived-addresses/) |
Oops, something went wrong.