forked from solana-labs/dapp-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Program structure, initial operation #1
Draft
y2kappa
wants to merge
2
commits into
PureMoney:main
Choose a base branch
from
y2kappa:feature/addBasicProgramStructureAndFirstInstruction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,20 @@ | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey, | ||
}; | ||
|
||
use crate::processor::Processor; | ||
|
||
entrypoint!(process_instruction); | ||
fn process_instruction( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction_data: &[u8], | ||
) -> ProgramResult { | ||
msg!( | ||
"process_instruction: {}: {:?} accounts, data={:?}", | ||
program_id, | ||
accounts, | ||
instruction_data | ||
); | ||
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,44 @@ | ||
use thiserror::Error; | ||
|
||
use solana_program::program_error::ProgramError; | ||
|
||
#[derive(Error, Debug, Copy, Clone)] | ||
pub enum TroveError { | ||
/// Invalid instruction | ||
#[error("Invalid Instruction")] | ||
InvalidInstruction, | ||
/// Not Rent Exempt | ||
#[error("Not Rent Exempt")] | ||
NotRentExempt, | ||
/// ExpectedAmountMismatch | ||
#[error("ExpectedAmountMismatch")] | ||
ExpectedAmountMismatch, | ||
/// AmountOverflow | ||
#[error("AmountOverflow")] | ||
AmountOverflow, | ||
/// AmountOverflow | ||
#[error("AccountParsingError")] | ||
AccountParsingError, | ||
|
||
/// The owner of the input isn't set to the program address generated by the program. | ||
#[error("Input account owner is not the program address")] | ||
InvalidAccountOwner, | ||
|
||
/// Account is not a signer | ||
#[error("Input account must be a signer")] | ||
InvalidSigner, | ||
|
||
/// The account cannot be initialized because it is already in use. | ||
#[error("Account is already initialized")] | ||
AlreadyInitialized, | ||
|
||
/// Math operation overflow | ||
#[error("Math operation overflow")] | ||
MathOverflow, | ||
} | ||
|
||
impl From<TroveError> for ProgramError { | ||
fn from(e: TroveError) -> 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,70 @@ | ||
use solana_program::program_error::ProgramError; | ||
use std::convert::TryInto; | ||
|
||
use crate::error::TroveError::InvalidInstruction; | ||
|
||
pub enum TroveInstruction { | ||
/// 0 | ||
/// Starts the trade by creating and populating a trove account and transferring ownership of the | ||
/// given SOL token account to the PDA | ||
/// | ||
/// | ||
/// Accounts expected: | ||
/// | ||
/// 0. `[signer]` (Borrower) The account of the person initializing the borrowing | ||
/// 1. `[writable]` (Trove SOL Account) Trove SOL token account that should be created prior to this instruction and owned by the initializer | ||
/// 2. `[writable]` (Trove USD Account) The initializer's USD token account for the USD token they will receive should the trade go through | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use |
||
/// 3. `[writable]` (Trove Data Account) The associated trove account, it will hold all necessary info about the trade. | ||
/// 4. `[writable]` (Temp Data Account) The associated trove account, it will hold all necessary info about the trade. | ||
/// 5. `[]` The rent sysvar | ||
/// 6. `[]` The token program | ||
ForgetInitTrove { | ||
/// The amount of SOL deposited | ||
amountSol: u64, | ||
/// The amount of USD borrowed | ||
amountUsd: u64, | ||
}, | ||
|
||
/// 1 | ||
/// Initializes a new trove (lending market obligation). | ||
/// | ||
/// Accounts expected by this instruction: | ||
/// | ||
/// 0. `[writable]` Trove account - uninitialized. | ||
/// 1. `[signer]` Trove owner. | ||
/// 2. `[]` Rent sysvar. | ||
InitTrove, | ||
} | ||
|
||
impl TroveInstruction { | ||
/// Unpacks a byte buffer into a [TroveInstruction](enum.TroveInstruction.html). | ||
pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> { | ||
let (tag, rest) = input.split_first().ok_or(InvalidInstruction)?; | ||
|
||
Ok(match tag { | ||
0 => Self::ForgetInitTrove { | ||
amountSol: Self::unpack_amount_sol(rest)?, | ||
amountUsd: Self::unpack_amount_usd(rest)?, | ||
}, | ||
1 => Self::InitTrove, | ||
_ => return Err(InvalidInstruction.into()), | ||
}) | ||
} | ||
|
||
fn unpack_amount_sol(input: &[u8]) -> Result<u64, ProgramError> { | ||
let amount = input | ||
.get(0..8) | ||
.and_then(|slice| slice.try_into().ok()) | ||
.map(u64::from_le_bytes) | ||
.ok_or(InvalidInstruction)?; | ||
Ok(amount) | ||
} | ||
fn unpack_amount_usd(input: &[u8]) -> Result<u64, ProgramError> { | ||
let amount = input | ||
.get(8..16) | ||
.and_then(|slice| slice.try_into().ok()) | ||
.map(u64::from_le_bytes) | ||
.ok_or(InvalidInstruction)?; | ||
Ok(amount) | ||
} | ||
} |
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,56 +1,7 @@ | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey, | ||
}; | ||
|
||
solana_program::declare_id!("BpfProgram1111111111111111111111111111111111"); | ||
|
||
entrypoint!(process_instruction); | ||
fn process_instruction( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction_data: &[u8], | ||
) -> ProgramResult { | ||
msg!( | ||
"process_instruction: {}: {} accounts, data={:?}", | ||
program_id, | ||
accounts.len(), | ||
instruction_data | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use { | ||
super::*, | ||
assert_matches::*, | ||
solana_program::instruction::{AccountMeta, Instruction}, | ||
solana_program_test::*, | ||
solana_sdk::{signature::Signer, transaction::Transaction}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn test_transaction() { | ||
let program_id = Pubkey::new_unique(); | ||
|
||
let (mut banks_client, payer, recent_blockhash) = ProgramTest::new( | ||
"bpf_program_template", | ||
program_id, | ||
processor!(process_instruction), | ||
) | ||
.start() | ||
.await; | ||
|
||
let mut transaction = Transaction::new_with_payer( | ||
&[Instruction { | ||
program_id, | ||
accounts: vec![AccountMeta::new(payer.pubkey(), false)], | ||
data: vec![1, 2, 3], | ||
}], | ||
Some(&payer.pubkey()), | ||
); | ||
transaction.sign(&[&payer], recent_blockhash); | ||
|
||
assert_matches!(banks_client.process_transaction(transaction).await, Ok(())); | ||
} | ||
} | ||
pub mod entrypoint; | ||
pub mod error; | ||
pub mod instruction; | ||
pub mod math; | ||
pub mod processor; | ||
pub mod state; | ||
pub mod state_utils; |
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,36 @@ | ||
//! Common module for Decimal and Rate | ||
|
||
use solana_program::program_error::ProgramError; | ||
|
||
/// Scale of precision | ||
pub const SCALE: usize = 18; | ||
/// Identity | ||
pub const WAD: u64 = 1_000_000_000_000_000_000; | ||
/// Half of identity | ||
pub const HALF_WAD: u64 = 500_000_000_000_000_000; | ||
/// Scale for percentages | ||
pub const PERCENT_SCALER: u64 = 10_000_000_000_000_000; | ||
|
||
/// Try to subtract, return an error on underflow | ||
pub trait TrySub: Sized { | ||
/// Subtract | ||
fn try_sub(self, rhs: Self) -> Result<Self, ProgramError>; | ||
} | ||
|
||
/// Try to subtract, return an error on overflow | ||
pub trait TryAdd: Sized { | ||
/// Add | ||
fn try_add(self, rhs: Self) -> Result<Self, ProgramError>; | ||
} | ||
|
||
/// Try to divide, return an error on overflow or divide by zero | ||
pub trait TryDiv<RHS>: Sized { | ||
/// Divide | ||
fn try_div(self, rhs: RHS) -> Result<Self, ProgramError>; | ||
} | ||
|
||
/// Try to multiply, return an error on overflow | ||
pub trait TryMul<RHS>: Sized { | ||
/// Multiply | ||
fn try_mul(self, rhs: RHS) -> Result<Self, ProgramError>; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PDA?