# Solana Develepment Tooling
## [create-solana-dapp](https://github.com/solana-developers/create-solana-dapp)
Create a new project:
```
npx create-solana-dapp@latest
```
build the smart contract
```
npm run anchor-build
cd anchor
anchor build
```
start the local validator in a new terminal for localhost development
```
npm run anchor-localnet
solana-test-validator
```
run tests, doesn't need the local validastor to run properly
```
npm run anchor-test
cd anchor
anchor test --skip-local-validator
```
deploy smart contract
```
cd anchor
anchor deploy
```
build the web app (frontend client)
```
npm run build
```
run the app
```
npm run dev
```
## [Anchor](https://www.anchor-lang.com/docs)
Anchor is a development framework for building secure Solana programs (smart
contracts).
set environment for local development
```
solana set config --url localhost
solana-test-validator
solana adress
solana balance
solana airdroip 100
```
create a new project. Code, build, write tests, run tests, fix loop in localhost
until you're ready to go to `devnet`.
The default Anchor program is located at `my-project/src/lib.rs` The value in the
`declare_id!` macro is the program ID, a unique identifier for your program. By
default, it is the public key of the keypair generated in
`/target/deploy/my_project-keypair.json`
```
anchor init <project_name>
# if you prefer rust for testing
anchor init --test-template rust <project_name>\
# Write tests for Solana programs in Rust using Mollusk
anchor init --test-template rust <project_name>
cd <project_name>
```
building the solana program
```
anchor build
```
this creates a new folder called `target`. Inside the `deploy` folder there ie the
actual code that is going to be deploy to the solana blockahin and here you can
find the keypair that could create, update and destroy this program.
```
anchor keys list
anchor keys sync
```
testing your program. This tells anchor no to run a local validator for testing
since we already have one running.
```
anchor test
# if there is a local validator running already
anchor test --skip-local-validator
```
Anchor uses [Mocha](https://mochajs.org/), Mocha is a feature-rich JavaScript test
framework running on Node.js and in the browser.
Anchor uses [Chai](https://www.chaijs.com/), Chai is a BDD / TDD assertion library
as the aseertion library.
Deployinmg to Devnet. To deploy your program to `devnet`, change the `cluster`
value to `Devnet` in the `Anchor.toml` config file.
```
[provider]
cluster = "Devnet"
```
To deploy to `mainnet`, simply update the `Anchor.toml` file to specify the mainnet
cluster.
```
[provider]
cluster = "Mainnet"
```
Solana programs can be updated by redeploying the program to the same program ID.
```
# Make changes to your solana program
anchor build
anchor deploy
```
To reclaim the SOL allocated to a program account, you can close your Solana
program. Note that once a program is closed, the program ID cannot be reused to
deploy a new program.
```
solana program close <program-id> --bypass-warning
```
The `/app` folder is an empty folder that can be optionally used for your frontend
code.
### Program Structure
The Anchor framework uses [Rust macros](https://rust-book.cs.brown.edu/ch20-06-
macros.html) to reduce boilerplate code and simplify the implementation of common
security checks required for writing Solana programs.
The main macros found in an Anchor program include:
- [declare_id](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/
account/src/id.rs#L40-L73): Specifies the program's on-chain address. By default,
the program ID is the public key of the keypair generated at
`/target/deploy/your_program_name.json`
- `anchor keys sync`. The `anchor keys sync` command is useful to run when
cloning a repository where the value of the program ID in a cloned repo's
`declare_id` macro won't match the one generated when you run `anchor build`
locally.
- #[program]: Specifies the module containing the program’s instruction logic.
Each public function within this module corresponds to an instruction that can be
invoked.
- #[derive(Accounts)]: Applied to structs to indicate a list of accounts required
by an instruction. Each field in the struct represents an account required by an
instruction.
- #[account]: The `#[account]` attribute is applied to structs that define the
structure of the data stored in custom accounts created by your program. The key
functionalities of the #[account] macro include:
- Assign Program Owner: When creating an account, the program owner of the
account is automatically set to the program specified in `declare_id`.
- Set Discriminator: A unique 8 byte discriminator, specific to the account
type, is added as the first 8 bytes of account data during its initialization.
- Data Serialization and Deserialization: In Solana, account data is stored as
raw bytes, so we need to serialize data when storing it (binary format) and
deserialize when reading it (in the account variable). When account data is
deserialized, the first 8 bytes (Discriminator) of account data is checked against
the discriminator of the expected account type.
#### Instruction Context
Instruction handlers are functions that define the logic executed when an
instruction is invoked. The first parameter of each handler is a `Context<T>` type,
where `T` is a struct implementing the `Accounts` trait and specifies the accounts
the instruction requires.
The Context fields can be accessed in an instruction using dot notation:
- ctx.accounts: The accounts required for the instruction
- ctx.program_id: The program's public key (address)
- ctx.remaining_accounts: Additional accounts not specified in the Accounts
struct.
- ctx.bumps: Bump seeds for any Program Derived Address (PDA) accounts specified
in the Accounts struct
Additional parameters are optional and can be included to specify arguments that
must be provided when the instruction is invoked.
```rust
pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {
ctx.accounts.new_account.data = data;
msg!("Changed data to: {}!", data);
Ok(())
}
```
#### Account Validation
To prevent security vulnerabilities, it's important to verify that accounts
provided to an instruction are the expected accounts.
- [Account Constraints](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/
syn/src/parser/accounts/constraints.rs): Constraints define additional conditions
that an account must satisfy to be considered valid for the instruction.
Constraints are applied using the `#[account(..)]` attribute, which is placed above
a field in a struct that implements the Accounts trait.
```rust
#[account(init, payer = signer, space = 8 + 8)]
pub new_account: Account<'info, NewAccount>,
```
- Account Types: Anchor provides various account types to help ensure that the
account provided by the client matches what the program expects. e.g `Account`,
`Signer`, `Program`
When an instruction in an Anchor program is invoked, the program first validates
the accounts provided before executing the instruction's logic.
### Program IDL File
An Interface Description Language (IDL) file for an Anchor program provides a
standardized JSON file describing the program's instructions and accounts. This
file simplifies the process of integrating your on-chain program with client
applications. It is used to generate client code to interact with the program. It's
generated when you execute: `anchor build`.