Blockchain-based decentralized prepaid-rental contracts built with Solana
[travis-image]:
https://travis-ci.org/solana-labs/example-helloworld.svg?branch=master
This project demonstrates how to use the Solana Javascript
API to
interact with programs on the Solana blockchain.
The project comprises of:
If you decide to open in Gitpod then refer to
README-gitpod.md, otherwise continue reading.
The following dependencies are required to build and run this example, depending
on your OS, they may already be installed:
If this is your first time using Rust, these Installation
Notes might be helpful.
If you’re on Windows, it is recommended to use WSL to run these commands
$ solana config set --url localhost
If this is your first time using the Solana CLI, you will need to generate a new keypair:
$ solana-keygen new
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
$ npm install
There is both a Rust and C version of the on-chain program, whichever is built
last will be the one used when running the example.
$ npm run build:program-rust
$ npm run build:program-c
$ solana program deploy dist/program/helloworld.so
$ npm run start
Public key values will differ:
Let's say hello to a Solana account...
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
solana logs
to see why the program failed.To customize the example, make changes to the files under /src
. If you change
any files under /src/program-rust
or /src/program-c
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.
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
The client in this example is written in TypeScript using:
The client’s
entrypoint
does five things.
The client establishes a connection with the cluster by callingestablishConnection
.
The client ensures there is an account available to pay for transactions,
and creates one if there is not, by callingestablishPayer
.
In checkProgram
,
the client loads the keypair of the deployed program from ./dist/program/helloworld-keypair.json
and uses
the public key for the keypair to fetch the program account. If the program doesn’t exist, the client halts
with an error. If the program does exist, it will create a new account with the program assigned as its owner
to store program state (number of hello’s processed).
The client then constructs and sends a “Hello” transaction to the program by
callingsayHello
.
The transaction contains a single very simple instruction that primarily carries
the public key of the helloworld program account to call and the “greeter”
account to which the client wishes to say “Hello” to.
Each time the client says “Hello” to an account, the program increments a
numerical count in the “greeter” account’s data. The client queries the
“greeter” account’s data to discover the current number of times the account has
been greeted by callingreportGreetings
.
The on-chain helloworld program is a Rust program
compiled to Berkley Packet Format
(BPF) and stored as an
Executable and Linkable Format (ELF) shared
object.
The program is written using:
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
Solana maintains three public clusters:
devnet
- Development cluster with airdrops enabledtestnet
- Tour De Sol test cluster without airdrops enabledmainnet-beta
- Main clusterUse 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
There is lots more to learn; The following examples demonstrate more advanced
features like custom errors, advanced account handling, suggestions for data
serialization, benchmarking, etc…