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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! The `system_transaction` module provides functionality for creating system transactions.

use crate::hash::Hash;
use crate::pubkey::Pubkey;
use crate::signature::Keypair;
use crate::system_instruction::SystemInstruction;
use crate::system_program;
use crate::transaction::{Instruction, Transaction};

pub struct SystemTransaction {}

impl SystemTransaction {
    /// Create and sign new SystemInstruction::CreateAccount transaction
    pub fn new_program_account(
        from_keypair: &Keypair,
        to: &Pubkey,
        recent_blockhash: Hash,
        lamports: u64,
        space: u64,
        program_id: &Pubkey,
        fee: u64,
    ) -> Transaction {
        let create = SystemInstruction::CreateAccount {
            lamports, //TODO, the lamports to allocate might need to be higher then 0 in the future
            space,
            program_id: *program_id,
        };
        Transaction::new(
            from_keypair,
            &[*to],
            &system_program::id(),
            &create,
            recent_blockhash,
            fee,
        )
    }

    /// Create and sign a transaction to create a system account
    pub fn new_account(
        from_keypair: &Keypair,
        to: &Pubkey,
        lamports: u64,
        recent_blockhash: Hash,
        fee: u64,
    ) -> Transaction {
        let program_id = system_program::id();
        Self::new_program_account(
            from_keypair,
            to,
            recent_blockhash,
            lamports,
            0,
            &program_id,
            fee,
        )
    }
    /// Create and sign new SystemInstruction::Assign transaction
    pub fn new_assign(
        from_keypair: &Keypair,
        recent_blockhash: Hash,
        program_id: &Pubkey,
        fee: u64,
    ) -> Transaction {
        let assign = SystemInstruction::Assign {
            program_id: *program_id,
        };
        Transaction::new(
            from_keypair,
            &[],
            &system_program::id(),
            &assign,
            recent_blockhash,
            fee,
        )
    }
    /// Create and sign new SystemInstruction::Move transaction
    pub fn new_move(
        from_keypair: &Keypair,
        to: &Pubkey,
        lamports: u64,
        recent_blockhash: Hash,
        fee: u64,
    ) -> Transaction {
        let move_lamports = SystemInstruction::Move { lamports };
        Transaction::new(
            from_keypair,
            &[*to],
            &system_program::id(),
            &move_lamports,
            recent_blockhash,
            fee,
        )
    }
    /// Create and sign new SystemInstruction::Move transaction to many destinations
    pub fn new_move_many(
        from: &Keypair,
        moves: &[(Pubkey, u64)],
        recent_blockhash: Hash,
        fee: u64,
    ) -> Transaction {
        let instructions: Vec<_> = moves
            .iter()
            .enumerate()
            .map(|(i, (_, amount))| {
                let spend = SystemInstruction::Move { lamports: *amount };
                Instruction::new(0, &spend, vec![0, i as u8 + 1])
            })
            .collect();
        let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect();

        Transaction::new_with_instructions(
            &[from],
            &to_keys,
            recent_blockhash,
            fee,
            vec![system_program::id()],
            instructions,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signature::KeypairUtil;

    #[test]
    fn test_move_many() {
        let from = Keypair::new();
        let t1 = Keypair::new();
        let t2 = Keypair::new();
        let moves = vec![(t1.pubkey(), 1), (t2.pubkey(), 2)];

        let tx = SystemTransaction::new_move_many(&from, &moves, Hash::default(), 0);
        assert_eq!(tx.account_keys[0], from.pubkey());
        assert_eq!(tx.account_keys[1], t1.pubkey());
        assert_eq!(tx.account_keys[2], t2.pubkey());
        assert_eq!(tx.instructions.len(), 2);
        assert_eq!(tx.instructions[0].accounts, vec![0, 1]);
        assert_eq!(tx.instructions[1].accounts, vec![0, 2]);
    }
}