Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example v2 #125

Merged
merged 5 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 1 addition & 1 deletion contracts/crates/arrayvec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This is a tiny arrayvec implementation (https://docs.rs/arrayvec/) that efficiently implements a few common operations
//! This is a tiny arrayvec implementation <https://docs.rs/arrayvec/> that efficiently implements a few common operations
//! We're able to simplify the code significantly due to the elements being Pod/Zeroable.

// use anchor_lang::prelude::*;
Expand Down
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)
}
6 changes: 6 additions & 0 deletions contracts/examples/hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

.anchor
.DS_Store
target
**/*.rs.bk
node_modules
21 changes: 21 additions & 0 deletions contracts/examples/hello-world/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[programs.localnet]
hello_world = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[registry]
url = "https://anchor.projectserum.com"

[provider]
cluster = "localnet"
# wallet = "~/.config/solana/id.json"
wallet = "../../id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

[[test.genesis]]
address = "A7Jh2nb1hZHwqEofm4N8SXbKTj82rx7KUfjParQXUyMQ"
program = "../../target/deploy/store.so"

[[test.genesis]]
address = "2F5NEkMnCRkmahEAcQfTQcZv1xtGgrWFfjENtTwHLuKg"
program = "../../target/deploy/access_controller.so"
Loading