mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 15:10:13 +00:00
cleanup
This commit is contained in:
@@ -10,7 +10,9 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, miner_info, system_program, token_program] = accounts else {
|
||||
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()?;
|
||||
@@ -18,14 +20,38 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
.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 miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
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)?;
|
||||
|
||||
// TODO Open miner account if it doesn't exist.
|
||||
// TODO Burn hash tokens
|
||||
// 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 {
|
||||
@@ -34,6 +60,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
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 {
|
||||
@@ -45,5 +72,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
// Update miner stats.
|
||||
miner.total_hashes += amount;
|
||||
|
||||
// Burn hash tokens.
|
||||
burn(sender_info, mint_info, signer_info, token_program, amount)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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, tokens_base_info, tokens_quote_info, vault_base_info, vault_quote_info, system_program, token_program] =
|
||||
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);
|
||||
@@ -31,9 +31,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|m| m.quote.reserves() > 0,
|
||||
OreError::InsufficientLiquidity.into(),
|
||||
)?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
mint_base_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
tokens_base_info
|
||||
@@ -75,17 +72,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
),
|
||||
};
|
||||
|
||||
// Update miner state.
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
miner.deployed += in_amount;
|
||||
assert!(miner.deployed <= miner.stake);
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
miner.deployed = miner.deployed.saturating_sub(out_amount);
|
||||
}
|
||||
}
|
||||
|
||||
// Transfer tokens.
|
||||
transfer(signer_info, in_from, in_to, token_program, in_amount)?;
|
||||
transfer_signed(
|
||||
|
||||
Reference in New Issue
Block a user