docs: Add Solana Playground quickstart page, and add examples page for anchor contraints and types (#2703)
This commit is contained in:
parent
c58d2f232e
commit
227ccb22fd
Binary file not shown.
After Width: | Height: | Size: 439 KiB |
Binary file not shown.
After Width: | Height: | Size: 408 KiB |
Binary file not shown.
After Width: | Height: | Size: 257 KiB |
Binary file not shown.
After Width: | Height: | Size: 301 KiB |
Binary file not shown.
After Width: | Height: | Size: 406 KiB |
Binary file not shown.
After Width: | Height: | Size: 263 KiB |
|
@ -26,6 +26,7 @@ const navigation = [
|
|||
title: 'Getting Started',
|
||||
links: [
|
||||
{ title: 'Introduction', href: '/' },
|
||||
{ title: 'Quickstart', href: '/docs/solana-playground' },
|
||||
{ title: 'Installation', href: '/docs/installation' },
|
||||
{ title: 'Hello World', href: '/docs/hello-world' },
|
||||
{ title: 'Intro to Solana', href: '/docs/intro-to-solana' },
|
||||
|
@ -67,6 +68,8 @@ const navigation = [
|
|||
{
|
||||
title: 'References',
|
||||
links: [
|
||||
{ title: 'Account Constraints', href: '/docs/account-constraints' },
|
||||
{ title: 'Account Types', href: '/docs/account-types' },
|
||||
{ title: 'Anchor.toml', href: '/docs/manifest' },
|
||||
{ title: 'CLI', href: '/docs/cli' },
|
||||
{ title: 'AVM', href: '/docs/avm' },
|
||||
|
|
|
@ -0,0 +1,331 @@
|
|||
---
|
||||
title: Account Constraints
|
||||
description: Anchor Account Constraint Examples
|
||||
---
|
||||
|
||||
Minimal reference examples for Anchor account [constraints](https://docs.rs/anchor-lang/latest/anchor_lang/derive.Accounts.html).
|
||||
|
||||
## Instruction Attribute
|
||||
|
||||
{% table %}
|
||||
|
||||
- Attribute
|
||||
- Example
|
||||
- Description
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[derive(Accounts)]
|
||||
#[instruction(...)]
|
||||
pub struct Initialize<'info> {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/instruction)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/instruction)
|
||||
- You can access the instruction’s arguments with the #[instruction(..)] attribute.
|
||||
You have to list them in the same order as in the instruction but you can omit all arguments after the last one you need.
|
||||
|
||||
{% /table %}
|
||||
|
||||
## Normal Constraints
|
||||
|
||||
{% table %}
|
||||
|
||||
- Attribute
|
||||
- Example
|
||||
- Description
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(signer)]
|
||||
#[account(signer @ <custom_error>)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/signer)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/signer)
|
||||
- Checks the given account signed the transaction. Custom errors are supported via @. Consider using the Signer type if you would only have this constraint on the account.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(mut)]
|
||||
#[account(mut @ <custom_error>)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mut)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mut)
|
||||
- Checks the given account is mutable.
|
||||
Makes anchor persist any state changes.
|
||||
Custom errors are supported via @.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
init,
|
||||
payer = <target_account>,
|
||||
space = <num_bytes>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init)
|
||||
- Creates the account via a CPI to the system program and initializes it (sets its account discriminator).
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
init_if_needed,
|
||||
payer = <target_account>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
init_if_needed,
|
||||
payer = <target_account>,
|
||||
space = <num_bytes>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init_if_needed)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/init_if_needed)
|
||||
- Exact same functionality as the init constraint but only runs if the account does not exist yet.
|
||||
|
||||
This feature should be used with care and is therefore behind a feature flag. You can enable it by importing anchor-lang with the init-if-needed cargo feature.
|
||||
When using init_if_needed, you need to make sure you properly protect yourself against re-initialization attacks.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
seeds = <seeds>,
|
||||
bump
|
||||
)]
|
||||
|
||||
#[account(
|
||||
seeds = <seeds>,
|
||||
bump,
|
||||
seeds::program = <expr>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
seeds = <seeds>,
|
||||
bump = <expr>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
seeds = <seeds>,
|
||||
bump = <expr>,
|
||||
seeds::program = <expr>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/seed-bump)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/seed-bump)
|
||||
- Checks that given account is a PDA derived from the currently executing program, the seeds, and if provided, the bump.
|
||||
If not provided, anchor uses the canonical bump.
|
||||
Add seeds::program = <expr> to derive the PDA from a different program than the currently executing one.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
has_one = <target_account>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
has_one = <target_account> @ <custom_error>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/has_one)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/has_one)
|
||||
- Checks the target_account field on the account matches the key of the target_account field in the Accounts struct.
|
||||
Custom errors are supported via @.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(address = <expr>)]
|
||||
#[account(address = <expr> @ <custom_error>)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/address)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/address)
|
||||
- Checks the account key matches the pubkey.
|
||||
Custom errors are supported via @.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(owner = <expr>)]
|
||||
#[account(owner = <expr> @ <custom_error>)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/owner)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/owner)
|
||||
- Checks the account owner matches expr.
|
||||
Custom errors are supported via @.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(executable)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/executable)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/executable)
|
||||
- Checks the account is executable (i.e. the account is a program).
|
||||
You may want to use the Program type instead.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(rent_exempt = skip)]
|
||||
#[account(rent_exempt = enforce)]
|
||||
```
|
||||
|
||||
- Github
|
||||
Solpg
|
||||
- Enforces rent exemption with = enforce.
|
||||
Skips rent exemption check that would normally be done through other constraints with = skip, e.g. when used with the zero constraint
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(zero)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/zero)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/zero)
|
||||
- Checks the account discriminator is zero.
|
||||
|
||||
Use this constraint if you want to create an account in a previous instruction and then initialize it in your instruction instead of using init. This is necessary for accounts that are larger than 10 Kibibyte because those accounts cannot be created via a CPI (which is what init would do).
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(close = <target_account>)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/close)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/close)
|
||||
- Marks the account as closed at the end of the instruction’s execution (sets its discriminator to the CLOSED_ACCOUNT_DISCRIMINATOR) and sends its lamports to the specified account.
|
||||
|
||||
Setting the discriminator to a special variant makes account revival attacks (where a subsequent instruction adds the rent exemption lamports again) impossible.
|
||||
|
||||
Requires mut to exist on the account.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(constraint = <expr>)]
|
||||
#[account(
|
||||
constraint = <expr> @ <custom_error>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/constraint)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/constraint)
|
||||
- Constraint that checks whether the given expression evaluates to true.
|
||||
Use this when no other constraint fits your use case.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
realloc = <space>,
|
||||
realloc::payer = <target>,
|
||||
realloc::zero = <bool>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/realloc)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/realloc)
|
||||
- Used to realloc program account space at the beginning of an instruction.
|
||||
|
||||
{% /table %}
|
||||
|
||||
## SPL Constraints
|
||||
|
||||
{% table %}
|
||||
|
||||
- Attribute
|
||||
- Example
|
||||
- Description
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
token::mint = <target_account>,
|
||||
token::authority = <target_account>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
token::mint = <target_account>,
|
||||
token::authority = <target_account>,
|
||||
token::token_program = <target_account>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token)
|
||||
- Can be used as a check or with init to create a token account with the given mint address and authority.
|
||||
When used as a check, it's possible to only specify a subset of the constraints.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
mint::authority = <target_account>,
|
||||
mint::decimals = <expr>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
mint::authority = <target_account>,
|
||||
mint::decimals = <expr>,
|
||||
mint::freeze_authority = <target_account>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mint)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/mint)
|
||||
- Can be used as a check or with init to create a mint account with the given mint decimals and mint authority.
|
||||
The freeze authority is optional when used with init.
|
||||
When used as a check, it's possible to only specify a subset of the constraints.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(
|
||||
associated_token::mint = <target_account>,
|
||||
associated_token::authority = <target_account>
|
||||
)]
|
||||
|
||||
#[account(
|
||||
associated_token::mint = <target_account>,
|
||||
associated_token::authority = <target_account>,
|
||||
associated_token::token_program = <target_account>
|
||||
)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/associated_token)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/associated_token)
|
||||
- Can be used as a standalone as a check or with init to create an associated token account with the given mint address and authority.
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
#[account(*::token_program = <target_account>)]
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token_program)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-constraints/token_program)
|
||||
- The token_program can optionally be overridden.
|
||||
|
||||
{% /table %}
|
|
@ -0,0 +1,135 @@
|
|||
---
|
||||
title: Account Types
|
||||
description: Anchor Account Type Examples
|
||||
---
|
||||
|
||||
Minimal reference examples for Anchor [account types](https://docs.rs/anchor-lang/latest/anchor_lang/accounts/index.html).
|
||||
|
||||
{% table %}
|
||||
|
||||
- Type
|
||||
- Example
|
||||
- Description
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Account<'info, T>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Account)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Account)
|
||||
- Account container that checks ownership on deserialization
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
AccountInfo<'info>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/AccountInfo)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/AccountInfo)
|
||||
- AccountInfo can be used as a type but Unchecked Account should be used instead
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
AccountLoader<'info, T>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/AccountLoader)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/AccountLoader)
|
||||
- Type facilitating on demand zero copy deserialization
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Box<Account<'info, T>>
|
||||
Box<InterfaceAccount<'info, T>>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Box)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Box)
|
||||
- Box type to save stack space
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Interface<'info, T>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Interface)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Interface)
|
||||
- Type validating that the account is one of a set of given Programs
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
InterfaceAccount<'info, T>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/InterfaceAccount)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/InterfaceAccount)
|
||||
- Account container that checks ownership on deserialization
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Option<Account<'info, T>>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Option)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Option)
|
||||
- Option type for optional accounts
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Program<'info, T>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Program)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Program)
|
||||
- Type validating that the account is the given Program
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Signer<'info>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Signer)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Signer)
|
||||
- Type validating that the account signed the transaction
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
SystemAccount<'info>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/SystemAccount)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/SystemAccount)
|
||||
- Type validating that the account is owned by the system program
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
Sysvar<'info, T>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/Sysvar)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/Sysvar)
|
||||
- Type validating that the account is a sysvar and deserializing it
|
||||
|
||||
---
|
||||
|
||||
- ```rust
|
||||
UncheckedAccount<'info>
|
||||
```
|
||||
|
||||
- [Github](https://github.com/solana-developers/anchor-examples/tree/main/account-types/UncheckedAccount)
|
||||
[Solpg](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/account-types/UncheckedAccount)
|
||||
- Explicit wrapper for AccountInfo types to emphasize that no checks are performed
|
||||
|
||||
{% /table %}
|
|
@ -0,0 +1,164 @@
|
|||
---
|
||||
title: Quickstart
|
||||
description: Getting Started with Solana Playground
|
||||
---
|
||||
|
||||
## Solana Playground
|
||||
|
||||
Solana Playground (Solpg) is a browser based IDE that allows you to quickly develop, deploy, and test Solana programs!
|
||||
|
||||
To get started, go to [https://beta.solpg.io/](https://beta.solpg.io/).
|
||||
|
||||
### Create Playground Wallet
|
||||
|
||||
If it is your first time using Solana Playground, you'll first need to create a Playground Wallet.
|
||||
|
||||
Click on the red status indicator button labeled "Not connected" at the bottom left of the screen, (optionally) save your wallet's keypair file to your computer for backup, then click "Continue".
|
||||
|
||||

|
||||
|
||||
After your Playground Wallet is created, you will notice the bottom of the window now states your wallet's address, your SOL balance, and the Solana cluster you are connected to (devnet by default).
|
||||
|
||||
{% callout type="warning" %}
|
||||
Your Playground Wallet will be saved in your browser's local storage. Clearing your browser cache will remove your saved wallet.
|
||||
{% /callout %}
|
||||
|
||||
To fund your Playground wallet with devnet SOL, run the following command in the Playground terminal:
|
||||
|
||||
```
|
||||
solana airdrop 5
|
||||
```
|
||||
|
||||
Alternatively, you can use this [devnet faucet](https://faucet.solana.com/).
|
||||
|
||||
### Create Anchor Project
|
||||
|
||||
Next, click the "Create a new project" button on the left-side panel.
|
||||
|
||||
Enter a project name, select Anchor as the framework, then click the "Create" button.
|
||||
|
||||

|
||||
|
||||
This will create a basic Anchor program that can be found in the `src/lib.rs` file.
|
||||
You can learn more about the details of an Anchor program in the Core concepts section of the docs.
|
||||
|
||||
```rust
|
||||
use anchor_lang::prelude::*;
|
||||
|
||||
// This is your program's public key and it will update
|
||||
// automatically when you build the project.
|
||||
declare_id!("11111111111111111111111111111111");
|
||||
|
||||
#[program]
|
||||
mod hello_anchor {
|
||||
use super::*;
|
||||
pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {
|
||||
ctx.accounts.new_account.data = data;
|
||||
msg!("Changed data to: {}!", data); // Message will show up in the tx logs
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Accounts)]
|
||||
pub struct Initialize<'info> {
|
||||
// We must specify the space in order to initialize an account.
|
||||
// First 8 bytes are default account discriminator,
|
||||
// next 8 bytes come from NewAccount.data being type u64.
|
||||
// (u64 = 64 bits unsigned integer = 8 bytes)
|
||||
#[account(init, payer = signer, space = 8 + 8)]
|
||||
pub new_account: Account<'info, NewAccount>,
|
||||
#[account(mut)]
|
||||
pub signer: Signer<'info>,
|
||||
pub system_program: Program<'info, System>,
|
||||
}
|
||||
|
||||
#[account]
|
||||
pub struct NewAccount {
|
||||
data: u64
|
||||
}
|
||||
```
|
||||
|
||||
### Build and Deploy Program
|
||||
|
||||
To build the program, simply run `build` in the terminal. Building the program will update the program address in `declare_id!()`. This is the on-chain address of your program.
|
||||
|
||||
Once the program is built, run `deploy` in the terminal to deploy the program to the cluster (devnet by default).
|
||||
To deploy a program, SOL must be allocated to the on-chain account that stores the program. If you do not have enough SOL, you may need to first request an airdrop.
|
||||
|
||||
You can also use the `Build` and `Deploy` buttons on the left-side panel.
|
||||
|
||||

|
||||
|
||||
### Test Program
|
||||
|
||||
Included with the starter code is a test file found in `tests/anchor.test.ts`.
|
||||
This file demonstrates how to interact with the program from the client.
|
||||
|
||||
```javascript
|
||||
// No imports needed: web3, anchor, pg and more are globally available
|
||||
|
||||
describe('Test', () => {
|
||||
it('initialize', async () => {
|
||||
// Generate keypair for the new account
|
||||
const newAccountKp = new web3.Keypair()
|
||||
|
||||
// Send transaction
|
||||
const data = new BN(42)
|
||||
const txHash = await pg.program.methods
|
||||
.initialize(data)
|
||||
.accounts({
|
||||
newAccount: newAccountKp.publicKey,
|
||||
signer: pg.wallet.publicKey,
|
||||
systemProgram: web3.SystemProgram.programId,
|
||||
})
|
||||
.signers([newAccountKp])
|
||||
.rpc()
|
||||
console.log(`Use 'solana confirm -v ${txHash}' to see the logs`)
|
||||
|
||||
// Confirm transaction
|
||||
await pg.connection.confirmTransaction(txHash)
|
||||
|
||||
// Fetch the created account
|
||||
const newAccount = await pg.program.account.newAccount.fetch(
|
||||
newAccountKp.publicKey
|
||||
)
|
||||
|
||||
console.log('On-chain data is:', newAccount.data.toString())
|
||||
|
||||
// Check whether the data on-chain is equal to local 'data'
|
||||
assert(data.eq(newAccount.data))
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
To run the test file once the program is deployed, run `test` in the terminal.
|
||||
|
||||
You can also use the `Test` button on the left-side panel.
|
||||
|
||||

|
||||
|
||||
Lastly, the SOL allocated to the on-chain program can be fully recovered by closing the program.
|
||||
|
||||
You can close a program by running the following command and specifying the program ID found in `declare_id!()`:
|
||||
|
||||
```
|
||||
solana program close <ProgramID>
|
||||
```
|
||||
|
||||
Congratulations! You've just built and deployed your first Solana program using the Anchor framework!
|
||||
|
||||
### Import from Github
|
||||
|
||||
Solana Playground offers a convenient feature allowing you to import or view projects using their GitHub URLs.
|
||||
|
||||
Open this [Solpg link](https://beta.solpg.io/https://github.com/solana-developers/anchor-examples/tree/main/quickstart) to view the Anchor project from this [Github repo](https://github.com/solana-developers/anchor-examples/tree/main/quickstart).
|
||||
|
||||
Click the `Import` button and enter a name for the project to add it to your list of projects in Solana Playground.
|
||||
|
||||

|
||||
|
||||
Once a project is imported, all changes are automatically saved and persisted within the Playground environment.
|
||||
|
||||
Additionally, you have the option to import projects directly by clicking the Github icon on the left-side panel.
|
||||
|
||||

|
Loading…
Reference in New Issue