Minimal program shape
Execution model
| Step | What normally happens |
|---|---|
| Enter | The VM jumps to a function marked with TSDK_ENTRYPOINT_FN. |
| Read inputs | The program reads transaction and account context through helpers like tsdk_get_txn, tsdk_get_account_meta, and tsdk_get_account_data_ptr. |
| Decode | Instruction bytes are read from the transaction body, usually through tsdk_txn_get_instr_data and tsdk_txn_get_instr_data_sz. |
| Validate | Programs check instruction size, account indices, ownership, authorization, and any variable-length payload boundaries before mutating state. |
| Mutate or invoke | Programs call syscalls such as tsys_account_resize, tsys_account_transfer, or tsys_invoke. |
| Exit | Programs terminate with tsdk_return(...) for success or tsdk_revert(...) for failure. |
Recommended file split
| File | Purpose |
|---|---|
your_program.h | Instruction tags, error codes, packed payload structs, account-data structs, and local constants. |
your_program.c | Entry point, validation, instruction dispatch, syscall usage, and local helper functions. |
Typical instruction dispatch pattern
Control-flow rules
tsdk_returndoes not come back.tsdk_revertdoes not come back.tsys_exitunderlies both helpers and is also terminal.- If you need cleanup, do it before calling either exit helper.
Common structure choices
| Goal | Common choice |
|---|---|
| Small fixed instruction | One packed struct with a leading tag. |
| Variable-length instruction | Fixed header plus byte tail, with explicit size checks before casting or copying. |
| Stateful program | One or more packed account-data structs plus helper functions to read and write them. |
| CPI-heavy program | Entry point plus small helpers to build invoke payloads and auth descriptors. |
Notes
- Prefer
tsdk_get_txn()as the root of most program reads. - Prefer a local header for your instruction/account layouts so the implementation page can stay focused on logic.
- Keep the entrypoint thin: validate, dispatch, return.