|
| 1 | +# Shank & Solita |
| 2 | + |
| 3 | +The devs at Metaplex created Shank & Solita for native Solana programs to be able to take advantage of serialization & IDLs just like Anchor programs. |
| 4 | + |
| 5 | +### Shank |
| 6 | + |
| 7 | +[Shank](https://docs.metaplex.com/developer-tools/shank) is the Rust crate responsible for generating an IDL for your program. |
| 8 | + |
| 9 | +It's super easy to use in your Rust code: |
| 10 | + |
| 11 | +Add this annotation to any struct to mark it as an account: |
| 12 | +```rust |
| 13 | +#[derive(ShankAccount)] |
| 14 | +``` |
| 15 | +ex: |
| 16 | +```rust |
| 17 | +#[derive(BorshDeserialize, BorshSerialize, Clone, ShankAccount)] |
| 18 | +pub struct Car { |
| 19 | + pub year: u16, |
| 20 | + pub make: String, |
| 21 | + pub model: String, |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Add this annotation to any enum to mark it as an instruction enum: |
| 26 | +```rust |
| 27 | +#[derive(ShankInstruction)] |
| 28 | +``` |
| 29 | +ex: |
| 30 | +```rust |
| 31 | +#[derive(BorshDeserialize, BorshSerialize, Clone, ShankInstruction)] |
| 32 | +pub enum CarRentalServiceInstruction { |
| 33 | + AddCar(Car), |
| 34 | + BookRental(RentalOrder), |
| 35 | + PickUpCar, |
| 36 | + ReturnCar, |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Then you just need to add the Shank CLI: |
| 41 | +```shell |
| 42 | +cargo install shank-cli |
| 43 | +``` |
| 44 | +```shell |
| 45 | +USAGE: |
| 46 | + shank <SUBCOMMAND> |
| 47 | + |
| 48 | +OPTIONS: |
| 49 | + -h, --help Print help information |
| 50 | + |
| 51 | +SUBCOMMANDS: |
| 52 | + help Print this message or the help of the given subcommand(s) |
| 53 | + idl |
| 54 | +``` |
| 55 | + |
| 56 | +> Note: You do have to make use of `declare_id` in order for Shank to work properly: |
| 57 | +```rust |
| 58 | +declare_id!("8avNGHVXDwsELJaWMSoUZ44CirQd4zyU9Ez4ZmP4jNjZ"); |
| 59 | +``` |
| 60 | + |
| 61 | +### Solita |
| 62 | + |
| 63 | +[Solita](https://docs.metaplex.com/developer-tools/solita/) is the JavaScript SDK responsible for building client-side SDK types from your program's IDL. |
| 64 | + |
| 65 | +> Note: Solita will work with an IDL from Shank or from Anchor! |
| 66 | +
|
| 67 | +First add Solita to your project: |
| 68 | +```shell |
| 69 | +yarn add -D @metaplex-foundation/solita |
| 70 | +``` |
| 71 | +Then add a Solita config `.solitarc.js`: |
| 72 | +```javascript |
| 73 | +const path = require('path'); |
| 74 | +const programDir = path.join(__dirname, 'program'); |
| 75 | +const idlDir = path.join(programDir, 'idl'); |
| 76 | +const sdkDir = path.join(__dirname, 'tests', 'generated'); |
| 77 | +const binaryInstallDir = path.join(__dirname, '.crates'); |
| 78 | + |
| 79 | +module.exports = { |
| 80 | + idlGenerator: 'shank', |
| 81 | + programName: 'car_rental_service', |
| 82 | + idlDir, |
| 83 | + sdkDir, |
| 84 | + binaryInstallDir, |
| 85 | + programDir, |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +Once you've got that file configured to match your repository layout, go ahead and run: |
| 90 | +```shell |
| 91 | +yarn solita |
| 92 | +``` |
| 93 | + |
| 94 | +That should build all your types from your IDL! Check for a folder called `generated` to see them! |
0 commit comments