This commit is contained in:
Hardhat Chad
2025-06-13 13:20:24 -07:00
parent f487593ba2
commit 1e4e9d9fdf
4 changed files with 70 additions and 30 deletions

View File

@@ -93,6 +93,34 @@ pub fn mine(signer: Pubkey, id: u64, amount: u64) -> Instruction {
}
}
pub fn deposit(signer: Pubkey, id: u64, amount: u64) -> Instruction {
let block_adddress = block_pda(id).0;
let collateral_address = get_associated_token_address(&block_adddress, &MINT_ADDRESS);
let stake_address = stake_pda(signer, id).0;
let sender = get_associated_token_address(&signer, &MINT_ADDRESS);
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(block_adddress, false),
AccountMeta::new(collateral_address, false),
AccountMeta::new(MINT_ADDRESS, false),
AccountMeta::new(sender, false),
AccountMeta::new(stake_address, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(spl_token::ID, false),
],
data: Deposit {
amount: amount.to_le_bytes(),
}
.to_bytes(),
}
}
// let [signer_info, block_info, collateral_info, mint_ore_info, sender_info, stake_info, system_program, token_program] =
// 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] =
pub fn swap(
signer: Pubkey,
id: u64,
@@ -103,6 +131,8 @@ pub fn swap(
let block_adddress = block_pda(id).0;
let market_address = market_pda(id).0;
let base_mint_address = mint_pda(id).0;
let stake_address = stake_pda(signer, id).0;
let collateral_address = get_associated_token_address(&block_adddress, &MINT_ADDRESS);
let tokens_base_address = get_associated_token_address(&signer, &base_mint_address);
let tokens_quote_address = get_associated_token_address(&signer, &MINT_ADDRESS);
let vault_base_address = get_associated_token_address(&market_address, &base_mint_address);
@@ -112,9 +142,11 @@ pub fn swap(
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(block_adddress, false),
AccountMeta::new(collateral_address, false),
AccountMeta::new(market_address, false),
AccountMeta::new(base_mint_address, false),
AccountMeta::new(MINT_ADDRESS, false),
AccountMeta::new(stake_address, false),
AccountMeta::new(tokens_base_address, false),
AccountMeta::new(tokens_quote_address, false),
AccountMeta::new(vault_base_address, false),

View File

@@ -42,6 +42,12 @@ async fn main() {
"blocks" => {
log_blocks(&rpc).await.unwrap();
}
"deposit" => {
deposit(&rpc, &payer).await.unwrap();
}
"swap" => {
swap(&rpc, &payer).await.unwrap();
}
_ => panic!("Invalid command"),
};
}
@@ -68,6 +74,34 @@ async fn close(
Ok(())
}
async fn deposit(
rpc: &RpcClient,
payer: &solana_sdk::signer::keypair::Keypair,
) -> Result<(), anyhow::Error> {
let id_str = std::env::var("ID").expect("Missing ID env var");
let id = id_str.parse::<u64>()?;
let ix = ore_api::sdk::deposit(payer.pubkey(), id, 10000000);
submit_transaction(rpc, payer, &[ix]).await?;
Ok(())
}
async fn swap(
rpc: &RpcClient,
payer: &solana_sdk::signer::keypair::Keypair,
) -> Result<(), anyhow::Error> {
let id_str = std::env::var("ID").expect("Missing ID env var");
let id = id_str.parse::<u64>()?;
let ix = ore_api::sdk::swap(
payer.pubkey(),
id,
10000000,
SwapDirection::Buy,
SwapPrecision::ExactIn,
);
submit_transaction(rpc, payer, &[ix]).await?;
Ok(())
}
async fn log_clock(rpc: &RpcClient) -> Result<(), anyhow::Error> {
let clock = get_clock(&rpc).await?;
println!("Clock: {:?}", clock);

View File

@@ -56,8 +56,8 @@ pub fn process_deposit(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
// Transfer collateral.
transfer(
sender_info,
signer_info,
sender_info,
collateral_info,
token_program,
amount,

View File

@@ -30,7 +30,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
.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
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)?;
@@ -44,28 +44,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
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(
@@ -127,6 +105,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Update stake state.
match direction {
SwapDirection::Buy => {
// TODO Fail if out_amount is zero
stake.utilization += in_amount;
}
SwapDirection::Sell => {
@@ -147,12 +126,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
out_to,
token_program,
out_amount,
&[
MARKET,
market.base.mint.as_ref(),
market.quote.mint.as_ref(),
market.id.to_le_bytes().as_ref(),
],
&[MARKET, market.id.to_le_bytes().as_ref()],
)?;
// Validate vault reserves.