This is a basic Solana development example that shows how to write Solana programs in Rust and interact with them using TypeScript. The program has been deployed on Solana's devnet.
.
├── program/ # Rust program (smart contract)
│ ├── src/ # Source code
│ │ ├── lib.rs # Main file
│ │ └── entrypoint.rs # Program entry point
│ └── Cargo.toml # Rust dependency configuration
└── client/ # TypeScript client
├── src/ # Source code
│ └── index.ts # Client code
├── package.json # Node.js dependency configuration
└── tsconfig.json # TypeScript configuration
Program ID: 4BVd3iznNQg2Spju9tU2QzQB1aVzDSBR1ZQ1yj2Cf2of
This program has been deployed to Solana's devnet network. You can interact with it directly using the client.
- Install Solana CLI:
sh -c "$(curl -sSfL https://release.solana.com/v1.14.29/install)"
export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH"
- Install Rust and Cargo:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
rustup component add rustfmt
rustup target add bpfel-unknown-unknown
- Install Node.js and npm:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
- Configure Solana network:
solana config set --url https://api.devnet.solana.com
- Create a wallet (if you haven't already):
solana-keygen new
- Get test SOL:
solana airdrop 2
- Compile the program:
cd program
cargo build-bpf
- Deploy the program:
solana program deploy target/deploy/solana_hello_world.so
After successful deployment, you'll receive a program ID. Make sure to save it.
- Update the program ID in the client (if you deployed a new program):
Edit the
client/src/index.ts
file and update thePROGRAM_ID
variable:
const PROGRAM_ID = new PublicKey('your-new-program-id');
This is a simple demo program that:
- Prints "Hello, Solana! 你好,索拉納!"
- Shows the program ID
- Lists all incoming accounts
- Reads and displays any instruction data sent to it
The TypeScript client shows how to:
- Connect to Solana devnet
- Load or create keypairs
- Build transactions and attach instructions
- Send transactions to our program
- Retrieve transaction logs and results
# Go to the client directory
cd client
# Install dependencies
npm install
# Run the client
npm start
Here are the key concepts of how the Rust program and TypeScript client interact:
-
Program Deployment: The Rust program is compiled and deployed to the Solana network, receiving a unique program ID.
-
Client Calls: The TypeScript client builds a transaction that specifies the program ID to call, the accounts needed, and the instruction data.
-
Data Transfer:
- Client → Program: Through the
data
field ofTransactionInstruction
- Program → Client: Through program logs (the
msg!
macro)
- Client → Program: Through the
-
Account List: The program accesses the list of transmitted accounts via the
AccountInfo
parameter, allowing it to read or modify their data.
