Skip to content

This is a beginner smart contract implementation in Solana using Rust and Typescript

License

Notifications You must be signed in to change notification settings

JohnBashombe/basic-smart-contract-in-Solana-Rust-TypeScript

Repository files navigation

Simple Message Sending Over Solana Network

This project demonstrates how to use the Solana Javascript API to interact with programs on the Solana blockchain.

The project comprises of:

  • Sending a custom message on the blockchain
  • A client that can send any message on the network and get it back

Quick Start

The following dependencies are required to build and run this example, depending on your OS, they may already be installed:

Configure CLI

If you're on Windows, it is recommended to use WSL to run these commands

  1. Set CLI config url to localhost cluster
solana config set --url localhost
  1. Create CLI Keypair

If this is your first time using the Solana CLI, you will need to generate a new keypair:

solana-keygen new

Start local Solana cluster

This example connects to a local Solana cluster by default.

Start a local Solana cluster:

solana-test-validator

Note: You may need to do some system tuning (and restart your computer) to get the validator to run

Listen to transaction logs:

solana logs

Install npm dependencies

npm install

Build the on-chain program

This is a Rust version of the on-chain program

npm run build:program-rust

Deploy the on-chain program

solana program deploy dist/program/helloworld.so

Run the JavaScript client

npm run start

Expected output

Public key values will differ:

Let's send a message on Sol
Connection to cluster established: http://localhost:8899 { 'feature-set': 2045430982, 'solana-core': '1.7.8' }
Using account AiT1QgeYaK86Lf9kudqKthQPCWwpG8vFA1bAAioBoF4X containing 0.00141872 SOL to pay for fees
Using program Dro9uk45fxMcKWGb1eWALujbTssh6DW8mb4x8x3Eq5h6
Creating account 8MBmHtJvxpKdYhdw6yPpedp6X6y2U9dCpdYaZJdmwV3A to say hello to
Saying hello to 8MBmHtJvxpKdYhdw6yPpedp6X6y2U9dCpdYaZJdmwV3A
8MBmHtJvxpKdYhdw6yPpedp6X6y2U9dCpdYaZJdmwV3A has been greeted 1 times
Success

Not seeing the expected output?

  • Ensure you've started the local cluster, built the on-chain program and deployed the program to the cluster.
  • Inspect the program logs by running solana logs to see why the program failed.
    • Transaction executed in slot 5621:
      Signature: 4pya5iyvNfAZj9sVWHzByrxdKB84uA5sCxLceBwr9UyuETX2QwnKg56MgBKWSM4breVRzHmpb1EZQXFPPmJnEtsJ
      Status: Error processing Instruction 0: Program failed to complete
      Log Messages:
        Program G5bbS1ipWzqQhekkiCLn6u7Y1jJdnGK85ceSYLx2kKbA invoke [1]
        Program log: Hello World Rust program entrypoint
        Program G5bbS1ipWzqQhekkiCLn6u7Y1jJdnGK85ceSYLx2kKbA consumed 200000 of 200000 compute units
        Program failed to complete: exceeded maximum number of instructions allowed (200000) at instruction #334
        Program G5bbS1ipWzqQhekkiCLn6u7Y1jJdnGK85ceSYLx2kKbA failed: Program failed to complete

Customizing the Program

To customize the example, make changes to the files under /src. If you change any files under /src/program-rust you will need to rebuild the on-chain program and redeploy the program.

Now when you rerun npm run start, you should see the results of your changes.

Learn about Solana

More information about how Solana works is available in the Solana documentation and all the source code is available on github

Further questions? Visit us on Discord

Learn about the client

The client in this example is written in TypeScript using:

Entrypoint

The client's entrypoint does five things.

Programming on Solana

To learn more about Solana programming model refer to the Programming Model Overview.

To learn more about developing programs on Solana refer to the On-Chain Programs Overview

Pointing to a public Solana cluster

Solana maintains three public clusters:

  • devnet - Development cluster with airdrops enabled
  • testnet - Tour De Sol test cluster without airdrops enabled
  • mainnet-beta - Main cluster

Use the Solana CLI to configure which cluster to connect to.

To point to devnet:

solana config set --url devnet

To point back to the local cluster:

solana config set --url localhost

About

This is a beginner smart contract implementation in Solana using Rust and Typescript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published