Coder Social home page Coder Social logo

solana-starter-kit's Introduction


Chainlink Solana logo


Open in Gitpod

Chainlink Solana Starter Kit

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.

Running the example on Devnet

Requirements

Building and Deploying the Consumer Program

First, ensure that you're in the solana-starter-kit directory in this repository

cd ./solana-starter-kit

Next step is to install all of the required dependencies:

npm install

Note 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/project-serum/anchor --tag v0.20.1 anchor-cli --locked

Next, generate a new wallet:

solana-keygen new -o id.json

You 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.json

Next, airdrop some SOL tokens into your new account. We will need to call this twice, because the Devnet faucet is limited to 2 SOL, and we need approximately 4 SOL. Be sure to replace both instances of <RECIPIENT_ACCOUNT_ADDRESS> with your wallet's public key from the previous step:

solana airdrop 2 $(solana-keygen pubkey ./id.json) --url https://api.devnet.solana.com && solana airdrop 2 $(solana-keygen pubkey ./id.json) --url https://api.devnet.solana.com

Next, build the 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. To do this, you need to 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

The next step is to edit the lib.rs file and replace the keypair in the declare_id!() definition with the value you obtained from the previous step:

declare_id!("JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm");

Finally, because you updated the source code with the generated program ID, you need to rebuild the program again, and then it can be deployed to devnet

anchor build
anchor deploy --provider.cluster devnet

Once you have successfully deployed the program, the terminal output will specify the program ID of the program, it should match the value you inserted into the lib.rs file and the Anchor.toml file. 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: JC16qi56dgcLoaTVe4BvnCoDL6FhH5NtahA7jmWZFdqm

Running the Client

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 the program ID obtained from the previous steps by using the --program flag pointing to the JSON file containing the account that owns the program, as well as the 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 --program $(solana address -k ./target/deploy/chainlink_solana_demo-keypair.json) --feed 	669U43LNHx7LsVj95uYksnhXUfWKDsdzVqev3V4Jpw3P

The 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.

Running client...
priceFeedAccount public key: DNQBqwGijKix2EmKhMMaftZgSywcbfnQZSzfDyEMEfLf
user public key: GWKzUMdSF8Y4xQ3JANTChkaJDFE4UdkvAkHCknmJtJUX
Fetching transaction logs...
[
  'Program BrEqc6zHVR77jrP6U6WZLUV24AZ9UnHrWfDQTDV7VoDY invoke [1]',
  'Program log: Instruction: Execute',
  'Program 11111111111111111111111111111111 invoke [2]',
  'Program 11111111111111111111111111111111 success',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny invoke [2]',
  'Program log: Instruction: Query',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny consumed 2551 of 1360424 compute units',
  'Program return: HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny RZ0GABn5swcAAAAA3ltiYgAVg8dFAAAAAAAAAAAAAAA=',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny success',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny invoke [2]',
  'Program log: Instruction: Query',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny consumed 2245 of 1328033 compute units',
  'Program return: HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny CQAAAEVUSCAvIFVTRA==',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny success',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny invoke [2]',
  'Program log: Instruction: Query',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny consumed 1826 of 1295650 compute units',
  'Program return: HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny CA==',
  'Program HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny success',
  'Program log: ETH / USD price is 2997.00000000',
  'Program BrEqc6zHVR77jrP6U6WZLUV24AZ9UnHrWfDQTDV7VoDY consumed 109699 of 1400000 compute units',
  'Program return: BrEqc6zHVR77jrP6U6WZLUV24AZ9UnHrWfDQTDV7VoDY CA==',
  'Program BrEqc6zHVR77jrP6U6WZLUV24AZ9UnHrWfDQTDV7VoDY success'
]
Price Is: 2997
Success

Running the Read Only Clients

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:

npm run read-data

JavaScript:

node read-data.js

The 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 % npm run read-data
> @ read-data /Users/pappas99/GitHub/22-hackathon/solana-starter-kit
> ts-node ./read-data.ts

301296000000
301250000000
301215000000
301205000000
301331000000

Testing

You can execute the integration test with the following command

anchor test

The 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.

solana-starter-kit's People

Contributors

chester-king avatar danielgruesso avatar dwightjl avatar pappas999 avatar patrickalphac avatar rgottleber avatar zeuslawyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

solana-starter-kit's Issues

`Invalid blockhash` error while deploying smart contract

I am trying to deploy the demo smart contract on solana for chainlink price feed but getting an error. I followed all the steps from https://docs.chain.link/docs/solana/using-data-feeds-solana/

$ anchor deploy --provider.wallet ./id.json --provider.cluster devnet

Deploying workspace: https://api.devnet.solana.com
Upgrade authority: ./id.json
Deploying program "chainlink_solana_demo"...
Program path: /home/test/solana-starter-kit/target/deploy/chainlink_solana_demo.so...
=============================================================================
Recover the intermediate account's ephemeral keypair file with
`solana-keygen recover` and the following 12-word seed phrase:
=============================================================================
until reason almost can clean wish trend buffalo future auto artefact balcony
=============================================================================
To resume a deploy, pass the recovered keypair as the
[BUFFER_SIGNER] to `solana program deploy` or `solana write-buffer'.
Or to recover the account's lamports, pass it as the
[BUFFER_ACCOUNT_ADDRESS] argument to `solana program close`.
=============================================================================
Error: Custom: Invalid blockhash
There was a problem deploying: Output { status: ExitStatus(unix_wait_status(256)), stdout: "", stderr: "" }.

'The given account is owned by a different program than expected'

I'm trying to get the repository setup with the instructions. I'm following the steps exactly:

  1. Install dependencies and run anchor build. Copying the Program Id.
  2. Modify lib.rs and anchor.tom to use the new Program Id string
  3. run anchor deploy to deploy on devnet
  4. execute command node client.js --program <Program Id> --feed 5zxs8888az8dgB5KauGEFoPuMANtrKtkpFiFRmo3cSa9

I get the following error:

node client.js --program 6mNYkLEbzxkjLdF73oLZtDZLaTkbhjzjmkLTD6VBUQV6 --feed 5zxs8888az8dgB5KauGEFoPuMANtrKtkpFiFRmo3cSa9
Running client...
priceFeedAccount public key: HLyaJrffarpjj8i4S17r6V1oXX5PncmicWcva8bfwEPU
user public key: M1YDPWDPromi4SDKkZ8RD6VhSgzFwFUQF53ypVX7yCM
Transaction simulation failed: Error processing Instruction 0: custom program error: 0xbbf
    Program 6mNYkLEbzxkjLdF73oLZtDZLaTkbhjzjmkLTD6VBUQV6 invoke [1]
    Program log: Instruction: Execute
    Program 11111111111111111111111111111111 invoke [2]
    Program 11111111111111111111111111111111 success
    Program log: Custom program error: 0xbbf
    Program 6mNYkLEbzxkjLdF73oLZtDZLaTkbhjzjmkLTD6VBUQV6 consumed 10343 of 1400000 compute units
    Program 6mNYkLEbzxkjLdF73oLZtDZLaTkbhjzjmkLTD6VBUQV6 failed: custom program error: 0xbbf
/Users/andy/dev/solana-starter-kit/node_modules/@project-serum/anchor/dist/cjs/error.js:40
            return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
                   ^

ProgramError: 3007: The given account is owned by a different program than expected
    at Function.parse (/Users/andy/dev/solana-starter-kit/node_modules/@project-serum/anchor/dist/cjs/error.js:40:20)
    at Object.rpc [as execute] (/Users/andy/dev/solana-starter-kit/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:38:61)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async main (/Users/andy/dev/solana-starter-kit/client.js:39:12) {
  code: 3007,
  msg: 'The given account is owned by a different program than expected'
}

Node.js v17.5.0

Is the onchain program working, and if so what am I doing wrong here?

Not able to fetch the price

logs: [
'Program 7R6ZpXj8WcStMFcQ3MwevwLWyc1BbzjQbC57esGCHgW3 invoke [1]',
'Program log: Instruction: Execute',
'Program 7R6ZpXj8WcStMFcQ3MwevwLWyc1BbzjQbC57esGCHgW3 consumed 4227 of 200000 compute units',
'Program 7R6ZpXj8WcStMFcQ3MwevwLWyc1BbzjQbC57esGCHgW3 failed: Instruction passed to inner instruction is too large (12884933988 > 1280)'
],

project-serum/anchor should be updated to v0.24.2 ?

hello @pappas999

"@project-serum/anchor": "^0.20.1",

I've tried to run the typescript version yarn run read-data and got the following issue

TSError: ⨯ Unable to compile TypeScript:
read-data.ts(13,62): error TS2345: Argument of type 'Provider' is not assignable to parameter of type 'AnchorProvider'.
  Property 'sendAndConfirm' is missing in type 'Provider' but required in type 'AnchorProvider'.

Note that everything works fine after :

  • I update package.json "@project-serum/anchor": "^0.24.2"
  • specify AnchorProvider. : const provider = anchor.AnchorProvider.env();

Struct field "chainlink_feed" is unsafe, but is not documented.

Hello, I followed your document on https://docs.chain.link/docs/solana/using-data-feeds-solana/ and got the error below. I couldn't figure out the solution.

I was on the 7th step:

With the new program ID added, run anchor build again. This recreates the necessary program files with the correct program ID: anchor build

barlas@~/hobby/solana-starter-kit$ cargo --version
cargo 1.59.0 (49d8809dc 2022-02-10)
barlas@~/hobby/solana-starter-kit$ node --version
v16.13.0
barlas@~/hobby/solana-starter-kit$ npm --version
8.1.0
barlas@~/hobby/solana-starter-kit$ solana --version
solana-cli 1.8.14 (src:9c01d90c; feat:1006352700)
barlas@~/hobby/solana-starter-kit$ anchor build
BPF SDK: /home/barlas/.local/share/solana/install/releases/1.8.14/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 0.07s
cargo-build-bpf child: /home/barlas/.local/share/solana/install/releases/1.8.14/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /home/barlas/hobby/solana-starter-kit/target/deploy/chainlink_solana_demo.so

To deploy this program:
  $ solana program deploy /home/barlas/hobby/solana-starter-kit/target/deploy/chainlink_solana_demo.so
The program address will default to this keypair (override with --program-id):
  /home/barlas/hobby/solana-starter-kit/target/deploy/chainlink_solana_demo-keypair.json
Error: 
        /home/barlas/hobby/solana-starter-kit/programs/chainlink_solana_demo/src/lib.rs:73:8
        Struct field "chainlink_feed" is unsafe, but is not documented.
        Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
        See https://book.anchor-lang.com/chapter_3/the_accounts_struct.html#safety-checks for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.