Skip to content

Instantly share code, notes, and snippets.

@deanmlittle
Last active October 30, 2024 07:22
Show Gist options
  • Save deanmlittle/bdf2967d3f4d31ca594e60990b0b70fd to your computer and use it in GitHub Desktop.
Save deanmlittle/bdf2967d3f4d31ca594e60990b0b70fd to your computer and use it in GitHub Desktop.
Generate a Mollusk SDK for a mainnet-beta program
#!/bin/bash
# Check if program ID and name are provided
if [ "$#" -ne 2 ]; then
echo "Usage: ./mollusk_generate.sh <ProgramID> <name>"
exit 1
fi
PROGRAM_ID="$1"
NAME="$2"
# Create project directory
PROJECT_DIR="mollusk-$NAME"
SRC_DIR="$PROJECT_DIR/src/elf"
LIB_RS="$PROJECT_DIR/src/lib.rs"
CARGO_TOML="$PROJECT_DIR/Cargo.toml"
mkdir -p "$SRC_DIR"
# Generate the Rust file
cat > "$LIB_RS" <<EOL
use {
mollusk_svm::Mollusk,
solana_sdk::{account::AccountSharedData, pubkey::Pubkey},
};
pub const ID: Pubkey = solana_sdk::pubkey!("$PROGRAM_ID");
pub fn add_program(mollusk: &mut Mollusk) {
mollusk.add_program_with_elf_and_loader(
&ID,
include_bytes!("elf/$NAME.so"),
&mollusk_svm::program::loader_keys::LOADER_V2,
);
}
pub fn account() -> AccountSharedData {
mollusk_svm::program::create_program_account_loader_v3(&ID)
}
/// Get the key and account for the system program.
pub fn keyed_account() -> (Pubkey, AccountSharedData) {
(ID, account())
}
// Last updated at mainnet-beta slot height:
EOL
# Run solana program dump to create the ELF file
solana program dump "$PROGRAM_ID" "$SRC_DIR/$NAME.so" -u mainnet-beta
# Get the current slot and update lib.rs
SLOT=$(solana slot -u mainnet-beta)
sed -i '' "s|// Last updated at mainnet-beta slot height:|// Last updated at mainnet-beta slot height: $SLOT|" "$LIB_RS"
# Create the Cargo.toml file
cat > "$CARGO_TOML" <<EOL
[package]
name = "mollusk-$NAME"
version = "0.1.0"
edition = "2021"
[lib]
[dependencies]
mollusk-svm = ">=0.0.6"
solana-sdk = "2.0"
EOL
echo "Project '$PROJECT_DIR' has been generated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment