Config program - useful for storing/updating simple config items on chain
This commit is contained in:
21
programs/config_api/Cargo.toml
Normal file
21
programs/config_api/Cargo.toml
Normal file
@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "solana-config-api"
|
||||
version = "0.13.0"
|
||||
description = "config program API"
|
||||
authors = ["Solana Maintainers <maintainers@solana.com>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bincode = "1.1.2"
|
||||
log = "0.4.2"
|
||||
serde = "1.0.89"
|
||||
serde_derive = "1.0.89"
|
||||
solana-sdk = { path = "../../sdk", version = "0.13.0" }
|
||||
|
||||
[lib]
|
||||
name = "solana_config_api"
|
||||
crate-type = ["lib"]
|
||||
|
29
programs/config_api/src/config_instruction.rs
Normal file
29
programs/config_api/src/config_instruction.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use crate::id;
|
||||
use crate::ConfigState;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
use solana_sdk::transaction::Instruction;
|
||||
|
||||
pub struct ConfigInstruction {}
|
||||
|
||||
impl ConfigInstruction {
|
||||
/// Create a new, empty configuration account
|
||||
pub fn new_account<T: ConfigState>(
|
||||
from_account_pubkey: &Pubkey,
|
||||
config_account_pubkey: &Pubkey,
|
||||
lamports: u64,
|
||||
) -> Instruction {
|
||||
SystemInstruction::new_program_account(
|
||||
from_account_pubkey,
|
||||
config_account_pubkey,
|
||||
lamports,
|
||||
T::max_space(),
|
||||
&id(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Store new data in a configuration account
|
||||
pub fn new_store<T: ConfigState>(config_account_pubkey: &Pubkey, data: &T) -> Instruction {
|
||||
Instruction::new(id(), data, vec![(*config_account_pubkey, true)])
|
||||
}
|
||||
}
|
45
programs/config_api/src/config_transaction.rs
Normal file
45
programs/config_api/src/config_transaction.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use crate::config_instruction::ConfigInstruction;
|
||||
use crate::ConfigState;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::transaction::Transaction;
|
||||
|
||||
pub struct ConfigTransaction {}
|
||||
|
||||
impl ConfigTransaction {
|
||||
/// Create a new, empty configuration account
|
||||
pub fn new_account<T: ConfigState>(
|
||||
from_keypair: &Keypair,
|
||||
config_account_pubkey: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
lamports: u64,
|
||||
fee: u64,
|
||||
) -> Transaction {
|
||||
let mut transaction = Transaction::new(vec![ConfigInstruction::new_account::<T>(
|
||||
&from_keypair.pubkey(),
|
||||
config_account_pubkey,
|
||||
lamports,
|
||||
)]);
|
||||
transaction.fee = fee;
|
||||
|
||||
transaction.sign(&[from_keypair], recent_blockhash);
|
||||
transaction
|
||||
}
|
||||
|
||||
/// Store new state in a configuration account
|
||||
pub fn new_store<T: ConfigState>(
|
||||
config_account_keypair: &Keypair,
|
||||
data: &T,
|
||||
recent_blockhash: Hash,
|
||||
fee: u64,
|
||||
) -> Transaction {
|
||||
let mut transaction = Transaction::new(vec![ConfigInstruction::new_store(
|
||||
&config_account_keypair.pubkey(),
|
||||
data,
|
||||
)]);
|
||||
transaction.fee = fee;
|
||||
transaction.sign(&[config_account_keypair], recent_blockhash);
|
||||
transaction
|
||||
}
|
||||
}
|
26
programs/config_api/src/lib.rs
Normal file
26
programs/config_api/src/lib.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use serde::Serialize;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
mod config_instruction;
|
||||
mod config_transaction;
|
||||
|
||||
pub use config_instruction::ConfigInstruction;
|
||||
pub use config_transaction::ConfigTransaction;
|
||||
|
||||
const CONFIG_PROGRAM_ID: [u8; 32] = [
|
||||
133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0,
|
||||
];
|
||||
|
||||
pub fn check_id(program_id: &Pubkey) -> bool {
|
||||
program_id.as_ref() == CONFIG_PROGRAM_ID
|
||||
}
|
||||
|
||||
pub fn id() -> Pubkey {
|
||||
Pubkey::new(&CONFIG_PROGRAM_ID)
|
||||
}
|
||||
|
||||
pub trait ConfigState: Serialize {
|
||||
/// Maximum space that the serialized representation will require
|
||||
fn max_space() -> u64;
|
||||
}
|
Reference in New Issue
Block a user