Getting Started
Hello World
This program will log our hello world message into program's on-chain transaction log. We will simply use msg!() macro for logging the hello world.
To initialize the project, simply run:
anchor init hello-world
Program's Code
Let's wirte our first hello world program a.k.a smart contract in anchor or just copy-pasta the following code into your lib.rs file 😉.
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod hello_world {
    use super::*;
    pub fn hello_world(_ctx: Context<Initialize>) -> Result<()> {
        msg!("Hello world, from solana smart contract");
        Ok(())
    }
}
#[derive(Accounts)]
pub struct Initialize {}
Now compile and build this program, by simply running:
anchor build
Test
Now It's time to write a test for our program! Copy-pasta the following code into your hello-world.ts file in tests folder in the root directory.
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { HelloWorld } from "../target/types/hello_world";
describe("hello-world", () => {
  anchor.setProvider(anchor.AnchorProvider.env());
  const program = anchor.workspace.HelloWorld as Program<HelloWorld>;
  it("Mic testing - Hello world", async () => {
    const tx = await program.methods.helloWorld().rpc();
    console.log("Your transaction signature", tx);
  });
});
Deployment 🎉
Time to deploy and test our first hello world smart contract, yay!
We are going to deploy on devnet. Here is our deployment checklist 🚀
- Run anchor build. Your program keypair is now intarget/deploy. Keep this keypair secret 🤫.
- Run anchor keys listto display the keypair's public key and copy it into yourdeclare_id!macro at the top oflib.rs.
- Run anchor buildagain. This step is necessary to include the new program id in the binary.
- Change the provider.clustervariable inAnchor.tomltodevnet.
- Run anchor deploy
- Run anchor test
On-Chain Result
> Program logged: "Instruction: HelloWorld"
> Program logged: "Hello world, from solana smart contract"
> Program consumed: 452 of 200000 compute units
> Program returned success