2020-11-26 14:19:50 -08:00
|
|
|
//! Instruction types
|
|
|
|
|
2020-12-17 22:38:28 -08:00
|
|
|
// use crate::error::Error;
|
|
|
|
#![allow(dead_code)]
|
2020-11-26 14:19:50 -08:00
|
|
|
|
|
|
|
use solana_program::{
|
2020-12-17 22:38:28 -08:00
|
|
|
sysvar,
|
2020-11-26 14:19:50 -08:00
|
|
|
pubkey::Pubkey,
|
2020-12-17 22:38:28 -08:00
|
|
|
program_error::ProgramError,
|
|
|
|
program_pack::{Pack, Sealed},
|
|
|
|
instruction::{AccountMeta, Instruction as SolInstruction},
|
2020-11-26 14:19:50 -08:00
|
|
|
};
|
|
|
|
|
2020-12-17 22:38:28 -08:00
|
|
|
// use std::convert::TryInto;
|
|
|
|
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
|
2020-11-26 14:19:50 -08:00
|
|
|
|
|
|
|
/// Maximum number of oracles
|
2020-12-17 22:38:28 -08:00
|
|
|
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
|
|
|
|
2020-12-17 22:38:28 -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
|
2020-12-17 22:38:28 -08:00
|
|
|
///
|
|
|
|
/// Accounts expected by this instruction:
|
|
|
|
///
|
|
|
|
/// 0. `[]` Rent sysvar
|
|
|
|
/// 1. `[writable]` The aggregator.
|
|
|
|
/// 2. `[signer]` The aggregator's authority.
|
2020-11-26 14:19:50 -08:00
|
|
|
Initialize {
|
2020-12-07 19:57:03 -08:00
|
|
|
/// 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
|
2020-11-27 22:52:54 -08:00
|
|
|
///
|
2020-12-17 22:38:28 -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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
/// 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
|
2020-12-17 22:38:28 -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
|
2020-12-17 22:38:28 -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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
/// 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
|
2020-12-17 22:38:28 -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,
|
2020-12-17 22:38:28 -08:00
|
|
|
/// program account nonced seed
|
|
|
|
seed: [u8; 32],
|
2020-12-01 19:42:09 -08:00
|
|
|
},
|
2020-11-26 14:19:50 -08:00
|
|
|
}
|
|
|
|
|
2020-12-17 22:38:28 -08:00
|
|
|
impl Sealed for Instruction {}
|
|
|
|
impl Pack for Instruction {
|
|
|
|
const LEN: usize = 53;
|
|
|
|
|
|
|
|
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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
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,
|
2020-12-17 22:38:28 -08:00
|
|
|
description: [u8; 32],
|
2021-01-08 09:39:16 -08:00
|
|
|
) -> SolInstruction {
|
|
|
|
|
2020-12-17 22:38:28 -08:00
|
|
|
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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
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(),
|
|
|
|
}
|
2020-12-17 22:38:28 -08:00
|
|
|
}
|
|
|
|
|
2021-01-08 09:39:16 -08:00
|
|
|
/// Creates a `add_oracle` instruction
|
2020-12-17 22:38:28 -08:00
|
|
|
pub fn add_oracle(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
oracle_pubkey: &Pubkey,
|
|
|
|
oracle_owner_pubkey: &Pubkey,
|
|
|
|
aggregator_pubkey: &Pubkey,
|
|
|
|
aggregator_owner_pubkey: &Pubkey,
|
|
|
|
description: [u8; 32]
|
2021-01-08 09:39:16 -08:00
|
|
|
) -> SolInstruction {
|
2020-12-17 22:38:28 -08:00
|
|
|
|
|
|
|
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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
program_id: *program_id,
|
|
|
|
accounts,
|
2021-01-08 09:39:16 -08:00
|
|
|
data: Instruction::AddOracle {
|
|
|
|
description,
|
|
|
|
}
|
|
|
|
.pack_into_vec(),
|
|
|
|
}
|
2020-12-17 22:38:28 -08:00
|
|
|
}
|
|
|
|
|
2021-01-08 09:39:16 -08:00
|
|
|
/// Creates a `remove_oracle` instruction
|
2020-12-17 22:38:28 -08:00
|
|
|
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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
|
|
|
|
let accounts = vec![
|
|
|
|
AccountMeta::new(*aggregator_pubkey, false),
|
|
|
|
AccountMeta::new_readonly(*aggregator_owner_pubkey, true),
|
|
|
|
];
|
|
|
|
|
2021-01-08 09:39:16 -08:00
|
|
|
SolInstruction {
|
2020-12-17 22:38:28 -08:00
|
|
|
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(),
|
|
|
|
}
|
2020-12-17 22:38:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
|
2020-12-17 22:38:28 -08:00
|
|
|
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 {
|
2020-12-17 22:38:28 -08:00
|
|
|
program_id: *program_id,
|
|
|
|
accounts,
|
2021-01-08 09:39:16 -08:00
|
|
|
data: Instruction::Submit {
|
|
|
|
submission,
|
|
|
|
}
|
|
|
|
.pack_into_vec(),
|
|
|
|
}
|
2020-12-17 22:38:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::borsh_utils;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_packed_len() {
|
|
|
|
assert_eq!(
|
|
|
|
Instruction::get_packed_len(),
|
|
|
|
borsh_utils::get_packed_len::<Instruction>()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize_bytes() {
|
|
|
|
let test_instruction = Instruction::Initialize {
|
|
|
|
submit_interval: 6u32,
|
|
|
|
min_submission_value: 0u64,
|
|
|
|
max_submission_value: 9999u64,
|
|
|
|
description: [1u8; 32],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_instruction.try_to_vec().unwrap(),
|
|
|
|
vec![
|
|
|
|
0,
|
|
|
|
6, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
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
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn state_deserialize_invalid() {
|
|
|
|
assert_eq!(
|
|
|
|
Instruction::unpack_from_slice(&[
|
|
|
|
0,
|
|
|
|
6, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
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::Initialize {
|
|
|
|
submit_interval: 6u32,
|
|
|
|
min_submission_value: 0u64,
|
|
|
|
max_submission_value: 9999u64,
|
|
|
|
description: [1u8; 32],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
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],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|