Skip to content

Commit

Permalink
add finished at to oracle item
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaviderb committed Aug 4, 2022
1 parent d61483f commit c1700fe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 53 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Devnet Program (Testing): CXCE5fYFEuGShPKXGTYafxr3iChkBzgxCcAx1TABXJ1D (we use this address to run the integration tests)
Devnet program: BFGzZeqA47Kgxv7x1D1bZ7RENbjgV1FjrDd3z7Pse9Q4
Devnet Program (Testing):
CXCE5fYFEuGShPKXGTYafxr3iChkBzgxCcAx1TABXJ1D (we use this address to run the integration tests)

Devnet program:
BFGzZeqA47Kgxv7x1D1bZ7RENbjgV1FjrDd3z7Pse9Q4
91 changes: 44 additions & 47 deletions programs/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ use chainlink_solana as chainlink;

declare_id!("YOUR_PROGRAM_ID");

#[account]
pub struct Decimal {
pub value: i128,
pub decimals: u32,
}

#[program]
pub mod oracle {
use super::*;
Expand All @@ -17,18 +11,21 @@ pub mod oracle {
let timestamp = Clock::get()?.unix_timestamp;
ctx.accounts.oracle_item.authority = ctx.accounts.oracle_authorizer.key();
ctx.accounts.oracle_item.started_at = timestamp;
ctx.accounts.oracle_item.closed_at = timestamp + ctx.accounts.oracle_authorizer.closed_seconds;
ctx.accounts.oracle_item.closed_at =
timestamp + ctx.accounts.oracle_authorizer.closed_seconds;
ctx.accounts.oracle_item.finished_at =
timestamp + ctx.accounts.oracle_authorizer.finished_seconds;

// get decimal value from chainlink program
let decimals = chainlink::decimals(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.feed_account.to_account_info()
ctx.accounts.feed_account.to_account_info(),
)?;

// get round value from chainlink program
// get round value from chainlink program
let round = chainlink::latest_round_data(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.feed_account.to_account_info()
ctx.accounts.feed_account.to_account_info(),
)?;

ctx.accounts.oracle_item.decimals = decimals;
Expand All @@ -37,11 +34,17 @@ pub mod oracle {
Ok(())
}

pub fn create_authorizer(ctx: Context<CreateAuthorizer>, _auth_id: i64, closed_seconds: i64) -> Result<()> {
pub fn create_authorizer(
ctx: Context<CreateAuthorizer>,
_auth_id: i64,
closed_seconds: i64,
finished_seconds: i64,
) -> Result<()> {
let timestamp = Clock::get()?.unix_timestamp;
ctx.accounts.oracle_authorizer.authority = *ctx.accounts.user.key;
ctx.accounts.oracle_authorizer.created_at = timestamp;
ctx.accounts.oracle_authorizer.closed_seconds = closed_seconds;
ctx.accounts.oracle_authorizer.finished_seconds = finished_seconds;

Ok(())
}
Expand All @@ -50,64 +53,61 @@ pub mod oracle {
let oracle_item = &ctx.accounts.oracle_item;
let timestamp = Clock::get()?.unix_timestamp;

if oracle_item.finished_at != None {
return err!(Errors::OracleExpired);
if oracle_item.is_finished {
return err!(Errors::OracleExpired);
}

if oracle_item.closed_at > timestamp {
return err!(Errors::TimeInvalid);
return err!(Errors::TimeInvalid);
}

// get decimal value from chainlink program
// get decimal value from chainlink program
let decimals = chainlink::decimals(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.feed_account.to_account_info()
ctx.accounts.feed_account.to_account_info(),
)?;
// get round value from chainlink program
// get round value from chainlink program
let round = chainlink::latest_round_data(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.feed_account.to_account_info()
ctx.accounts.feed_account.to_account_info(),
)?;

ctx.accounts.oracle_item.decimals = decimals;
ctx.accounts.oracle_item.round = round.answer;
ctx.accounts.oracle_item.finished_at = Some(timestamp);

ctx.accounts.oracle_item.finished_at = timestamp;
ctx.accounts.oracle_item.is_finished = true;

Ok(())
}
}


#[account]
pub struct FeedItem {
}

#[account]
pub struct OracleAuthorizer {
authority: Pubkey, // 32
created_at: i64, // 8
closed_seconds: i64 // 8
authority: Pubkey, // 32
created_at: i64, // 8
closed_seconds: i64, // 8
finished_seconds: i64, // 8
}

#[account]
pub struct OracleItem {
authority: Pubkey, // 32
authority: Pubkey, // 32
chainlink_feed: Pubkey, // 32
started_at: i64, // 8
closed_at: i64, // 8
finished_at: Option<i64>, // 9
decimals: u8, // 1
round: i128 // 16
started_at: i64, // 8
closed_at: i64, // 8
finished_at: i64, // 8
is_finished: bool, // 1
decimals: u8, // 1
round: i128, // 16
}

#[derive(Accounts)]
#[instruction(auth_id: i64)]
pub struct CreateAuthorizer<'info> {
#[account(
init,
payer = user,
space = 8 + 32 + 8 + 8,
init,
payer = user,
space = 8 + 32 + 8 + 8 + 8,
seeds = [user.key().as_ref(), format!("id-{}", auth_id).as_bytes().as_ref()],
bump
)]
Expand All @@ -117,14 +117,13 @@ pub struct CreateAuthorizer<'info> {
system_program: Program<'info, System>,
}


#[derive(Accounts)]
#[instruction(oracle_id: i64)]
pub struct CreateOracle<'info> {
#[account(
init,
payer = user,
space = 8 + 32 + 32 + 8 + 8 + 9 + 1 + 16,
init,
payer = user,
space = 8 + 32 + 32 + 8 + 8 + 8 + 1 + 16 + 1,
seeds = [user.key().as_ref(), format!("id-{}", oracle_id).as_bytes().as_ref()],
bump,
constraint = oracle_authorizer.authority == *user.key
Expand All @@ -135,12 +134,11 @@ pub struct CreateOracle<'info> {
system_program: Program<'info, System>,
oracle_authorizer: Account<'info, OracleAuthorizer>,
/// CHECK: Todo
feed_account: UncheckedAccount<'info>,
feed_account: AccountInfo<'info>,
/// CHECK: This is the Chainlink program library
pub chainlink_program: AccountInfo<'info>
pub chainlink_program: AccountInfo<'info>,
}


#[derive(Accounts)]
pub struct UpdateOracle<'info> {
#[account(
Expand All @@ -154,15 +152,14 @@ pub struct UpdateOracle<'info> {
/// CHECK: Todo
feed_account: UncheckedAccount<'info>,
/// CHECK: This is the Chainlink program library
pub chainlink_program: AccountInfo<'info>
pub chainlink_program: AccountInfo<'info>,
}

// errors

#[error_code]
pub enum Errors {
#[msg("the current timestamp should be greater than closed_at")]
TimeInvalid,
#[msg("Oracle expired")]
OracleExpired
OracleExpired,
}
11 changes: 7 additions & 4 deletions tests/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { assert } from "chai";
import { BN } from "bn.js";

const ORACLE_CLOSES_IN = 0; // seconds
const ORACLE_FINISHED_IN = 0; // seconds


describe("oracle", () => {
const provider = anchor.AnchorProvider.env()
Expand All @@ -27,7 +29,7 @@ describe("oracle", () => {

await program
.methods
.createAuthorizer(new BN(authId), new BN(ORACLE_CLOSES_IN))
.createAuthorizer(new BN(authId), new BN(ORACLE_CLOSES_IN), new BN(ORACLE_FINISHED_IN))
.accounts({
oracleAuthorizer: oracleAuthorizer,
user: provider.wallet.publicKey
Expand Down Expand Up @@ -101,9 +103,10 @@ describe("oracle", () => {

const oracleItemData = await program.account.oracleItem.fetch(oracleItemAccount);

// assert.ok(
// oracleItemData.authority.equals(oracleAuthorizer)
// );
assert.ok(
oracleItemData.isFinished,
"oracle should be finalized"
);
});
});
});

0 comments on commit c1700fe

Please sign in to comment.