mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
cleanup
This commit is contained in:
@@ -1,136 +0,0 @@
|
||||
use ore_api::{prelude::*, sdk::program_log};
|
||||
use solana_program::rent::Rent;
|
||||
use steel::*;
|
||||
|
||||
/// Commit to a block.
|
||||
pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Commit::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
let executor = Pubkey::new_from_array(args.executor);
|
||||
let fee = u64::from_le_bytes(args.fee);
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, commitment_info, market_info, miner_info, mint_info, permit_info, sender_info, system_program, token_program, ore_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)?;
|
||||
commitment_info
|
||||
.has_address(&commitment_pda(block.id).0)?
|
||||
.as_token_account()?
|
||||
.assert(|t| t.mint() == *mint_info.key)?
|
||||
.assert(|t| t.owner() == *block_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()?;
|
||||
let sender = sender_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, &mint_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Normalize amount.
|
||||
let amount = sender.amount().min(amount);
|
||||
|
||||
// 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_committed = 0;
|
||||
miner.total_deployed = 0;
|
||||
miner.total_rewards = 0;
|
||||
miner
|
||||
} else {
|
||||
miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?
|
||||
};
|
||||
|
||||
// Load permit account.
|
||||
let permit = if permit_info.data_is_empty() {
|
||||
create_program_account::<Permit>(
|
||||
permit_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[PERMIT, &signer_info.key.to_bytes(), &block.id.to_le_bytes()],
|
||||
)?;
|
||||
let permit = permit_info.as_account_mut::<Permit>(&ore_api::ID)?;
|
||||
permit.authority = *signer_info.key;
|
||||
permit.block_id = block.id;
|
||||
permit.commitment = 0;
|
||||
permit.executor = Pubkey::default();
|
||||
permit.fee = 0;
|
||||
permit.seed = [0; 32];
|
||||
permit
|
||||
} else {
|
||||
permit_info
|
||||
.as_account_mut::<Permit>(&ore_api::ID)?
|
||||
.assert_mut(|p| p.authority == *signer_info.key)?
|
||||
.assert_mut(|p| p.block_id == block.id)?
|
||||
};
|
||||
|
||||
// Update executor logic.
|
||||
permit.executor = executor;
|
||||
permit.fee = fee;
|
||||
permit.seed = args.seed;
|
||||
|
||||
// Send lamports to permit account to pay for fee.
|
||||
if permit.fee > 0 {
|
||||
let rent_exempt_balance = Rent::get()?.minimum_balance(std::mem::size_of::<Permit>());
|
||||
let surplus_balance = permit_info.lamports().saturating_sub(rent_exempt_balance);
|
||||
let total_fees = permit.commitment * permit.fee;
|
||||
let required_fees = total_fees.saturating_sub(surplus_balance);
|
||||
permit_info.collect(required_fees, signer_info)?;
|
||||
}
|
||||
|
||||
// Transfer hash tokens.
|
||||
transfer(
|
||||
signer_info,
|
||||
sender_info,
|
||||
commitment_info,
|
||||
token_program,
|
||||
amount,
|
||||
)?;
|
||||
|
||||
// Update block.
|
||||
permit.commitment += amount;
|
||||
miner.total_committed += amount;
|
||||
block.total_committed += amount;
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
block.id,
|
||||
&[block_info.clone(), ore_program.clone()],
|
||||
&CommitEvent {
|
||||
disc: OreEvent::Commit as u64,
|
||||
authority: *signer_info.key,
|
||||
block_id: block.id,
|
||||
amount,
|
||||
block_commitment: block.total_committed,
|
||||
permit_commitment: permit.commitment,
|
||||
fee,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.to_bytes(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
use ore_api::{prelude::*, sdk::program_log};
|
||||
use steel::*;
|
||||
|
||||
/// Deposits collateral.
|
||||
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 clock = Clock::get()?;
|
||||
let [signer_info, block_info, collateral_info, mint_ore_info, sender_info, stake_info, system_program, token_program, ore_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)?;
|
||||
collateral_info
|
||||
.is_writable()?
|
||||
.has_address(&collateral_pda(block.id).0)?
|
||||
.as_token_account()?
|
||||
.assert(|t| t.mint() == *mint_ore_info.key)?
|
||||
.assert(|t| t.owner() == *block_info.key)?;
|
||||
mint_ore_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
sender_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, &mint_ore_info.key)?
|
||||
.assert(|t| t.amount() >= amount)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Load stake account.
|
||||
let stake = if stake_info.data_is_empty() {
|
||||
create_program_account::<Stake>(
|
||||
stake_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[STAKE, &signer_info.key.to_bytes(), &block.id.to_le_bytes()],
|
||||
)?;
|
||||
let stake = stake_info.as_account_mut::<Stake>(&ore_api::ID)?;
|
||||
stake.authority = *signer_info.key;
|
||||
stake.block_id = block.id;
|
||||
stake.collateral = 0;
|
||||
stake.spend = 0;
|
||||
stake
|
||||
} else {
|
||||
stake_info
|
||||
.as_account_mut::<Stake>(&ore_api::ID)?
|
||||
.assert_mut(|p| p.authority == *signer_info.key)?
|
||||
.assert_mut(|p| p.block_id == block.id)?
|
||||
};
|
||||
|
||||
// Update stake state.
|
||||
stake.collateral += amount;
|
||||
|
||||
// Transfer collateral.
|
||||
transfer(
|
||||
signer_info,
|
||||
sender_info,
|
||||
collateral_info,
|
||||
token_program,
|
||||
amount,
|
||||
)?;
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
block.id,
|
||||
&[block_info.clone(), ore_program.clone()],
|
||||
&DepositEvent {
|
||||
disc: OreEvent::Deposit as u64,
|
||||
authority: *signer_info.key,
|
||||
block_id: block.id,
|
||||
amount,
|
||||
collateral: stake.collateral,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.to_bytes(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
84
program/src/initialize.rs
Normal file
84
program/src/initialize.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Sets the admin.
|
||||
pub fn process_initialize(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
// let args = Initialize::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts.
|
||||
let [signer_info, config_info, market_info, treasury_info, system_program] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?.has_address(&ADMIN_ADDRESS)?;
|
||||
config_info
|
||||
.is_writable()?
|
||||
.is_empty()?
|
||||
.has_seeds(&[CONFIG], &ore_api::ID)?;
|
||||
market_info
|
||||
.is_writable()?
|
||||
.is_empty()?
|
||||
.has_seeds(&[MARKET], &ore_api::ID)?;
|
||||
treasury_info
|
||||
.is_writable()?
|
||||
.is_empty()?
|
||||
.has_seeds(&[TREASURY], &ore_api::ID)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
|
||||
// Create config account.
|
||||
create_program_account::<Config>(
|
||||
config_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[CONFIG],
|
||||
)?;
|
||||
let config = config_info.as_account_mut::<Config>(&ore_api::ID)?;
|
||||
config.admin = *signer_info.key;
|
||||
config.fee_collector = *signer_info.key;
|
||||
config.fee_rate = 0;
|
||||
|
||||
// Initialize market.
|
||||
let initial_id: u64 = 0;
|
||||
create_program_account::<Market>(
|
||||
market_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[MARKET],
|
||||
)?;
|
||||
let market = market_info.as_account_mut::<Market>(&ore_api::ID)?;
|
||||
market.base = TokenParams {
|
||||
mint: Pubkey::default(), // Virtual token
|
||||
balance: 0,
|
||||
balance_virtual: 0,
|
||||
};
|
||||
market.quote = TokenParams {
|
||||
mint: MINT_ADDRESS,
|
||||
balance: 0,
|
||||
balance_virtual: 0,
|
||||
};
|
||||
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.block_id = initial_id;
|
||||
|
||||
// Create treasury account.
|
||||
create_program_account::<Treasury>(
|
||||
treasury_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[TREASURY],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,30 +1,18 @@
|
||||
mod close;
|
||||
mod commit;
|
||||
mod deposit;
|
||||
mod initialize;
|
||||
mod log;
|
||||
mod mine;
|
||||
mod open;
|
||||
mod set_admin;
|
||||
mod set_block_limit;
|
||||
mod set_fee_collector;
|
||||
mod set_fee_rate;
|
||||
mod swap;
|
||||
mod uncommit;
|
||||
mod withdraw;
|
||||
|
||||
use close::*;
|
||||
use commit::*;
|
||||
use deposit::*;
|
||||
use initialize::*;
|
||||
use log::*;
|
||||
use mine::*;
|
||||
use open::*;
|
||||
use set_admin::*;
|
||||
use set_block_limit::*;
|
||||
use set_fee_collector::*;
|
||||
use set_fee_rate::*;
|
||||
use swap::*;
|
||||
use uncommit::*;
|
||||
use withdraw::*;
|
||||
|
||||
use ore_api::instruction::*;
|
||||
use steel::*;
|
||||
@@ -37,19 +25,18 @@ pub fn process_instruction(
|
||||
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::Commit => process_commit(accounts, data)?,
|
||||
OreInstruction::Deposit => process_deposit(accounts, data)?,
|
||||
// User
|
||||
OreInstruction::Log => process_log(accounts, data)?,
|
||||
OreInstruction::Mine => process_mine(accounts, data)?,
|
||||
OreInstruction::Swap => process_swap(accounts, data)?,
|
||||
OreInstruction::Uncommit => process_uncommit(accounts, data)?,
|
||||
OreInstruction::Withdraw => process_withdraw(accounts, data)?,
|
||||
OreInstruction::Initialize => process_initialize(accounts, data)?,
|
||||
|
||||
// Admin
|
||||
OreInstruction::SetAdmin => process_set_admin(accounts, data)?,
|
||||
OreInstruction::SetBlockLimit => process_set_block_limit(accounts, data)?,
|
||||
OreInstruction::SetFeeCollector => process_set_fee_collector(accounts, data)?,
|
||||
OreInstruction::SetFeeRate => process_set_fee_rate(accounts, data)?,
|
||||
|
||||
_ => panic!("Not implemented"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use ore_api::{prelude::*, sdk::program_log};
|
||||
use solana_nostd_keccak::hash;
|
||||
use solana_program::slot_hashes::SlotHashes;
|
||||
use solana_program::{log::sol_log, slot_hashes::SlotHashes};
|
||||
use steel::*;
|
||||
|
||||
/// Mine a block.
|
||||
@@ -11,34 +11,23 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, authority_info, block_info, commitment_info, market_info, miner_info, mint_hash_info, mint_ore_info, permit_info, recipient_info, treasury_info, system_program, token_program, ore_program, slot_hashes_sysvar] =
|
||||
let [signer_info, authority_info, block_info, market_info, miner_info, mint_info, recipient_info, treasury_info, system_program, token_program, ore_program, slot_hashes_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
authority_info.is_writable()?;
|
||||
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)?;
|
||||
commitment_info
|
||||
.is_writable()?
|
||||
.has_address(&commitment_pda(block.id).0)?
|
||||
.as_token_account()?;
|
||||
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_hash_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_ore_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
.assert(|m| m.block_id == block.id)?;
|
||||
mint_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *authority_info.key)?;
|
||||
let permit = permit_info
|
||||
.as_account_mut::<Permit>(&ore_api::ID)?
|
||||
.assert_mut(|p| p.authority == miner.authority)?
|
||||
.assert_mut(|p| p.block_id == block.id)?
|
||||
.assert_mut(|p| p.executor == *signer_info.key || p.executor == Pubkey::default())?;
|
||||
recipient_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(&miner.authority, &MINT_ADDRESS)?;
|
||||
@@ -48,130 +37,82 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Reduce permit amount.
|
||||
let amount = permit.commitment.min(amount);
|
||||
permit.commitment -= amount;
|
||||
|
||||
// Pay executor fee.
|
||||
// if permit.fee > 0 {
|
||||
// permit_info.send(permit.fee * amount, signer_info);
|
||||
// }
|
||||
|
||||
// Close permit account, if empty.
|
||||
let permit_commitment = permit.commitment;
|
||||
if permit_commitment == 0 {
|
||||
permit_info.close(authority_info)?;
|
||||
}
|
||||
|
||||
// Burn hash tokens.
|
||||
burn_signed(
|
||||
commitment_info,
|
||||
mint_hash_info,
|
||||
block_info,
|
||||
token_program,
|
||||
amount,
|
||||
&[BLOCK, &block.id.to_le_bytes()],
|
||||
)?;
|
||||
// burn_signed(
|
||||
// commitment_info,
|
||||
// mint_hash_info,
|
||||
// block_info,
|
||||
// token_program,
|
||||
// amount,
|
||||
// &[BLOCK, &block.id.to_le_bytes()],
|
||||
// )?;
|
||||
|
||||
// Set block slot hash.
|
||||
if block.slot_hash == [0; 32] {
|
||||
let slot_hashes =
|
||||
bincode::deserialize::<SlotHashes>(slot_hashes_sysvar.data.borrow().as_ref()).unwrap();
|
||||
let Some(slot_hash) = slot_hashes.get(&block.start_slot) else {
|
||||
// If mine is not called within ~2.5 minutes of the block starting,
|
||||
// then the slot hash will be unavailable and secure hashes cannot be generated.
|
||||
return Ok(());
|
||||
};
|
||||
block.slot_hash = slot_hash.to_bytes();
|
||||
}
|
||||
// if block.slot_hash == [0; 32] {
|
||||
// let slot_hashes =
|
||||
// bincode::deserialize::<SlotHashes>(slot_hashes_sysvar.data.borrow().as_ref()).unwrap();
|
||||
// let Some(slot_hash) = slot_hashes.get(&block.start_slot) else {
|
||||
// // If mine is not called within ~2.5 minutes of the block starting,
|
||||
// // then the slot hash will be unavailable and secure hashes cannot be generated.
|
||||
// return Ok(());
|
||||
// };
|
||||
// block.slot_hash = slot_hash.to_bytes();
|
||||
// }
|
||||
|
||||
// Reset miner hash if mining new block.
|
||||
if miner.block_id != block.id {
|
||||
let mut args = [0u8; 104];
|
||||
args[..8].copy_from_slice(&block.id.to_le_bytes());
|
||||
args[8..40].copy_from_slice(&block.slot_hash);
|
||||
args[40..72].copy_from_slice(&permit.authority.to_bytes());
|
||||
args[72..].copy_from_slice(&permit.seed);
|
||||
miner.hash = hash(&args);
|
||||
miner.block_id = block.id;
|
||||
}
|
||||
// if miner.block_id != block.id {
|
||||
// let mut args = [0u8; 104];
|
||||
// args[..8].copy_from_slice(&block.id.to_le_bytes());
|
||||
// args[8..40].copy_from_slice(&block.slot_hash);
|
||||
// args[40..72].copy_from_slice(&miner.authority.to_bytes());
|
||||
// args[72..].copy_from_slice(&miner.seed);
|
||||
// miner.hash = hash(&args);
|
||||
// miner.block_id = block.id;
|
||||
// }
|
||||
|
||||
// Mine and accumulate rewards.
|
||||
let mut nugget_reward = 0;
|
||||
for _ in 0..amount {
|
||||
// Update stats
|
||||
block.total_deployed += 1;
|
||||
miner.total_deployed += 1;
|
||||
// let mut nugget_reward = 0;
|
||||
// for _ in 0..amount {
|
||||
// // Update stats
|
||||
// // block.total_deployed += 1;
|
||||
|
||||
// Generate hash.
|
||||
miner.hash = hash(miner.hash.as_ref());
|
||||
// // Generate hash.
|
||||
// miner.hash = hash(miner.hash.as_ref());
|
||||
|
||||
// Score and increment rewards.
|
||||
let score = difficulty(miner.hash) as u64;
|
||||
if score >= block.reward.nugget_threshold {
|
||||
nugget_reward += block.reward.nugget_reward;
|
||||
}
|
||||
// // Score and increment rewards.
|
||||
// let score = difficulty(miner.hash) as u64;
|
||||
// if score >= block.reward.nugget_threshold {
|
||||
// nugget_reward += block.reward.nugget_reward;
|
||||
// }
|
||||
|
||||
// If hash is best hash, update best hash.
|
||||
if miner.hash < block.reward.lode_hash {
|
||||
block.reward.lode_hash = miner.hash;
|
||||
block.reward.lode_authority = miner.authority;
|
||||
}
|
||||
}
|
||||
// // If hash is best hash, update best hash.
|
||||
// if miner.hash < block.reward.lode_hash {
|
||||
// block.reward.lode_hash = miner.hash;
|
||||
// block.reward.lode_authority = miner.authority;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Payout ORE.
|
||||
if nugget_reward > 0 {
|
||||
// Limit payout to supply cap.
|
||||
let ore_mint = mint_ore_info.as_mint()?;
|
||||
let max_reward = MAX_SUPPLY.saturating_sub(ore_mint.supply());
|
||||
let reward_amount = nugget_reward.min(max_reward);
|
||||
|
||||
// Update stats.
|
||||
block.total_rewards += reward_amount;
|
||||
miner.total_rewards += reward_amount;
|
||||
|
||||
// Mint to recipient.
|
||||
mint_to_signed_with_bump(
|
||||
mint_ore_info,
|
||||
recipient_info,
|
||||
treasury_info,
|
||||
token_program,
|
||||
reward_amount,
|
||||
&[TREASURY],
|
||||
TREASURY_BUMP,
|
||||
)?;
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
block.id,
|
||||
&[block_info.clone(), ore_program.clone()],
|
||||
&RewardEvent {
|
||||
disc: OreEvent::Reward as u64,
|
||||
amount: reward_amount,
|
||||
authority: miner.authority,
|
||||
block_id: block.id,
|
||||
rewards_type: RewardsType::Nugget as u64,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.to_bytes(),
|
||||
)?;
|
||||
}
|
||||
// Log mint.
|
||||
// let ore_mint = mint_ore_info.as_mint()?;
|
||||
// let mint_authority = ore_mint.mint_authority();
|
||||
// sol_log(format!("mint_authority: {:?}", mint_authority).as_str());
|
||||
// sol_log(format!("treasury: {:?}", treasury_info.key).as_str());
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
block.id,
|
||||
&[block_info.clone(), ore_program.clone()],
|
||||
&MineEvent {
|
||||
disc: OreEvent::Mine as u64,
|
||||
authority: miner.authority,
|
||||
block_id: block.id,
|
||||
deployed: amount,
|
||||
total_deployed: block.total_deployed,
|
||||
remaining_commitment: permit_commitment,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.to_bytes(),
|
||||
)?;
|
||||
// program_log(
|
||||
// block.id,
|
||||
// &[block_info.clone(), ore_program.clone()],
|
||||
// &MineEvent {
|
||||
// disc: OreEvent::Mine as u64,
|
||||
// authority: miner.authority,
|
||||
// block_id: block.id,
|
||||
// deployed: amount,
|
||||
// total_deployed: block.total_deployed,
|
||||
// remaining_commitment: 0,
|
||||
// ts: clock.unix_timestamp,
|
||||
// }
|
||||
// .to_bytes(),
|
||||
// )?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
.assert(|t| t.owner() == *block_info.key)?;
|
||||
}
|
||||
|
||||
// Initialize vault token accounts.
|
||||
// Initialize base vault token account.
|
||||
if vault_base_info.data_is_empty() {
|
||||
let vault_base_pda = vault_base_pda(id);
|
||||
allocate_account_with_bump(
|
||||
@@ -256,6 +256,8 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
.assert(|t| t.mint() == *mint_base_info.key)?
|
||||
.assert(|t| t.owner() == *market_info.key)?;
|
||||
}
|
||||
|
||||
// Initialize quote vault token account.
|
||||
if vault_quote_info.data_is_empty() {
|
||||
let vault_quote_pda = vault_quote_pda(id);
|
||||
allocate_account_with_bump(
|
||||
|
||||
@@ -12,33 +12,11 @@ pub fn process_set_admin(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRe
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let config = config_info
|
||||
.as_account_mut::<Config>(&ore_api::ID)?
|
||||
.assert_mut(|c| c.admin == *signer_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
|
||||
// Load config account.
|
||||
let config = if config_info.data_is_empty() {
|
||||
// Assert signer is admin.
|
||||
// signer_info.has_address(&ADMIN_ADDRESS)?;
|
||||
|
||||
// Create config account.
|
||||
create_program_account::<Config>(
|
||||
config_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[CONFIG],
|
||||
)?;
|
||||
let config = config_info.as_account_mut::<Config>(&ore_api::ID)?;
|
||||
config.admin = *signer_info.key;
|
||||
config.block_limit = 100;
|
||||
config.fee_collector = *signer_info.key;
|
||||
config.fee_rate = 0;
|
||||
config
|
||||
} else {
|
||||
config_info
|
||||
.as_account_mut::<Config>(&ore_api::ID)?
|
||||
.assert_mut(|c| c.admin == *signer_info.key)?
|
||||
};
|
||||
|
||||
// Set admin.
|
||||
config.admin = new_admin;
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Sets the block limit.
|
||||
pub fn process_set_block_limit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = SetBlockLimit::try_from_bytes(data)?;
|
||||
let new_block_limit = u64::from_le_bytes(args.block_limit);
|
||||
|
||||
// Load accounts.
|
||||
let [signer_info, config_info, system_program] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let config = config_info
|
||||
.as_account_mut::<Config>(&ore_api::ID)?
|
||||
.assert_mut(|c| c.admin == *signer_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
|
||||
// Set block limit.
|
||||
config.block_limit = new_block_limit;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -11,35 +11,23 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, stake_info, tokens_base_info, tokens_quote_info, vault_base_info, vault_quote_info, system_program, token_program, associated_token_program, ore_program] =
|
||||
let [signer_info, block_info, market_info, mint_quote_info, tokens_info, vault_info, system_program, token_program, associated_token_program, ore_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block: &mut Block = block_info
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| clock.slot < b.start_slot)?;
|
||||
let block: &mut Block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
// .assert_mut(|b| clock.slot < b.start_slot)?;
|
||||
let market = market_info
|
||||
.as_account_mut::<Market>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.id == block.id)?
|
||||
.assert_mut(|m| m.block_id == block.id)?
|
||||
.assert_mut(|m| m.base.liquidity() > 0)?
|
||||
.assert_mut(|m| m.quote.liquidity() > 0)?;
|
||||
mint_base_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
let stake = stake_info
|
||||
.as_account_mut::<Stake>(&ore_api::ID)?
|
||||
.assert_mut(|p| p.authority == *signer_info.key)?
|
||||
.assert_mut(|p| p.block_id == block.id)?;
|
||||
vault_base_info
|
||||
vault_info
|
||||
.is_writable()?
|
||||
.has_address(&vault_base_pda(block.id).0)?
|
||||
.as_token_account()?
|
||||
.assert(|t| t.mint() == *mint_base_info.key)?
|
||||
.assert(|t| t.owner() == *market_info.key)?;
|
||||
vault_quote_info
|
||||
.is_writable()?
|
||||
.has_address(&vault_quote_pda(block.id).0)?
|
||||
.has_address(&vault_pda().0)?
|
||||
.as_token_account()?
|
||||
.assert(|t| t.mint() == *mint_quote_info.key)?
|
||||
.assert(|t| t.owner() == *market_info.key)?;
|
||||
@@ -49,33 +37,18 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Load token acccounts.
|
||||
if tokens_base_info.data_is_empty() {
|
||||
if tokens_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
signer_info,
|
||||
tokens_base_info,
|
||||
mint_base_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
tokens_base_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, mint_base_info.key)?;
|
||||
}
|
||||
if tokens_quote_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
signer_info,
|
||||
tokens_quote_info,
|
||||
tokens_info,
|
||||
mint_quote_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
tokens_quote_info
|
||||
tokens_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, mint_quote_info.key)?;
|
||||
}
|
||||
@@ -85,57 +58,33 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
swap_event.authority = *signer_info.key;
|
||||
swap_event.block_id = block.id;
|
||||
|
||||
// Get transfer amounts and accounts.
|
||||
let (in_amount, in_from, in_to, out_amount, out_from, out_to) = match direction {
|
||||
SwapDirection::Buy => (
|
||||
swap_event.quote_to_transfer,
|
||||
tokens_quote_info,
|
||||
vault_quote_info,
|
||||
swap_event.base_to_transfer,
|
||||
vault_base_info,
|
||||
tokens_base_info,
|
||||
),
|
||||
SwapDirection::Sell => (
|
||||
swap_event.base_to_transfer,
|
||||
tokens_base_info,
|
||||
vault_base_info,
|
||||
swap_event.quote_to_transfer,
|
||||
vault_quote_info,
|
||||
tokens_quote_info,
|
||||
),
|
||||
};
|
||||
|
||||
// Update stake state.
|
||||
// Transfer tokens
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
// TODO Fail if out_amount is zero
|
||||
stake.spend += in_amount;
|
||||
transfer(
|
||||
signer_info,
|
||||
tokens_info,
|
||||
vault_info,
|
||||
token_program,
|
||||
swap_event.quote_to_transfer,
|
||||
)?;
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
stake.spend = stake.spend.saturating_sub(out_amount);
|
||||
transfer_signed(
|
||||
market_info,
|
||||
vault_info,
|
||||
tokens_info,
|
||||
token_program,
|
||||
swap_event.quote_to_transfer,
|
||||
&[MARKET],
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Assert utilization is not greater than capacity.
|
||||
if stake.spend > stake.collateral {
|
||||
panic!("spend is greater than collateral");
|
||||
}
|
||||
|
||||
// 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.id.to_le_bytes().as_ref()],
|
||||
)?;
|
||||
};
|
||||
|
||||
// Validate vault reserves.
|
||||
let vault_base = vault_base_info.as_token_account()?;
|
||||
let vault_quote = vault_quote_info.as_token_account()?;
|
||||
market.check_vaults(&vault_base, &vault_quote)?;
|
||||
// let vault_base = vault_base_info.as_token_account()?;
|
||||
// let vault_quote = vault_quote_info.as_token_account()?;
|
||||
// market.check_vaults(&vault_base, &vault_quote)?;
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
use ore_api::{prelude::*, sdk::program_log};
|
||||
use steel::*;
|
||||
|
||||
/// Uncommit from a block.
|
||||
pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Uncommit::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, commitment_info, market_info, miner_info, mint_info, permit_info, recipient_info, system_program, token_program, ore_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)?;
|
||||
commitment_info
|
||||
.is_writable()?
|
||||
.has_address(&commitment_pda(block.id).0)?
|
||||
.as_token_account()?
|
||||
.assert(|t| t.mint() == *mint_info.key)?
|
||||
.assert(|t| t.owner() == *block_info.key)?;
|
||||
let market = market_info
|
||||
.as_account::<Market>(&ore_api::ID)?
|
||||
.assert(|m| m.id == block.id)?;
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.authority == *signer_info.key)?;
|
||||
mint_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
let permit = permit_info
|
||||
.as_account_mut::<Permit>(&ore_api::ID)?
|
||||
.assert_mut(|p| p.authority == *signer_info.key)?
|
||||
.assert_mut(|p| p.block_id == block.id)?;
|
||||
recipient_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, &mint_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Normalize amount.
|
||||
let amount = permit.commitment.min(amount);
|
||||
|
||||
// Transfer hash tokens.
|
||||
transfer_signed(
|
||||
block_info,
|
||||
commitment_info,
|
||||
recipient_info,
|
||||
token_program,
|
||||
amount,
|
||||
&[BLOCK, &block.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Update block.
|
||||
permit.commitment -= amount;
|
||||
miner.total_committed -= amount;
|
||||
block.total_committed -= amount;
|
||||
|
||||
// Close permit account, if empty.
|
||||
let fee = permit.fee;
|
||||
let commitment = permit.commitment;
|
||||
if permit.commitment == 0 {
|
||||
permit_info.close(signer_info)?;
|
||||
}
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
block.id,
|
||||
&[block_info.clone(), ore_program.clone()],
|
||||
&UncommitEvent {
|
||||
disc: OreEvent::Uncommit as u64,
|
||||
authority: *signer_info.key,
|
||||
block_id: block.id,
|
||||
block_commitment: block.total_committed,
|
||||
permit_commitment: commitment,
|
||||
amount,
|
||||
fee,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.to_bytes(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
use ore_api::{prelude::*, sdk::program_log};
|
||||
use steel::*;
|
||||
|
||||
/// Withdraws collateral.
|
||||
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 clock = Clock::get()?;
|
||||
let [signer_info, block_info, collateral_info, mint_ore_info, recipient_info, stake_info, system_program, token_program, ore_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
mint_ore_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
recipient_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, &mint_ore_info.key)?;
|
||||
let stake = stake_info
|
||||
.as_account_mut::<Stake>(&ore_api::ID)?
|
||||
.assert_mut(|p| p.authority == *signer_info.key)?;
|
||||
let block_pda = block_pda(stake.block_id);
|
||||
block_info.has_address(&block_pda.0)?;
|
||||
collateral_info
|
||||
.is_writable()?
|
||||
.has_address(&collateral_pda(stake.block_id).0)?
|
||||
.as_token_account()?
|
||||
.assert(|t| t.mint() == *mint_ore_info.key)?
|
||||
.assert(|t| t.owner() == *block_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
ore_program.is_program(&ore_api::ID)?;
|
||||
|
||||
// Check timestamp. Collateral can only be withdrawn after the block has started mining
|
||||
let start_slot = stake.block_id * 1500;
|
||||
if clock.slot < start_slot {
|
||||
return Err(ProgramError::InvalidArgument);
|
||||
}
|
||||
|
||||
// Normalize amount
|
||||
let amount = amount.min(stake.collateral);
|
||||
|
||||
// Update stake state.
|
||||
stake.collateral -= amount;
|
||||
|
||||
// Transfer collateral.
|
||||
transfer_signed_with_bump(
|
||||
block_info,
|
||||
collateral_info,
|
||||
recipient_info,
|
||||
token_program,
|
||||
amount,
|
||||
&[BLOCK, &stake.block_id.to_le_bytes()],
|
||||
block_pda.1,
|
||||
)?;
|
||||
|
||||
// Close stake account, if empty.
|
||||
if stake.collateral == 0 {
|
||||
stake_info.close(signer_info)?;
|
||||
}
|
||||
|
||||
// Emit event.
|
||||
program_log(
|
||||
stake.block_id,
|
||||
&[block_info.clone(), ore_program.clone()],
|
||||
&WithdrawEvent {
|
||||
disc: OreEvent::Withdraw as u64,
|
||||
authority: *signer_info.key,
|
||||
block_id: stake.block_id,
|
||||
amount,
|
||||
collateral: stake.collateral,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.to_bytes(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user