Search
Chainlink Data Feeds are the quickest way to connect your smart contracts to the real-world market prices of assets. This guide demonstrates how to deploy a contract to the Solana Devnet and access Data Feeds on-chain using the Chainlink Solana Starter Kit. To learn how to read price feed data using off-chain applications, see the Using Data Feeds Off-Chain guide.
To get the full list of available Chainlink Data Feeds on Solana, see the Solana Feeds page. View the program that owns the Chainlink Data Feeds in the Solana Devnet Explorer.
Select quality data feeds
Be aware of the quality of the data that you use. Learn more about making responsible data quality decisions.
This guide demonstrates the following tasks:
This example shows you how to work with a program that you deploy, but you can refactor the client section to work with a program ID of your choice.
Table of contents:
Before you begin, set up your environment for development on Solana:
Install Git if it is not already configured on your system.
Install Node.js 14 or higher. Run node --version
to verify which version you have installed:
node --version
Install Yarn to simplify package management and run code samples.
Install a C compiler such as the one included in GCC. Some dependencies require a C compiler.
sudo apt install gcc
Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh &&
source $HOME/.cargo/env
Install the latest Mainnet version of the Solana CLI and export the path to the CLI:
sh -c "$(curl -sSfL https://release.solana.com/v1.9.18/install)" &&
export PATH="~/.local/share/solana/install/active_release/bin:$PATH"
Run solana --version
to make sure the Solana CLI is installed correctly.
solana --version
Use the Node package manager to Install Anchor globally. The global install allows you to run the Anchor CLI. Depending on your environment, this step might require sudo
permissions:
npm i -g @project-serum/anchor-cli
On some operating systems, you might need to build and install Anchor locally. See the Anchor documentation for instructions.
After you install the required tools, build and deploy the example program from the solana-starter-kit repository.
This example includes a contract written in Rust. Deploy the contract to the Solana Devnet cluster.
In a terminal, clone the solana-starter-kit repository and change to the solana-starter-kit
directory:
git clone https://github.com/smartcontractkit/solana-starter-kit &&
cd ./solana-starter-kit
You can see the complete code for the example on GitHub.
In the ./solana-starter-kit
directory, install Node.js dependencies defined in the package.json
file:
yarn install
Create a temporary Solana wallet to use for this example. Use a temporary wallet to isolate development and testing from your other wallets. Alternatively, if you have an existing wallet that you want to use, locate the path to your keypair file and use it as the keypair for the rest of this guide.
solana-keygen new --outfile ./id.json
Always follow the security best practices in the Solana Wallet Guide when managing your wallets and keypairs.
Fund your Solana wallet. On Devnet, use solana airdrop
to add tokens to your account. The contract requires at least 4 SOL to deploy and the faucet limits each request to 2 SOL, so you must make two requests to get a total of 4 SOL on your wallet:
solana airdrop 2 --keypair ./id.json --url devnet &&
solana airdrop 2 --keypair ./id.json --url devnet
If the command line faucet does not work, run solana address
on the temporary wallet to print the public key value for the wallet and request tokens from SolFaucet:
solana address -k ./id.json
Run anchor build
to build the example program. If you receive the no such subcommand: 'build-bpf'
error, restart your terminal session and run anchor build
again:
anchor build
The build process generates the keypair for your program's account. Before you deploy your program, you must add this public key to the lib.rs
file:
Get the keypair from the ./target/deploy/chainlink_solana_demo-keypair.json
file that Anchor generated:
solana address -k ./target/deploy/chainlink_solana_demo-keypair.json
Edit the ./programs/chainlink_solana_demo/src/lib.rs
file and replace the keypair in the declare_id!()
definition:
vi ./programs/chainlink_solana_demo/src/lib.rs
declare_id!("JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm");
With the new program ID added, run anchor build
again. This recreates the necessary program files with the correct program ID:
anchor build
Run anchor deploy
to deploy the program to the Solana Devnet. Remember to specify the keypair file for your wallet and override the default. This wallet is the account owner (authority) for the program:
anchor deploy --provider.wallet ./id.json --provider.cluster devnet
To confirm that the program deployed correctly, run solana program show --programs
to get a list of deployed programs that your wallet owns. For this example, check the list of deployed programs for the id.json
wallet on the Solana Devnet:
solana program show --programs --keypair ./id.json --url devnet
The command prints the program ID, slot number, the wallet address that owns the program, and the program balance:
Program Id | Slot | Authority | Balance
GRt21UnJFHZvcaWLbcUrXaTCFMREewDrm1DweDYBak3Z | 110801571 | FsQPnANKDhqpoayxCL3oDHFCBmrhP34NrfbDR34qbQUt | 3.07874904 SOL
To see additional details of your deployed program, copy the program ID and look it up in the Solana Devnet Explorer.
Now that the program is on-chain, you can call it using the Anchor Web3 module.
Use your deployed program to retrieve price data from a Chainlink data feed on Solana Devnet. For this example, call your deployed program using the Anchor Web3 module and the client.js
example code.
Set the Anchor environment variables. Anchor uses these to determine which wallet to use and Solana cluster to use.
export ANCHOR_PROVIDER_URL=https://api.devnet.solana.com &&
export ANCHOR_WALLET=./id.json
Run the client.js
example and pass the program address in using the --program
flag:
node client.js --program $(solana address -k ./target/deploy/chainlink_solana_demo-keypair.json)
If the script executes correctly, you will see output with the current price of SOL / USD.
â‹®
Price Is: 96.79778375
Success
â‹®
Each request costs an amount of SOL that is subtracted from the id.json
wallet. Run solana balance
to check the remaining balance for your temporary wallet on Devnet.
solana balance --keypair ./id.json --url devnet
To get prices for a different asset pair, run client.js
again and add the --feed
flag with one of the available Chainlink data feeds on the Solana Devnet. For example, to get the price of LINK / USD, use the following command:
node client.js \
--program $(solana address -k ./target/deploy/chainlink_solana_demo-keypair.json) \
--feed 6N2eCQv8hBkyZjSzN1pe5QtznZL9bGn6TZzcFhaYSLTs
Price Is: 12.4215826
Success
The program that owns the data feeds is HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny, which you can see defined for const CHAINLINK_PROGRAM_ID
in the client.js
file.
After you are done with your deployed contract and no longer need it, it is nice to close the program and withdraw the Devnet SOL tokens for future use. In a production environment, you will want to withdraw unused SOL tokens from any Solana program that you no longer plan to use, so it is good to practice the process when you are done with programs on Devnet.
Run solana program show
to see the list of deployed programs that your wallet owns and the balances for each of those programs:
solana program show --programs --keypair ./id.json --url devnet
Program Id | Slot | Authority | Balance
GRt21UnJFHZvcaWLbcUrXaTCFMREewDrm1DweDYBak3Z | 110801571 | FsQPnANKDhqpoayxCL3oDHFCBmrhP34NrfbDR34qbQUt | 3.07874904 SOL
Run solana program close
and specify the program that you want to close:
solana program close [YOUR_PROGRAM_ID] --keypair ./id.json --url devnet
The program closes and the remaining SOL is transferred to your temporary wallet.
If you have deployments that failed, they might still be in the buffer holding SOL tokens. Run solana program show
again with the --buffers
flag:
solana program show --buffers --keypair ./id.json --url devnet
If you have open buffers, they will appear in the list.
Buffer Address | Authority | Balance
CSc9hnBqYJoYtBgsryJAmrjAE6vZ918qaFhL6N6BdEmB | FsQPnANKDhqpoayxCL3oDHFCBmrhP34NrfbDR34qbQUt | 1.28936088 SOL
If you have any buffers that you do not plan to finish deploying, run the same solana program close
command to close them and retrieve the unused SOL tokens:
solana program close [YOUR_PROGRAM_ID] --keypair ./id.json --url devnet
Check the balance on your temporary wallet.
solana balance --keypair ./id.json --url devnet
If you are done using this wallet for examples and testing, you can use solana transfer
to send the remaining SOL tokens to your default wallet or another Solana wallet that you use. For example, if your default wallet keypair is at ~/.config/solana/id.json
, you can send ALL
of the temporary wallet's balance with the following command:
solana transfer ~/.config/solana/id.json ALL --keypair ./id.json --url devnet
Alternatively, you can send the remaining balance to a web wallet. Specify the public key for your wallet instead of the path the default wallet keypair. Now you can use those Devnet funds for other examples and development.
To learn more about Solana and Anchor, see the Solana Documentation and the Anchor Documentation.