mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-13 23:16:52 +00:00
delegate
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
[package]
|
||||
name = "ore-program"
|
||||
description.workspace = true
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
homepage.workspace = true
|
||||
documentation.workspace = true
|
||||
repository.workspace = true
|
||||
readme.workspace = true
|
||||
keywords.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "lib"]
|
||||
name = "ore"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
[dependencies]
|
||||
bincode.workspace = true
|
||||
mpl-token-metadata.workspace = true
|
||||
ore-api.workspace = true
|
||||
ore-boost-api.workspace = true
|
||||
solana-program.workspace = true
|
||||
spl-token.workspace = true
|
||||
spl-token-2022.workspace = true
|
||||
spl-associated-token-account.workspace = true
|
||||
steel.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "0.8.5"
|
||||
@@ -1,69 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Closes a block.
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, recipient_info, treasury_info, vault_base_info, vault_quote_info, system_program, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| clock.slot >= b.start_slot + 1500)?;
|
||||
let market = market_info
|
||||
.as_account_mut::<Market>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.id == block.id)?;
|
||||
mint_base_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
let vault_base =
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
let vault_quote =
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Payout block reward.
|
||||
if block.best_miner != Pubkey::default() {
|
||||
recipient_info.as_associated_token_account(&block.best_miner, &MINT_ADDRESS)?;
|
||||
mint_to_signed(
|
||||
mint_quote_info,
|
||||
recipient_info,
|
||||
treasury_info,
|
||||
token_program,
|
||||
block.reward,
|
||||
&[TREASURY],
|
||||
)?;
|
||||
}
|
||||
|
||||
// Burn base liquidity.
|
||||
burn_signed(
|
||||
vault_base_info,
|
||||
mint_base_info,
|
||||
market_info,
|
||||
token_program,
|
||||
vault_base.amount(),
|
||||
&[MARKET, &market.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Burn quote liquidity.
|
||||
burn_signed(
|
||||
vault_quote_info,
|
||||
mint_quote_info,
|
||||
market_info,
|
||||
token_program,
|
||||
vault_quote.amount(),
|
||||
&[MARKET, &market.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Close block.
|
||||
block_info.close(signer_info)?;
|
||||
|
||||
// Close market.
|
||||
market_info.close(signer_info)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
mod close;
|
||||
mod mine;
|
||||
mod open;
|
||||
mod swap;
|
||||
|
||||
use close::*;
|
||||
use mine::*;
|
||||
use open::*;
|
||||
use swap::*;
|
||||
|
||||
use ore_api::instruction::*;
|
||||
use steel::*;
|
||||
|
||||
pub fn process_instruction(
|
||||
program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
data: &[u8],
|
||||
) -> ProgramResult {
|
||||
let (ix, data) = parse_instruction(&ore_api::ID, program_id, data)?;
|
||||
|
||||
match ix {
|
||||
OreInstruction::Open => process_open(accounts, data)?,
|
||||
OreInstruction::Close => process_close(accounts, data)?,
|
||||
OreInstruction::Mine => process_mine(accounts, data)?,
|
||||
OreInstruction::Swap => process_swap(accounts, data)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
@@ -1,79 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use solana_program::keccak;
|
||||
use steel::*;
|
||||
|
||||
/// Mine a block.
|
||||
pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Mine::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_info, miner_info, mint_info, sender_info, system_program, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| clock.slot >= b.start_slot)?
|
||||
.assert_mut(|b| clock.slot < b.start_slot + 1500)?;
|
||||
let market = market_info
|
||||
.as_account::<Market>(&ore_api::ID)?
|
||||
.assert(|m| m.id == block.id)?;
|
||||
mint_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
sender_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, &mint_info.key)?
|
||||
.assert(|t| t.amount() >= amount)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Load miner account.
|
||||
let miner = if miner_info.data_is_empty() {
|
||||
create_program_account::<Miner>(
|
||||
miner_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[MINER, &signer_info.key.to_bytes()],
|
||||
)?;
|
||||
let miner = miner_info.as_account_mut::<Miner>(&ore_api::ID)?;
|
||||
miner.authority = *signer_info.key;
|
||||
miner.block_id = 0;
|
||||
miner.hash = [0; 32];
|
||||
miner.total_hashes = 0;
|
||||
miner.total_rewards = 0;
|
||||
miner
|
||||
} else {
|
||||
miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?
|
||||
};
|
||||
|
||||
// Reset miner hash if mining new block.
|
||||
if miner.block_id != block.id {
|
||||
miner.block_id = block.id;
|
||||
miner.hash =
|
||||
keccak::hashv(&[block.slot_hash.as_ref(), miner.authority.as_ref()]).to_bytes();
|
||||
}
|
||||
|
||||
// Mine.
|
||||
for _ in 0..amount {
|
||||
miner.hash = keccak::hashv(&[miner.hash.as_ref()]).to_bytes();
|
||||
if miner.hash < block.best_hash {
|
||||
block.best_hash = miner.hash;
|
||||
block.best_miner = miner.authority;
|
||||
}
|
||||
}
|
||||
|
||||
// Update miner stats.
|
||||
miner.total_hashes += amount;
|
||||
|
||||
// Burn hash tokens.
|
||||
burn(sender_info, mint_info, signer_info, token_program, amount)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use solana_program::program_pack::Pack;
|
||||
use spl_token_2022::instruction::AuthorityType;
|
||||
use steel::*;
|
||||
|
||||
/// Opens a new block.
|
||||
pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Open::try_from_bytes(data)?;
|
||||
let id = u64::from_le_bytes(args.id);
|
||||
|
||||
// Load accounts.
|
||||
let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, system_program, vault_base_info, vault_quote_info, token_program, associated_token_program, rent_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
block_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BLOCK, &id.to_le_bytes()], &ore_api::ID)?;
|
||||
market_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[MARKET, &id.to_le_bytes()], &ore_api::ID)?;
|
||||
mint_base_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[MINT, &id.to_le_bytes()], &ore_api::ID)?;
|
||||
mint_quote_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
associated_token_program.is_program(&spl_associated_token_account::ID)?;
|
||||
rent_sysvar.is_sysvar(&sysvar::rent::ID)?;
|
||||
|
||||
// Initialize config.
|
||||
create_program_account::<Block>(
|
||||
block_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
block.best_hash = [0; 32];
|
||||
block.best_miner = Pubkey::default();
|
||||
block.id = id;
|
||||
block.reward = ONE_ORE * 10;
|
||||
block.slot_hash = [0; 32];
|
||||
block.start_slot = 1500 * id;
|
||||
|
||||
// Initialize market.
|
||||
create_program_account::<Market>(
|
||||
market_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[MARKET, &id.to_le_bytes()],
|
||||
)?;
|
||||
let market = market_info.as_account_mut::<Market>(&ore_api::ID)?;
|
||||
market.base = TokenParams {
|
||||
mint: *mint_base_info.key,
|
||||
balance: HASH_TOKEN_SUPPLY,
|
||||
balance_virtual: 0,
|
||||
};
|
||||
market.quote = TokenParams {
|
||||
mint: *mint_quote_info.key,
|
||||
balance: 0,
|
||||
balance_virtual: ONE_ORE,
|
||||
};
|
||||
market.fee = FeeParams {
|
||||
rate: FEE_RATE_BPS,
|
||||
uncollected: 0,
|
||||
cumulative: 0,
|
||||
};
|
||||
market.snapshot = Snapshot {
|
||||
enabled: 1,
|
||||
base_balance: 0,
|
||||
quote_balance: 0,
|
||||
slot: 0,
|
||||
};
|
||||
market.id = id;
|
||||
|
||||
// Initialize hash token mint.
|
||||
allocate_account(
|
||||
mint_base_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
spl_token::state::Mint::LEN,
|
||||
&spl_token::ID,
|
||||
&[MINT, &id.to_le_bytes()],
|
||||
)?;
|
||||
initialize_mint_signed(
|
||||
mint_base_info,
|
||||
block_info,
|
||||
None,
|
||||
token_program,
|
||||
rent_sysvar,
|
||||
0,
|
||||
&[MINT, &id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// TODO Initialize hash token metadata.
|
||||
|
||||
// Initialize vault token accounts.
|
||||
if vault_base_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
market_info,
|
||||
vault_base_info,
|
||||
mint_base_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
}
|
||||
if vault_quote_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
market_info,
|
||||
vault_quote_info,
|
||||
mint_quote_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
}
|
||||
|
||||
// Mint hash tokens to market.
|
||||
mint_to_signed(
|
||||
mint_base_info,
|
||||
vault_base_info,
|
||||
block_info,
|
||||
token_program,
|
||||
HASH_TOKEN_SUPPLY,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Burn mint authority.
|
||||
set_authority_signed(
|
||||
mint_base_info,
|
||||
block_info,
|
||||
None,
|
||||
AuthorityType::MintTokens,
|
||||
token_program,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Swap in a hashpower market.
|
||||
pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse args.
|
||||
let args = Swap::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
let direction = SwapDirection::try_from(args.direction).unwrap();
|
||||
let precision = SwapPrecision::try_from(args.precision).unwrap();
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_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);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| clock.slot >= b.start_slot + 1500)?;
|
||||
let market = market_info
|
||||
.as_account_mut::<Market>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.id == block.id)?
|
||||
.assert_mut_err(
|
||||
|m| m.base.liquidity() > 0,
|
||||
OreError::InsufficientLiquidity.into(),
|
||||
)?
|
||||
.assert_mut_err(
|
||||
|m| m.quote.liquidity() > 0,
|
||||
OreError::InsufficientLiquidity.into(),
|
||||
)?;
|
||||
mint_base_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
tokens_base_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, mint_base_info.key)?;
|
||||
tokens_quote_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, mint_quote_info.key)?;
|
||||
vault_base_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
vault_quote_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Update market state.
|
||||
let swap_result = market.swap(amount, direction, precision, clock)?;
|
||||
swap_result.log_return();
|
||||
|
||||
// Get transfer amounts and accounts.
|
||||
let (in_amount, in_from, in_to, out_amount, out_from, out_to) = match direction {
|
||||
SwapDirection::Buy => (
|
||||
swap_result.quote_to_transfer,
|
||||
tokens_quote_info,
|
||||
vault_quote_info,
|
||||
swap_result.base_to_transfer,
|
||||
vault_base_info,
|
||||
tokens_base_info,
|
||||
),
|
||||
SwapDirection::Sell => (
|
||||
swap_result.base_to_transfer,
|
||||
tokens_base_info,
|
||||
vault_base_info,
|
||||
swap_result.quote_to_transfer,
|
||||
vault_quote_info,
|
||||
tokens_quote_info,
|
||||
),
|
||||
};
|
||||
|
||||
// Transfer tokens.
|
||||
transfer(signer_info, in_from, in_to, token_program, in_amount)?;
|
||||
transfer_signed(
|
||||
market_info,
|
||||
out_from,
|
||||
out_to,
|
||||
token_program,
|
||||
out_amount,
|
||||
&[
|
||||
MARKET,
|
||||
market.base.mint.as_ref(),
|
||||
market.quote.mint.as_ref(),
|
||||
market.id.to_le_bytes().as_ref(),
|
||||
],
|
||||
)?;
|
||||
|
||||
// Validate vault reserves.
|
||||
let vault_base =
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
let vault_quote =
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
market.check_vaults(&vault_base, &vault_quote)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user