-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgenerate-subgraph.js
245 lines (217 loc) · 7.49 KB
/
generate-subgraph.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const fs = require("fs");
const path = require("path")
const yaml = require("js-yaml");
const { migrationFileLocation: defaultMigrationFileLocation,
network ,startBlock} = require("./settings");
const { versionToNum, forEachTemplate } = require("./utils");
var mappings = require("./mappings.json")[network].mappings;
const { subgraphLocation: defaultSubgraphLocation } = require('./graph-cli')
let existingAddresses = []
/**
* Generate a `subgraph.yaml` file from `datasource.yaml` fragments in
* `mappings` directory `mappings.json` and `migration.json`
*/
async function generateSubgraph(opts={}) {
// Get the addresses of our predeployed contracts
const migrationFile = opts.migrationFileLocation || defaultMigrationFileLocation;
opts.subgraphLocation = opts.subgraphLocation || defaultSubgraphLocation;
const addresses = JSON.parse(fs.readFileSync(migrationFile, "utf-8"));
const missingAddresses = {};
if (network === 'xdai' || network === 'sokol') {
mappings.push( //workaround :(
{name: 'UGenericScheme',
contractName: 'UGenericScheme',
dao: 'base',
mapping: 'UGenericScheme',
arcVersion: '0.0.1-rc.39' });
addresses[network].base['0.0.1-rc.39']['UGenericScheme'] = "0xA92A766d62318B9c06Eb548753bD34acbD7C5f3c" //dummy
}
// Filter out 0.0.1-rc.18 & 0.0.1-rc.17
const latestMappings = mappings.filter(mapping =>
!(mapping.arcVersion === "0.0.1-rc.18" ||
mapping.arcVersion === "0.0.1-rc.17")
);
// Build our subgraph's datasources from the mapping fragments
const dataSources = combineFragments(
latestMappings, false, addresses, missingAddresses
).filter(el => el != null);
// Throw an error if there are contracts that're missing an address
// and are never used as a template
const missing = Object.keys(missingAddresses).map(function(key, index) {
return missingAddresses[key] ? key : null
}).filter(el => el != null);
if (missing.length) {
throw Error(`The following contracts are missing addresses: ${missing.toString()}`);
}
const templates = buildTemplates();
const subgraph = {
specVersion: "0.0.1",
schema: { file: "./schema.graphql" },
dataSources,
templates
};
fs.writeFileSync(
opts.subgraphLocation,
yaml.safeDump(subgraph, { noRefs: true }),
"utf-8"
);
}
function combineFragments(fragments, isTemplate, addresses, missingAddresses) {
let ids = [];
return fragments.map(mapping => {
const contract = mapping.name;
const version = mapping.arcVersion;
const fragment = `${__dirname}/../src/mappings/${mapping.mapping}/datasource.yaml`;
var abis, entities, eventHandlers, file, yamlLoad, abi;
if (fs.existsSync(fragment)) {
yamlLoad = yaml.safeLoad(fs.readFileSync(fragment, "utf-8"));
file = `${__dirname}/../src/mappings/${mapping.mapping}/mapping.ts`;
eventHandlers = yamlLoad.eventHandlers;
entities = yamlLoad.entities;
abis = (yamlLoad.abis || [contract]).map(contractName => {
const versionNum = versionToNum(version);
if ((versionNum >= 34) && (contractName === "UGenericScheme")) {
return {
name: contractName,
file: `${__dirname}/../abis/0.0.1-rc.33/UGenericScheme.json`
};
}
if ((versionNum < 24) && (contractName === "UGenericScheme")) {
return {
name: contractName,
file: `${__dirname}/../abis/${version}/GenericScheme.json`
};
}
if ((versionNum < 24) && (contractName === "ContinuousLocking4Reputation")) {
return {
name: contractName,
file: `${__dirname}/../abis/0.0.1-rc.24/ContinuousLocking4Reputation.json`
};
}
if ((versionNum < 36) && (contractName === "ContributionRewardExt")) {
return {
name: contractName,
file: `${__dirname}/../abis/0.0.1-rc.36/ContributionRewardExt.json`
};
}
if ((versionNum < 47) && (contractName === "GenericSchemeMultiCall")) {
return {
name: contractName,
file: `${__dirname}/../abis/0.0.1-rc.47/GenericSchemeMultiCall.json`
};
}
if ((versionNum < 47) && (contractName === "DxDaoSchemeConstraints")) {
return {
name: contractName,
file: `${__dirname}/../abis/0.0.1-rc.47/DxDaoSchemeConstraints.json`
};
}
if ((versionNum < 47) && (contractName === "SchemeConstraints")) {
return {
name: contractName,
file: `${__dirname}/../abis/0.0.1-rc.47/SchemeConstraints.json`
};
}
return {
name: contractName,
file: `${__dirname}/../abis/${version}/${contractName}.json`
};
});
abi = yamlLoad.abis && yamlLoad.abis.length ? yamlLoad.abis[0] : contract;
} else {
file = path.resolve(`${__dirname}/emptymapping.ts`);
eventHandlers = [{ event: "Dummy()",handler: "handleDummy" }];
entities = ["nothing"];
abis = [{ name: contract, file: path.resolve(`${__dirname}/dummyabi.json`) }];
abi = contract;
}
var contractAddress;
if (isTemplate === false) {
if (mapping.dao === 'address') {
contractAddress = mapping.address;
} else if (mapping.dao === 'organs') {
contractAddress = addresses[network].test[version][mapping.dao][mapping.contractName];
} else {
contractAddress = addresses[network][mapping.dao][version][mapping.contractName];
}
// If the contract isn't predeployed
if (!contractAddress) {
// Keep track of contracts that're missing addresses.
// These contracts should have addresses because they're not
// templated contracts aren't used as templates
const daoContract = `${network}-${version}-${mapping.contractName}-${mapping.dao}`;
if (missingAddresses[daoContract] === undefined) {
missingAddresses[daoContract] = true;
}
return null;
}
if (existingAddresses.indexOf(contractAddress) !== -1) {
return null;
}
existingAddresses.push(contractAddress);
}
const source = isTemplate ? {
abi
} : {
address: contractAddress,
abi,
startBlock
};
let name = contract;
if (ids.indexOf(contract) == -1) {
ids.push(contract);
} else if (ids.indexOf(contract+contractAddress) == -1) {
ids.push(contract+contractAddress);
name = contract+contractAddress;
} else {
return;
}
if (isTemplate) {
// convert name to be version specific
const classVersion = version.replace(/\.|-/g, '_');
name = `${name}_${classVersion}`
}
var result = {
kind: 'ethereum/contract',
name: `${name}`,
network: `${network}`,
source,
mapping: {
kind: "ethereum/events",
apiVersion: "0.0.1",
language: "wasm/assemblyscript",
file: path.resolve(file),
entities: entities ? entities : ["nothing"],
abis,
eventHandlers
}
};
return result;
});
}
function buildTemplates() {
let results = [];
forEachTemplate((name, mapping, arcVersion) => {
results.push(
...combineFragments(
[{
name,
mapping,
arcVersion
}],
true,
undefined,
undefined
)
);
});
return results;
}
if (require.main === module) {
generateSubgraph().catch(err => {
console.log(err);
process.exit(1);
});
} else {
module.exports = generateSubgraph;
}