在 Solana 上使用 Chainlink 的示例代码
The Chainlink Solana Starter Kit is an Anchor based program and client that shows developers how to use and interact with Chainlink Price Feeds on Solana. The demo is configured to run on the Devnet cluster, and is comprised of an on-chain program written in Rust, and an off-chain client written in JavaScript. The program takes parameters and account information from the off-chain client, retrieves the latest price data from the specified Chainlink Price Feed on Devnet, then writes the data out to the specified account, which can then be read by the off-chain client.
The following environment variables must be set before running the scripts:
| Variable | Description | Required For |
|---|---|---|
EVM_PRIVATE_KEY |
The private key for the EVM account | All EVM chain operations |
EVM_RPC_URL |
RPC URL for Ethereum Sepolia | Operations on Ethereum Sepolia |
AVAX_RPC_URL |
RPC URL for Avalanche Fuji | Operations on Avalanche Fuji |
SOLANA_RPC_URL |
RPC URL for Solana Devnet (defaults to public endpoint if not set) | Operations on Solana Devnet |
Create a .env file in the root directory with the following format:
EVM_PRIVATE_KEY=your_private_key_here
EVM_RPC_URL=https://your-ethereum-sepolia-rpc-url
AVAX_RPC_URL=https://your-avalanche-fuji-rpc-url
SOLANA_RPC_URL=https://your-solana-devnet-rpc-urlImportant: The RPC URLs must be valid and accessible for the scripts to work. If an RPC URL is not provided for a chain, an error will be thrown when attempting to use that chain.
First, ensure that you're in the solana-starter-kit directory in this repository
cd ./solana-starter-kitNext step is to install all of the required dependencies:
npm installNote for Apple M1 chipsets: You will need to perform an extra step to get the Anchor framework installed manually from source, as the NPM package only support x86_64 chipsets currently, please run the following command to install it manually:
cargo install --git https://github.com/coral-xyz/anchor --tag v0.31.1 anchor-cli --lockedNext, generate a new wallet:
solana-keygen new -o id.jsonYou should see the public key in the terminal output. Alternatively, you can find the public key with the following CLI command:
solana-keygen pubkey id.jsonNext, airdrop 5 SOL tokens into your new account. Be sure to replace both instances of with your wallet's public key from the previous step:
solana airdrop $(solana address --keypair id.json) --url https://api.devnet.solana.comConfirm that you have received the 5 SOL by running solana balance --keypair id.json.
Next, build the program:
anchor buildBefore you deploy the chainlink_solana_demo program, you must update its public key in ./programs/chainlink_solana_demo/src/lib.rs:
declare_id!("####GEg__SOME__KEY__HERE__F2TsL#####");This declare_id! macro is used to hardcode the program's public key directly into the
program's bytecode.
To update this public key, you need to run the command below:
anchor keys syncAfter this command, check ./programs/chainlink_solana_demo/src/lib.rs again. The declare_id! line will be updated with your program address, for example:
declare_id!("JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm");To avoid having to add --keypair id.json to future commands now is a good time to add the following in your terminal:
export ANCHOR_WALLET="./id.json" --> include the quotes
Finally, because you updated the source code with its generated public key, you need to rebuild the program to regenerate the associated files for deployment, and then it can be deployed to devnet
anchor build
anchor deploy --provider.cluster devnetOnce you have successfully deployed the program, the terminal output will specify the program ID. Make sure you're looking for the Program ID of the chainlink_solana_demo program.
The Program ID must match the value in ./programs/chainlink_solana_demo/src/lib.rs.
The declare_id! and your deployed program ID should be the same. If they're different, it could indicate:
Once again, take note of this Program ID, as it will be required when executing the client:
Deploying workspace: https://api.devnet.solana.com
Upgrade authority: ./id.json
Deploying program "chainlink_solana_demo"...
Program path: ./target/deploy/chainlink_solana_demo.so...
Program Id: JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqmNext we set up the JavaScript client that interacts with our deployed program on Devnet.
The first step is to set the Anchor environment variables. These are required by the Anchor framework to determine which provider to use and which wallet to use for interacting with the deployed program:
export ANCHOR_PROVIDER_URL='https://api.devnet.solana.com'
export ANCHOR_WALLET='./id.json'Now you are ready to run the JavaScript client. Be sure to pass Chainlink data feed address that you want to query. This can be taken from the Chainlink Solana Data Feeds page, and the value will be defaulted to the Devnet SOL/USD feed address if you don't specify a value. In this example, we specified the ETH/USD feed:
node client.js --feed 669U43LNHx7LsVj95uYksnhXUfWKDsdzVqev3V4Jpw3PThe client will generate a new account and pass it to the deployed program, which will then populate the account with the current price from the specified price feed. The client will then read the price from the account, and output the value to the console.
…To facilitate the scenario of purely requiring Chainlink Price Feed data off-chain, we have also included a second read-data client that queries a specified price feed and returns the latest price data. This version of the client does not generate a transaction, and therefore requires no accounts created or transaction fees. To run the read-data client, first you should ensure you have set the required Anchor environment variables. You can skip this step if you already did it earlier before running the normal client:
export ANCHOR_PROVIDER_URL='https://api.devnet.solana.com'
export ANCHOR_WALLET='./id.json'Next, you can set the value of the CHAINLINK_FEED_ADDRESS static variable to the value of the Price Feed account address that you wish to query. This example queries the ETH/USD feed on Devnet:
const CHAINLINK_FEED_ADDRESS="669U43LNHx7LsVj95uYksnhXUfWKDsdzVqev3V4Jpw3P"Once you save your file, you can then execute the client. There is a Typescript and a JavaScript version:
Typescript:
yarn read-dataJavaScript:
node read-data.jsThe client will query the specified price feed using the published Chainlink Solana NPM package, and will then continuously just print the latest price to the console.
pappas99@Pappas solana-starter-kit % yarn read-data
> @ read-data /Users/pappas99/GitHub/22-hackathon/solana-starter-kit
> ts-node ./read-data.ts
301296000000
301250000000
301215000000
301205000000
301331000000You can execute the integration test with the following command
anchor testThe integration test checks that the value of the specified price feed account (defaulted to SOL/USD) on Devnet is greater than 0.
solana-starter-kit
Price Is: 105.52
✔ Query SOL/USD Price Feed! (4521ms)
1 passing (5s)
✨ Done in 10.49s.If you'd like to run the test after initial deployment of the contracts, you can run the tests directly via yarn.
yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.tsYou will be asked to provide anchor environment variables. See .env.example for details.
This repository also includes scripts and libraries for Chainlink CCIP, enabling cross-chain communication between Solana and EVM-compatible blockchains.
Cross-chain token (CCT) pool setup (burn-mint pools, admin registry, ALT management) lives in ccip-solana-bs58-generator, not this repo.
# Estimate fees (read-only, no private key required)
yarn evm:check-fee
# Get test tokens
yarn evm:token:drip
# Send BnM tokens to Solana
yarn evm:transfer --token 0x779877A7B0D9E8603169DdbD7836e478b4624789 --amount 1000000000000000000# Delegate authority for CCIP transfers
yarn svm:token:delegate
# Send tokens cross-chain
yarn svm:token-transfer --token-mint YourTokenMintAddress --token-amount 1000000For comprehensive CCIP documentation:
Track your cross-chain messages on the CCIP Explorer
暂无开放 Issues,或尚未同步最近议题。