-
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.
Add ability for user to place bids (#4)
- Loading branch information
Showing
12 changed files
with
229 additions
and
58 deletions.
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 |
---|---|---|
@@ -1,27 +1,85 @@ | ||
name: test | ||
|
||
name: Run Tests | ||
on: | ||
push: | ||
branches: [main] | ||
branches: | ||
- main | ||
pull_request: | ||
branches: [main] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
solana_version: v1.18.8 | ||
|
||
jobs: | ||
build: | ||
install: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/cache@v4 | ||
name: cache solana cli | ||
id: cache-solana | ||
with: | ||
path: | | ||
~/.cache/solana/ | ||
~/.local/share/solana/ | ||
key: solana-${{ runner.os }}-v0000-${{ env.solana_version }} | ||
|
||
- name: install essentials | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -y pkg-config build-essential libudev-dev | ||
npm install --global yarn | ||
- name: install rust | ||
uses: dtolnay/rust-toolchain@stable | ||
with: | ||
toolchain: stable | ||
|
||
- name: Cache rust | ||
uses: Swatinem/rust-cache@v2 | ||
|
||
- name: install solana | ||
if: steps.cache-solana.outputs.cache-hit != 'true' | ||
run: | | ||
sh -c "$(curl -sSfL https://release.solana.com/${{ env.solana_version }}/install)" | ||
export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH" | ||
solana --version | ||
lint: | ||
needs: install | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Install Solana CLI | ||
- name: Run fmt | ||
run: cargo fmt -- --check | ||
- name: Run clippy | ||
run: cargo clippy | ||
|
||
test: | ||
needs: [install, lint] | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/cache@v4 | ||
name: cache solana cli | ||
id: cache-solana | ||
with: | ||
path: | | ||
~/.cache/solana/ | ||
~/.local/share/solana/ | ||
key: solana-${{ runner.os }}-v0000-${{ env.solana_version }} | ||
|
||
- name: setup solana | ||
run: | | ||
sh -c "$(curl -sSfL https://release.solana.com/v1.18.4/install)" | ||
echo 'export PATH="/home/runner/.local/share/solana/install/active_release/bin:$PATH"' >> $HOME/.bashrc | ||
- name: Build | ||
export PATH="/home/runner/.local/share/solana/install/active_release/bin:$PATH" | ||
solana --version | ||
solana-keygen new --silent --no-bip39-passphrase | ||
- name: run build | ||
run: | | ||
cargo build | ||
- name: run tests | ||
run: | | ||
export PATH="/home/runner/.local/share/solana/install/active_release/bin:$PATH" | ||
cargo build-bpf | ||
- name: Run tests | ||
run: cargo test --verbose | ||
cargo test-sbf |
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 @@ | ||
# TinyBlob | ||
# Pith |
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,2 +1,4 @@ | ||
// The seed for market PDAs. | ||
// The seed for the market PDA. | ||
pub const MARKET: &[u8] = b"market"; | ||
// The seed for the bid PDA. | ||
pub const BID: &[u8] = b"bid"; |
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
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,73 @@ | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use solana_program::{ | ||
account_info::AccountInfo, borsh1::try_from_slice_unchecked, entrypoint::ProgramResult, msg, | ||
program_error::ProgramError, pubkey::Pubkey, system_program, | ||
}; | ||
|
||
use crate::{ | ||
instruction::BidArgs, | ||
loaders::{load_program, load_signer, load_uninitialized_pda}, | ||
state::{Bid, Market}, | ||
utils::create_pda, | ||
BID, | ||
}; | ||
|
||
/// process_bid handles the creation of a new bid on a market. | ||
pub fn process_bid<'a, 'info>( | ||
_program_id: &Pubkey, | ||
accounts: &'a [AccountInfo<'info>], | ||
data: &[u8], | ||
) -> ProgramResult { | ||
// Parse args | ||
let args = BidArgs::try_from_slice(data)?; | ||
|
||
// Load account data | ||
let [signer, market_info, bid_info, system_program] = accounts else { | ||
return Err(ProgramError::NotEnoughAccountKeys); | ||
}; | ||
|
||
let mut market_data = try_from_slice_unchecked::<Market>(&market_info.data.borrow())?; | ||
|
||
load_signer(signer)?; | ||
load_uninitialized_pda( | ||
bid_info, | ||
&[ | ||
BID, | ||
market_data.id.to_le_bytes().as_ref(), | ||
signer.key.as_ref(), | ||
args.id.to_le_bytes().as_ref(), | ||
], | ||
args.bump, | ||
&crate::id(), | ||
)?; | ||
load_program(system_program, system_program::id())?; | ||
|
||
// create bid Program Derived Address. | ||
create_pda( | ||
bid_info, | ||
&crate::id(), | ||
Bid::SIZE, | ||
&[ | ||
BID, | ||
market_data.id.to_le_bytes().as_ref(), | ||
signer.key.as_ref(), | ||
args.id.to_le_bytes().as_ref(), | ||
&[args.bump], | ||
], | ||
system_program, | ||
signer, | ||
)?; | ||
|
||
let mut bid_data = try_from_slice_unchecked::<Bid>(&bid_info.data.borrow()).unwrap(); | ||
|
||
bid_data.discriminator = Bid::DISCRIMINATOR.to_string(); | ||
bid_data.market = *market_info.key; | ||
bid_data.amount = args.amount; | ||
bid_data.authority = *signer.key; | ||
bid_data.serialize(&mut &mut bid_info.data.borrow_mut()[..])?; | ||
|
||
market_data.counter += 1; | ||
market_data.serialize(&mut &mut market_info.data.borrow_mut()[..])?; | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod bid; | ||
mod market; | ||
|
||
pub use bid::*; | ||
pub use market::*; |
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 @@ | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use shank::ShankAccount; | ||
use solana_program::pubkey::Pubkey; | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, ShankAccount)] | ||
pub struct Bid { | ||
/// Account discriminator. | ||
pub discriminator: String, | ||
/// The market key associated with this bid. | ||
pub market: Pubkey, | ||
/// The amount of the bid in lamports. | ||
pub amount: u64, | ||
/// The account that placed this bid. | ||
pub authority: Pubkey, | ||
} | ||
|
||
impl Bid { | ||
pub const DISCRIMINATOR: &'static str = "bid"; | ||
pub const SIZE: usize = (4 + Bid::DISCRIMINATOR.len()) + 32 + 8 + 32; | ||
} |
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,13 +1,33 @@ | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use shank::ShankAccount; | ||
use solana_program::pubkey::Pubkey; | ||
|
||
// Market is an account that tracks the current state of the market. | ||
/// Market is the parent account that stores a tradable asset and keeps track of | ||
/// the bids placed on the specific market via a counter. | ||
#[repr(C)] | ||
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, ShankAccount)] | ||
pub struct Market { | ||
// The proof PDAs bump. | ||
/// Account discriminator | ||
pub discriminator: String, | ||
/// The market account PDA. | ||
pub bump: u8, | ||
// Transaction ID used to keep track of client state. | ||
/// The accounts authority. | ||
pub authority: Pubkey, | ||
/// The unique market ID. | ||
pub id: u64, | ||
// A none-unique string used to identify a market. | ||
/// The title string for a specific market. | ||
pub title: String, | ||
/// Counter keeps track of the number of bids placed on this market. | ||
pub counter: u16, | ||
/// The market account key. Useful since `getMultipleAccountsInfo` does not | ||
/// return a `keyedAccountInfo`. | ||
pub key: Pubkey, | ||
} | ||
|
||
impl Market { | ||
pub const DISCRIMINATOR: &'static str = "market"; | ||
|
||
pub fn get_account_size(title: &String, discriminator: &String) -> usize { | ||
return (4 + discriminator.len()) + 1 + 32 + 8 + (4 + title.len()) + 2 + 32; | ||
} | ||
} |
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,3 +1,5 @@ | ||
mod bid; | ||
mod market; | ||
|
||
pub use bid::*; | ||
pub use market::*; |