Run local network on M1
solana config set --url localhost
solana-test-validator --no-bpf-jit
anchor init my-app
# Compiles your program.
anchor build
# Deploys your compiled program.
anchor deploy
Get rent for storing 1376 bytes
solana rent 1376
Update your program ID
solana address -k target/deploy/solana_twitter-keypair.json
# Outputs something like: 2EKFZUwMrNdo8YLRHn3CyZa98zp6WH7Zpg16qYGU7htD
# Anchor.toml
[programs.localnet]
solana_twitter = "2EKFZUwMrNdo8YLRHn3CyZa98zp6WH7Zpg16qYGU7htD"
# programs/solana-twitter/src/lib.rs
declare_id!("2EKFZUwMrNdo8YLRHn3CyZa98zp6WH7Zpg16qYGU7htD");
anchor build
anchor deploy
https://docs.rs/anchor-lang/0.18.2/anchor_lang/derive.Accounts.html
use anchor_lang::solana_program::system_program;
#[derive(Accounts)]
pub struct SendTweet<'info> {
#[account(init, payer = author, space = Tweet::LEN)]
pub tweet: Account<'info, Tweet>,
#[account(mut)]
pub author: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
if topic.chars().count() > 50 {
return Err(ErrorCode::TopicTooLong.into());
}
#[error]
pub enum ErrorCode {
#[msg("The provided topic should be 50 chars long maximum.")]
TopicTooLong,
}
Provider settings set in Anchor.toml
[provider]
cluster = "localnet"
wallet = "/Users/andy/.config/solana/id.json"
anchor run test
anchor test
is equivalent of
# Start the local ledger.
solana-test-validator
# Then, on a separate terminal session.
anchor build
anchor deploy
anchor run test
- Local network should be down
anchor localnet
is equivalent of
solana-test-validator --reset
anchor build
anchor deploy
# The local ledger will stay active after deployment.