-
Notifications
You must be signed in to change notification settings - Fork 4
/
subgraph-deployer.ts
208 lines (181 loc) · 5.49 KB
/
subgraph-deployer.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import path, {resolve} from 'path';
import {execSync} from 'child_process';
import {config as dotenvConfig} from 'dotenv';
dotenvConfig({path: resolve(__dirname, '.env')});
const GRAPH_NODE_TYPE = process.env.GRAPH_NODE_TYPE;
const GITHUB_USERNAME = process.env.GITHUB_USERNAME;
const GRAPH_ACCESS_TOKEN = process.env.GRAPH_ACCESS_TOKEN;
const GRAPH_DEPLOYMENT_KEY = process.env.GRAPH_DEPLOYMENT_KEY;
enum GRAPH_NODE {
LOCAL = 'local',
HOST = 'host',
NETWORK = 'network',
}
const SUBGRAPH_SLUGS = {
/**
* CORE
* Mandatory core subgraph (DaoFactory, DaoRegistry, BankExtension)
*/
Core: 'core-dev',
/**
* ADAPTERS
* Add your adapter subgraphs datasource and subgraph slug
*/
// CouponOnboarding: 'coupon-onboarding-dev',
/**
* EXTENSIONS
* Add your extension subgraphs datasource and subgraph slug
*/
// NFTExtension: 'nft-extension-dev',
};
// Execute Child Processes
const srcDir = path.join(__dirname);
export const exec = (cmd: string, cwdDir?: string) => {
try {
return execSync(cmd, {cwd: cwdDir ?? srcDir, stdio: 'inherit'});
} catch (e) {
throw new Error(`Failed to run command \`${cmd}\``);
}
};
let executedDeployments: number = 0;
(function () {
if (!GRAPH_NODE_TYPE) {
throw new Error('Please set a GRAPH_NODE_TYPE in a .env file');
}
if (GRAPH_NODE_TYPE === GRAPH_NODE.HOST) {
if (!GITHUB_USERNAME) {
throw new Error('Please set your GITHUB_USERNAME in a .env file');
}
if (!GRAPH_ACCESS_TOKEN) {
throw new Error('Please set your GRAPH_ACCESS_TOKEN in a .env file');
}
}
if (GRAPH_NODE_TYPE === GRAPH_NODE.NETWORK && !GRAPH_DEPLOYMENT_KEY) {
throw new Error('Please set your GRAPH_DEPLOYMENT_KEY in a .env file');
}
// Compile the solidity contracts
console.log('⛓ ### Compiling the smart contracts...');
exec(`npm run compile`);
Object.entries(SUBGRAPH_SLUGS).forEach(
async ([datasourceName, subgraphSlug], index) => {
try {
// Display deployment index
console.log(
`✨ ### DEPLOYMENT ${index + 1}/${
Object.keys(SUBGRAPH_SLUGS).length
}...`
);
// Define the subgraph dataSource path
const datasourcePath = `${resolve(
__dirname,
'subgraphs',
datasourceName
)}`;
// 📦 ### 1/2 Create the graph code generation files
taskGraphCodegen(datasourceName, datasourcePath);
// 📦 ### 2/2 Building the graph scheme
taskGraphBuild(datasourceName, datasourcePath);
// 🏎 ### Deploy subgraph <SUBGRAPH_SLUG>
switch (GRAPH_NODE_TYPE) {
case GRAPH_NODE.NETWORK:
taskDeployToNetwork(datasourceName, datasourcePath, subgraphSlug);
break;
case GRAPH_NODE.HOST:
taskDeployToHosted(datasourcePath, subgraphSlug);
break;
case GRAPH_NODE.LOCAL:
default:
taskDeployToLocal(datasourcePath, subgraphSlug);
}
console.log('🦾 ### Done.');
// Increment deployment counter
executedDeployments++;
} catch (error) {
console.error(error);
}
}
);
console.log(
`${
executedDeployments === 0 ? '😵' : '🎉'
} ### ${executedDeployments} Deployment(s) Successful!`
);
})();
/**
* taskGraphCodegen()
*
* Runs the code generation
* @param datasourceName
* @param datasourcePath
*/
function taskGraphCodegen(datasourceName: string, datasourcePath: string) {
// Create the graph code generation files
console.log('📦 ### 1/2 Creating the graph scheme for...', datasourceName);
exec(`graph codegen`, datasourcePath);
}
/**
* taskGraphBuild()
*
* Builds the graph scheme
* @param datasourceName
* @param datasourcePath
*/
function taskGraphBuild(datasourceName: string, datasourcePath: string) {
// Building the graph scheme
console.log('📦 ### 2/2 Building the graph scheme for...', datasourceName);
exec(`graph build`, datasourcePath);
}
/**
* taskDeployToNetwork()
*
* Deploys subgraph to The Graph decentralized service
* @param datasourceName
* @param datasourcePath
* @param subgraphSlug
*/
function taskDeployToNetwork(
datasourceName: string,
datasourcePath: string,
subgraphSlug: string
) {
console.log(
`
==== READY TO DEPLOY SUBGRAPH... ${datasourceName} ====
⚠️ IMPORTANT: When prompted, enter a version label for the subgraph!
`
);
// Deploy subgraph <SUBGRAPH_SLUG>
console.log('🏎 ### Deploying subgraph...');
exec(`graph auth --studio ${GRAPH_DEPLOYMENT_KEY}`, datasourcePath);
exec(`graph deploy --studio ${subgraphSlug}`, datasourcePath);
}
/**
* taskDeployToHosted()
*
* Deploys the subgraph to The Graph hosted service
* @param datasourcePath
* @param subgraphSlug
*/
function taskDeployToHosted(datasourcePath: string, subgraphSlug: string) {
exec(
`graph deploy --access-token ${GRAPH_ACCESS_TOKEN} --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ ${GITHUB_USERNAME}/${subgraphSlug}`,
datasourcePath
);
}
/**
* taskDeployToLocal()
*
* Allocates the subgraph name in the Graph Node and deploys the subgraphs to your local Graph Node
* @param datasourcePath
* @param subgraphSlug
*/
function taskDeployToLocal(datasourcePath: string, subgraphSlug: string) {
exec(
`graph create tribute/${subgraphSlug} --node http://127.0.0.1:8020`,
datasourcePath
);
exec(
`graph deploy tribute/${subgraphSlug} --ipfs http://127.0.0.1:5001 --node http://127.0.0.1:8020`,
datasourcePath
);
}