solana_central/types/
link.rs

1use solana_sdk::pubkey::Pubkey;
2
3/**
4Struct used to track a "link" between two accounts. For there to be a link, both accounts must be
5on curve. We return link as a 64 byte array with first 32 bytes being "smaller" account and last 32
6bytes being "larger" account. Accounts are compared from most to least significant bit. In addition
7to storing the 2 wallets, we also store the earliest tx where they were first observed linking, and
8we order by block_time, slot, index, and atomic_instruction_index.
9*/
10
11#[derive(Clone)]
12pub struct Link {
13  pub link: [u8; 64],
14  pub block_time: u64,
15  pub slot: u64,
16  pub index: u64,
17  pub atomic_instruction_index: u8,
18}
19
20impl Link {
21  /// Constructor to check if there exists a "link" between two accounts. For there to be a link,
22  /// both accounts must be on curve. Returns an option where if its not none, the link is valid.
23  pub fn check_link(
24    account1: &Pubkey,
25    account2: &Pubkey,
26    block_time: u64,
27    slot: u64,
28    index: u64,
29    atomic_instruction_index: u8,
30  ) -> Option<Self> {
31    if !(account1.is_on_curve() && account2.is_on_curve()) {
32      return None;
33    }
34
35    let mut link = [0u8; 64];
36    if account1 < account2 {
37      link[0..32].copy_from_slice(account1.as_ref());
38      link[32..64].copy_from_slice(account2.as_ref());
39    } else if account1 > account2 {
40      link[0..32].copy_from_slice(account2.as_ref());
41      link[32..64].copy_from_slice(account1.as_ref());
42    }
43    // Two identical accounts have no link
44    else {
45      return None;
46    }
47    Some(Self {
48      link,
49      block_time,
50      slot,
51      index,
52      atomic_instruction_index,
53    })
54  }
55
56  /// Debug tostring for link
57  pub fn to_string(&self) -> String {
58    let wallet_1 = Pubkey::new_from_array(self.link[0..32].try_into().expect("slice with incorrect length"));
59    let wallet_2 = Pubkey::new_from_array(self.link[32..64].try_into().expect("slice with incorrect length"));
60    format!(
61      "Wallet 1: {}, Wallet 2: {}, Block Time: {}, Slot: {}, Index: {}, Atomic Instruction Index: {}",
62      wallet_1, wallet_2, self.block_time, self.slot, self.index, self.atomic_instruction_index
63    )
64  }
65}