How to Leverage Solita for Your Solana Rust Programs

Aug 15, 2024 | Blockchain

Creating a seamless, low-level TypeScript SDK from your Solana Rust programs just got easier! In this blog, we’ll explore how you can utilize the Solita IDL (Interface Description Language) generator, simplifying your workflow using the Shank or Anchor frameworks. Let’s dive in!

How Does It Work?

Solita is a tool that creates a low-level TypeScript SDK for your Solana Rust programs. It extracts the IDL using either Anchor or Shank, allowing developers to effectively interface with their applications.

Shank + Solita Example (Recommended)

First, let’s see how to use Solita with the Shank library.

  • Add the Shank library to your Rust project:
  • cargo add shank
  • Annotate your Rust program as outlined in the Shank Documentation.
  • Add Solita to your SDK package’s development dependencies:
  • yarn add -D @metaplex-foundation/solita
  • Add the following configuration into the .solitarc.js file in your SDK package root:
  • const path = require('path');
    const programDir = path.join(__dirname, '..', 'program');
    const idlDir = path.join(__dirname, 'idl');
    const sdkDir = path.join(__dirname, 'src', 'generated');
    const binaryInstallDir = path.join(__dirname, '.crates');
    
    module.exports = {
      idlGenerator: shank,
      programName: 'mpl_token_vault',
      idlDir,
      sdkDir,
      binaryInstallDir,
      programDir,
    };

Running yarn solita from this folder will install the matching Shank binary and generate the IDL and SDK. Remember to run it each time you modify your program to regenerate your TypeScript SDK!

Full Example: Token Metadata Solita + Shank Setup

Anchor + Solita Example (Recommended)

Similar to Shank, you can utilize Solita with Anchor as well by following these steps:

  • Annotate your Rust program with Anchor attributes.
  • Add Solita to your SDK package’s development dependencies:
  • yarn add -D @metaplex-foundation/solita
  • Implement the configuration in your .solitarc.js file (similarly to Shank example).

Running yarn solita will install the appropriate Anchor binary and generate the IDL and SDK.

Type Aliases and Custom DeSerializers

If you need to map specific types to Rust built-in types, you can provide a type alias map:

module.exports = {
  idlGenerator: anchor,
  typeAliases: {
    UnixTimestamp: 'i64'
  },
};

In cases where the generated deserializers do not work correctly, you can specify a custom deserializer as follows:

export function deserialize(buf: Buffer, offset = 0): [Account, number] {
  // custom implementation here
}

Troubleshooting Tips

If you encounter issues when running Solita, here are a few troubleshooting steps to follow:

  • Ensure your Rust program is properly annotated with the correct macros.
  • Check if the Shank or Anchor binaries are correctly installed and accessible in your PATH.
  • If you receive errors about missing files, ensure that your .gitignore includes the .crates directory.
  • Read the error messages carefully; they often provide clues on what to fix.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By using Solita, you streamline the process of creating SDKs for your Solana Rust programs. Establishing a fast, efficient development workflow not only saves time but also enhances your project’s overall quality. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox