solana-flux-aggregator/program/src/instruction.rs

283 lines
7.9 KiB
Rust
Raw Normal View History

2020-11-26 14:19:50 -08:00
//! Instruction types
// use crate::error::Error;
#![allow(dead_code)]
2020-11-26 14:19:50 -08:00
use solana_program::{
2021-01-21 15:00:16 -08:00
instruction::{AccountMeta, Instruction as SolInstruction},
program_error::ProgramError,
program_pack::{Pack, Sealed},
2021-01-21 15:00:16 -08:00
pubkey::Pubkey,
sysvar,
2020-11-26 14:19:50 -08:00
};
// use std::convert::TryInto;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
2020-11-26 14:19:50 -08:00
/// Maximum number of oracles
pub const MAX_ORACLES: usize = 12;
2020-11-30 09:41:09 -08:00
/// The amount paid of TOKEN paid to each oracle per submission, in lamports (10e-10 SOL)
pub const PAYMENT_AMOUNT: u64 = 10;
2020-11-26 14:19:50 -08:00
/// Instructions supported by the program
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, BorshSchema, PartialEq)]
2020-11-26 14:19:50 -08:00
pub enum Instruction {
/// Initializes a new Aggregator
2021-01-19 19:17:47 -08:00
///
/// Accounts expected by this instruction:
2021-01-19 19:17:47 -08:00
///
/// 0. `[]` Rent sysvar
/// 1. `[writable]` The aggregator.
/// 2. `[signer]` The aggregator's authority.
2020-11-26 14:19:50 -08:00
Initialize {
/// The interval(seconds) of an oracle's each submission
submit_interval: u32,
2020-11-26 14:19:50 -08:00
/// min submission value
min_submission_value: u64,
/// max submission value
max_submission_value: u64,
2021-01-12 02:26:59 -08:00
/// submission decimals
2021-01-14 04:17:25 -08:00
submission_decimals: u8,
2020-12-02 22:45:16 -08:00
/// A short description of what is being reported
description: [u8; 32],
2020-11-26 14:19:50 -08:00
},
/// Add an oracle
2021-01-19 19:17:47 -08:00
///
/// Accounts expected by this instruction:
/// 0. `[writable]` The oracle
/// 1. `[]` The oracle owner
/// 2. `[]` Clock sysvar
/// 3. `[writable]` The aggregator
/// 4. `[signer]` The aggregator owner
2020-11-26 14:19:50 -08:00
AddOracle {
/// Is usually the oracle name
2020-11-30 09:41:09 -08:00
description: [u8; 32],
2020-11-26 14:19:50 -08:00
},
/// Remove an oracle
2021-01-19 19:17:47 -08:00
///
/// Accounts expected by this instruction:
/// 0. `[writable]` The aggregator.
/// 1. `[signer]` The aggregator onwer.
2020-11-26 14:19:50 -08:00
RemoveOracle {
2021-01-10 18:02:48 -08:00
/// the oracle pubkey
pubkey: [u8; 32],
2020-11-26 14:19:50 -08:00
},
/// Called by oracles when they have witnessed a need to update
2021-01-19 19:17:47 -08:00
///
/// Accounts expected by this instruction:
/// 0. `[writable]` The aggregator(key).
/// 1. `[]` Clock sysvar
/// 2. `[writable]` The oracle key.
/// 3. `[signer]` The oracle owner.
2020-11-26 14:19:50 -08:00
Submit {
/// the updated data that the oracle is submitting
2020-11-26 14:19:50 -08:00
submission: u64,
},
2020-11-30 09:41:09 -08:00
/// Oracle withdraw token
2021-01-19 19:17:47 -08:00
///
/// Accounts expected by this instruction:
/// 0. `[writable]` The aggregator (key).
/// 1. `[writable]` The faucet (which token transfer from)
/// 2. `[writable]` The recevier (which token withdraw to)
/// 3. `[]` SPL Token program id
/// 4. `[]` The faucet owner
/// 5. `[signer, writable]` The oracle's authority.
2020-11-30 09:41:09 -08:00
Withdraw {
/// withdraw amount
amount: u64,
/// program account nonced seed
seed: [u8; 32],
2020-12-01 19:42:09 -08:00
},
2020-11-26 14:19:50 -08:00
}
impl Sealed for Instruction {}
impl Pack for Instruction {
2021-01-19 19:45:54 -08:00
const LEN: usize = 54;
fn pack_into_slice(&self, dst: &mut [u8]) {
let data = self.pack_into_vec();
dst[..data.len()].copy_from_slice(&data);
}
fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError> {
let mut mut_src: &[u8] = src;
Self::deserialize(&mut mut_src).map_err(|_| ProgramError::InvalidInstructionData)
}
}
2020-11-26 14:19:50 -08:00
impl Instruction {
fn pack_into_vec(&self) -> Vec<u8> {
self.try_to_vec().expect("try_to_vec")
}
}
/// Below is for test
/// Creates a `intialize` instruction.
pub fn initialize(
program_id: &Pubkey,
aggregator_pubkey: &Pubkey,
aggregator_owner_pubkey: &Pubkey,
submit_interval: u32,
min_submission_value: u64,
max_submission_value: u64,
2021-01-14 04:17:25 -08:00
submission_decimals: u8,
description: [u8; 32],
2021-01-08 09:39:16 -08:00
) -> SolInstruction {
let accounts = vec![
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new(*aggregator_pubkey, false),
AccountMeta::new_readonly(*aggregator_owner_pubkey, true),
];
2021-01-08 09:39:16 -08:00
SolInstruction {
program_id: *program_id,
accounts,
2021-01-08 09:39:16 -08:00
data: Instruction::Initialize {
submit_interval,
min_submission_value,
max_submission_value,
2021-01-12 02:26:59 -08:00
submission_decimals,
2021-01-08 09:39:16 -08:00
description,
}
.pack_into_vec(),
}
}
2021-01-08 09:39:16 -08:00
/// Creates a `add_oracle` instruction
pub fn add_oracle(
program_id: &Pubkey,
oracle_pubkey: &Pubkey,
oracle_owner_pubkey: &Pubkey,
aggregator_pubkey: &Pubkey,
aggregator_owner_pubkey: &Pubkey,
2021-01-21 15:00:16 -08:00
description: [u8; 32],
2021-01-08 09:39:16 -08:00
) -> SolInstruction {
let accounts = vec![
AccountMeta::new(*oracle_pubkey, false),
AccountMeta::new(*oracle_owner_pubkey, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new(*aggregator_pubkey, false),
AccountMeta::new_readonly(*aggregator_owner_pubkey, true),
];
2021-01-08 09:39:16 -08:00
SolInstruction {
program_id: *program_id,
accounts,
2021-01-21 15:00:16 -08:00
data: Instruction::AddOracle { description }.pack_into_vec(),
2021-01-08 09:39:16 -08:00
}
}
2021-01-08 09:39:16 -08:00
/// Creates a `remove_oracle` instruction
pub fn remove_oracle(
program_id: &Pubkey,
aggregator_pubkey: &Pubkey,
aggregator_owner_pubkey: &Pubkey,
2021-01-10 18:02:48 -08:00
pubkey: &Pubkey,
2021-01-08 09:39:16 -08:00
) -> SolInstruction {
let accounts = vec![
AccountMeta::new(*aggregator_pubkey, false),
AccountMeta::new_readonly(*aggregator_owner_pubkey, true),
];
2021-01-08 09:39:16 -08:00
SolInstruction {
program_id: *program_id,
accounts,
2021-01-08 09:39:16 -08:00
data: Instruction::RemoveOracle {
2021-01-10 18:02:48 -08:00
pubkey: pubkey.to_bytes(),
2021-01-08 09:39:16 -08:00
}
.pack_into_vec(),
}
}
/// Creates a `submit` instruction
pub fn submit(
program_id: &Pubkey,
aggregator_pubkey: &Pubkey,
oracle_pubkey: &Pubkey,
oracle_owner_pubkey: &Pubkey,
submission: u64,
2021-01-08 09:39:16 -08:00
) -> SolInstruction {
let accounts = vec![
AccountMeta::new(*aggregator_pubkey, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new(*oracle_pubkey, false),
AccountMeta::new_readonly(*oracle_owner_pubkey, true),
];
2021-01-08 09:39:16 -08:00
SolInstruction {
program_id: *program_id,
accounts,
2021-01-21 15:00:16 -08:00
data: Instruction::Submit { submission }.pack_into_vec(),
2021-01-08 09:39:16 -08:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::borsh_utils;
2021-01-19 19:45:54 -08:00
use anyhow::Result;
2021-01-21 15:00:16 -08:00
use hex;
#[test]
fn test_get_packed_len() {
assert_eq!(
Instruction::get_packed_len(),
borsh_utils::get_packed_len::<Instruction>()
)
}
#[test]
2021-01-19 19:45:54 -08:00
fn test_serialize_bytes() -> Result<()> {
let test_instruction = Instruction::Initialize {
2021-01-19 19:45:54 -08:00
submit_interval: 0x11221122,
min_submission_value: 0xaabbaabbaabbaabb,
max_submission_value: 0xccddccddccddccdd,
2021-01-19 19:17:47 -08:00
submission_decimals: 6,
2021-01-19 19:45:54 -08:00
description: [0xff; 32],
};
2021-01-19 19:45:54 -08:00
let bytes = test_instruction.try_to_vec()?;
assert_eq!(
2021-01-19 19:45:54 -08:00
"0022112211bbaabbaabbaabbaaddccddccddccddcc06ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
hex::encode(bytes),
);
2021-01-19 19:45:54 -08:00
Ok(())
}
#[test]
2021-01-19 19:45:54 -08:00
fn state_deserialize_invalid() -> Result<()> {
assert_eq!(
2021-01-19 19:45:54 -08:00
Instruction::unpack_from_slice(&hex::decode("0022112211bbaabbaabbaabbaaddccddccddccddcc06ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")?),
Ok(Instruction::Initialize {
2021-01-19 19:45:54 -08:00
submit_interval: 0x11221122,
min_submission_value: 0xaabbaabbaabbaabb,
max_submission_value: 0xccddccddccddccdd,
2021-01-19 19:17:47 -08:00
submission_decimals: 6,
2021-01-19 19:45:54 -08:00
description: [0xff; 32],
}),
);
2021-01-19 19:45:54 -08:00
// assert_eq!(
// Instruction::unpack_from_slice(&[
// 4,
// 15, 39, 0, 0, 0, 0, 0, 0,
// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
// ]),
// Ok(Instruction::Withdraw {
// amount: 9999u64,
// seed: [1u8; 32],
// }),
// );
Ok(())
}
}