1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use anchor_lang::prelude::*;
use anchor_spl::token::TokenAccount;
use vipers::{assert_keys_eq, invariant};

use crate::{Action, Continuation, SwapActionEvent, TokenAmount};

pub trait ActionInputOutput<'info>: Action {
    fn input_account(&self) -> &Account<'info, TokenAccount>;
    fn output_account(&self) -> &Account<'info, TokenAccount>;
}

pub struct ActionContext<'a, 'b, 'c, 'info, T> {
    /// Currently executing program id.
    pub program_id: &'a Pubkey,
    /// Deserialized accounts.
    pub action: &'b T,
    /// Remaining accounts given but not deserialized or validated.
    /// Be very careful when using this directly.
    pub remaining_accounts: &'c [AccountInfo<'info>],
    /// The spl_token program.
    pub token_program: AccountInfo<'info>,
    /// The relevant swap program.
    pub swap_program: AccountInfo<'info>,
    /// The owner of all involved token accounts.
    pub owner: AccountInfo<'info>,
}

/// Processes a context.
pub trait Processor<'info>: ActionInputOutput<'info> {
    fn process_unchecked(&self, amount_in: u64, minimum_amount_out: u64) -> ProgramResult;

    fn process(&self, continuation: &mut Account<'info, Continuation>) -> ProgramResult {
        msg!("Router action: {:?}", Self::TYPE);
        let continuation = continuation;
        invariant!(continuation.steps_left > 0, NoMoreSteps);

        let input_account = self.input_account();
        assert_keys_eq!(input_account, continuation.input, PathInputOutputMismatch);
        assert_keys_eq!(input_account.owner, continuation.owner, InputOwnerMismatch);
        assert_keys_eq!(
            input_account.mint,
            continuation.amount_in.mint,
            InputMintMismatch
        );

        // ensure swap is non-zero
        let amount_in = continuation.amount_in;
        invariant!(amount_in.amount != 0, ZeroSwap);

        // ensure amount in is at least the desired amount
        invariant!(
            input_account.amount >= amount_in.amount,
            InsufficientInputBalance
        );

        // ensure output account is owned by the owner
        let output_account = self.output_account();
        assert_keys_eq!(
            output_account.owner,
            continuation.owner,
            OutputOwnerMismatch
        );

        // process step
        let initial_balance = output_account.amount;
        let minimum_amount_out = if continuation.steps_left == 1 {
            assert_keys_eq!(
                continuation.minimum_amount_out.mint,
                output_account.mint,
                OutputMintMismatch
            );
            continuation.minimum_amount_out.amount
        } else {
            0
        };
        self.process_unchecked(amount_in.amount, minimum_amount_out)?;
        let output_account = &mut output_account.clone();
        output_account.reload()?;
        let result_balance = output_account.amount;

        // ensure that the new balance is higher than the old balance
        invariant!(result_balance >= initial_balance, BalanceLower);
        let next_amount_in = result_balance - initial_balance;

        // write results
        continuation.input = output_account.key();
        continuation.amount_in = TokenAmount::new(output_account.mint, next_amount_in);
        continuation.steps_left -= 1;

        emit!(SwapActionEvent {
            action_type: Self::TYPE,
            owner: continuation.owner,
            input_amount: amount_in,
            output_account: continuation.input,
            output_amount: continuation.amount_in,
        });
        Ok(())
    }
}