break dependency of programs on solana core (#1371)

* break dependency of programs on Solana core
This commit is contained in:
jackcmay
2018-09-27 07:49:26 -07:00
committed by GitHub
parent 874addc51a
commit 9c47e022dc
44 changed files with 95 additions and 66 deletions

22
common/Cargo.toml Normal file
View File

@@ -0,0 +1,22 @@
[package]
name = "solana_program_interface"
version = "0.1.0"
authors = [
"Anatoly Yakovenko <anatoly@solana.com>",
"Greg Fitzgerald <greg@solana.com>",
"Stephen Akridge <stephen@solana.com>",
"Michael Vines <mvines@solana.com>",
"Rob Walker <rob@solana.com>",
"Pankaj Garg <pankaj@solana.com>",
"Tyera Eulberg <tyera@solana.com>",
"Jack May <jack@solana.com>",
]
[dependencies]
bincode = "1.0.0"
bs58 = "0.2.0"
generic-array = { version = "0.12.0", default-features = false, features = ["serde"] }
serde = "1.0.27"
serde_derive = "1.0.27"

29
common/src/account.rs Normal file
View File

@@ -0,0 +1,29 @@
use pubkey::Pubkey;
/// An Account with userdata that is stored on chain
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Account {
/// tokens in the account
pub tokens: i64,
/// user data
/// A transaction can write to its userdata
pub userdata: Vec<u8>,
/// contract id this contract belongs to
pub program_id: Pubkey,
}
impl Account {
pub fn new(tokens: i64, space: usize, program_id: Pubkey) -> Account {
Account {
tokens,
userdata: vec![0u8; space],
program_id,
}
}
}
#[derive(Debug)]
pub struct KeyedAccount<'a> {
pub key: &'a Pubkey,
pub account: &'a mut Account,
}

7
common/src/lib.rs Normal file
View File

@@ -0,0 +1,7 @@
pub mod account;
pub mod pubkey;
extern crate bincode;
extern crate bs58;
extern crate generic_array;
#[macro_use]
extern crate serde_derive;

31
common/src/pubkey.rs Normal file
View File

@@ -0,0 +1,31 @@
use bs58;
use generic_array::typenum::U32;
use generic_array::GenericArray;
use std::fmt;
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Pubkey(GenericArray<u8, U32>);
impl Pubkey {
pub fn new(pubkey_vec: &[u8]) -> Self {
Pubkey(GenericArray::clone_from_slice(&pubkey_vec))
}
}
impl AsRef<[u8]> for Pubkey {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl fmt::Debug for Pubkey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}
impl fmt::Display for Pubkey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}