Skip to content

Commit

Permalink
Add chainlink-solana, a client for consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jan 21, 2022
1 parent 8fe5899 commit eb19fad
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 35 deletions.
9 changes: 9 additions & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions contracts/crates/chainlink-solana/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "chainlink_solana"
description = "Chainlink client for Solana"
version = "0.1.0"
edition = "2018"
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "lib"]
name = "chainlink_solana"

[features]
default = []

[dependencies]
solana-program = "1.8.6"
borsh = "0.9.1"
borsh-derive = "0.9.1"
21 changes: 21 additions & 0 deletions contracts/crates/chainlink-solana/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 SmartContract ChainLink, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions contracts/crates/chainlink-solana/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# chainlink-solana

Chainlink client for Solana.
115 changes: 115 additions & 0 deletions contracts/crates/chainlink-solana/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! Chainlink feed client for Solana.
#![deny(rustdoc::all)]
#![allow(rustdoc::missing_doc_code_examples)]
#![deny(missing_docs)]

use borsh::{BorshDeserialize, BorshSerialize};

use solana_program::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction},
program::invoke,
program_error::ProgramError,
pubkey::Pubkey,
};

#[derive(BorshSerialize, BorshDeserialize)]
enum Query {
Version,
Decimals,
Description,
RoundData { round_id: u32 },
LatestRoundData,
Aggregator,
}

/// Represents a single oracle round.
#[derive(BorshSerialize, BorshDeserialize)]
pub struct Round {
/// The round id.
pub round_id: u32,
/// Round timestamp, as reported by the oracle.
pub timestamp: u64,
/// Current answer, formatted to `decimals` decimal places.
pub answer: i128,
}

fn query<'info, T: BorshDeserialize>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
scope: Query,
) -> Result<T, ProgramError> {
use std::io::{Cursor, Write};

const QUERY_INSTRUCTION_DISCRIMINATOR: &[u8] =
&[0x27, 0xfb, 0x82, 0x9f, 0x2e, 0x88, 0xa4, 0xa9];

// Avoid array resizes by using the maximum response size as the initial capacity.
const MAX_SIZE: usize = QUERY_INSTRUCTION_DISCRIMINATOR.len() + std::mem::size_of::<Pubkey>();

let mut data = Cursor::new(Vec::with_capacity(MAX_SIZE));
data.write_all(QUERY_INSTRUCTION_DISCRIMINATOR)?;
scope.serialize(&mut data)?;

let ix = Instruction {
program_id: *program_id.key,
accounts: vec![AccountMeta::new_readonly(*feed.key, false)],
data: data.into_inner(),
};

invoke(&ix, &[feed.clone()])?;

let (_key, data) =
solana_program::program::get_return_data().expect("chainlink store had no return_data!");
let data = T::try_from_slice(&data)?;
Ok(data)
}

/// Query the feed version.
pub fn version<'info>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
) -> Result<u8, ProgramError> {
query(program_id, feed, Query::Version)
}

/// Returns the amount of decimal places.
pub fn decimals<'info>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
) -> Result<u8, ProgramError> {
query(program_id, feed, Query::Decimals)
}

/// Returns the feed description.
pub fn description<'info>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
) -> Result<String, ProgramError> {
query(program_id, feed, Query::Description)
}

/// Returns round data for a specific `round_id`.
pub fn round_data<'info>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
round_id: u32,
) -> Result<Round, ProgramError> {
query(program_id, feed, Query::RoundData { round_id })
}

/// Returns round data for the latest round.
pub fn latest_round_data<'info>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
) -> Result<Round, ProgramError> {
query(program_id, feed, Query::LatestRoundData)
}

/// Returns the address of the underlying OCR2 aggregator.
pub fn aggregator<'info>(
program_id: AccountInfo<'info>,
feed: AccountInfo<'info>,
) -> Result<Pubkey, ProgramError> {
query(program_id, feed, Query::Aggregator)
}
42 changes: 11 additions & 31 deletions contracts/examples/hello-world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ default = ["localnet"]

[dependencies]
anchor-lang = "0.20.1"
chainlink-solana = { version = "0.1.0", package = "store", path = "../../../../programs/store", default-features = false, features = ["cpi"] }
chainlink-solana = "0.1.0"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anchor_lang::prelude::*;

use chainlink_solana::accessors as chainlink;
use chainlink_solana as chainlink;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/hello-world/tests/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ describe('hello-world', () => {
});
console.log("Your transaction signature", tx);
let t = await provider.connection.getConfirmedTransaction(tx, "confirmed");
console.log(t.logMessages)
console.log(t.meta.logMessages)
});
});
2 changes: 1 addition & 1 deletion contracts/programs/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum Scope {
// Owner
}

#[account]
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct Round {
pub round_id: u32,
pub timestamp: u64,
Expand Down

0 comments on commit eb19fad

Please sign in to comment.