Chainlink Data Feeds are the quickest way to connect your smart contracts to the real-world market prices of assets. For example, one use for data feeds is to enable smart contracts to retrieve the latest pricing data of an asset in a single call.
This guide applies specifically to using data feeds on Solana clusters. To get the full list of 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 12 or higher.
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:
sh -c "$(curl -sSfL https://release.solana.com/v1.8.14/install)"
Export the path to the CLI or reopen your terminal to make sure the Solana CLI is installed correctly.
export PATH="~/.local/share/solana/install/active_release/bin:$PATH" &&
solana --version
Use the Node package manager to Install Anchor. Depending on your environment, this step might require sudo permissions:
npm i -g @project-serum/anchor-cli
After you install the required tools, try the example code 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:
npm 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:
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:
declare_id!("JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm");
Edit the Anchor.toml file and replace the keypair in the chainlink_solana_demo definition:
chainlink_solana_demo = "JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm"
Run the tests to make sure everything works properly. The test deploys the Solana program to Devnet and uses the ./id.json wallet as the account owner (authority):
anchor test
If the test is successful, it prints the current price obtained from the SOL/USD price data feed:
Price Is: 9011370000
✔ Query SOL/USD Price Feed! (5229ms)
1 passing (5s)
Done in 10.80s.
Alternatively, you can use anchor deploy to deploy the program to the Solana Devnet without running the tests:
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: 9056000000
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 DxXQdNEz5atWLbNj2pwzi9xuePDEL9BwzfKCtavW7MHZ
Price Is: 1517000000
Success
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.