-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdeploy.rs
More file actions
100 lines (85 loc) · 2.86 KB
/
deploy.rs
File metadata and controls
100 lines (85 loc) · 2.86 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
use std::sync::Arc;
use graph::prelude::{
anyhow::{anyhow, bail, Result},
serde_json::{json, Value},
SubgraphName, SubgraphStore,
};
use crate::manager::deployment::DeploymentSearch;
// Function to send an RPC request and handle errors
async fn send_rpc_request(url: &str, payload: Value) -> Result<()> {
let client = reqwest::Client::new();
let response = client.post(url).json(&payload).send().await?;
if response.status().is_success() {
Ok(())
} else {
Err(response
.error_for_status()
.expect_err("Failed to parse error response")
.into())
}
}
// Function to send subgraph_create request
async fn send_create_request(name: &str, url: &str) -> Result<()> {
// Construct the JSON payload for subgraph_create
let create_payload = json!({
"jsonrpc": "2.0",
"method": "subgraph_create",
"params": {
"name": name,
},
"id": "1"
});
// Send the subgraph_create request
send_rpc_request(url, create_payload)
.await
.map_err(|e| e.context(format!("Failed to create subgraph with name `{}`", name)))
}
// Function to send subgraph_deploy request
async fn send_deploy_request(name: &str, deployment: &str, url: &str) -> Result<()> {
// Construct the JSON payload for subgraph_deploy
let deploy_payload = json!({
"jsonrpc": "2.0",
"method": "subgraph_deploy",
"params": {
"name": name,
"ipfs_hash": deployment,
},
"id": "1"
});
// Send the subgraph_deploy request
send_rpc_request(url, deploy_payload).await.map_err(|e| {
e.context(format!(
"Failed to deploy subgraph `{}` to `{}`",
deployment, name
))
})
}
pub async fn run(
subgraph_store: Arc<impl SubgraphStore>,
deployment: DeploymentSearch,
search: DeploymentSearch,
url: String,
) -> Result<()> {
let hash = match deployment {
DeploymentSearch::Hash { hash, shard: _ } => hash,
_ => bail!("The `deployment` argument must be a valid IPFS hash"),
};
let name = match search {
DeploymentSearch::Name { name } => name,
_ => bail!("The `name` must be a valid subgraph name"),
};
let subgraph_name =
SubgraphName::new(name.clone()).map_err(|_| anyhow!("Invalid subgraph name"))?;
let exists = subgraph_store.subgraph_exists(&subgraph_name).await?;
if !exists {
println!("Creating subgraph `{}`", name);
// Send the subgraph_create request
send_create_request(&name, &url).await?;
println!("Subgraph `{}` created", name);
}
// Send the subgraph_deploy request
println!("Deploying subgraph `{}` to `{}`", hash, name);
send_deploy_request(&name, &hash, &url).await?;
println!("Subgraph `{}` deployed to `{}`", name, url);
Ok(())
}