mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
collateral
This commit is contained in:
@@ -18,8 +18,8 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account::<Block>(&ore_api::ID)?
|
||||
.assert(|b| clock.slot < b.start_slot)?;
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| clock.slot < b.start_slot)?;
|
||||
commitment_info.as_associated_token_account(block_info.key, mint_info.key)?;
|
||||
let market = market_info
|
||||
.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);
|
||||
|
||||
// Load miner account.
|
||||
let _miner = if miner_info.data_is_empty() {
|
||||
let miner = if miner_info.data_is_empty() {
|
||||
create_program_account::<Miner>(
|
||||
miner_info,
|
||||
system_program,
|
||||
@@ -47,7 +47,8 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
|
||||
miner.authority = *signer_info.key;
|
||||
miner.block_id = 0;
|
||||
miner.hash = [0; 32];
|
||||
miner.total_hashes = 0;
|
||||
miner.total_committed = 0;
|
||||
miner.total_deployed = 0;
|
||||
miner.total_rewards = 0;
|
||||
miner
|
||||
} 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()],
|
||||
)?;
|
||||
let permit = permit_info.as_account_mut::<Permit>(&ore_api::ID)?;
|
||||
permit.amount = 0;
|
||||
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
|
||||
@@ -92,7 +96,9 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
|
||||
)?;
|
||||
|
||||
// Update block.
|
||||
permit.amount += amount;
|
||||
permit.commitment += amount;
|
||||
miner.total_committed += amount;
|
||||
block.total_committed += amount;
|
||||
|
||||
// Emit event.
|
||||
CommitEvent {
|
||||
@@ -100,7 +106,7 @@ pub fn process_commit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResul
|
||||
authority: *signer_info.key,
|
||||
block_id: block.id,
|
||||
amount,
|
||||
commitment: permit.amount,
|
||||
commitment: permit.commitment,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.log_return();
|
||||
|
||||
@@ -41,8 +41,8 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
|
||||
let stake = stake_info.as_account_mut::<Stake>(&ore_api::ID)?;
|
||||
stake.authority = *signer_info.key;
|
||||
stake.block_id = block.id;
|
||||
stake.capacity = 0;
|
||||
stake.utilization = 0;
|
||||
stake.collateral = 0;
|
||||
stake.spend = 0;
|
||||
stake
|
||||
} else {
|
||||
stake_info
|
||||
@@ -52,7 +52,7 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
|
||||
};
|
||||
|
||||
// Update stake state.
|
||||
stake.capacity += amount;
|
||||
stake.collateral += amount;
|
||||
|
||||
// Transfer collateral.
|
||||
transfer(
|
||||
@@ -69,7 +69,7 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
|
||||
authority: *signer_info.key,
|
||||
block_id: block.id,
|
||||
amount,
|
||||
capacity: stake.capacity,
|
||||
collateral: stake.collateral,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.log_return();
|
||||
|
||||
@@ -50,8 +50,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Reduce permit amount.
|
||||
let amount = permit.amount.min(amount);
|
||||
permit.amount -= amount;
|
||||
let amount = permit.commitment.min(amount);
|
||||
permit.commitment -= amount;
|
||||
|
||||
// Pay executor fee.
|
||||
if permit.fee > 0 {
|
||||
@@ -59,7 +59,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
}
|
||||
|
||||
// Close permit account, if empty.
|
||||
if permit.amount == 0 {
|
||||
if permit.commitment == 0 {
|
||||
permit_info.close(authority_info)?;
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
let mut nugget_reward = 0;
|
||||
for _ in 0..amount {
|
||||
// Update stats
|
||||
block.total_hashes += 1;
|
||||
miner.total_hashes += 1;
|
||||
block.total_deployed += 1;
|
||||
miner.total_deployed += 1;
|
||||
|
||||
// Generate hash.
|
||||
miner.hash = hash(miner.hash.as_ref());
|
||||
|
||||
@@ -78,7 +78,9 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
};
|
||||
block.slot_hash = [0; 32];
|
||||
block.start_slot = start_slot;
|
||||
block.total_hashes = 0;
|
||||
block.total_committed = 0;
|
||||
block.total_deployed = 0;
|
||||
block.total_rewards = 0;
|
||||
|
||||
// Select reward strategy.
|
||||
let noise_seed = block.id.to_le_bytes();
|
||||
|
||||
@@ -105,16 +105,16 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
// TODO Fail if out_amount is zero
|
||||
stake.utilization += in_amount;
|
||||
stake.spend += in_amount;
|
||||
}
|
||||
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.
|
||||
if stake.utilization > stake.capacity {
|
||||
panic!("utilization is greater than capacity");
|
||||
if stake.spend > stake.collateral {
|
||||
panic!("spend is greater than collateral");
|
||||
}
|
||||
|
||||
// Transfer tokens.
|
||||
|
||||
@@ -16,17 +16,17 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account::<Block>(&ore_api::ID)?
|
||||
.assert(|b| clock.slot < b.start_slot)?;
|
||||
.as_account_mut::<Block>(&ore_api::ID)?
|
||||
.assert_mut(|b| clock.slot < b.start_slot)?;
|
||||
commitment_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(block_info.key, mint_info.key)?;
|
||||
let market = market_info
|
||||
.as_account::<Market>(&ore_api::ID)?
|
||||
.assert(|m| m.id == block.id)?;
|
||||
miner_info
|
||||
.as_account::<Miner>(&ore_api::ID)?
|
||||
.assert(|m| m.authority == *signer_info.key)?;
|
||||
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)?
|
||||
@@ -39,7 +39,7 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Normalize amount.
|
||||
let amount = permit.amount.min(amount);
|
||||
let amount = permit.commitment.min(amount);
|
||||
|
||||
// Transfer hash tokens.
|
||||
transfer_signed(
|
||||
@@ -52,10 +52,12 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
)?;
|
||||
|
||||
// Update block.
|
||||
permit.amount -= amount;
|
||||
permit.commitment -= amount;
|
||||
miner.total_committed -= amount;
|
||||
block.total_committed -= amount;
|
||||
|
||||
// Close permit account, if empty.
|
||||
if permit.amount == 0 {
|
||||
if permit.commitment == 0 {
|
||||
permit_info.close(signer_info)?;
|
||||
}
|
||||
|
||||
@@ -64,7 +66,7 @@ pub fn process_uncommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
disc: OreEvent::Uncommit as u64,
|
||||
authority: *signer_info.key,
|
||||
block_id: block.id,
|
||||
commitment: permit.amount,
|
||||
commitment: permit.commitment,
|
||||
amount,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
}
|
||||
|
||||
// Update stake state.
|
||||
stake.capacity -= amount;
|
||||
stake.collateral -= amount;
|
||||
|
||||
// Transfer collateral.
|
||||
transfer_signed(
|
||||
@@ -49,7 +49,7 @@ pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
)?;
|
||||
|
||||
// Close stake account, if empty.
|
||||
if stake.capacity == 0 {
|
||||
if stake.collateral == 0 {
|
||||
stake_info.close(signer_info)?;
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ pub fn process_withdraw(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
authority: *signer_info.key,
|
||||
block_id: stake.block_id,
|
||||
amount,
|
||||
capacity: stake.capacity,
|
||||
collateral: stake.collateral,
|
||||
ts: clock.unix_timestamp,
|
||||
}
|
||||
.log_return();
|
||||
|
||||
Reference in New Issue
Block a user