mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-15 07:26:45 +00:00
remove staking
This commit is contained in:
12
README.md
12
README.md
@@ -10,26 +10,16 @@
|
||||
|
||||
## Instructions
|
||||
|
||||
#### Mine
|
||||
- [`Open`](program/src/open.rs) - Opens a new block for mining.
|
||||
- [`Close`](program/src/close.rs) - Closes a block and pays out rewards.
|
||||
- [`Mine`](program/src/mine.rs) - Mines the current block by computing hashes.
|
||||
|
||||
#### Stake
|
||||
- [`Deposit`](program/src/deposit.rs) - Deposits stake into a miner account.
|
||||
- [`Withdraw`](program/src/withdraw.rs) - Withdraws stake from a miner account.
|
||||
- [`Free`](program/src/free.rs) - Frees up miner capacity after block ends.
|
||||
|
||||
#### Trade
|
||||
- [`Buy`](program/src/buy.rs) - Buys hash tokens from the market.
|
||||
- [`Sell`](program/src/sell.rs) - Sells hash tokens to the market.
|
||||
- [`Swap`](program/src/swap.rs) - Trade in a hashpower market.
|
||||
|
||||
## State
|
||||
- [`Block`](api/src/state/block.rs) - A period of time for mining.
|
||||
- [`Config`](api/src/state/config.rs) - Global program configuration.
|
||||
- [`Market`](api/src/state/market.rs) - Hashpower market for a given block.
|
||||
- [`Miner`](api/src/state/miner.rs) - A user's mining and staking state.
|
||||
- [`Receipt`](api/src/state/receipt.rs) - Tracks a miner's deployed capital.
|
||||
- [`Treasury`](api/src/state/treasury.rs) - The mint authority on the ORE token.
|
||||
|
||||
|
||||
|
||||
@@ -3,18 +3,10 @@ use steel::*;
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
|
||||
pub enum OreInstruction {
|
||||
// Mine
|
||||
Open = 0,
|
||||
Close = 1,
|
||||
Mine = 2,
|
||||
|
||||
// Stake
|
||||
Deposit = 3,
|
||||
Withdraw = 4,
|
||||
|
||||
// Trade
|
||||
Free = 5,
|
||||
Swap = 6,
|
||||
Swap = 3,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
@@ -33,22 +25,6 @@ pub struct Mine {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Deposit {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Withdraw {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Free {}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Swap {
|
||||
@@ -60,7 +36,4 @@ pub struct Swap {
|
||||
instruction!(OreInstruction, Open);
|
||||
instruction!(OreInstruction, Close);
|
||||
instruction!(OreInstruction, Mine);
|
||||
instruction!(OreInstruction, Deposit);
|
||||
instruction!(OreInstruction, Withdraw);
|
||||
instruction!(OreInstruction, Free);
|
||||
instruction!(OreInstruction, Swap);
|
||||
|
||||
@@ -2,14 +2,12 @@ mod block;
|
||||
mod config;
|
||||
mod market;
|
||||
mod miner;
|
||||
mod receipt;
|
||||
mod treasury;
|
||||
|
||||
pub use block::*;
|
||||
pub use config::*;
|
||||
pub use market::*;
|
||||
pub use miner::*;
|
||||
pub use receipt::*;
|
||||
pub use treasury::*;
|
||||
|
||||
use crate::consts::*;
|
||||
@@ -23,8 +21,7 @@ pub enum OreAccount {
|
||||
Config = 101,
|
||||
Market = 102,
|
||||
Miner = 103,
|
||||
Receipt = 104,
|
||||
Treasury = 105,
|
||||
Treasury = 104,
|
||||
}
|
||||
|
||||
pub fn block_pda(id: u64) -> (Pubkey, u8) {
|
||||
@@ -43,13 +40,6 @@ pub fn miner_pda(authority: Pubkey) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[MINER, &authority.to_bytes()], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn receipt_pda(authority: Pubkey, id: u64) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(
|
||||
&[RECEIPT, &authority.to_bytes(), &id.to_le_bytes()],
|
||||
&crate::ID,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn treasury_pda() -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[TREASURY], &crate::ID)
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
use steel::*;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Receipt {
|
||||
/// The authority of this receipt account.
|
||||
pub authority: Pubkey,
|
||||
|
||||
/// The amount of ORE this miner has deployed in the hashpower market corresponding to the block id.
|
||||
pub amount: u64,
|
||||
|
||||
/// The id of the block this receipt is associated with.
|
||||
pub block_id: u64,
|
||||
}
|
||||
|
||||
account!(OreAccount, Receipt);
|
||||
@@ -21,8 +21,8 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
market_hash_info.as_associated_token_account(market_info.key, mint_hash_info.key)?;
|
||||
let market_ore =
|
||||
market_ore_info.as_associated_token_account(market_info.key, mint_ore_info.key)?;
|
||||
mint_hash_info.has_address(&market.base.mint)?.as_mint();
|
||||
mint_ore_info.has_address(&market.quote.mint)?.as_mint();
|
||||
mint_hash_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_ore_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Deposits stake.
|
||||
pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Deposit::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// Load accounts.
|
||||
let [signer_info, miner_info, sender_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
sender_info.as_associated_token_account(&signer_info.key, &MINT_ADDRESS)?;
|
||||
let treasury = treasury_info.as_account_mut::<Treasury>(&ore_api::ID)?;
|
||||
treasury_tokens_info.as_associated_token_account(&treasury_info.key, &MINT_ADDRESS)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Update account state.
|
||||
miner.stake += amount;
|
||||
treasury.total_stake += amount;
|
||||
|
||||
// Execute transfer.
|
||||
transfer(
|
||||
signer_info,
|
||||
sender_info,
|
||||
treasury_tokens_info,
|
||||
token_program,
|
||||
amount,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Free up capacity.
|
||||
pub fn process_free(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, miner_info, receipt_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
let receipt = receipt_info
|
||||
.as_account_mut::<Receipt>(&ore_api::ID)?
|
||||
.assert_mut(|r| r.authority == *signer_info.key)?;
|
||||
|
||||
// Asset that block has ended.
|
||||
let start_slot = 1500 * receipt.block_id;
|
||||
let end_slot = start_slot + 1500;
|
||||
assert!(clock.slot >= end_slot, "Block has not yet closed.");
|
||||
|
||||
// Free up miner capacity.
|
||||
miner.deployed -= receipt.amount;
|
||||
|
||||
// Close the receipt.
|
||||
receipt_info.close(signer_info)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,18 +1,12 @@
|
||||
mod close;
|
||||
mod deposit;
|
||||
mod free;
|
||||
mod mine;
|
||||
mod open;
|
||||
mod swap;
|
||||
mod withdraw;
|
||||
|
||||
use close::*;
|
||||
use deposit::*;
|
||||
use free::*;
|
||||
use mine::*;
|
||||
use open::*;
|
||||
use swap::*;
|
||||
use withdraw::*;
|
||||
|
||||
use ore_api::instruction::*;
|
||||
use steel::*;
|
||||
@@ -25,17 +19,9 @@ pub fn process_instruction(
|
||||
let (ix, data) = parse_instruction(&ore_api::ID, program_id, data)?;
|
||||
|
||||
match ix {
|
||||
// Mine
|
||||
OreInstruction::Open => process_open(accounts, data)?,
|
||||
OreInstruction::Close => process_close(accounts, data)?,
|
||||
OreInstruction::Mine => process_mine(accounts, data)?,
|
||||
|
||||
// Stake
|
||||
OreInstruction::Deposit => process_deposit(accounts, data)?,
|
||||
OreInstruction::Withdraw => process_withdraw(accounts, data)?,
|
||||
|
||||
// Trade
|
||||
OreInstruction::Free => process_free(accounts, data)?,
|
||||
OreInstruction::Swap => process_swap(accounts, data)?,
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_info, miner_info, mint_base_info, mint_quote_info, receipt_info, tokens_base_info, tokens_quote_info, vault_base_info, vault_quote_info, system_program, token_program] =
|
||||
let [signer_info, block_info, market_info, miner_info, mint_base_info, mint_quote_info, tokens_base_info, tokens_quote_info, vault_base_info, vault_quote_info, system_program, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -51,8 +51,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// TODO Buy hash tokens
|
||||
|
||||
// Update market state.
|
||||
let swap_result = market.swap(amount, direction, precision, clock)?;
|
||||
swap_result.log_return();
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Withdraws stake.
|
||||
pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Withdraw::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// Load accounts.
|
||||
let [signer_info, miner_info, recipient_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
recipient_info.as_associated_token_account(&signer_info.key, &MINT_ADDRESS)?;
|
||||
let treasury = treasury_info.as_account_mut::<Treasury>(&ore_api::ID)?;
|
||||
treasury_tokens_info.as_associated_token_account(&treasury_info.key, &MINT_ADDRESS)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Update account state.
|
||||
miner.stake -= amount;
|
||||
treasury.total_stake -= amount;
|
||||
|
||||
// Asset miner has enough stake to cover the withdrawal.
|
||||
assert!(
|
||||
miner.stake >= miner.deployed,
|
||||
"Withdrawal cannot reduce capacity below deployment levels."
|
||||
);
|
||||
|
||||
// Execute transfer.
|
||||
transfer_signed(
|
||||
signer_info,
|
||||
treasury_tokens_info,
|
||||
recipient_info,
|
||||
token_program,
|
||||
amount,
|
||||
&[TREASURY],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user