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
143
144
145
146
147
148
use crate::{AccountMeta, Instruction, PublicKey};
use core::fmt;

#[derive(Clone)]
pub struct MessageBuilder {
    pub(crate) instructions: Vec<Instruction>,
    pub(crate) program_ids: Vec<AccountMeta>,
    pub(crate) signed_keys: Vec<PublicKey>,
    pub(crate) unsigned_keys: Vec<PublicKey>,
    pub(crate) num_readonly_signed_accounts: u8,
    pub(crate) num_readonly_unsigned_accounts: u8,
    pub(crate) payer: Option<PublicKey>,
}

impl Default for MessageBuilder {
    fn default() -> Self {
        MessageBuilder::new()
    }
}

impl MessageBuilder {
    pub fn new() -> Self {
        MessageBuilder {
            instructions: Vec::default(),
            program_ids: Vec::default(),
            signed_keys: Vec::default(),
            unsigned_keys: Vec::default(),
            num_readonly_signed_accounts: u8::default(),
            num_readonly_unsigned_accounts: u8::default(),
            payer: Option::default(),
        }
    }

    pub fn add_instruction(&mut self, instruction: Instruction) -> &mut Self {
        self.instructions.push(instruction);

        self
    }

    pub fn add_payer(&mut self, payer: PublicKey) -> &mut Self {
        self.payer = Some(payer);

        self
    }

    pub fn build(&mut self) -> &mut Self {
        self.instructions.iter().for_each(|instruction| {
            self.program_ids.push(AccountMeta {
                pubkey: instruction.program_id,
                is_signer: false,
                is_writable: false,
            });
        });

        let mut instruction_account_metas: Vec<_> = self
            .instructions
            .iter()
            .flat_map(|ix| ix.accounts.iter())
            .collect();

        instruction_account_metas.extend(&self.program_ids);

        // Make signers first
        instruction_account_metas.sort_by(|x, y| {
            y.is_signer
                .cmp(&x.is_signer)
                .then(y.is_writable.cmp(&x.is_writable))
        });

        let payer_account_meta;
        if let Some(payer) = self.payer {
            payer_account_meta = AccountMeta {
                pubkey: payer,
                is_signer: true,
                is_writable: true,
            };

            instruction_account_metas.insert(0, &payer_account_meta);
        }

        let mut unique_account_metas: Vec<AccountMeta> = Vec::default();
        for account_meta in instruction_account_metas {
            // Promote to writable if a later AccountMeta requires it
            if let Some(x) = unique_account_metas
                .iter_mut()
                .find(|x| x.pubkey == account_meta.pubkey)
            {
                x.is_writable |= account_meta.is_writable;
                continue;
            }
            unique_account_metas.push(account_meta.clone());
        }

        unique_account_metas.iter().for_each(|account_meta| {
            if account_meta.is_signer {
                self.signed_keys.push(account_meta.pubkey);
                if !account_meta.is_writable {
                    self.num_readonly_signed_accounts += 1;
                }
            } else {
                self.unsigned_keys.push(account_meta.pubkey);
                if !account_meta.is_writable {
                    self.num_readonly_unsigned_accounts += 1;
                }
            }
        });

        self
    }
}

impl fmt::Debug for MessageBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let signed_keys = self
            .signed_keys
            .iter()
            .map(|public_key| bs58::encode(&public_key).into_string())
            .collect::<Vec<String>>();

        let unsigned_keys = self
            .unsigned_keys
            .iter()
            .map(|public_key| bs58::encode(&public_key).into_string())
            .collect::<Vec<String>>();

        f.debug_struct("MessageBuilder")
            .field("instructions", &self.instructions)
            .field("program_ids", &self.program_ids)
            .field("signed_keys", &signed_keys)
            .field("unsigned_keys", &unsigned_keys)
            .field(
                "num_readonly_signed_accounts",
                &self.num_readonly_signed_accounts,
            )
            .field(
                "num_readonly_unsigned_accounts",
                &self.num_readonly_unsigned_accounts,
            )
            .field(
                "payer",
                &match self.payer {
                    Some(payer) => Some(bs58::encode(payer).into_string()),
                    None => None,
                },
            )
            .finish()
    }
}