0%
Overview page background
HomeOverviewsSolana
How to Write Solana Programs in Python Using Seahorse

How to Write Solana Programs in Python Using Seahorse

Published on November 4, 20225 min read

Solana is a high-performance blockchain network with smart contract functionality built to scale for global adoption. Solana processes an average of 3,000 transactions per second, and is one of the fastest blockchain protocols. Solana Programs are smart contracts written on the Solana blockchain network used to perform every activity, including token transfers, minting NFTs, and voting on-chain.

This article will explain how to write Solana Programs in Python using the Seahorse development framework from a local development environment and from an online IDE.

To develop Solana Programs, you need to learn Rust, build with the Anchor development framework, or use Seahorse, a pythonic development framework for Solana.

Rust is the programming language on which Solana was built, and it is the default web3 programming language that can be used to interact with Solana's Sealevel Runtime. 

Anchor is a framework used to make Solana application development easier by abstracting the complexities of the Rust language from the average user.

Anchor provides Solana developers with tools like the Interface Definition Language (IDL), Typescript packages that generate clients from the IDL, Rust crates, and Command Line Interface (CLI).

Seahorse is a community-led Solana development framework that grants developers the ability to write Solana programs in the Python programming language.

Seahorse allows every program written in Python to have the same safety measures that come with Rust programs. Seahorse is fully compatible with Anchor, and is also interoperable with Rust code.

Python is used in the everyday programming of web applications and machine learning projects, and is the most popular programming language.

Since many developers interested in building blockchain-based applications might already have Python programming skills, they can use Seahorse to build a Solana application without having to learn a brand new programming language (Rust).

A platform like Solana Playground is only for building and testing applications quickly and should not be depended on for creating large-scale dapps. To set up the environment needed for building with Seahorse on our local workspace, we are going to install the following:

  1. Rust

  2. The Solana Tool Suite

  3. Node

  4. Anchor

  5. Seahorse

To install Rust, the Solana Tool Suite, Node, and Anchor, refer to this tutorial on setting up a local Solana developer environment.

If you're running a Linux-based machine you may need to run the following command to add additional dependencies:

Copied
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install -y pkg-config build-essential libudev-dev

Seahorse depends on Anchor and the Rustfmt to work properly. Because Anchor is already be working on your machine, installing Rustfmt package and Anchor is easy.

Run the following command to add the Rustfmt package:

Copied
rustup component add rustfmt 

Run the following command in your terminal to install Seahorse:

Copied
Cargo install seahorse-lang

Run one final command to check the Seahorse was properly installed:

Copied
seahorse -- version

Since you already have the Solana Command Line Tools installed, you can create a local "paper" wallet to use while working locally.

Run the following command to verify the Solana CLI tools are installed:  

Copied
Solana-keygen - -version 

Then type the next command to generate a new keypair:

Copied
Solana-keygen new

Save the details properly so you can start using them in your local environment.

To fund your paper wallet:

Run the solana airdrop 2 command to acquire 2 test SOL tokens

If you want to use an online developer environment to test Seahorse instead of running it locally, a good option is Solana Playground.

Solana Playground is an online Integrated Development Environment built for jumpstarting Solana application development. 

Solana Playground offers the following prebuilt Crates:

  1. Native (Rust) - for developers who intend to build on Solana without any framework

  2. Anchor (Rust) - for developers to build on Solana in Rust additional tooling

  3. Seahorse (Python) - for Solana developers with experience programming with Python

Solana Playground selection screen for choosing a development framework: Native, Anchor, and Seahorse.
Solana Playground selection screen for choosing a development framework: Native, Anchor, and Seahorse.

Create a new project on Solana Playground with a name for the directory. After creating a name, a demo project named, FizzBuzz, will appear. This mini dapp is what we will use within this tutorial. 

You must first generate a Playground Wallet for interacting with your Seahorse programs. The Playground Wallet acts as a test wallet for the application. 

  • Click on the “playground wallet”

  • Select a new wallet

  • Follow the prompts and download the JSON file

Solana Playground interface for creating a new Playground Wallet
Solana Playground interface for creating a new Playground Wallet

Type the following commands in the playground terminal to get devnet SOL tokens:

$ connect

The connect command links your test wallet with the project. 

$ solana address

The solana address command prints out your Solana wallet address. We will use this address later to initiate the FizzBuzz app.

$ solana airdrop 2

The solana airdrop command deposits test SOL tokens in your wallet to cover the fees required to execute transactions such as “Build” or “Deploy.”

Solana Playground's FizzBuzz Seahorse dapp project file
Solana Playground's FizzBuzz Seahorse dapp project file

In this section, we will walk through creating the popular but simple FizzBuzz app on-chain. There are three main sections in our code:

  1. The Account

  2. Instruction 1

  3. Instruction 2

Executable Accounts are used to define the executable code (smart contract) that runs on the Solana blockchain. In the Accounts definition, you can assign the data types needed in your Solana programs. 

Fizz and Buzz are defined to be Boolean data types, while the “n” variable is an unsigned integer type. 

Copied
class FizzBuzz(Account):   fizz: bool   buzz: bool   n: u64

There are other forms of accounts called Built-In Accounts. Most of these built-in accounts are used in calling specific instructions on-chain. 

Instructions that require signers need a wallet’s approval. A Signer is the wallet that signs the transaction with the instruction call defined in this part of the code. 

Copied
@instructiondef init(owner: Signer):

The owner keyword is used to hold the value of the Signer’s details. To get the public key of the Signer, you can add this code:

Copied
key = owner.key()

An empty account type is used to initialize the class of the main Account (FizzBuzz) even though it is initially empty. 

Copied
@instruction def init(owner: Signer, fizzbuzz: Empty[FizzBuzz]): fizzbuzz.init(payer = owner, seeds = ['fizzbuzz', owner])

By using the Empty keyword, you can construct the class defined in the main Account. When the FizzBuzz account is initialized, the creation fee is paid by the Owner (i.e. the signer of the transaction).

Once that account has been created on-chain, it can not be re-created when called again by the same owner. The seed generated from the initialization is able to identify the original account.

The second instruction call uses the already created accounts with their values. 

Copied
@instruction def do_fizzbuzz(fizzbuzz: FizzBuzz, n: u64):   fizzbuzz.fizz = n % 3 == 0   fizzbuzz.buzz = n % 5 == 0   if not fizzbuzz.fizz and not fizzbuzz.buzz:   fizzbuzz.n = n   else:   fizzbuzz.n = 0

Here is the complete code sample for FizzBuzz: 

Copied
# fizzbuzz # Built with Seahorse v0.1.6 # On-chain, persistent FizzBuzz! from seahorse.prelude import * # This is your program's public key and it will update #automatically when you build the project. declare_id('9q23KbCZWzeeCoyAdS9JTCeno6gWV9U6stzP7yozYWKh');   class FizzBuzz(Account):   fizz: bool   buzz: bool   n: u64   @instruction def init(owner: Signer, fizzbuzz: Empty[FizzBuzz]):   fizzbuzz.init(payer = owner, seeds = ['fizzbuzz', owner])   @instruction def do_fizzbuzz(fizzbuzz: FizzBuzz, n: u64):   fizzbuzz.fizz = n % 3 == 0   fizzbuzz.buzz = n % 5 == 0   if not fizzbuzz.fizz and not fizzbuzz.buzz: fizzbuzz.n = n   else: fizzbuzz.n = 0

Seahorse is a community-led open-source project that is entirely free for public use. Seahorse is currently in beta and many of its features are still yet to be implemented. While it is not production-ready, it can be used to experiment and develop applications on Solana's devnet

With Seahorse, building on Solana with Python is possible and simple. If you're a Python developer interested in learning web3 development, exploring Seahorse with Solana Playground is a safe and fun way to start acquiring skills using Anchor, Rust, and Solana programs.

Overview cards background graphic
Section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key