Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add toolkit content #673

Merged
merged 26 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced/confirmation.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ decreased, users don't have enough time to submit their transaction.
Currently, Solana clusters require that transactions use blockhashes that are no
more than 151 blocks old.

> This [Github issue](https://github.com/solana-labs/solana/issues/23582)
> This [GitHub issue](https://github.com/solana-labs/solana/issues/23582)
> contains some calculations that estimate that mainnet-beta validators need
> about 150MB of memory to track transactions. This could be slimmed down in the
> future if necessary without decreasing expiration time as are detailed in that
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
metaOnly: true
title: Advanced Topics
sidebarSortOrder: 3
sidebarSortOrder: 5
---
3 changes: 2 additions & 1 deletion docs/core/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ specifies the privileges of accounts included in the transaction's account
address array. It is comprised of three bytes, each containing a u8 integer,
which collectively specify:

1. The number of required signatures for the transaction and message version number.
1. The number of required signatures for the transaction and message version
number.
2. The number of read-only account addresses that require signatures.
3. The number of read-only account addresses that do not require signatures.

Expand Down
2 changes: 1 addition & 1 deletion docs/programs/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Developing Programs
sidebarSortOrder: 2
sidebarSortOrder: 4
metaOnly: true
---
119 changes: 119 additions & 0 deletions docs/toolkit/best-practices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
title: Solana Smart Contract Best Practices
sidebarSortOrder: 3
sidebarLabel: Best Practices
---

> This is a beta version of the [Solana Toolkit](/docs/toolkit/index.md), and is
> still a WIP. Please post all feedback as a GitHub issue
> [here](https://github.com/solana-foundation/developer-content/issues/new?title=%5Btoolkit%5D%20).

## Optimize Compute Usage

To prevent abuse of computational resources, each transaction is allocated a
"compute budget". This budget specifies details about
[compute units](https://solana.com/docs/core/fees#compute-units) and includes:

- the compute costs associated with different types of operations the
transaction may perform (compute units consumed per operation),
- the maximum number of compute units that a transaction can consume (compute
unit limit),
- and the operational bounds the transaction must adhere to (like account data
size limits)

When the transaction consumes its entire compute budget (compute budget
exhaustion), or exceeds a bound such as attempting to exceed the
[max call stack depth](https://github.com/anza-xyz/agave/blob/b7bbe36918f23d98e2e73502e3c4cba78d395ba9/program-runtime/src/compute_budget.rs#L138)
or
[max loaded account](https://solana.com/docs/core/fees#accounts-data-size-limit)
data size limit, the runtime halts the transaction processing and returns an
error. Resulting in a failed transaction and no state changes (aside from the
transaction fee being
[collected](https://solana.com/docs/core/fees#fee-collection)).

### Additional References

- [How to Optimize Compute](https://solana.com/developers/guides/advanced/how-to-optimize-compute).
- [How to Request Optimal Compute](https://solana.com/developers/guides/advanced/how-to-request-optimal-compute)

## Saving Bumps

> Program Derived Address (PDAs) are addresses that PDAs are addresses that are
> deterministically derived and look like standard public keys, but have no
> associated private keys. These PDAs are derived using a numerical value,
> called a "bump", to guarantee that the PDA is off-curve and cannot have an
> associated private key. It "bumps" the address off the cryptographic curve.

Saving the bump to your Solana smart contract account state ensures
deterministic address generation, efficiency in address reconstruction, reduced
transaction failure, and consistency across invocations.

### Additional References

- [How to derive a PDA](/docs/core/pda.md#how-to-derive-a-pda)
- [PDA Bumps Core Concepts](/docs/core/pda.md#canonical-bump)
- [Bump Seed Canonicalization Lesson](https://solana.com/developers/courses/program-security/bump-seed-canonicalization)

## Payer-Authority Pattern

The Payer-Authority pattern is an elegant way to handle situations where the
account’s funder (payer) differs from the account’s owner or manager
(authority). By requiring separate signers and validating them in your onchain
logic, you can maintain clear, robust, and flexible ownership semantics in your
program.

### Shank Example

```rust
// Create a new account.
#[account(0, writable, signer, name="account", desc = "The address of the new account")]
#[account(1, writable, signer, name="payer", desc = "The account paying for the storage fees")]
#[account(2, optional, signer, name="authority", desc = "The authority signing for the account creation")]
#[account(3, name="system_program", desc = "The system program")]
CreateAccountV1(CreateAccountV1Args),
```

### Anchor Example

```rust
#[derive(Accounts)]
pub struct CreateAccount<'info> {
/// The address of the new account
#[account(init, payer = payer_one, space = 8 + NewAccount::MAXIMUM_SIZE)]
pub account: Account<'info, NewAccount>,

/// The account paying for the storage fees
#[account(mut)]
pub payer: Signer<'info>,

/// The authority signing for the account creation
pub authority: Option<Signer<'info>>,

// The system program
pub system_program: Program<'info, System>
}
```

### Additional References

- [Metaplex Guide on Payer-Authority Pattern](https://developers.metaplex.com/guides/general/payer-authority-pattern)
- [Helium Program Library using the Payer-Authority Pattern](https://github.com/helium/helium-program-library/blob/master/programs/data-credits/src/instructions/change_delegated_sub_dao_v0.rs#L18)

## Invariants

Implement invariants, which are functions that you can call at the end of your
instruction to assert specific properties because they help ensure the
correctness and reliability of programs.

```rust
require!(amount > 0, ErrorCode::InvalidAmount);
```

### Additional References

- [Complete Project Code Example](https://github.com/solana-developers/developer-bootcamp-2024/blob/main/project-9-token-lottery/programs/token-lottery/src/lib.rs#L291)

## Optimize Indexing

You can make indexing easier by placing static size fields at the beginning and
variable size fields at the end of your onchain structures.
91 changes: 91 additions & 0 deletions docs/toolkit/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
sidebarSortOrder: 1
sidebarLabel: Getting Started
title: Getting Started with the Solana Toolkit
---

> This is a beta version of the [Solana Toolkit](/docs/toolkit/index.md), and is
> still a WIP. Please post all feedback as a GitHub issue
> [here](https://github.com/solana-foundation/developer-content/issues/new?title=%5Btoolkit%5D%20).

The Solana Program development toolkit is publish as the
[`mucho` npm package](https://www.npmjs.com/package/mucho). The `mucho` command
will be used to run most of the Solana program development tools within the
toolkit - _mucho tools, one cli_.

## Installation

To get started, install The Solana Toolkit:

```shell
npx -y mucho@latest install
```

This will install the latest versions of the following:

- [Solana CLI / Agave Tool Suite](https://docs.anza.xyz/cli/): A command line
tool that allows developers to interact with the Solana blockchain, manage
accounts, send transactions, and deploy programs.
- [Mucho CLI](https://github.com/solana-developers/mucho) - a superset of
popular developer tools within the Solana ecosystem used to simplify the
development and testing of Solana blockchain programs.
- [Rust](https://doc.rust-lang.org/book/): The programming language that Solana
Smart Contracts are written in.
- [Anchor](https://www.anchor-lang.com/): A framework for writing Solana
programs that abstracts many complexities to speed up smart contract
development.
- [Fuzz Tester](https://ackee.xyz/trident/docs/latest/): Rust-based fuzzing
framework for Solana programs to help you ship secure code.
- [Code Coverage Tool](https://github.com/LimeChain/zest?tab=readme-ov-file): A
code coverage CLI tool for Solana programs.

## Generate a keypair

For a fresh installation of the [Solana CLI](https://docs.anza.xyz/cli/), you're
required to generate a new keypair.

```shell
solana-keygen new
```

_This will store the your new keypair at the Solana CLI's default path
(`~/.config/solana/id.json`) and print the pubkey that was generated._

## Get your keypair's public key

```shell
solana address
```

## Fund your keypair

```shell
solana airdrop 10 --url localhost
```

## Set your network configuration

Check your current config:

```shell
solana config get
```

To use this toolkit, update your config to connect to localhost:

```shell
solana config set --url localhost
```

To test locally, you must also spin up a local node to run on your localhost:

```shell
mucho validator
```

For a more information, read the
[Local Testing Guide](/docs/toolkit/local-validator.md).

## Next Steps

Now let's [Create a Solana Project](/docs/toolkit/projects/overview.md)!
18 changes: 18 additions & 0 deletions docs/toolkit/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: The Solana Toolkit
sidebarSortOrder: 3
---

> This is a beta version of the [Solana Toolkit](/docs/toolkit/index.md), and is
> still a WIP. Please post all feedback as a GitHub issue
> [here](https://github.com/solana-foundation/developer-content/issues/new?title=%5Btoolkit%5D%20).

The Solana Program development toolkit is published as the
[`mucho` npm package](https://www.npmjs.com/package/mucho). The `mucho` command
will be used to run most of the Solana program development tools - _mucho tools,
one cli_.

You can contribute to this
[Toolkit book on GitHub](https://github.com/solana-foundation/developer-content/tree/main/docs/toolkit).

Now let's [Get Started](/docs/toolkit/getting-started.md)!
Loading
Loading