mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-17 07:26:52 +00:00
sdk, miner
This commit is contained in:
@@ -5,7 +5,7 @@ use steel::*;
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, miner_info, miner_tokens_info, mint_info, opener_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
|
||||
let [signer_info, block_info, miner_info, miner_tokens_info, mint_info, opener_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -41,11 +41,10 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
|
||||
// Payout block reward to winning miner.
|
||||
if block.best_hash_miner != Pubkey::default() {
|
||||
// Load recipient.
|
||||
recipient_info.as_associated_token_account(&block.best_hash_miner, &mint_info.key)?;
|
||||
// Load winning miner.
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == block.best_hash_miner)?;
|
||||
.has_address(&block.best_hash_miner)?
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?;
|
||||
|
||||
// Update stats.
|
||||
miner.total_rewards += block.reward;
|
||||
|
||||
@@ -38,7 +38,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
// If hash is best hash, update best hash.
|
||||
if h < block.best_hash {
|
||||
block.best_hash = h;
|
||||
block.best_hash_miner = miner.authority;
|
||||
block.best_hash_miner = *miner_info.key;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -8,7 +8,7 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
let id = u64::from_le_bytes(args.id);
|
||||
|
||||
// Load accounts.
|
||||
let [signer_info, block_info, market_info, system_program, ore_program] = accounts else {
|
||||
let [signer_info, block_info, market_info, system_program] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
@@ -16,7 +16,6 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
.as_account::<Market>(&ore_api::ID)?
|
||||
.assert(|m| m.block_id < id)?; // Only allow opening blocks in forward bias
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Create block, if it doesn't exist.
|
||||
if block_info.data_is_empty() {
|
||||
|
||||
@@ -30,9 +30,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
.assert_mut(|m| m.block_id == block.id)?
|
||||
.assert_mut(|m| m.base.liquidity() > 0)?
|
||||
.assert_mut(|m| m.quote.liquidity() > 0)?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
mint_info
|
||||
.has_address(&market.quote.mint)?
|
||||
.has_address(&MINT_ADDRESS)?
|
||||
@@ -46,10 +43,35 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
associated_token_program.is_program(&spl_associated_token_account::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Load miner.
|
||||
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 = block.id;
|
||||
miner.hashpower = 0;
|
||||
miner.seed = [0; 32];
|
||||
miner.total_hashpower = 0;
|
||||
miner.total_rewards = 0;
|
||||
miner
|
||||
} else {
|
||||
miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?
|
||||
.assert_mut(|m| m.block_id <= block.id)?
|
||||
};
|
||||
|
||||
// Reset miner.
|
||||
if miner.block_id != block.id {
|
||||
miner.block_id = block.id;
|
||||
miner.hashpower = 0;
|
||||
miner.seed = args.seed;
|
||||
}
|
||||
|
||||
// Pay swap fee.
|
||||
|
||||
Reference in New Issue
Block a user