Overview
Pinocchio is a zero-dependency library to create Solana programs in Rust. It takes advantage of the way SBF loaders serialize the program input parameters into a byte array that is then passed to the program's entrypoint to define zero-copy types to read the input. Since the communication between a program and SBF loader — either at the first time the program is called or when one program invokes the instructions of another program — is done via a byte array, a program can define its own types. This completely eliminates the dependency on the solana-program crate, which in turn mitigates dependency issues by having a crate specifically designed to create on-chain programs.
Pinocchio can be used as a replacement for solana-program to write on-chain programs.
The library defines:
- program entrypoint
- core data types
- logging macros
syscallfunctions- access to system accounts (
sysvars) - cross-program invocation
Features
- Zero dependencies and
no_stdcrate - Efficient
entrypoint!macro – no copies or allocations - Improved CU consumption of cross-program invocations
Getting started
From your project folder:
Pinocchio provides two different entrypoint macros: an entrypoint that looks similar to the "standard" one found in solana-program and a lightweight lazy_entrypoint. The main difference between them is how much work the entrypoint performs. While the entrypoint parsers the whole input and provide the program_id, accounts and instruction_data separately, the lazy_entrypoint only wraps the input at first. It then provides methods to parse the input on demand. The benefit in this case is that you have more control when the parsing is happening — even whether the parsing is needed or not.
The lazy_entrypoint is suitable for programs that have a single or very few instructions, since it requires the program to handle the parsing, which can become complex as the number of instructions increases. For "larger" programs, the entrypoint will likely be easier and more efficient to use.
⚠️ Note: In both cases you should use the types from the
pinocchiocrate instead ofsolana-program. If you need to invoke a different program, you will need to redefine its instruction builder to create an equivalent instruction data usingpinocchiotypes.
🚪 entrypoint!
To use the entrypoint! macro, use the following in your entrypoint definition:
use ;
entrypoint!;
The information from the input is parsed into their own entities:
program_id: theIDof the program being calledaccounts: the accounts receivedinstruction_data: data for the instruction
🚪 lazy_entrypoint!
To use the lazy_entrypoint! macro, use the following in your entrypoint definition:
use ;
lazy_entrypoint!;
The InstructionContext provides on-demand access to the information of the input:
available(): number of available accountsnext_account(): parsers the next available account (can be used as many times as accounts available)instruction_data(): parsers the intruction data and program id
License
The code is licensed under the Apache License Version 2.0
The library in this repository is based/includes code from: