Anchor Skeleton
Battery-included Solana/Anchor project skeleton.
Features
- Rust test only: All tests (integration, unit) are written in Rust, so the compiler will also check them. No Typescript based tests.
- CI integration: CI automatically deploys the project to Devnet and runs all the tests in Github Action.
- Feature-based Program ID switch: switch the
declare_id
inlib.rs
based on cargo features: by default, the program_id will be declared inlib.rs
(production program-id). If thedev
cargo feature is enabled (enabled by default in the Justfile), thedeclare_id
will be the one loaded fromprograms/contract-skeleton/key.json
(dev program-id). You can usejust keygen contract-skeleton
to generate a new one. - Battery included: the skeleton demonstrates the usage of errors, events, examples and integration tests and is immediately deployable and testable.
Prerequisites
For CI:
- Copy and paste your deployer key (a JSON array, e.g. the content of
~/.config/solana/id.json
) into the Github SecretDEPLOY_KEY
. There should be some SOL inside the deployer's account; otherwise, CI cannot deploy the project to pay the rent.
Explanation on the Justfile recipes
just keygen contract-skeleton
: Generate a key.json into the program's folder. Once thedev
feature is enabled, the program-id will be the one described in this key.just deploy contract-skeleton
: Deploy the contract. Which network to deploy depends on your Solana config (solana config get
).just test
: Run all the tests locally.just test create_pool
to run thecreate_pool
test specifically.just cli
: Run the binaries defined inprograms/contract-skeleton/examples
folder. e.g.just cli create_pool
. This command uses the production program-id.just devcli
: Run the binaries defined inprograms/contract-skeleton/examples
folder. e.g.just cli create_pool
. This uses the dev program-id.just idl contract-skeleton
: generate an idl totarget/idl
, and populate the program-id using the dev program-id.just dangerously-close contract-skeleton
: Close the program and reclaim the rent.
How-tos
- How do I add a new instruction? First, create a new context file in
programs/contract-skeleton/src/contexts.rs
. Then create the required account and implement the process method. Finally, add a new method in thelib.rs
that calls the process method. Try to makelib.rs
clean for someone to know what's included in this program quickly. - How do I add a new error type? Add it in
programs/contract-skeleton/src/errors.rs
. - How do I add a new event? Add it at the bottom of the context file because an event usually only gets emitted in one specific instruction.
- How do I add a new integration test? Add it to the tests folder. Also take a look at the existing tests.