A Solana program built with Anchor that demonstrates different logging methods through key-value pair operations. Includes a TypeScript client for interaction and event monitoring.
This program showcases three logging methodologies on Solana:
- Simple text logging with
msg! - Structured binary logging with
sol_log_data - Anchor's structured events with
emit!
Features:
- Stores operation count in a Program Derived Address (PDA)
- Swaps key/value pairs in subsequent operations
- Provides comprehensive TypeScript client integration
- Supports real-time event monitoring
my_anchor_project/
├── programs/
│ └── my_anchor_project/
│ └── src/
│ └── lib.rs # Program logic
├── app/
│ └── key-value-client.ts # TS client implementation
├── migrations/
│ ├── deploy.ts # Deployment scripts
│ ├── fetchTxData.js # Transaction inspector
│ └── solListener.js # Event listener
├── target/
│ └── idl/
│ └── my_anchor_project.json # Generated IDL
├── Anchor.toml # Config
├── Cargo.toml # Rust dependencies
└── README.md # Documentation
- Rust (v1.65.0+)
- Solana CLI (v1.18.4+)
- Anchor CLI
- Node.js (v18.x+)
- npm/yarn
- Clone repository & install dependencies:
git clone https://github.com/your-username/my_anchor_project.git
cd my_anchor_project
npm install- Set up Solana devnet environment:
solana config set --url devnet
solana-keygen new- Build and deploy:
anchor build
anchor deploy- Run client with test data:
node app/key-value-client.js "temperature" "23.5°C"- Creates PDA with seed "logger"
- Sets initial nonce to 0
- Accepts key/value pair (max 32 bytes each)
- Increments nonce
- Logs data using all three methods
- Triggers
swap_and_logwith swapped values
| Method | Format | Size Limit | Decoding | Use Case |
|---|---|---|---|---|
msg! |
Plain text | 512 bytes | Direct read | Simple debugging |
sol_log_data |
Binary | 10KB | Manual deserialization | Structured large data |
| Anchor Events | JSON (IDL) | 10KB | Automatic via client | Production applications |
// Original submission
struct KeyValueEvent {
key: String,
value: String,
nonce: u64,
}
// After swap operation
struct KeyValueSwappedEvent {
swapped_key: String,
swapped_value: String,
nonce: u64,
}interface KeyValueClient {
initialize(): Promise<TransactionSignature>;
logKeyValue(key: string, value: string): Promise<TransactionSignature>;
subscribeToLogs(callback: (log: any) => void): void;
decodeBinaryLog(data: string): { key: string; value: string; nonce: number };
}node migrations/solListener.jsnode migrations/fetchTxData.js 5VERv8NMvzbJMEkV8xnrLkEaWRtSz9ChKDcpq3Mxcs3poaH9g4KdYL4iPEgfQ25238huKGnMHCUnmN4VbSv7nD9- Account Model: Unlike EVM's contract storage, Solana requires explicit account passing
- PDAs: Program-controlled addresses (no private keys) for deterministic account access
- Log Limits: Maximum 10KB per log entry across all methods
- Gas Costs: Logging impacts compute units - binary logs are most efficient
MIT License - See LICENSE for details
