-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.