Skip to content

Commit

Permalink
Merge pull request #23 from streamingfast/enol/fix-accounts-tests
Browse files Browse the repository at this point in the history
solana-common: transactions_by_programid_and_account_without_votes tests
  • Loading branch information
maoueh authored Feb 6, 2025
2 parents 9fc8a47 + 6a0c30d commit 46ee123
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.84"
channel = "1.83"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
40 changes: 40 additions & 0 deletions solana-common/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,43 @@ pub(crate) fn transaction_program_and_account_keys(
.map(|inst| format!("program:{}", inst.program_id())),
)
}

#[cfg(test)]
mod tests {
use super::*;
use substreams_solana::pb::sf::solana::r#type::v1::Block;

#[test]
fn test_transaction_program_and_account_keys() {
// Given
let block: Block =
testing::read_block("./src/testdata/solana_mainnet_313000000.binpb.base64");
let confirmed_transaction = block.transactions.get(8).unwrap();

// When
let mut result = transaction_program_and_account_keys(confirmed_transaction);

// Expected
confirmed_transaction
.transaction
.clone()
.unwrap()
.message
.unwrap()
.account_keys
.iter()
.for_each(|acct| {
assert_eq!(
result.any(|index| index == format!("account:{}", base58::encode(acct))),
true
)
});

confirmed_transaction.walk_instructions().for_each(|inst| {
assert_eq!(
result.any(|index| index == format!("program:{}", inst.program_id().to_string())),
true
)
});
}
}

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions solana-common/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ fn _transactions_by_programid_and_account_without_votes(

#[cfg(test)]
mod tests {
use substreams_solana::{base58, pb::sf::solana::r#type::v1::ConfirmedTransaction};

use super::*;

#[test]
Expand Down Expand Up @@ -102,6 +104,19 @@ mod tests {
result.transactions.into_iter().for_each(|transaction| {
let mut matched = true;

if !transaction
.transaction
.clone()
.unwrap()
.message
.unwrap()
.account_keys
.iter()
.any(|acct| base58::encode(acct) == "5qrvgpvr55Eo7c5bBcwopdiQ6TpvceiRm42yjHTbtDvc")
{
matched = false
}

// Check if the given program id is contained within the instructions.
if !transaction.walk_instructions().any(|instruction| {
instruction.program_id().to_string()
Expand All @@ -126,4 +141,61 @@ mod tests {
assert_eq!(matched, true)
});
}

#[test]
fn test_transactions_by_programid_and_account_without_votes_account_keys() {
// Given
let block: Block =
testing::read_block("./src/testdata/solana_mainnet_318251413.binpb.base64");

// When
let result = _transactions_by_programid_and_account_without_votes(
"program:JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 && account:3EsvvyqporKr5DVpzWsdYCphpXqXnQBMQLGNwSH5MmRE".to_owned(),
block.clone(),
)
.expect("Failed to execute function");

// Expect

let expected_transactions: Vec<&ConfirmedTransaction> = block
.transactions()
.filter(|transaction| {
let mut matched = true;

if !transaction.walk_instructions().any(|inst| {
inst.program_id().to_string() == "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"
}) {
matched = false;
}

if !transaction
.transaction
.clone()
.unwrap()
.message
.unwrap()
.account_keys
.iter()
.any(|acct| {
base58::encode(acct) == "3EsvvyqporKr5DVpzWsdYCphpXqXnQBMQLGNwSH5MmRE"
})
{
matched = false;
}

return matched;
})
.collect();

assert!(result.transactions.len() > 0);
assert_eq!(result.transactions.len(), expected_transactions.len());
result.transactions.iter().for_each(|result_transaction| {
assert_eq!(
expected_transactions.iter().any(
|expected_transaction| expected_transaction.id() == result_transaction.id()
),
true
)
});
}
}

0 comments on commit 46ee123

Please sign in to comment.