mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-20 23:16:46 +00:00
sdk, miner
This commit is contained in:
@@ -68,6 +68,7 @@ pub struct Swap {
|
||||
pub amount: [u8; 8],
|
||||
pub direction: u8,
|
||||
pub precision: u8,
|
||||
pub seed: [u8; 32],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
|
||||
118
api/src/sdk.rs
118
api/src/sdk.rs
@@ -21,26 +21,41 @@ pub fn program_log(accounts: &[AccountInfo], msg: &[u8]) -> Result<(), ProgramEr
|
||||
invoke_signed(&log(*accounts[0].key, msg), accounts, &crate::ID, &[MARKET])
|
||||
}
|
||||
|
||||
pub fn initialize(signer: Pubkey) -> Instruction {
|
||||
let config_address = config_pda().0;
|
||||
let market_address = market_pda().0;
|
||||
let mint_address = MINT_ADDRESS;
|
||||
let treasury_address = TREASURY_ADDRESS;
|
||||
let treasury_tokens_address = treasury_tokens_address();
|
||||
let vault_address = vault_pda().0;
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(config_address, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(mint_address, false),
|
||||
AccountMeta::new(treasury_address, false),
|
||||
AccountMeta::new(treasury_tokens_address, false),
|
||||
AccountMeta::new(vault_address, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
|
||||
],
|
||||
data: Initialize {}.to_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mine(signer: Pubkey, authority: Pubkey, id: u64, nonce: u64) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let market_address = market_pda().0;
|
||||
let miner_address = miner_pda(authority).0;
|
||||
let recipient = get_associated_token_address(&authority, &MINT_ADDRESS);
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(authority, false),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(miner_address, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(recipient, false),
|
||||
AccountMeta::new(TREASURY_ADDRESS, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(crate::ID, false),
|
||||
AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
|
||||
],
|
||||
data: Mine {
|
||||
nonce: nonce.to_le_bytes(),
|
||||
@@ -49,15 +64,94 @@ pub fn mine(signer: Pubkey, authority: Pubkey, id: u64, nonce: u64) -> Instructi
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open(signer: Pubkey, id: u64) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let market_address = market_pda().0;
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
],
|
||||
data: Open {
|
||||
id: id.to_le_bytes(),
|
||||
}
|
||||
.to_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(signer: Pubkey, opener: Pubkey, winner: Pubkey, id: u64) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let miner_tokens_address = get_associated_token_address(&winner, &MINT_ADDRESS);
|
||||
let mint_address = MINT_ADDRESS;
|
||||
let treasury_address = TREASURY_ADDRESS;
|
||||
let treasury_tokens_address = treasury_tokens_address();
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(winner, false),
|
||||
AccountMeta::new(miner_tokens_address, false),
|
||||
AccountMeta::new(mint_address, false),
|
||||
AccountMeta::new(opener, false),
|
||||
AccountMeta::new(treasury_address, false),
|
||||
AccountMeta::new(treasury_tokens_address, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
|
||||
],
|
||||
data: Close {}.to_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset(signer: Pubkey, fee_collector: Pubkey, id: u64) -> Instruction {
|
||||
let block_prev_adddress = block_pda(id).0;
|
||||
let block_next_adddress = block_pda(id + 1).0;
|
||||
let config_address = config_pda().0;
|
||||
let market_address = market_pda().0;
|
||||
let mint_address = MINT_ADDRESS;
|
||||
let treasury_address = TREASURY_ADDRESS;
|
||||
let treasury_tokens_address = treasury_tokens_address();
|
||||
let vault_address = vault_pda().0;
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_prev_adddress, false),
|
||||
AccountMeta::new(block_next_adddress, false),
|
||||
AccountMeta::new_readonly(config_address, false),
|
||||
AccountMeta::new(fee_collector, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(mint_address, false),
|
||||
AccountMeta::new(treasury_address, false),
|
||||
AccountMeta::new(treasury_tokens_address, false),
|
||||
AccountMeta::new(vault_address, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(crate::ID, false),
|
||||
AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
|
||||
],
|
||||
data: Reset {}.to_bytes(),
|
||||
}
|
||||
}
|
||||
// let [signer_info, block_info, config_info, fee_collector_info, market_info, miner_info, mint_info, tokens_info, vault_info, system_program, token_program, associated_token_program, ore_program] =
|
||||
|
||||
pub fn swap(
|
||||
signer: Pubkey,
|
||||
id: u64,
|
||||
fee_collector: Pubkey,
|
||||
amount: u64,
|
||||
direction: SwapDirection,
|
||||
precision: SwapPrecision,
|
||||
seed: [u8; 32],
|
||||
) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let config_address = config_pda().0;
|
||||
let market_address = market_pda().0;
|
||||
let miner_address = miner_pda(signer).0;
|
||||
let tokens_quote_address = get_associated_token_address(&signer, &MINT_ADDRESS);
|
||||
let vault_address = vault_pda().0;
|
||||
Instruction {
|
||||
@@ -65,7 +159,10 @@ pub fn swap(
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(config_address, false),
|
||||
AccountMeta::new(fee_collector, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(miner_address, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(tokens_quote_address, false),
|
||||
AccountMeta::new(vault_address, false),
|
||||
@@ -78,6 +175,7 @@ pub fn swap(
|
||||
amount: amount.to_le_bytes(),
|
||||
direction: direction as u8,
|
||||
precision: precision as u8,
|
||||
seed: seed,
|
||||
}
|
||||
.to_bytes(),
|
||||
}
|
||||
|
||||
@@ -13,9 +13,6 @@ pub struct Miner {
|
||||
/// The ID of the last block this miner mined in.
|
||||
pub block_id: u64,
|
||||
|
||||
/// The hash of the last block this miner mined in.
|
||||
pub hash: [u8; 32],
|
||||
|
||||
/// The amount of hashpower this miner has committed to the current block.
|
||||
pub hashpower: u64,
|
||||
|
||||
|
||||
@@ -55,3 +55,7 @@ pub fn vault_pda() -> (Pubkey, u8) {
|
||||
pub fn treasury_pda() -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[TREASURY], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn treasury_tokens_address() -> Pubkey {
|
||||
spl_associated_token_account::get_associated_token_address(&TREASURY_ADDRESS, &MINT_ADDRESS)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user