-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpin_subgraph_files.sh
executable file
·124 lines (101 loc) · 3.94 KB
/
pin_subgraph_files.sh
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
#!/usr/bin/env bash
set -e
########################################################################
# Configuration
DEPLOYMENT_ID="$1"
OLD_CHAIN_NAME="$2"
NEW_CHAIN_NAME="$3"
THIS_DIR=$(dirname $(realpath "$0"))
HOME_DIR=$(dirname "$THIS_DIR")
CONTRACTS_SOURCE="./contracts-dev"
export GNS_CONTRACT_ADDRESS=$(jq '."1337".GNS.address' "$CONTRACTS_SOURCE/addresses.json")
export NETWORK_IPFS_URL=https://ipfs.network.thegraph.com
export LOCAL_IPFS_URL=http://localhost:5001
########################################################################
# Functions
# Replace string in file
# 1: find
# 2: replace
# 3: file
find_replace_sed() {
sed -i -e "s/${1}/${2}/g" "${3}"
}
# Add/pin a file to network IPFS node
# 1: file location
ipfs_add() {
# Add file to local ipfs node, save ipfs hash
ipfs_path=$(curl -s -F "file=@$1" "$LOCAL_IPFS_URL/api/v0/add" | jq -r '.Hash') #FIXME. no response?
echo "$ipfs_path"
# Pin file to network IPFS gateway, save ipfs hash
added_hash=$(curl -s -X POST "$NETWORK_IPFS_URL/api/v0/pin/add?arg=/ipfs/$ipfs_path" | \
cut -d "[" -f2 | cut -d "]" -f1 | tr -d ' "')
}
# Pin file to network IPFS node
# 1: IPFS hash
ipfs_pin() {
#sleep 30
# Pin file to network IPFS gateway, save ipfs hash
result=$(curl --write-out '%{http_code}' -s -X POST "$NETWORK_IPFS_URL/api/v0/pin/add?arg=/ipfs/$1")
#| \
# cut -d "[" -f2 | cut -d "]" -f1 | tr -d ' "')
#echo "result: $result"
#echo "pinned hash: $1"
}
########################################################################
# Update subgraph manifest, pin new one to network IPFS gateway
# Get subgraph manifest and pin to local IPFS node
# Fetch manifest
echo "Download deployment manifest, $DEPLOYMENT_ID..."
curl -s -X POST "$NETWORK_IPFS_URL/api/v0/cat?arg=$DEPLOYMENT_ID" > deployment_manifest_temp
echo "Replace network name in manifest, $OLD_CHAIN_NAME -> $NEW_CHAIN_NAME..."
# Replace network name: xdai --> gnosis
find_replace_sed \
$OLD_CHAIN_NAME \
$NEW_CHAIN_NAME \
deployment_manifest_temp
echo "Pin new manifest..."
new_deployment_id=$(ipfs_add deployment_manifest_temp)
echo "new deployment id: $new_deployment_id"
########################################################################
# Get all IPFS files referenced in manifest, ensure they're pinned to the network IPFS node
echo "Pin all subgraph files mentioned in manifest..."
grep 'Qm.*' deployment_manifest_temp > ipfshashes
sed -i -e 's/\/: \/ipfs\///g' ipfshashes
sed -i -e 's/^[[:space:]]\{1,\}//' ipfshashes
failed=0
while read hash; do
result=$(curl --connect-timeout 300 --max-time 600 --write-out '%{http_code}' --silent --output /dev/null -X POST "$NETWORK_IPFS_URL/api/v0/pin/add?arg=/ipfs/$hash")
echo "result: $result"
#result=$(ipfs_pin "$hash")
if [[ $result -ne 200 ]]
then
echo "failed: $hash"
failed=1
echo $hash >> failed_hashes_$DEPLOYMENT_ID
else
echo "success: $hash"
fi
done < ipfshashes
if [ $failed -gt 0 ]
then
echo "looping failed ones..."
while read failed_hash; do
echo "(looping failed) failed hash: $failed_hash"
#result=$(ipfs_pin "$hash")
result=$(curl --connect-timeout 300 --max-time 600 --write-out '%{http_code}' --silent --output /dev/null -X POST "$NETWORK_IPFS_URL/api/v0/pin/add?arg=/ipfs/$failed_hash")
echo "result: $result"
if [[ $result == 200 ]]
then
# remove failed from failed list
echo "removing $failed_hash"
grep -v $failed_hash failed_hashes_$DEPLOYMENT_ID > tmpfile && mv tmpfile failed_hashes_$DEPLOYMENT_ID
fi
done < failed_hashes_$DEPLOYMENT_ID
fi
########################################################################
# Pin metadata files to IPFS node
echo "Pin metadata files to IPFS node..."
subgraph_metadata=$(ipfs_add "./contracts-dev/cli/subgraphs/$DEPLOYMENT_ID/metadata.json")
version_metadata=$(ipfs_add "./contracts-dev/cli/subgraphs/$DEPLOYMENT_ID/version-metadata.json")
echo "subgraph metadata pinned: $subgraph_metadata"
echo "version metadata pinned: $version_metadata"