Skip to content

Commit

Permalink
feat: Test SDK which uses Photon indexer
Browse files Browse the repository at this point in the history
To break the dependency of examples and third-party programs on Light
Protocol program crates, test them with a new test SDK and Photon
indexed, which has no dependency on them.
  • Loading branch information
vadorovsky committed Sep 12, 2024
1 parent 35241f1 commit 5392383
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
12 changes: 0 additions & 12 deletions sdk/src/legacy.rs

This file was deleted.

20 changes: 20 additions & 0 deletions test-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "light-test-sdk"
version = "0.8.0"
edition = "2021"

[dependencies]
anchor-lang = { workspace = true }

solana-client = { workspace = true }
solana-sdk = { workspace = true }

tokio = { workspace = true, features = ["process"] }
async-trait = { workspace = true }
async-dropper = { workspace = true }

light-macros = { path = "../macros/light", version = "1.0.0" }
light-sdk = { path = "../sdk", version = "0.8.0" }
photon-api = { path = "../photon-api", version = "0.44.0" }

cargo_metadata = { workspace = true }
86 changes: 86 additions & 0 deletions test-sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::process::Stdio;

use cargo_metadata::MetadataCommand;
use light_macros::pubkey;
use light_sdk::merkle_context::AddressMerkleContext;
use photon_api::apis::configuration::Configuration;
use solana_client::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;
use tokio::process::Command;

/// A test environment for programs using Light Protocol which manages:
///
/// - test validator
/// - Photon indexer
pub struct LightTest {
payer: Keypair,
pub photon_configuration: Configuration,
}

impl LightTest {
/// Creates a new test environment.
pub async fn new(program_name: &str) -> Self {
let validator = Command::new("light")
.arg("test-validator")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
// .spawn()
.status()
.await
.unwrap();
println!("{validator:?}");
if !validator.success() {
panic!("failed to run the validator");
}

let payer = Keypair::new();

let metadata = MetadataCommand::new().no_deps().exec().unwrap();
let workspace_root = metadata.workspace_root;

let program_so_path = format!("{workspace_root}/target/deploy/{program_name}.so");
let program_keypair_path =
format!("{workspace_root}/target/deploy/{program_name}-keypair.json");

let out = Command::new("solana")
.arg("program")
.arg("deploy")
.arg(program_so_path)
.arg("--program-id")
.arg(program_keypair_path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.output()
.await
.unwrap();
println!("{out:?}");

let photon_configuration = Configuration {
base_path: "http://localhost:8784".to_string(),
api_key: None,
..Default::default()
};

Self {
payer,
photon_configuration,
}
}

/// Returns a Solana RPC client connected to the test validator.
pub fn client(&self) -> RpcClient {
RpcClient::new("http://localhost:8899")
}

pub fn address_merkle_context(&self) -> AddressMerkleContext {
AddressMerkleContext {
address_merkle_tree_pubkey: pubkey!("amt1Ayt45jfbdw5YSo7iz6WZxUmnZsQTYXy82hVwyC2"),
address_queue_pubkey: pubkey!("aq1S9z4reTSQAdgWHGD2zDaS39sjGrAxbR31vxJ2F4F"),
}
}

/// Returns a payer keypair.
pub fn payer(&self) -> Keypair {
self.payer.insecure_clone()
}
}

0 comments on commit 5392383

Please sign in to comment.