mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 15:10:13 +00:00
cleanup
This commit is contained in:
56
program/src/close.rs
Normal file
56
program/src/close.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Closes a block.
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, vault_base_info, vault_quote_info, system_program, token_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 + 1500)?;
|
||||
let market = market_info
|
||||
.as_account_mut::<Market>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.id == block.id)?;
|
||||
mint_base_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
let vault_base =
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
let vault_quote =
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Burn base liquidity.
|
||||
burn_signed(
|
||||
vault_base_info,
|
||||
mint_base_info,
|
||||
market_info,
|
||||
token_program,
|
||||
vault_base.amount(),
|
||||
&[MARKET, &market.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Burn quote liquidity.
|
||||
burn_signed(
|
||||
vault_quote_info,
|
||||
mint_quote_info,
|
||||
market_info,
|
||||
token_program,
|
||||
vault_quote.amount(),
|
||||
&[MARKET, &market.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Close block.
|
||||
block_info.close(signer_info)?;
|
||||
|
||||
// Close market.
|
||||
market_info.close(signer_info)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
98
program/src/commit.rs
Normal file
98
program/src/commit.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
use ore_api::prelude::*;
|
||||
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, slot_hashes_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account::<Block>(&ore_api::ID)?
|
||||
.assert(|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)?
|
||||
.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)?;
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::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_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)?
|
||||
};
|
||||
|
||||
// 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.amount = 0;
|
||||
permit.authority = *signer_info.key;
|
||||
permit.block_id = block.id;
|
||||
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;
|
||||
|
||||
// Transfer hash tokens.
|
||||
transfer(
|
||||
signer_info,
|
||||
sender_info,
|
||||
commitment_info,
|
||||
token_program,
|
||||
amount,
|
||||
)?;
|
||||
|
||||
// Update block.
|
||||
permit.amount += amount;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
64
program/src/decommit.rs
Normal file
64
program/src/decommit.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Decommit from a block.
|
||||
pub fn process_decommit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Decommit::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, slot_hashes_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
let block = block_info
|
||||
.as_account::<Block>(&ore_api::ID)?
|
||||
.assert(|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)?;
|
||||
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)?;
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Normalize amount.
|
||||
let amount = permit.amount.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.amount -= amount;
|
||||
|
||||
// Close permit account, if empty.
|
||||
if permit.amount == 0 {
|
||||
permit_info.close(signer_info)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
67
program/src/deposit.rs
Normal file
67
program/src/deposit.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use ore_api::prelude::*;
|
||||
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] =
|
||||
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()?
|
||||
.as_associated_token_account(block_info.key, mint_ore_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)?;
|
||||
|
||||
// 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.capacity = 0;
|
||||
stake.utilization = 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.capacity += amount;
|
||||
|
||||
// Transfer collateral.
|
||||
transfer(
|
||||
sender_info,
|
||||
signer_info,
|
||||
collateral_info,
|
||||
token_program,
|
||||
amount,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
43
program/src/lib.rs
Normal file
43
program/src/lib.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
mod close;
|
||||
mod commit;
|
||||
mod decommit;
|
||||
mod deposit;
|
||||
mod mine;
|
||||
mod open;
|
||||
mod swap;
|
||||
mod withdraw;
|
||||
|
||||
use close::*;
|
||||
use commit::*;
|
||||
use decommit::*;
|
||||
use deposit::*;
|
||||
use mine::*;
|
||||
use open::*;
|
||||
use swap::*;
|
||||
use withdraw::*;
|
||||
|
||||
use ore_api::instruction::*;
|
||||
use steel::*;
|
||||
|
||||
pub fn process_instruction(
|
||||
program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
data: &[u8],
|
||||
) -> ProgramResult {
|
||||
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::Decommit => process_decommit(accounts, data)?,
|
||||
OreInstruction::Deposit => process_deposit(accounts, data)?,
|
||||
OreInstruction::Mine => process_mine(accounts, data)?,
|
||||
OreInstruction::Swap => process_swap(accounts, data)?,
|
||||
OreInstruction::Withdraw => process_withdraw(accounts, data)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
140
program/src/mine.rs
Normal file
140
program/src/mine.rs
Normal file
@@ -0,0 +1,140 @@
|
||||
use ore_api::prelude::*;
|
||||
use solana_nostd_keccak::hash;
|
||||
use solana_program::slot_hashes::SlotHashes;
|
||||
use steel::*;
|
||||
|
||||
/// Mine a block.
|
||||
pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Mine::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// 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, 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()?
|
||||
.as_associated_token_account(block_info.key, mint_hash_info.key)?;
|
||||
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()?;
|
||||
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)?;
|
||||
treasury_info.has_address(&TREASURY_ADDRESS)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Reduce permit amount.
|
||||
let amount = permit.amount.min(amount);
|
||||
permit.amount -= amount;
|
||||
|
||||
// Pay executor fee.
|
||||
if permit.fee > 0 {
|
||||
permit_info.send(permit.fee * amount, signer_info);
|
||||
}
|
||||
|
||||
// Close permit account, if empty.
|
||||
if permit.amount == 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()],
|
||||
)?;
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
// Reset miner hash if mining new block.
|
||||
if miner.block_id != block.id {
|
||||
miner.block_id = block.id;
|
||||
let mut args = [0u8; 96];
|
||||
args[..32].copy_from_slice(&block.id.to_le_bytes());
|
||||
args[32..64].copy_from_slice(&block.slot_hash);
|
||||
args[64..].copy_from_slice(&miner.authority.to_bytes());
|
||||
miner.hash = hash(&args);
|
||||
}
|
||||
|
||||
// Mine and accumulate rewards.
|
||||
let mut miner_reward = 0;
|
||||
for _ in 0..amount {
|
||||
// Update stats
|
||||
block.total_hashes += 1;
|
||||
miner.total_hashes += 1;
|
||||
|
||||
// Generate hash.
|
||||
miner.hash = hash(miner.hash.as_ref());
|
||||
|
||||
// Score and increment rewards.
|
||||
let score = difficulty(miner.hash) as u64;
|
||||
if score >= block.min_difficulty {
|
||||
block.winning_hashes += 1;
|
||||
miner.winning_hashes += 1;
|
||||
miner_reward += block.reward_rate;
|
||||
}
|
||||
}
|
||||
|
||||
// Payout ORE.
|
||||
block.total_rewards += miner_reward;
|
||||
miner.total_rewards += miner_reward;
|
||||
mint_to_signed(
|
||||
mint_ore_info,
|
||||
recipient_info,
|
||||
treasury_info,
|
||||
token_program,
|
||||
miner_reward,
|
||||
&[TREASURY],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the number of leading zeros on a 32 byte buffer.
|
||||
pub fn difficulty(hash: [u8; 32]) -> u32 {
|
||||
let mut count = 0;
|
||||
for &byte in &hash {
|
||||
let lz = byte.leading_zeros();
|
||||
count += lz;
|
||||
if lz < 8 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
196
program/src/open.rs
Normal file
196
program/src/open.rs
Normal file
@@ -0,0 +1,196 @@
|
||||
use ore_api::prelude::*;
|
||||
use solana_program::program_pack::Pack;
|
||||
use spl_token_2022::instruction::AuthorityType;
|
||||
use steel::*;
|
||||
|
||||
/// Opens a new block.
|
||||
pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Open::try_from_bytes(data)?;
|
||||
let id = u64::from_le_bytes(args.id);
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, collateral_info, commitment_info, market_info, mint_base_info, mint_quote_info, vault_base_info, vault_quote_info, system_program, token_program, associated_token_program, rent_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
block_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BLOCK, &id.to_le_bytes()], &ore_api::ID)?;
|
||||
market_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[MARKET, &id.to_le_bytes()], &ore_api::ID)?;
|
||||
mint_base_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[MINT, &id.to_le_bytes()], &ore_api::ID)?;
|
||||
mint_quote_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
associated_token_program.is_program(&spl_associated_token_account::ID)?;
|
||||
rent_sysvar.is_sysvar(&sysvar::rent::ID)?;
|
||||
|
||||
// Error out if start slot is within the current period.
|
||||
let start_slot = id * 1500;
|
||||
let current_period_start = (clock.slot / 1500) * 1500;
|
||||
let current_period_end = current_period_start + 1500;
|
||||
if start_slot < current_period_end {
|
||||
return Err(ProgramError::InvalidArgument);
|
||||
}
|
||||
|
||||
// Initialize config.
|
||||
create_program_account::<Block>(
|
||||
block_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
block.id = id;
|
||||
block.min_difficulty = MIN_DIFFICULTY;
|
||||
block.reward_rate = REWARD_RATE;
|
||||
block.slot_hash = [0; 32];
|
||||
block.start_slot = start_slot;
|
||||
block.total_hashes = 0;
|
||||
block.winning_hashes = 0;
|
||||
|
||||
// Initialize market.
|
||||
create_program_account::<Market>(
|
||||
market_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[MARKET, &id.to_le_bytes()],
|
||||
)?;
|
||||
let market = market_info.as_account_mut::<Market>(&ore_api::ID)?;
|
||||
market.base = TokenParams {
|
||||
mint: *mint_base_info.key,
|
||||
balance: HASH_TOKEN_SUPPLY,
|
||||
balance_virtual: 0,
|
||||
};
|
||||
market.quote = TokenParams {
|
||||
mint: *mint_quote_info.key,
|
||||
balance: 0,
|
||||
balance_virtual: VIRTUAL_LIQUIDITY,
|
||||
};
|
||||
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.id = id;
|
||||
|
||||
// Initialize hash token mint.
|
||||
let mint_bump = mint_pda(block.id).1;
|
||||
allocate_account_with_bump(
|
||||
mint_base_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
spl_token::state::Mint::LEN,
|
||||
&spl_token::ID,
|
||||
&[MINT, &id.to_le_bytes()],
|
||||
mint_bump,
|
||||
)?;
|
||||
initialize_mint_signed_with_bump(
|
||||
mint_base_info,
|
||||
block_info,
|
||||
None,
|
||||
token_program,
|
||||
rent_sysvar,
|
||||
0,
|
||||
&[MINT, &id.to_le_bytes()],
|
||||
mint_bump,
|
||||
)?;
|
||||
|
||||
// TODO Initialize hash token metadata.
|
||||
|
||||
// Initialize collateral and commitment token accounts.
|
||||
if collateral_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
block_info,
|
||||
collateral_info,
|
||||
mint_quote_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
collateral_info.as_associated_token_account(block_info.key, mint_quote_info.key)?;
|
||||
}
|
||||
if commitment_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
block_info,
|
||||
commitment_info,
|
||||
mint_base_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
commitment_info.as_associated_token_account(block_info.key, mint_base_info.key)?;
|
||||
}
|
||||
|
||||
// Initialize vault token accounts.
|
||||
if vault_base_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
market_info,
|
||||
vault_base_info,
|
||||
mint_base_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
}
|
||||
if vault_quote_info.data_is_empty() {
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
market_info,
|
||||
vault_quote_info,
|
||||
mint_quote_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
}
|
||||
|
||||
// Mint hash tokens to market.
|
||||
mint_to_signed(
|
||||
mint_base_info,
|
||||
vault_base_info,
|
||||
block_info,
|
||||
token_program,
|
||||
HASH_TOKEN_SUPPLY,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Burn mint authority.
|
||||
set_authority_signed(
|
||||
mint_base_info,
|
||||
block_info,
|
||||
None,
|
||||
AuthorityType::MintTokens,
|
||||
token_program,
|
||||
&[BLOCK, &id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
165
program/src/swap.rs
Normal file
165
program/src/swap.rs
Normal file
@@ -0,0 +1,165 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Swap in a hashpower market.
|
||||
pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse args.
|
||||
let args = Swap::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
let direction = SwapDirection::try_from(args.direction).unwrap();
|
||||
let precision = SwapPrecision::try_from(args.precision).unwrap();
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, collateral_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] =
|
||||
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)?;
|
||||
collateral_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(block_info.key, mint_quote_info.key)?;
|
||||
let market = market_info
|
||||
.as_account_mut::<Market>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.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()?;
|
||||
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
|
||||
.is_writable()?
|
||||
.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
vault_quote_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
associated_token_program.is_program(&spl_associated_token_account::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.capacity = 0;
|
||||
stake.utilization = 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)?
|
||||
};
|
||||
|
||||
// Load token acccounts.
|
||||
if tokens_base_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,
|
||||
mint_quote_info,
|
||||
system_program,
|
||||
token_program,
|
||||
associated_token_program,
|
||||
)?;
|
||||
} else {
|
||||
tokens_quote_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(signer_info.key, mint_quote_info.key)?;
|
||||
}
|
||||
|
||||
// Update market state.
|
||||
let mut swap_result = market.swap(amount, direction, precision, clock)?;
|
||||
swap_result.block_id = block.id;
|
||||
swap_result.log_return();
|
||||
|
||||
// Get transfer amounts and accounts.
|
||||
let (in_amount, in_from, in_to, out_amount, out_from, out_to) = match direction {
|
||||
SwapDirection::Buy => (
|
||||
swap_result.quote_to_transfer,
|
||||
tokens_quote_info,
|
||||
vault_quote_info,
|
||||
swap_result.base_to_transfer,
|
||||
vault_base_info,
|
||||
tokens_base_info,
|
||||
),
|
||||
SwapDirection::Sell => (
|
||||
swap_result.base_to_transfer,
|
||||
tokens_base_info,
|
||||
vault_base_info,
|
||||
swap_result.quote_to_transfer,
|
||||
vault_quote_info,
|
||||
tokens_quote_info,
|
||||
),
|
||||
};
|
||||
|
||||
// Update stake state.
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
stake.utilization += in_amount;
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
stake.utilization = stake.utilization.saturating_sub(out_amount);
|
||||
}
|
||||
}
|
||||
|
||||
// Assert utilization is not greater than capacity.
|
||||
if stake.utilization > stake.capacity {
|
||||
panic!("utilization is greater than capacity");
|
||||
}
|
||||
|
||||
// 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.base.mint.as_ref(),
|
||||
market.quote.mint.as_ref(),
|
||||
market.id.to_le_bytes().as_ref(),
|
||||
],
|
||||
)?;
|
||||
|
||||
// Validate vault reserves.
|
||||
let vault_base =
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
let vault_quote =
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
market.check_vaults(&vault_base, &vault_quote)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
57
program/src/withdraw.rs
Normal file
57
program/src/withdraw.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use ore_api::prelude::*;
|
||||
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] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?;
|
||||
collateral_info
|
||||
.is_writable()?
|
||||
.as_associated_token_account(block_info.key, mint_ore_info.key)?;
|
||||
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)?;
|
||||
block_info.has_seeds(&[BLOCK, &stake.block_id.to_le_bytes()], &ore_api::ID)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::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);
|
||||
}
|
||||
|
||||
// Update stake state.
|
||||
stake.capacity -= amount;
|
||||
|
||||
// Transfer collateral.
|
||||
transfer_signed(
|
||||
block_info,
|
||||
collateral_info,
|
||||
recipient_info,
|
||||
token_program,
|
||||
amount,
|
||||
&[BLOCK, &stake.block_id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Close stake account, if empty.
|
||||
if stake.capacity == 0 {
|
||||
stake_info.close(signer_info)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user