forked from helius-labs/photon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx_event_parser_v2.rs
More file actions
153 lines (143 loc) · 6.49 KB
/
tx_event_parser_v2.rs
File metadata and controls
153 lines (143 loc) · 6.49 KB
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
use crate::common::typedefs::serializable_pubkey::SerializablePubkey;
use crate::ingester::error::IngesterError;
use crate::ingester::parser::indexer_events::{
BatchPublicTransactionEvent, CompressedAccount, CompressedAccountData,
MerkleTreeSequenceNumberV1, MerkleTreeSequenceNumberV2,
OutputCompressedAccountWithPackedContext, PublicTransactionEvent,
};
use crate::ingester::parser::state_update::StateUpdate;
use crate::ingester::parser::tx_event_parser::create_state_update_v1;
use light_compressed_account::indexer_event::parse::event_from_light_transaction;
use light_compressed_account::Pubkey as LightPubkey;
use solana_pubkey::Pubkey;
use solana_sdk::signature::Signature;
use super::state_update::AddressQueueUpdate;
pub fn parse_public_transaction_event_v2(
program_ids: &[Pubkey],
instructions: &[Vec<u8>],
accounts: Vec<Vec<Pubkey>>,
) -> Option<Vec<BatchPublicTransactionEvent>> {
let light_program_ids: Vec<LightPubkey> = program_ids.iter().map(|p| (*p).into()).collect();
let light_accounts: Vec<Vec<LightPubkey>> = accounts
.into_iter()
.map(|acc_vec| acc_vec.into_iter().map(|acc| acc.into()).collect())
.collect();
let events =
event_from_light_transaction(&light_program_ids, instructions, light_accounts).ok()?;
events.map(|events| {
events
.into_iter()
.map(|public_transaction_event| {
let event = PublicTransactionEvent {
input_compressed_account_hashes: public_transaction_event
.event
.input_compressed_account_hashes,
output_compressed_account_hashes: public_transaction_event
.event
.output_compressed_account_hashes,
output_compressed_accounts: public_transaction_event
.event
.output_compressed_accounts
.iter()
.map(|x| OutputCompressedAccountWithPackedContext {
compressed_account: CompressedAccount {
owner: x.compressed_account.owner.into(),
lamports: x.compressed_account.lamports,
address: x.compressed_account.address,
data: x.compressed_account.data.as_ref().map(|d| {
CompressedAccountData {
discriminator: d.discriminator,
data: d.data.clone(),
data_hash: d.data_hash,
}
}),
},
merkle_tree_index: x.merkle_tree_index,
})
.collect(),
output_leaf_indices: public_transaction_event.event.output_leaf_indices,
sequence_numbers: public_transaction_event
.event
.sequence_numbers
.iter()
.map(|x| MerkleTreeSequenceNumberV1 {
pubkey: x.tree_pubkey.into(),
seq: x.seq,
})
.collect(),
relay_fee: public_transaction_event.event.relay_fee,
is_compress: public_transaction_event.event.is_compress,
compression_lamports: public_transaction_event
.event
.compress_or_decompress_lamports,
pubkey_array: public_transaction_event
.event
.pubkey_array
.into_iter()
.map(|p| p.into())
.collect(),
message: public_transaction_event.event.message,
};
let batch_public_transaction_event = BatchPublicTransactionEvent {
event,
new_addresses: public_transaction_event.new_addresses,
input_sequence_numbers: public_transaction_event
.input_sequence_numbers
.iter()
.map(|x| MerkleTreeSequenceNumberV2 {
tree_pubkey: x.tree_pubkey.into(),
queue_pubkey: x.queue_pubkey.into(),
tree_type: x.tree_type,
seq: x.seq,
})
.collect(),
address_sequence_numbers: public_transaction_event
.address_sequence_numbers
.iter()
.map(|x| MerkleTreeSequenceNumberV2 {
tree_pubkey: x.tree_pubkey.into(),
queue_pubkey: x.queue_pubkey.into(),
tree_type: x.tree_type,
seq: x.seq,
})
.collect(),
batch_input_accounts: public_transaction_event.batch_input_accounts,
tx_hash: public_transaction_event.tx_hash,
};
batch_public_transaction_event
})
.collect::<Vec<_>>()
})
}
pub fn create_state_update_v2(
tx: Signature,
slot: u64,
transaction_event: Vec<BatchPublicTransactionEvent>,
) -> Result<StateUpdate, IngesterError> {
if transaction_event.is_empty() {
return Ok(StateUpdate::new());
}
let mut state_updates = Vec::new();
for event in transaction_event.iter() {
let mut state_update_event = create_state_update_v1(tx, slot, event.clone().event.into())?;
state_update_event
.batch_nullify_context
.extend(event.batch_input_accounts.clone());
state_update_event
.batch_new_addresses
.extend(
event
.new_addresses
.clone()
.iter()
.map(|x| AddressQueueUpdate {
tree: SerializablePubkey::from(x.mt_pubkey),
address: x.address,
queue_index: x.queue_index,
}),
);
state_updates.push(state_update_event);
}
let merged = StateUpdate::merge_updates(state_updates);
Ok(merged)
}