How to Develop and Deploy a Solana Smart Contract

·

If you want to learn how to develop smart contracts and projects on Solana, you're in the right place. Solana is an emerging high-performance permissionless public blockchain that offers fast, inexpensive, and scalable transactions. It supports writing smart contracts in Rust, C++, and C.

In this technical tutorial, we'll explore how to write, deploy, and interact with a contract on the Solana Devnet (test network), and how to use Chainlink Price Feeds within Solana smart contracts.

Understanding Solana's Architecture and Programming Model

Solana is a high-performance blockchain capable of processing thousands of transactions per second with sub-second block times. It achieves this through a Byzantine Fault Tolerant (BFT) consensus mechanism that utilizes an innovative cryptographic function called Proof of History (PoH).

What Is Proof of History?

Proof of History (PoH) establishes a cryptographically verifiable order of events (transactions in this case) over time by using a high-frequency Verifiable Delay Function (VDF). Essentially, PoH acts like a cryptographic clock that helps the network agree on time and event sequence without waiting for messages from other nodes.

This allows for parallel processing of ordered events, enhancing network performance compared to traditional blockchains where a single process verifies and packages all transactions for inclusion in the next block.

Smart Contract Architecture

Solana offers a different smart contract model than traditional EVM-based blockchains. On EVM-based chains, contract code/logic and state are combined into a single deployed contract. In Solana, programs (smart contracts) are read-only or stateless and contain only program logic. Once deployed, these programs can be interacted with via external accounts.

Accounts that interact with programs store data related to those interactions. This creates a logical separation between state (accounts) and contract logic (programs)—a key difference between Solana and EVM-based smart contracts.

Solana provides both a CLI and JSON RPC API for decentralized applications to interact with its blockchain. Existing SDKs can also be used for client-side communication with the blockchain and Solana programs.

Deploying Your First Solana Smart Contract

In this section, you'll create and deploy a "hello world" program on Solana, written in Rust.

Prerequisites

Before continuing, ensure you have the following tools installed:

The HelloWorld Program

The HelloWorld program is a smart contract that prints output to the console and counts how many times a given account has called the program, storing this number on-chain.

The code uses standard Solana program parameters and defines the program entry point (process_instruction function). It also uses Borsh for serializing and deserializing parameters passed to and from the deployed program.

The process_instruction function accepts the program_id (the public key/address where the program is deployed) and accountInfo (the account interacting with the contract). The ProgramResult contains the main program logic, which prints a message, iterates through accounts, verifies account permissions, increments the stored count, and writes the result back.

Deploying the Program

First, clone the example code:

git clone https://github.com/solana-labs/example-helloworld
cd example-helloworld

Set your environment to devnet, the test network for Solana developers:

solana config set --url https://api.devnet.solana.com

Create a keypair for your account (necessary for deployment on Solana testnet). Note: This method of storing keypairs is insecure and should only be used for demo purposes.

solana-keygen new --force

Use the airdrop program to get some SOL tokens (lamports) needed for deployment:

solana airdrop 5

Build the hello world program:

npm run build:program-rust

Deploy the program to devnet (the exact command may vary slightly based on build output):

solana program deploy dist/program/helloworld.so

The result is a successfully deployed hello world program on devnet with a specific Program ID, which can be checked on the Solana Devnet Explorer.

Interacting with the Deployed Program

The code repository provides a simple client written in TypeScript using Solana's web3.js library and web3 API.

The client establishes a connection to the node, ensures a paying account exists, checks the program, sends a "hello" transaction instruction to the program, and queries account data to retrieve how many times the sayHello transaction has been executed.

Install client dependencies:

npm install

Start the client:

npm run start

The output will show the program executed successfully and display how many times the account has been greeted. Running it again will increment this number.

Congratulations! You've successfully deployed and interacted with a program on the Solana testnet. Now let's explore another Solana program and client example, this time using Chainlink Price Feeds.

Using Chainlink Price Feeds on Solana

The DeFi application ecosystem on Solana is growing rapidly. These dApps require access to highly reliable, high-quality market data to support basic DeFi mechanisms and perform critical on-chain functions like issuing loans at fair market prices or liquidating undercollateralized positions.

Solana recently integrated Chainlink Price Feeds on their devnet, providing developers with highly decentralized, high-quality, sub-secondly updated price reference data for building hybrid smart contracts.

Combined with Solana's ability to handle up to 65,000 transactions per second and its extremely low transaction fees, Chainlink Price Feeds can enhance DeFi protocol infrastructure to compete with traditional financial systems in terms of transaction execution and risk management quality.

In the following code example, we'll deploy and interact with a contract on the Solana testnet.

Prerequisites

The Chainlink Solana Demo Program

The Chainlink Solana Demo program is a smart contract that fetches and stores the latest price for a specified pair by connecting to a Chainlink Price Feed account on devnet.

Chainlink Price Feeds on Solana all use the same program, with each individual Price Feed having a separate account that uses this program to submit price updates.

The code includes standard inclusions and declarations using Borsh for parameter serialization/deserialization, plus struct setup for storing Price Feed results and decimal data.

The program declares the entry point as the process_instruction function. This function contains the smart contract's main logic, receiving transactions with specified Price Feed addresses and handling the steps for querying and storing price data on-chain.

Deploying the Program

First, clone the demo code:

git clone https://github.com/smartcontractkit/chainlink-solana-demo
cd chainlink-solana-demo

Set your environment to devnet:

solana config set --url https://api.devnet.solana.com

Create a keypair for your account:

mkdir solana-wallet
solana-keygen new --outfile solana-wallet/keypair.json

Use the airdrop program to get some SOL tokens (lamports) needed for deployment:

solana airdrop 5 $(solana-keygen pubkey solana-wallet/keypair.json)

If this doesn't work, you can view the public key and use this address to request SOL tokens from a faucet:

solana-keygen pubkey ./solana-wallet/keypair.json

Build the Chainlink Solana demo program using Solana BPF:

cargo build-bpf

Deploy the program to devnet (add the --keypair option as shown):

solana program deploy target/deploy/chainlink_solana_demo.so --keypair solana-wallet/keypair.json

The result is a successfully deployed program on devnet with a specific Program ID, which can be checked on the Solana Devnet Explorer.

Interacting with the Deployed Program

The code repository provides a simple client written in TypeScript using Solana's web3.js library and web3 API.

The client establishes a connection to the node, ensures a paying account exists, checks the program, sends a transaction with a Price Feed account as a parameter (in this case, the SOL/USD Feed account listed on the Chainlink Solana Devnet Price Feeds page), waits for transaction confirmation, and queries account data to retrieve the price data stored from the SOL/USD Price Feed.

Install client dependencies:

cd client
yarn

Start the client:

yarn start

The output will show the program executed successfully and should display the stored current SOL/USD price data.

Conclusion

Solana provides a high-speed, low-cost, scalable blockchain for building smart contracts and decentralized applications. By using Solana smart contracts with Chainlink Price Feeds, developers can leverage the advantages of Chainlink's high-quality data and sub-second updates available on the Solana blockchain to create fast, scalable DeFi applications.

👉 Explore real-time development tools to enhance your blockchain projects.

Frequently Asked Questions

What programming languages can I use for Solana smart contracts?
Solana supports Rust, C++, and C for writing smart contracts. Rust is currently the most widely used and well-supported language for Solana development with extensive documentation and community resources.

How much does it cost to deploy a smart contract on Solana?
Deployment costs vary but are generally very low compared to other blockchains. You'll need a small amount of SOL (lamports) for account creation and transaction fees, but there's no ongoing cost for having a contract deployed on the network.

What is the difference between Solana programs and Ethereum smart contracts?
The key difference is that Solana separates program logic from state storage. Programs are stateless and contain only code, while accounts store the data. This differs from Ethereum where contract code and state are combined in a single deployed contract.

How do Chainlink Price Feeds work on Solana?
Chainlink Price Feeds on Solana use a single program with separate accounts for each price pair. These accounts submit price updates to the program, allowing smart contracts to retrieve the latest price data for any supported asset pair.

Can I test my Solana smart contracts before deploying to mainnet?
Yes, Solana provides a devnet (development network) for testing purposes where you can deploy contracts and interact with them without using real funds. This is ideal for development and testing before mainnet deployment.

What resources are available for learning Solana development?
The Solana documentation provides comprehensive guides, and there are many community tutorials, video courses, and development tools available. The Solana Devnet allows for practical experimentation with smart contract deployment and interaction.