collateral

This commit is contained in:
Hardhat Chad
2025-06-24 14:37:16 -05:00
parent 8d77aca7c0
commit fdeb2cf82a
13 changed files with 65 additions and 47 deletions

View File

@@ -195,7 +195,7 @@ pub struct DepositEvent {
pub amount: u64, pub amount: u64,
/// The total amount of ORE this user has deposited as collateral. /// The total amount of ORE this user has deposited as collateral.
pub capacity: u64, pub collateral: u64,
/// The timestamp of the event. /// The timestamp of the event.
pub ts: i64, pub ts: i64,
@@ -217,7 +217,7 @@ pub struct WithdrawEvent {
pub amount: u64, pub amount: u64,
/// The total amount of ORE this user has deposited as collateral. /// The total amount of ORE this user has deposited as collateral.
pub capacity: u64, pub collateral: u64,
/// The timestamp of the event. /// The timestamp of the event.
pub ts: i64, pub ts: i64,

View File

@@ -26,7 +26,10 @@ pub struct Block {
pub start_slot: u64, pub start_slot: u64,
/// The total number of hashes submitted to the block. /// The total number of hashes submitted to the block.
pub total_hashes: u64, pub total_committed: u64,
/// The total number of hashes deployed to the block.
pub total_deployed: u64,
/// The total amount of rewards paid out to miners. /// The total amount of rewards paid out to miners.
pub total_rewards: u64, pub total_rewards: u64,

View File

@@ -16,10 +16,13 @@ pub struct Miner {
/// The hash of the last block this miner mined in. /// The hash of the last block this miner mined in.
pub hash: [u8; 32], pub hash: [u8; 32],
/// The total number of hashes this miner has submitted. /// The total number of hashes this miner has committed to the block.
pub total_hashes: u64, pub total_committed: u64,
/// The amount of ORE this miner has mined. /// The total number of hashes this miner has deployed to the block.
pub total_deployed: u64,
/// The total amount of ORE this miner has mined across all blocks.
pub total_rewards: u64, pub total_rewards: u64,
} }

View File

@@ -7,15 +7,15 @@ use super::OreAccount;
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Permit { pub struct Permit {
/// The amount of hash tokens this miner has committed to the block.
pub amount: u64,
/// The authority of the miner account. /// The authority of the miner account.
pub authority: Pubkey, pub authority: Pubkey,
/// The ID of the block this permit is for. /// The ID of the block this permit is for.
pub block_id: u64, pub block_id: u64,
/// The amount of hash tokens this miner has committed to the block.
pub commitment: u64,
/// The executor of the permit. /// The executor of the permit.
pub executor: Pubkey, pub executor: Pubkey,

View File

@@ -13,11 +13,11 @@ pub struct Stake {
/// The ID of the block this collateral is associated with. /// The ID of the block this collateral is associated with.
pub block_id: u64, pub block_id: u64,
/// The amount of ORE this miner can commit to the block. /// The amount of ORE this miner has deposited as collateral for trading.
pub capacity: u64, pub collateral: u64,
/// The amount of ORE this miner has committed to the block. /// The amount of ORE this miner has spent on hashpower in this market.
pub utilization: u64, pub spend: u64,
} }
impl Stake { impl Stake {

View File

@@ -148,7 +148,9 @@ fn print_block(block: Block, clock: &Clock) {
println!(" Start slot: {:?}", block.start_slot); println!(" Start slot: {:?}", block.start_slot);
println!(" Starts in: {:?} sec", elapsed_time as u64); println!(" Starts in: {:?} sec", elapsed_time as u64);
println!(" Slot hash: {:?}", block.slot_hash); println!(" Slot hash: {:?}", block.slot_hash);
println!(" Total hashes: {:?}", block.total_hashes); println!(" Total hashes: {:?}", block.total_committed);
println!(" Total deployed: {:?}", block.total_deployed);
println!(" Total rewards: {:?}", block.total_rewards);
println!(" Lode reward: {:?}", block.reward.lode_reward); println!(" Lode reward: {:?}", block.reward.lode_reward);
println!(" Lode authority: {:?}", block.reward.lode_authority); println!(" Lode authority: {:?}", block.reward.lode_authority);
println!(" Lode hash: {:?}", block.reward.lode_hash); println!(" Lode hash: {:?}", block.reward.lode_hash);

View File

@@ -18,8 +18,8 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
}; };
signer_info.is_signer()?; signer_info.is_signer()?;
let block = block_info let block = block_info
.as_account::<Block>(&ore_api::ID)? .as_account_mut::<Block>(&ore_api::ID)?
.assert(|b| clock.slot < b.start_slot)?; .assert_mut(|b| clock.slot < b.start_slot)?;
commitment_info.as_associated_token_account(block_info.key, mint_info.key)?; commitment_info.as_associated_token_account(block_info.key, mint_info.key)?;
let market = market_info let market = market_info
.as_account::<Market>(&ore_api::ID)? .as_account::<Market>(&ore_api::ID)?
@@ -35,7 +35,7 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
let amount = sender.amount().min(amount); let amount = sender.amount().min(amount);
// Load miner account. // Load miner account.
let _miner = if miner_info.data_is_empty() { let miner = if miner_info.data_is_empty() {
create_program_account::<Miner>( create_program_account::<Miner>(
miner_info, miner_info,
system_program, system_program,
@@ -47,7 +47,8 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
miner.authority = *signer_info.key; miner.authority = *signer_info.key;
miner.block_id = 0; miner.block_id = 0;
miner.hash = [0; 32]; miner.hash = [0; 32];
miner.total_hashes = 0; miner.total_committed = 0;
miner.total_deployed = 0;
miner.total_rewards = 0; miner.total_rewards = 0;
miner miner
} else { } else {
@@ -66,9 +67,12 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
&[PERMIT, &signer_info.key.to_bytes(), &block.id.to_le_bytes()], &[PERMIT, &signer_info.key.to_bytes(), &block.id.to_le_bytes()],
)?; )?;
let permit = permit_info.as_account_mut::<Permit>(&ore_api::ID)?; let permit = permit_info.as_account_mut::<Permit>(&ore_api::ID)?;
permit.amount = 0;
permit.authority = *signer_info.key; permit.authority = *signer_info.key;
permit.block_id = block.id; permit.block_id = block.id;
permit.commitment = 0;
permit.executor = Pubkey::default();
permit.fee = 0;
permit.seed = [0; 32];
permit permit
} else { } else {
permit_info permit_info
@@ -92,7 +96,9 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
)?; )?;
// Update block. // Update block.
permit.amount += amount; permit.commitment += amount;
miner.total_committed += amount;
block.total_committed += amount;
// Emit event. // Emit event.
CommitEvent { CommitEvent {
@@ -100,7 +106,7 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
authority: *signer_info.key, authority: *signer_info.key,
block_id: block.id, block_id: block.id,
amount, amount,
commitment: permit.amount, commitment: permit.commitment,
ts: clock.unix_timestamp, ts: clock.unix_timestamp,
} }
.log_return(); .log_return();

View File

@@ -41,8 +41,8 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
let stake = stake_info.as_account_mut::<Stake>(&ore_api::ID)?; let stake = stake_info.as_account_mut::<Stake>(&ore_api::ID)?;
stake.authority = *signer_info.key; stake.authority = *signer_info.key;
stake.block_id = block.id; stake.block_id = block.id;
stake.capacity = 0; stake.collateral = 0;
stake.utilization = 0; stake.spend = 0;
stake stake
} else { } else {
stake_info stake_info
@@ -52,7 +52,7 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
}; };
// Update stake state. // Update stake state.
stake.capacity += amount; stake.collateral += amount;
// Transfer collateral. // Transfer collateral.
transfer( transfer(
@@ -69,7 +69,7 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
authority: *signer_info.key, authority: *signer_info.key,
block_id: block.id, block_id: block.id,
amount, amount,
capacity: stake.capacity, collateral: stake.collateral,
ts: clock.unix_timestamp, ts: clock.unix_timestamp,
} }
.log_return(); .log_return();

View File

@@ -50,8 +50,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?; slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
// Reduce permit amount. // Reduce permit amount.
let amount = permit.amount.min(amount); let amount = permit.commitment.min(amount);
permit.amount -= amount; permit.commitment -= amount;
// Pay executor fee. // Pay executor fee.
if permit.fee > 0 { if permit.fee > 0 {
@@ -59,7 +59,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
} }
// Close permit account, if empty. // Close permit account, if empty.
if permit.amount == 0 { if permit.commitment == 0 {
permit_info.close(authority_info)?; permit_info.close(authority_info)?;
} }
@@ -100,8 +100,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
let mut nugget_reward = 0; let mut nugget_reward = 0;
for _ in 0..amount { for _ in 0..amount {
// Update stats // Update stats
block.total_hashes += 1; block.total_deployed += 1;
miner.total_hashes += 1; miner.total_deployed += 1;
// Generate hash. // Generate hash.
miner.hash = hash(miner.hash.as_ref()); miner.hash = hash(miner.hash.as_ref());

View File

@@ -78,7 +78,9 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
}; };
block.slot_hash = [0; 32]; block.slot_hash = [0; 32];
block.start_slot = start_slot; block.start_slot = start_slot;
block.total_hashes = 0; block.total_committed = 0;
block.total_deployed = 0;
block.total_rewards = 0;
// Select reward strategy. // Select reward strategy.
let noise_seed = block.id.to_le_bytes(); let noise_seed = block.id.to_le_bytes();

View File

@@ -105,16 +105,16 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
match direction { match direction {
SwapDirection::Buy => { SwapDirection::Buy => {
// TODO Fail if out_amount is zero // TODO Fail if out_amount is zero
stake.utilization += in_amount; stake.spend += in_amount;
} }
SwapDirection::Sell => { SwapDirection::Sell => {
stake.utilization = stake.utilization.saturating_sub(out_amount); stake.spend = stake.spend.saturating_sub(out_amount);
} }
} }
// Assert utilization is not greater than capacity. // Assert utilization is not greater than capacity.
if stake.utilization > stake.capacity { if stake.spend > stake.collateral {
panic!("utilization is greater than capacity"); panic!("spend is greater than collateral");
} }
// Transfer tokens. // Transfer tokens.

View File

@@ -16,17 +16,17 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
}; };
signer_info.is_signer()?; signer_info.is_signer()?;
let block = block_info let block = block_info
.as_account::<Block>(&ore_api::ID)? .as_account_mut::<Block>(&ore_api::ID)?
.assert(|b| clock.slot < b.start_slot)?; .assert_mut(|b| clock.slot < b.start_slot)?;
commitment_info commitment_info
.is_writable()? .is_writable()?
.as_associated_token_account(block_info.key, mint_info.key)?; .as_associated_token_account(block_info.key, mint_info.key)?;
let market = market_info let market = market_info
.as_account::<Market>(&ore_api::ID)? .as_account::<Market>(&ore_api::ID)?
.assert(|m| m.id == block.id)?; .assert(|m| m.id == block.id)?;
miner_info let miner = miner_info
.as_account::<Miner>(&ore_api::ID)? .as_account_mut::<Miner>(&ore_api::ID)?
.assert(|m| m.authority == *signer_info.key)?; .assert_mut(|m| m.authority == *signer_info.key)?;
mint_info.has_address(&market.base.mint)?.as_mint()?; mint_info.has_address(&market.base.mint)?.as_mint()?;
let permit = permit_info let permit = permit_info
.as_account_mut::<Permit>(&ore_api::ID)? .as_account_mut::<Permit>(&ore_api::ID)?
@@ -39,7 +39,7 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
token_program.is_program(&spl_token::ID)?; token_program.is_program(&spl_token::ID)?;
// Normalize amount. // Normalize amount.
let amount = permit.amount.min(amount); let amount = permit.commitment.min(amount);
// Transfer hash tokens. // Transfer hash tokens.
transfer_signed( transfer_signed(
@@ -52,10 +52,12 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
)?; )?;
// Update block. // Update block.
permit.amount -= amount; permit.commitment -= amount;
miner.total_committed -= amount;
block.total_committed -= amount;
// Close permit account, if empty. // Close permit account, if empty.
if permit.amount == 0 { if permit.commitment == 0 {
permit_info.close(signer_info)?; permit_info.close(signer_info)?;
} }
@@ -64,7 +66,7 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
disc: OreEvent::Uncommit as u64, disc: OreEvent::Uncommit as u64,
authority: *signer_info.key, authority: *signer_info.key,
block_id: block.id, block_id: block.id,
commitment: permit.amount, commitment: permit.commitment,
amount, amount,
ts: clock.unix_timestamp, ts: clock.unix_timestamp,
} }

View File

@@ -36,7 +36,7 @@ pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
} }
// Update stake state. // Update stake state.
stake.capacity -= amount; stake.collateral -= amount;
// Transfer collateral. // Transfer collateral.
transfer_signed( transfer_signed(
@@ -49,7 +49,7 @@ pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
)?; )?;
// Close stake account, if empty. // Close stake account, if empty.
if stake.capacity == 0 { if stake.collateral == 0 {
stake_info.close(signer_info)?; stake_info.close(signer_info)?;
} }
@@ -59,7 +59,7 @@ pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
authority: *signer_info.key, authority: *signer_info.key,
block_id: stake.block_id, block_id: stake.block_id,
amount, amount,
capacity: stake.capacity, collateral: stake.collateral,
ts: clock.unix_timestamp, ts: clock.unix_timestamp,
} }
.log_return(); .log_return();