Skip to content

Commit 60dd188

Browse files
0xrinegadeclaude
andcommitted
docs: Add comprehensive Solana program macros reference to CLAUDE.md
Documents all OVSM macros for sBPF compilation: - Struct operations (define-struct, struct-get/set, struct-ptr) - Account access (data-ptr, lamports, signer/writable checks) - Zero-copy direct memory access - CPI helpers (system-transfer, spl-token-*) - PDA derivation and bump caching - Event emission (Anchor-style) - Sysvar access (Clock, Rent) - Instruction introspection - Borsh serialization 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 38c809f commit 60dd188

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

CLAUDE.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,71 @@ null
650650
(getBalance address)
651651
;; ... more blockchain-specific functions
652652
653+
;; ============================================
654+
;; SOLANA PROGRAM MACROS (sBPF Compilation)
655+
;; ============================================
656+
657+
;; --- Struct Operations ---
658+
(define-struct MyStruct (field1 u64) (field2 pubkey) (field3 u8))
659+
(struct-size MyStruct) ;; -> total bytes
660+
(struct-offset MyStruct field1) ;; -> byte offset of field
661+
(struct-field-size MyStruct field1) ;; -> byte size of field
662+
(struct-get MyStruct ptr field1) ;; -> value at field
663+
(struct-set MyStruct ptr field1 value) ;; -> set field value
664+
(struct-ptr MyStruct ptr field1) ;; -> pointer to field
665+
666+
;; --- Account Access ---
667+
(account-data-ptr idx) ;; -> pointer to account data
668+
(account-data-len idx) ;; -> length of account data
669+
(account-lamports idx) ;; -> lamport balance
670+
(is-signer idx) ;; -> 1 if signer, 0 if not
671+
(is-writable idx) ;; -> 1 if writable, 0 if not
672+
(assert-signer idx) ;; -> abort if not signer
673+
(assert-writable idx) ;; -> abort if not writable
674+
(assert-owner idx expected-owner-ptr) ;; -> abort if owner mismatch
675+
676+
;; --- Zero-Copy Access (Direct Memory) ---
677+
(zerocopy-load StructName idx field) ;; -> load field directly from account
678+
(zerocopy-store StructName idx field v) ;; -> store value directly to account
679+
680+
;; --- CPI (Cross-Program Invocation) ---
681+
(system-transfer from-idx to-idx lamports)
682+
(spl-token-transfer prog src dst auth amt)
683+
(spl-token-transfer-signed prog src dst auth amt seeds)
684+
(spl-token-mint-to prog mint dest auth amount)
685+
(spl-token-burn prog source mint auth amount)
686+
(system-create-account payer new-acct lamports space owner-ptr)
687+
688+
;; --- PDA (Program Derived Address) ---
689+
(derive-pda program-pk-ptr seeds-ptr bump-ptr)
690+
(create-pda dest-ptr program-pk-ptr seeds-array)
691+
(get-ata wallet-ptr token-prog-ptr mint-ptr) ;; -> ATA address
692+
693+
;; --- PDA Bump Caching ---
694+
(pda-cache-init cache-account-idx)
695+
(pda-cache-store cache-idx seed-hash bump)
696+
(pda-cache-lookup cache-idx seed-hash) ;; -> cached bump or 0
697+
(derive-pda-cached cache-idx prog seeds bump dest)
698+
699+
;; --- Event Emission ---
700+
(emit-event EventStruct data-ptr) ;; -> Anchor-style event with discriminator
701+
(emit-log "message" val1 val2 val3) ;; -> log message + up to 5 values
702+
703+
;; --- Sysvar Access ---
704+
(clock-unix-timestamp sysvar-idx) ;; -> Unix timestamp
705+
(clock-epoch sysvar-idx) ;; -> Current epoch
706+
(rent-minimum-balance sysvar-idx size) ;; -> Rent exemption lamports
707+
708+
;; --- Instruction Introspection ---
709+
(instruction-count sysvar-idx) ;; -> Number of instructions
710+
(current-instruction-index sysvar-idx) ;; -> Current instruction index
711+
(assert-not-cpi sysvar-idx) ;; -> Abort if called via CPI
712+
713+
;; --- Borsh Serialization ---
714+
(borsh-serialize StructName src-ptr dest-ptr) ;; -> bytes written
715+
(borsh-deserialize StructName src-ptr dest-ptr) ;; -> bytes read
716+
(borsh-size StructName) ;; -> serialized size
717+
653718
;; ============================================
654719
;; COMPLETE EXAMPLES
655720
;; ============================================

0 commit comments

Comments
 (0)