Skip to content

Commit 78b209d

Browse files
committed
refactor: add more helper functions and rename module to utils
1 parent 9179f84 commit 78b209d

File tree

2 files changed

+95
-47
lines changed

2 files changed

+95
-47
lines changed

integration-tests/src/receipt.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

integration-tests/src/utils.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use rand::{rng, Rng};
5+
use serde_json::json;
6+
use std::time::{Duration, SystemTime};
7+
use std::{str::FromStr, sync::Arc};
8+
9+
use anyhow::Result;
10+
use reqwest::Client;
11+
use tap_core::{signed_message::Eip712SignedMessage, tap_eip712_domain};
12+
use tap_graph::Receipt;
13+
use thegraph_core::alloy::{primitives::Address, signers::local::PrivateKeySigner};
14+
15+
pub fn create_tap_receipt(
16+
value: u128,
17+
allocation_id: &Address,
18+
verifier_contract: &str,
19+
chain_id: u64,
20+
wallet: &PrivateKeySigner,
21+
) -> Result<Eip712SignedMessage<Receipt>> {
22+
let nonce = rng().random::<u64>();
23+
24+
// Get timestamp in nanoseconds
25+
let timestamp = SystemTime::now()
26+
.duration_since(SystemTime::UNIX_EPOCH)?
27+
.as_nanos();
28+
let timestamp_ns = timestamp as u64;
29+
30+
// Create domain separator
31+
let eip712_domain_separator =
32+
tap_eip712_domain(chain_id, Address::from_str(verifier_contract)?);
33+
34+
// Create and sign receipt
35+
println!("Creating and signing receipt...");
36+
let receipt = Eip712SignedMessage::new(
37+
&eip712_domain_separator,
38+
Receipt {
39+
allocation_id: *allocation_id,
40+
nonce,
41+
timestamp_ns,
42+
value,
43+
},
44+
wallet,
45+
)?;
46+
47+
Ok(receipt)
48+
}
49+
50+
// Function to create a configured request
51+
pub fn create_request(
52+
client: &reqwest::Client,
53+
url: &str,
54+
receipt_json: &str,
55+
query: &serde_json::Value,
56+
) -> reqwest::RequestBuilder {
57+
client
58+
.post(url)
59+
.header("Content-Type", "application/json")
60+
.header("Tap-Receipt", receipt_json)
61+
.json(query)
62+
.timeout(Duration::from_secs(10))
63+
}
64+
65+
pub async fn find_allocation(http_client: Arc<Client>, url: &str) -> Result<String> {
66+
println!("Querying for active allocations...");
67+
let response = http_client
68+
.post(url)
69+
.json(&json!({
70+
"query": "{ allocations(where: { status: Active }) { id indexer { id } subgraphDeployment { id } } }"
71+
}))
72+
.send()
73+
.await?;
74+
75+
if !response.status().is_success() {
76+
return Err(anyhow::anyhow!(
77+
"Network subgraph request failed with status: {}",
78+
response.status()
79+
));
80+
}
81+
82+
// Try to find a valid allocation
83+
let response_text = response.text().await?;
84+
85+
let json_value = serde_json::from_str::<serde_json::Value>(&response_text)?;
86+
json_value
87+
.get("data")
88+
.and_then(|d| d.get("allocations"))
89+
.and_then(|a| a.as_array())
90+
.filter(|arr| !arr.is_empty())
91+
.and_then(|arr| arr[0].get("id"))
92+
.and_then(|id| id.as_str())
93+
.map(|id| id.to_string())
94+
.ok_or_else(|| anyhow::anyhow!("No valid allocation ID found"))
95+
}

0 commit comments

Comments
 (0)