titan-api-types 5.2.0

Definitions of types used across the Titan API
Documentation
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Contains type definitions for Solana Instructions and account metadata.
//!
//! These types are based on those from the [`solana_instruction`] crate, but using more efficient
//! serialization of binary data by default.
//!
//! Conversions between these types and the solana SDK types can be enabled via the
//! `solana-instruction-convert` feature.
//!
//! [`solana_instruction`]: https://crates.io/crates/solana-instruction

use super::pubkey::Pubkey;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Contains metadata for a single account read or written to by a program during instruction
/// execution.
///
/// See [`solana_instruction::AccountMeta`] for more information.
#[repr(C)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct AccountMeta {
    /// The account's public key.
    ///
    /// When serialized, encoded to a field named "p" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "p"))]
    pub pubkey: Pubkey,
    /// True if an instruction requires a transaction signature matching this account's `pubkey`.
    ///
    /// When serialized, encoded to a field named "s" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "s"))]
    pub is_signer: bool,
    /// True if the account data or metadata may be mutated during program execution.
    ///
    /// When serialized, encoded to a field named "w" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "w"))]
    pub is_writable: bool,
}

impl AccountMeta {
    /// Constructs metatdata for a writable account.
    pub fn new(pubkey: Pubkey, is_signer: bool) -> Self {
        Self {
            pubkey,
            is_signer,
            is_writable: true,
        }
    }

    /// Constructs metadata for a readable account.
    pub fn new_readonly(pubkey: Pubkey, is_signer: bool) -> Self {
        Self {
            pubkey,
            is_signer,
            is_writable: false,
        }
    }
}

/// Contains account lookup table metadata
///
/// See [`solana_instruction::AccountMeta`] for more information.
#[repr(C)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct AddressLookupTableAccount {
    /// The address lookup tables public key.
    ///
    /// When serialized, encoded to a field named "p" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "p"))]
    pub key: Pubkey,
    /// List of addresses which the address lookup table has coverage for.
    ///
    /// When serialized, encoded to a field named "a" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "a"))]
    pub addresses: Vec<Pubkey>,
}

#[cfg(feature = "solana-alt-account-convert-v2")]
impl From<solana_message_v2::AddressLookupTableAccount> for AddressLookupTableAccount {
    #[inline]
    fn from(value: solana_message_v2::AddressLookupTableAccount) -> Self {
        Self {
            key: value.key.into(),
            addresses: value.addresses.into_iter().map(|key| key.into()).collect(),
        }
    }
}

#[cfg(feature = "solana-alt-account-convert-v2")]
impl From<AddressLookupTableAccount> for solana_message_v2::AddressLookupTableAccount {
    #[inline]
    fn from(value: AddressLookupTableAccount) -> Self {
        Self {
            key: value.key.into(),
            addresses: value.addresses.into_iter().map(|key| key.into()).collect(),
        }
    }
}

#[cfg(feature = "solana-alt-account-convert-v3")]
impl From<solana_message_v3::AddressLookupTableAccount> for AddressLookupTableAccount {
    #[inline]
    fn from(value: solana_message_v3::AddressLookupTableAccount) -> Self {
        Self {
            key: value.key.into(),
            addresses: value.addresses.into_iter().map(|key| key.into()).collect(),
        }
    }
}

#[cfg(feature = "solana-alt-account-convert-v3")]
impl From<AddressLookupTableAccount> for solana_message_v3::AddressLookupTableAccount {
    #[inline]
    fn from(value: AddressLookupTableAccount) -> Self {
        Self {
            key: value.key.into(),
            addresses: value.addresses.into_iter().map(|key| key.into()).collect(),
        }
    }
}

#[cfg(feature = "solana-alt-account-convert-v4")]
impl From<solana_message_v4::AddressLookupTableAccount> for AddressLookupTableAccount {
    #[inline]
    fn from(value: solana_message_v4::AddressLookupTableAccount) -> Self {
        Self {
            key: value.key.into(),
            addresses: value.addresses.into_iter().map(|key| key.into()).collect(),
        }
    }
}

#[cfg(feature = "solana-alt-account-convert-v4")]
impl From<AddressLookupTableAccount> for solana_message_v4::AddressLookupTableAccount {
    #[inline]
    fn from(value: AddressLookupTableAccount) -> Self {
        Self {
            key: value.key.into(),
            addresses: value.addresses.into_iter().map(|key| key.into()).collect(),
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v2")]
impl From<solana_instruction_v2::AccountMeta> for AccountMeta {
    #[inline]
    fn from(value: solana_instruction_v2::AccountMeta) -> Self {
        Self {
            pubkey: value.pubkey.into(),
            is_signer: value.is_signer,
            is_writable: value.is_writable,
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v2")]
impl From<AccountMeta> for solana_instruction_v2::AccountMeta {
    #[inline]
    fn from(value: AccountMeta) -> Self {
        Self {
            pubkey: value.pubkey.into(),
            is_signer: value.is_signer,
            is_writable: value.is_writable,
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v3")]
impl From<solana_instruction_v3::AccountMeta> for AccountMeta {
    #[inline]
    fn from(value: solana_instruction_v3::AccountMeta) -> Self {
        Self {
            pubkey: value.pubkey.into(),
            is_signer: value.is_signer,
            is_writable: value.is_writable,
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v3")]
impl From<AccountMeta> for solana_instruction_v3::AccountMeta {
    #[inline]
    fn from(value: AccountMeta) -> Self {
        Self {
            pubkey: value.pubkey.into(),
            is_signer: value.is_signer,
            is_writable: value.is_writable,
        }
    }
}

/// A directive for a single invocation of a Solana program.
///
/// See [`solana_instruction::Instruction`] for more information.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Instruction {
    /// Pubkey of the program that executes this transaction.
    ///
    /// When serialized, encoded to a field named "p" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "p"))]
    pub program_id: Pubkey,
    /// Metadata describing accounts that should be passed to the program.
    ///
    /// When serialized, encoded to a field named "a" for space efficiency.
    #[cfg_attr(feature = "serde", serde(rename = "a"))]
    pub accounts: Vec<AccountMeta>,
    /// Opaque data passed to the program for its own interpretation.
    ///
    /// When serialized, encoded to a field named "d" for space efficiency.
    #[cfg_attr(feature = "serde", serde(with = "data_bytes", rename = "d"))]
    pub data: Vec<u8>,
}

#[cfg(feature = "serde")]
mod data_bytes {
    use serde::Serialize;

    #[cfg(feature = "serde-instruction-human-base64")]
    use base64::{
        alphabet,
        engine::{
            general_purpose::{GeneralPurpose, GeneralPurposeConfig},
            DecodePaddingMode,
        },
        Engine,
    };
    use serde::{de::Visitor, Deserializer, Serializer};
    #[cfg(feature = "serde-instruction-human-base64")]
    const CONFIG: GeneralPurposeConfig = GeneralPurposeConfig::new()
        .with_encode_padding(false)
        .with_decode_padding_mode(DecodePaddingMode::Indifferent);
    #[cfg(feature = "serde-instruction-human-base64")]
    const ENGINE: GeneralPurpose = GeneralPurpose::new(&alphabet::STANDARD, CONFIG);

    struct BinaryVisitor;

    impl<'de> Visitor<'de> for BinaryVisitor {
        type Value = Vec<u8>;
        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("binary data")
        }

        #[cfg(feature = "serde-instruction-human-base64")]
        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            ENGINE.decode(v).map_err(E::custom)
        }

        fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            Ok(Vec::from(v))
        }

        fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            Ok(v)
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::SeqAccess<'de>,
        {
            let mut data = Vec::with_capacity(seq.size_hint().unwrap_or(0));
            while let Some(elem) = seq.next_element()? {
                data.push(elem);
            }
            Ok(data)
        }
    }

    pub fn serialize<S, T>(value: T, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
        T: AsRef<[u8]>,
    {
        #[cfg(feature = "serde-instruction-human-base64")]
        if serializer.is_human_readable() {
            let b64_str = ENGINE.encode(value);
            return b64_str.serialize(serializer);
        }

        serde_bytes::serialize(value.as_ref(), serializer)
    }

    /// Deserializes the value from binary, array of u8s, or a Base64 string, with or without padding.
    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(BinaryVisitor)
    }
}

#[cfg(feature = "solana-instruction-convert-v2")]
impl From<solana_instruction_v2::Instruction> for Instruction {
    fn from(value: solana_instruction_v2::Instruction) -> Self {
        Self {
            program_id: value.program_id.into(),
            accounts: value.accounts.into_iter().map(Into::into).collect(),
            data: value.data,
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v2")]
impl From<Instruction> for solana_instruction_v2::Instruction {
    fn from(value: Instruction) -> Self {
        Self {
            program_id: value.program_id.into(),
            accounts: value.accounts.into_iter().map(Into::into).collect(),
            data: value.data,
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v3")]
impl From<solana_instruction_v3::Instruction> for Instruction {
    fn from(value: solana_instruction_v3::Instruction) -> Self {
        Self {
            program_id: value.program_id.into(),
            accounts: value.accounts.into_iter().map(Into::into).collect(),
            data: value.data,
        }
    }
}

#[cfg(feature = "solana-instruction-convert-v3")]
impl From<Instruction> for solana_instruction_v3::Instruction {
    fn from(value: Instruction) -> Self {
        Self {
            program_id: value.program_id.into(),
            accounts: value.accounts.into_iter().map(Into::into).collect(),
            data: value.data,
        }
    }
}

#[cfg(test)]
mod test {
    use super::AccountMeta;
    use crate::common::pubkey::{Pubkey, PUBKEY_BYTES};
    use rmpv::Value;

    const SOL_ADDR_BYTES: [u8; PUBKEY_BYTES] = [
        6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57, 220,
        26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0,
    ];
    const SOL_PUBKEY: Pubkey = Pubkey::new_from_array(SOL_ADDR_BYTES);
    const SOL_ADDR_BASE58: &str = "So11111111111111111111111111111111111111111";

    #[cfg(feature = "serde")]
    #[test]
    fn test_accountmeta_serialize_messagepack_unnamed() {
        // Should encode to a fixarray with length 3 (0x93) with the elements:
        // - bin8 with 32 bytes of data (0xc4 0x20 <data>)
        // - bool false (0xc2)
        // - bool true (0xc3)
        let account = AccountMeta {
            pubkey: SOL_PUBKEY,
            is_signer: false,
            is_writable: true,
        };

        let encoded = rmp_serde::encode::to_vec(&account).expect("should encode via messagepack");
        assert_eq!(encoded.len(), 37);
        assert_eq!(encoded[0], 0x93_u8); // fixarray with length 3
        assert_eq!(encoded[1..3], [0xc4_u8, 32_u8]); // bin8 with length 32
        assert_eq!(encoded[3..35], SOL_ADDR_BYTES); // pubkey data
        assert_eq!(encoded[35], 0xc2_u8); // bool false
        assert_eq!(encoded[36], 0xc3_u8); // bool true
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_accountmeta_deserialize_messagepack_unnamed() {
        let expected = AccountMeta {
            pubkey: SOL_PUBKEY,
            is_signer: false,
            is_writable: true,
        };
        let encoded: [u8; 37] = [
            0x93, // fixarray len=3
            0xc4, 32, // bin8 len=32
            // 32 bytes of data
            6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
            220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, 0xc2, // bool false
            0xc3, // bool true
        ];

        let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
            .expect("should decode via messagepack");
        assert_eq!(decoded, expected);
    }

    // Test to see if decoding will ignore extra fields while decoding if new optional fields are
    // added in the future.
    // The unnamed encoding fails due to requiring the lengths to match exactly
    #[cfg(feature = "serde")]
    #[test]
    fn test_accountmeta_deserialize_messagepack_extra_field_unnamed() {
        let encoded: [u8; 38] = [
            0x94, // fixarray len=4
            0xc4, 32, // bin8 len=32
            // 32 bytes of data
            6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
            220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, 0xc2, // bool false
            0xc3, // bool true
            0xc0, // nil
        ];

        let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
            .expect_err("should fail to decode");
        match decoded {
            rmp_serde::decode::Error::LengthMismatch(_) => (),
            other => panic!("unexpected error: {}", other),
        }
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_accountmeta_serialize_messagepack_named() {
        // Should encode to a fixmap with length 3 (0x83) with the elements:
        // - fixstr len=1 (0xa1) followed by "p"
        // - bin8 with 32 bytes of data (0xc4 0x20 <data>)
        // - fixstr len=1 (0xa1) data "s"
        // - bool false (0xc2)
        // - fixstr len=1 (0xa1) data "w"
        // - bool true (0xc3)
        //
        // total len = 43
        let account = AccountMeta {
            pubkey: SOL_PUBKEY,
            is_signer: false,
            is_writable: true,
        };

        let encoded =
            rmp_serde::encode::to_vec_named(&account).expect("should encode via messagepack");
        assert_eq!(encoded.len(), 43);
        assert_eq!(encoded[0], 0x83_u8); // fixmap with length 3
        assert_eq!(encoded[1..3], [0xa1_u8, 0x70_u8]); // fixstr len=1, data="p"
        assert_eq!(encoded[3..5], [0xc4_u8, 32_u8]); // bin8 with length 32
        assert_eq!(encoded[5..37], SOL_ADDR_BYTES); // pubkey data
        assert_eq!(encoded[37..39], [0xa1_u8, 0x73_u8]); // fixstr len=1, data="s"
        assert_eq!(encoded[39], 0xc2_u8); // bool false
        assert_eq!(encoded[40..42], [0xa1_u8, 0x77_u8]); // fixstr len=1, data="w"
        assert_eq!(encoded[42], 0xc3_u8); // bool true
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_accountmeta_deserialize_messagepack_named() {
        let expected = AccountMeta {
            pubkey: SOL_PUBKEY,
            is_signer: false,
            is_writable: true,
        };
        let encoded: [u8; 43] = [
            0x83, // fixmap len=3
            0xa1, 0x70, // fixstr "p"
            0xc4, 32, // bin8 len=32
            6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
            220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, // 32 bytes of data
            0xa1, 0x73, // fixstr "s"
            0xc2, // bool false
            0xa1, 0x77, // fixstr "w"
            0xc3, // bool true
        ];

        let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
            .expect("should decode via messagepack");
        assert_eq!(decoded, expected);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_accountmeta_deserialize_messagepack_extra_field_named() {
        let expected = AccountMeta {
            pubkey: SOL_PUBKEY,
            is_signer: false,
            is_writable: true,
        };
        let encoded: [u8; 46] = [
            0x84, // fixmap len=4
            0xa1, 0x70, // fixstr "p"
            0xc4, 32, // bin8 len=32
            6, 155, 136, 87, 254, 171, 129, 132, 251, 104, 127, 99, 70, 24, 192, 53, 218, 196, 57,
            220, 26, 235, 59, 85, 152, 160, 240, 0, 0, 0, 0, 0, // 32 bytes of data
            0xa1, 0x73, // fixstr "s"
            0xc2, // bool false
            0xa1, 0x77, // fixstr "w"
            0xc3, // bool true
            0xa1, 0x65, // fixstr "e" for extra
            0xc0, // nil
        ];

        let decoded = rmp_serde::decode::from_slice::<AccountMeta>(&encoded)
            .expect("should decode via messagepack");
        assert_eq!(decoded, expected);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_instruction_messagepack_roundtrip() {
        let instruction = super::Instruction {
            program_id: SOL_PUBKEY,
            accounts: Vec::new(),
            data: Vec::from([104, 101, 108, 108, 111]), // hello
        };
        let expected_msgpack = Value::Map(vec![
            (
                Value::String("p".into()),
                Value::Binary(Vec::from(SOL_ADDR_BYTES)),
            ),
            (Value::String("a".into()), Value::Array(vec![])),
            (
                Value::String("d".into()),
                Value::Binary(vec![104, 101, 108, 108, 111]),
            ),
        ]);

        let encoded_instruction =
            rmp_serde::to_vec_named(&instruction).expect("should encode to messagepack");
        let encoded_expected =
            rmp_serde::to_vec_named(&expected_msgpack).expect("should encode to messagepack");
        assert_eq!(encoded_instruction, encoded_expected);

        let decoded: super::Instruction =
            rmp_serde::from_slice(&encoded_expected).expect("should decode from messagepack");
        assert_eq!(decoded, instruction);
    }

    #[cfg(feature = "serde-instruction-human-base64")]
    #[test]
    fn test_instruction_json_human() {
        let instruction = super::Instruction {
            program_id: SOL_PUBKEY,
            accounts: Vec::new(),
            data: Vec::from([104, 101, 108, 108, 111]), // hello
        };
        let expected_json = serde_json::json!({
            "p": SOL_ADDR_BASE58,
            "a": [],
            "d": "aGVsbG8",
        });
        let encoded_value = serde_json::to_value(&instruction).expect("should encode to JSON");
        assert_eq!(encoded_value, expected_json);

        let decoded: super::Instruction =
            serde_json::from_value(expected_json).expect("should decoded from JSON");
        assert_eq!(decoded, instruction);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_instruction_decode_json_nonhuman() {
        use super::Instruction;
        let instruction = Instruction {
            program_id: SOL_PUBKEY,
            accounts: Vec::new(),
            data: Vec::from([104, 101, 108, 108, 111]), // hello
        };
        let json_data = serde_json::json!({
            "p": SOL_ADDR_BYTES,
            "a": [],
            "d": [104, 101, 108, 108, 111]
        });

        let decoded: Instruction =
            serde_json::from_value(json_data).expect("should decode from JSON");
        assert_eq!(decoded, instruction);
    }
}