Remove Instruction wrapper structs and name functions after enum fields
This commit is contained in:
@ -59,89 +59,88 @@ pub enum ExchangeInstruction {
|
||||
/// key 6 - Token account in which to deposit the brokers profit from the swap.
|
||||
SwapRequest,
|
||||
}
|
||||
impl ExchangeInstruction {
|
||||
pub fn new_account_request(owner: &Pubkey, new: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*new, false),
|
||||
];
|
||||
Instruction::new(id(), &ExchangeInstruction::AccountRequest, account_metas)
|
||||
}
|
||||
|
||||
pub fn new_transfer_request(
|
||||
owner: &Pubkey,
|
||||
to: &Pubkey,
|
||||
from: &Pubkey,
|
||||
token: Token,
|
||||
tokens: u64,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*to, false),
|
||||
AccountMeta::new(*from, false),
|
||||
];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&ExchangeInstruction::TransferRequest(token, tokens),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_trade_request(
|
||||
owner: &Pubkey,
|
||||
trade: &Pubkey,
|
||||
direction: Direction,
|
||||
pair: TokenPair,
|
||||
tokens: u64,
|
||||
price: u64,
|
||||
src_account: &Pubkey,
|
||||
dst_account: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*trade, false),
|
||||
AccountMeta::new(*src_account, false),
|
||||
];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&ExchangeInstruction::TradeRequest(TradeRequestInfo {
|
||||
direction,
|
||||
pair,
|
||||
tokens,
|
||||
price,
|
||||
dst_account: *dst_account,
|
||||
}),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_trade_cancellation(owner: &Pubkey, trade: &Pubkey, account: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*trade, false),
|
||||
AccountMeta::new(*account, false),
|
||||
];
|
||||
Instruction::new(id(), &ExchangeInstruction::TradeCancellation, account_metas)
|
||||
}
|
||||
|
||||
pub fn new_swap_request(
|
||||
owner: &Pubkey,
|
||||
swap: &Pubkey,
|
||||
to_trade: &Pubkey,
|
||||
from_trade: &Pubkey,
|
||||
to_trade_account: &Pubkey,
|
||||
from_trade_account: &Pubkey,
|
||||
profit_account: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*swap, false),
|
||||
AccountMeta::new(*to_trade, false),
|
||||
AccountMeta::new(*from_trade, false),
|
||||
AccountMeta::new(*to_trade_account, false),
|
||||
AccountMeta::new(*from_trade_account, false),
|
||||
AccountMeta::new(*profit_account, false),
|
||||
];
|
||||
Instruction::new(id(), &ExchangeInstruction::SwapRequest, account_metas)
|
||||
}
|
||||
pub fn account_request(owner: &Pubkey, new: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*new, false),
|
||||
];
|
||||
Instruction::new(id(), &ExchangeInstruction::AccountRequest, account_metas)
|
||||
}
|
||||
|
||||
pub fn transfer_request(
|
||||
owner: &Pubkey,
|
||||
to: &Pubkey,
|
||||
from: &Pubkey,
|
||||
token: Token,
|
||||
tokens: u64,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*to, false),
|
||||
AccountMeta::new(*from, false),
|
||||
];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&ExchangeInstruction::TransferRequest(token, tokens),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn trade_request(
|
||||
owner: &Pubkey,
|
||||
trade: &Pubkey,
|
||||
direction: Direction,
|
||||
pair: TokenPair,
|
||||
tokens: u64,
|
||||
price: u64,
|
||||
src_account: &Pubkey,
|
||||
dst_account: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*trade, false),
|
||||
AccountMeta::new(*src_account, false),
|
||||
];
|
||||
Instruction::new(
|
||||
id(),
|
||||
&ExchangeInstruction::TradeRequest(TradeRequestInfo {
|
||||
direction,
|
||||
pair,
|
||||
tokens,
|
||||
price,
|
||||
dst_account: *dst_account,
|
||||
}),
|
||||
account_metas,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn trade_cancellation(owner: &Pubkey, trade: &Pubkey, account: &Pubkey) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*trade, false),
|
||||
AccountMeta::new(*account, false),
|
||||
];
|
||||
Instruction::new(id(), &ExchangeInstruction::TradeCancellation, account_metas)
|
||||
}
|
||||
|
||||
pub fn swap_request(
|
||||
owner: &Pubkey,
|
||||
swap: &Pubkey,
|
||||
to_trade: &Pubkey,
|
||||
from_trade: &Pubkey,
|
||||
to_trade_account: &Pubkey,
|
||||
from_trade_account: &Pubkey,
|
||||
profit_account: &Pubkey,
|
||||
) -> Instruction {
|
||||
let account_metas = vec![
|
||||
AccountMeta::new(*owner, true),
|
||||
AccountMeta::new(*swap, false),
|
||||
AccountMeta::new(*to_trade, false),
|
||||
AccountMeta::new(*from_trade, false),
|
||||
AccountMeta::new(*to_trade_account, false),
|
||||
AccountMeta::new(*from_trade_account, false),
|
||||
AccountMeta::new(*profit_account, false),
|
||||
];
|
||||
Instruction::new(id(), &ExchangeInstruction::SwapRequest, account_metas)
|
||||
}
|
||||
|
@ -412,11 +412,12 @@ pub fn process_instruction(
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::exchange_instruction;
|
||||
use solana_runtime::bank::Bank;
|
||||
use solana_runtime::bank_client::BankClient;
|
||||
use solana_sdk::genesis_block::GenesisBlock;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
use solana_sdk::system_instruction;
|
||||
use std::mem;
|
||||
|
||||
fn try_calc(
|
||||
@ -527,7 +528,7 @@ mod test {
|
||||
|
||||
fn create_account(client: &BankClient, owner: &Keypair) -> Pubkey {
|
||||
let new = Pubkey::new_rand();
|
||||
let instruction = SystemInstruction::new_account(
|
||||
let instruction = system_instruction::create_account(
|
||||
&owner.pubkey(),
|
||||
&new,
|
||||
1,
|
||||
@ -542,7 +543,7 @@ mod test {
|
||||
|
||||
fn create_token_account(client: &BankClient, owner: &Keypair) -> Pubkey {
|
||||
let new = Pubkey::new_rand();
|
||||
let instruction = SystemInstruction::new_account(
|
||||
let instruction = system_instruction::create_account(
|
||||
&owner.pubkey(),
|
||||
&new,
|
||||
1,
|
||||
@ -552,7 +553,7 @@ mod test {
|
||||
client
|
||||
.process_instruction(owner, instruction)
|
||||
.expect(&format!("{}:{}", line!(), file!()));
|
||||
let instruction = ExchangeInstruction::new_account_request(&owner.pubkey(), &new);
|
||||
let instruction = exchange_instruction::account_request(&owner.pubkey(), &new);
|
||||
client
|
||||
.process_instruction(owner, instruction)
|
||||
.expect(&format!("{}:{}", line!(), file!()));
|
||||
@ -561,7 +562,7 @@ mod test {
|
||||
|
||||
fn transfer(client: &BankClient, owner: &Keypair, to: &Pubkey, token: Token, tokens: u64) {
|
||||
let instruction =
|
||||
ExchangeInstruction::new_transfer_request(&owner.pubkey(), to, &id(), token, tokens);
|
||||
exchange_instruction::transfer_request(&owner.pubkey(), to, &id(), token, tokens);
|
||||
client
|
||||
.process_instruction(owner, instruction)
|
||||
.expect(&format!("{}:{}", line!(), file!()));
|
||||
@ -582,7 +583,7 @@ mod test {
|
||||
let dst = create_token_account(&client, &owner);
|
||||
transfer(&client, &owner, &src, from_token, src_tokens);
|
||||
|
||||
let instruction = ExchangeInstruction::new_trade_request(
|
||||
let instruction = exchange_instruction::trade_request(
|
||||
&owner.pubkey(),
|
||||
&trade,
|
||||
direction,
|
||||
@ -633,7 +634,7 @@ mod test {
|
||||
let (client, owner) = create_client(&bank, mint_keypair);
|
||||
|
||||
let new = create_token_account(&client, &owner);
|
||||
let instruction = ExchangeInstruction::new_account_request(&owner.pubkey(), &new);
|
||||
let instruction = exchange_instruction::account_request(&owner.pubkey(), &new);
|
||||
client
|
||||
.process_instruction(&owner, instruction)
|
||||
.expect_err(&format!("{}:{}", line!(), file!()));
|
||||
@ -648,7 +649,7 @@ mod test {
|
||||
let new = create_token_account(&client, &owner);
|
||||
|
||||
let instruction =
|
||||
ExchangeInstruction::new_transfer_request(&owner.pubkey(), &new, &id(), Token::A, 42);
|
||||
exchange_instruction::transfer_request(&owner.pubkey(), &new, &id(), Token::A, 42);
|
||||
client
|
||||
.process_instruction(&owner, instruction)
|
||||
.expect(&format!("{}:{}", line!(), file!()));
|
||||
@ -743,7 +744,7 @@ mod test {
|
||||
3000,
|
||||
);
|
||||
|
||||
let instruction = ExchangeInstruction::new_swap_request(
|
||||
let instruction = exchange_instruction::swap_request(
|
||||
&owner.pubkey(),
|
||||
&swap,
|
||||
&to_trade,
|
||||
|
@ -1,120 +1,103 @@
|
||||
use crate::exchange_instruction::*;
|
||||
use crate::exchange_instruction;
|
||||
use crate::exchange_state::*;
|
||||
use crate::id;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
use solana_sdk::system_instruction;
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use std::mem;
|
||||
|
||||
pub struct ExchangeTransaction {}
|
||||
|
||||
impl ExchangeTransaction {
|
||||
pub fn new_account_request(
|
||||
owner: &Keypair,
|
||||
new: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let space = mem::size_of::<ExchangeState>() as u64;
|
||||
let create_ix = SystemInstruction::new_account(owner_id, new, 1, space, &id());
|
||||
let request_ix = ExchangeInstruction::new_account_request(owner_id, new);
|
||||
Transaction::new_signed_instructions(
|
||||
&[owner],
|
||||
vec![create_ix, request_ix],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_transfer_request(
|
||||
owner: &Keypair,
|
||||
to: &Pubkey,
|
||||
from: &Pubkey,
|
||||
token: Token,
|
||||
tokens: u64,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let request_ix =
|
||||
ExchangeInstruction::new_transfer_request(owner_id, to, from, token, tokens);
|
||||
Transaction::new_signed_instructions(&[owner], vec![request_ix], recent_blockhash)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new_trade_request(
|
||||
owner: &Keypair,
|
||||
trade: &Pubkey,
|
||||
direction: Direction,
|
||||
pair: TokenPair,
|
||||
tokens: u64,
|
||||
price: u64,
|
||||
src_account: &Pubkey,
|
||||
dst_account: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let space = mem::size_of::<ExchangeState>() as u64;
|
||||
let create_ix = SystemInstruction::new_account(owner_id, trade, 1, space, &id());
|
||||
let request_ix = ExchangeInstruction::new_trade_request(
|
||||
owner_id,
|
||||
trade,
|
||||
direction,
|
||||
pair,
|
||||
tokens,
|
||||
price,
|
||||
src_account,
|
||||
dst_account,
|
||||
);
|
||||
Transaction::new_signed_instructions(
|
||||
&[owner],
|
||||
vec![create_ix, request_ix],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_trade_cancellation(
|
||||
owner: &Keypair,
|
||||
trade: &Pubkey,
|
||||
account: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let request_ix = ExchangeInstruction::new_trade_cancellation(owner_id, trade, account);
|
||||
Transaction::new_signed_instructions(&[owner], vec![request_ix], recent_blockhash)
|
||||
}
|
||||
|
||||
pub fn new_swap_request(
|
||||
owner: &Keypair,
|
||||
swap: &Pubkey,
|
||||
to_trade: &Pubkey,
|
||||
from_trade: &Pubkey,
|
||||
to_trade_account: &Pubkey,
|
||||
from_trade_account: &Pubkey,
|
||||
profit_account: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let space = mem::size_of::<ExchangeState>() as u64;
|
||||
let create_ix = SystemInstruction::new_account(owner_id, swap, 1, space, &id());
|
||||
let request_ix = ExchangeInstruction::new_swap_request(
|
||||
owner_id,
|
||||
swap,
|
||||
to_trade,
|
||||
from_trade,
|
||||
to_trade_account,
|
||||
from_trade_account,
|
||||
profit_account,
|
||||
);
|
||||
Transaction::new_signed_instructions(
|
||||
&[owner],
|
||||
vec![create_ix, request_ix],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
pub fn account_request(
|
||||
owner: &Keypair,
|
||||
new: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let space = mem::size_of::<ExchangeState>() as u64;
|
||||
let create_ix = system_instruction::create_account(owner_id, new, 1, space, &id());
|
||||
let request_ix = exchange_instruction::account_request(owner_id, new);
|
||||
Transaction::new_signed_instructions(&[owner], vec![create_ix, request_ix], recent_blockhash)
|
||||
}
|
||||
|
||||
pub fn transfer_request(
|
||||
owner: &Keypair,
|
||||
to: &Pubkey,
|
||||
from: &Pubkey,
|
||||
token: Token,
|
||||
tokens: u64,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let request_ix = exchange_instruction::transfer_request(owner_id, to, from, token, tokens);
|
||||
Transaction::new_signed_instructions(&[owner], vec![request_ix], recent_blockhash)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn trade_request(
|
||||
owner: &Keypair,
|
||||
trade: &Pubkey,
|
||||
direction: Direction,
|
||||
pair: TokenPair,
|
||||
tokens: u64,
|
||||
price: u64,
|
||||
src_account: &Pubkey,
|
||||
dst_account: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let space = mem::size_of::<ExchangeState>() as u64;
|
||||
let create_ix = system_instruction::create_account(owner_id, trade, 1, space, &id());
|
||||
let request_ix = exchange_instruction::trade_request(
|
||||
owner_id,
|
||||
trade,
|
||||
direction,
|
||||
pair,
|
||||
tokens,
|
||||
price,
|
||||
src_account,
|
||||
dst_account,
|
||||
);
|
||||
Transaction::new_signed_instructions(&[owner], vec![create_ix, request_ix], recent_blockhash)
|
||||
}
|
||||
|
||||
pub fn trade_cancellation(
|
||||
owner: &Keypair,
|
||||
trade: &Pubkey,
|
||||
account: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let request_ix = exchange_instruction::trade_cancellation(owner_id, trade, account);
|
||||
Transaction::new_signed_instructions(&[owner], vec![request_ix], recent_blockhash)
|
||||
}
|
||||
|
||||
pub fn swap_request(
|
||||
owner: &Keypair,
|
||||
swap: &Pubkey,
|
||||
to_trade: &Pubkey,
|
||||
from_trade: &Pubkey,
|
||||
to_trade_account: &Pubkey,
|
||||
from_trade_account: &Pubkey,
|
||||
profit_account: &Pubkey,
|
||||
recent_blockhash: Hash,
|
||||
_fee: u64,
|
||||
) -> Transaction {
|
||||
let owner_id = &owner.pubkey();
|
||||
let space = mem::size_of::<ExchangeState>() as u64;
|
||||
let create_ix = system_instruction::create_account(owner_id, swap, 1, space, &id());
|
||||
let request_ix = exchange_instruction::swap_request(
|
||||
owner_id,
|
||||
swap,
|
||||
to_trade,
|
||||
from_trade,
|
||||
to_trade_account,
|
||||
from_trade_account,
|
||||
profit_account,
|
||||
);
|
||||
Transaction::new_signed_instructions(&[owner], vec![create_ix, request_ix], recent_blockhash)
|
||||
}
|
||||
|
Reference in New Issue
Block a user