-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add chainlink-solana, a client for consumers
- Loading branch information
Showing
10 changed files
with
183 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# chainlink-solana | ||
|
||
Chainlink client for Solana. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters