Skip to content

Commit

Permalink
Solana staking example
Browse files Browse the repository at this point in the history
  • Loading branch information
Trolyaka committed Apr 18, 2023
1 parent 2cf0e5f commit 7385341
Show file tree
Hide file tree
Showing 15,394 changed files with 2,578,925 additions and 1 deletion.
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/interface/node_modules/
/contracts/target
/contracts/Cargo.lock
/test-ledger
/interface/test-ledger
/admin-keypair.json
/payer-keypair.json
/program-keypair.json
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# solana-staking-example
# solana-staking-example

./test.sh - it will build, deploy, run tests
21 changes: 21 additions & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "solana-staking-example"
authors = ["<[email protected]>"]
version = "0.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
solana-program = "1.15.2"
serde_with = "2.3.2"
borsh = "0.9.1"
thiserror = "1.0.30"
spl-token = {version = "3.2.0", features = ["no-entrypoint"]}

[dev-dependencies]
solana-sdk = "1.15.2"
serde = { version = "1.0.159", features = ["derive"] }

[lib]
crate-type = ["cdylib", "lib"]
15 changes: 15 additions & 0 deletions contracts/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, pubkey::Pubkey,
};

use crate::processor::Processor;

// defining the entry point for a Solana program
entrypoint!(process_instruction);
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
Processor::process(program_id, accounts, instruction_data)
}
16 changes: 16 additions & 0 deletions contracts/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use solana_program::program_error::ProgramError;
use thiserror::Error;

#[derive(Error, Debug, Copy, Clone)]
pub enum CustomError {
/// Invalid instruction
#[error("Invalid Instruction")]
InvalidInstruction,
// more errors which can be in solana contract
}

impl From<CustomError> for ProgramError {
fn from(e: CustomError) -> Self {
ProgramError::Custom(e as u32)
}
}
53 changes: 53 additions & 0 deletions contracts/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::convert::TryInto;
use crate::error::CustomError::InvalidInstruction;
use solana_program::program_error::ProgramError;

/// Enum with which you contract
/// determine the desired instruction
/// one instruction = one method
pub enum Instruction {
InitializePool {
nonce: u8
},
CreateUser {
nonce: u8,
},
Stake {
amount: u64,
},
Unstake {
amount: u64,
},
ClaimRewards,
ClosePool,
CloseUser,
}

impl Instruction {
// method for getting instruction
pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
Ok(match input[0] {
0 => Self::InitializePool { nonce: input[1] },
1 => Self::CreateUser { nonce: input[1] },
2 => Self::Stake {
amount: Self::unpack_to_u64(&input[1..9])?,
},
3 => Self::Unstake {
amount: Self::unpack_to_u64(&input[1..9])?,
},
4 => Self::ClaimRewards,
5 => Self::ClosePool,
6 => Self::CloseUser,
_ => return Err(InvalidInstruction.into()),
})
}

fn unpack_to_u64(input: &[u8]) -> Result<u64, ProgramError> {
let out_value = input
.get(..8)
.and_then(|slice| slice.try_into().ok())
.map(u64::from_le_bytes)
.ok_or(InvalidInstruction)?;
Ok(out_value)
}
}
6 changes: 6 additions & 0 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod error;
pub mod instruction;
pub mod processor;
pub mod state;
pub mod entrypoint;
pub mod processor_methods;
52 changes: 52 additions & 0 deletions contracts/src/processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::instruction::Instruction;

use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, msg, pubkey::Pubkey};
use crate::processor_methods::{claim_rewards, close_pool, close_user, create_user, initialize_pool, stake, unstake};

pub struct Processor;
impl Processor {
pub fn process(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let instruction = Instruction::unpack(instruction_data)?;
match instruction {
Instruction::InitializePool {nonce}=> {
msg!("Instruction::InitializePool");
initialize_pool(accounts,
nonce,
program_id)
}
Instruction::CreateUser {nonce}=> {
msg!("Instruction::CreateUser");
create_user(accounts, nonce, program_id)
}

Instruction::Stake {amount} => {
msg!("Instruction::Stake");
stake(accounts, amount, program_id)
}

Instruction::Unstake {amount} => {
msg!("Instruction::Unstake");
unstake(accounts, amount, program_id)
}

Instruction::ClaimRewards => {
msg!("Instruction::ClaimRewards");
claim_rewards(accounts, program_id)
}

Instruction::ClosePool => {
msg!("Instruction::ClosePool");
close_pool(accounts, program_id)
}

Instruction::CloseUser => {
msg!("Instruction::CloseUser");
close_user(accounts, program_id)
}
}
}
}
Loading

0 comments on commit 7385341

Please sign in to comment.