protobook-api 0.1.4

API for interacting with the Protobook program
Documentation
use spl_associated_token_account::get_associated_token_address;
use steel::*;

use crate::prelude::*;

// let [signer_info, order_info] = accounts else {

pub fn cancel(authority: Pubkey, order: Pubkey) -> Instruction {
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(order, false),
        ],
        data: Cancel {}.to_bytes(),
    }
}

// let [signer_info, beneficiary_a_info, beneficiary_b_info, mint_a_info, mint_b_info, order_info, vault_a_info, vault_b_info, system_program, token_program, associated_token_program] =

pub fn close(authority: Pubkey, order: Pubkey, mint_a: Pubkey, mint_b: Pubkey) -> Instruction {
    let beneficiary_a = get_associated_token_address(&authority, &mint_a);
    let beneficiary_b = get_associated_token_address(&authority, &mint_b);
    let vault_a = get_associated_token_address(&order, &mint_a);
    let vault_b = get_associated_token_address(&order, &mint_b);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(beneficiary_a, false),
            AccountMeta::new(beneficiary_b, false),
            AccountMeta::new(mint_a, false),
            AccountMeta::new(mint_b, false),
            AccountMeta::new(order, false),
            AccountMeta::new(vault_a, false),
            AccountMeta::new(vault_b, 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(),
    }
}

// let [signer_info, beneficiary_info, fee_collector_info, mint_info, order_info, vault_info, system_program, token_program, associated_token_program] =

pub fn collect(authority: Pubkey, beneficiary: Pubkey, order: Pubkey, mint: Pubkey) -> Instruction {
    let vault = get_associated_token_address(&order, &mint);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(beneficiary, false),
            AccountMeta::new(Pubkey::default(), false),
            AccountMeta::new(mint, false),
            AccountMeta::new(order, false),
            AccountMeta::new(vault, 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: Collect {}.to_bytes(),
    }
}

// let [signer_info, order_info, receipt_info, sender_info, vault_b_info, system_program, token_program] =

pub fn fill(authority: Pubkey, order: Pubkey, mint_b: Pubkey, amount: u64) -> Instruction {
    let vault_b = get_associated_token_address(&order, &mint_b);
    let receipt_address = receipt_pda(authority, order).0;
    let sender = get_associated_token_address(&authority, &mint_b);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(order, false),
            AccountMeta::new(receipt_address, false),
            AccountMeta::new(sender, false),
            AccountMeta::new(vault_b, false),
            AccountMeta::new_readonly(system_program::ID, false),
            AccountMeta::new_readonly(spl_token::ID, false),
        ],
        data: Fill {
            amount: amount.to_le_bytes(),
        }
        .to_bytes(),
    }
}

// let [signer_info, fee_collector_info, mint_a_info, mint_b_info, order_info, sender_info, vault_a_info, vault_b_info, system_program, token_program, associated_token_program] =

#[allow(deprecated)]
pub fn open(
    authority: Pubkey,
    amount_a: u64,
    amount_b: u64,
    expires_at: i64,
    id: u64,
    mint_a: Pubkey,
    mint_b: Pubkey,
) -> Instruction {
    let sender = get_associated_token_address(&authority, &mint_a);
    let order_address = order_pda(authority, id).0;
    let vault_a = get_associated_token_address(&order_address, &mint_a);
    let vault_b = get_associated_token_address(&order_address, &mint_b);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(Pubkey::default(), false),
            AccountMeta::new(mint_a, false),
            AccountMeta::new(mint_b, false),
            AccountMeta::new(order_address, false),
            AccountMeta::new(sender, false),
            AccountMeta::new(vault_a, false),
            AccountMeta::new(vault_b, 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: Open {
            amount_a: amount_a.to_le_bytes(),
            amount_b: amount_b.to_le_bytes(),
            expires_at: expires_at.to_le_bytes(),
            fee: 0u64.to_le_bytes(),
            id: id.to_le_bytes(),
        }
        .to_bytes(),
    }
}

// let [signer_info, beneficiary_info, mint_info, order_info, receipt_info, vault_info, system_program, token_program, associated_token_program] =

pub fn redeem(authority: Pubkey, beneficiary: Pubkey, order: Pubkey, mint: Pubkey) -> Instruction {
    let receipt_address = receipt_pda(authority, order).0;
    let vault = get_associated_token_address(&order, &mint);
    Instruction {
        program_id: crate::ID,
        accounts: vec![
            AccountMeta::new(authority, true),
            AccountMeta::new(beneficiary, false),
            AccountMeta::new(mint, false),
            AccountMeta::new(order, false),
            AccountMeta::new(receipt_address, false),
            AccountMeta::new(vault, 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: Redeem {}.to_bytes(),
    }
}