Skip to content

Commit ac79b57

Browse files
committed
ENS registration almost working
1 parent 4d0fd12 commit ac79b57

37 files changed

+358
-3987
lines changed

Diff for: packages/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@graphprotocol/graph-ts": "0.18.1",
1515
"@textile/hub": "^0.3.3",
1616
"@web3api/client-test-env": "0.0.1-alpha.1",
17+
"axios": "0.19.2",
1718
"assemblyscript": "0.14.7",
1819
"chalk": "^4.1.0",
1920
"fs-extra": "9.0.1",

Diff for: packages/cli/src/commands/build.ts

+40-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Compiler } from "../lib/Compiler";
2+
import { Publisher } from "../lib/Publisher";
23
import { fixParameters } from "../lib/helpers/parameters";
4+
35
import { GluegunToolbox } from "gluegun";
46
import chalk from "chalk";
7+
import axios from "axios";
58

69
const HELP = `
710
${chalk.bold('w3 build')} [options] ${chalk.bold('[<web3api-manifest>]')}
@@ -10,8 +13,9 @@ Options:
1013
-h, --help Show usage information
1114
-i, --ipfs <node> Upload build results to an IPFS node
1215
-o, --output-dir <path> Output directory for build results (default: build/)
13-
-t, --output-format <format> Output format for WASM modules (wasm, wast) (default: wasm)
16+
-f, --output-format <format> Output format for WASM modules (wasm, wast) (default: wasm)
1417
-w, --watch Regenerate types when web3api files change (default: false)
18+
-e, --test-ens <domain> Publish the package to a test ENS domain locally
1519
`
1620

1721
export default {
@@ -24,15 +28,17 @@ export default {
2428
i, ipfs,
2529
h, help,
2630
o, outputDir,
27-
t, outputFormat,
28-
w, watch
31+
f, outputFormat,
32+
w, watch,
33+
e, testEns
2934
} = parameters.options;
3035

3136
ipfs = ipfs || i;
3237
help = help || h;
3338
outputDir = outputDir || o;
34-
outputFormat = outputFormat || t;
39+
outputFormat = outputFormat || f;
3540
watch = watch || w;
41+
testEns = testEns || e;
3642

3743
let manifestPath;
3844
try {
@@ -83,7 +89,6 @@ export default {
8389

8490
const compiler = new Compiler({
8591
manifestPath,
86-
ipfs: ipfs ? ipfs : undefined,
8792
outputDir,
8893
outputFormat
8994
});
@@ -95,7 +100,37 @@ export default {
95100
const result = await compiler.compile();
96101
if (result === false) {
97102
process.exitCode = 1;
103+
return;
98104
}
105+
106+
// publish to IPFS
107+
if (ipfs !== undefined) {
108+
const publisher = new Publisher({
109+
buildPath: outputDir,
110+
ipfs
111+
});
112+
113+
const cid = await publisher.publishToIPFS();
114+
console.log(`IPFS { ${cid} }`);
115+
116+
if (testEns) {
117+
// ask the dev server to publish the CID to ENS
118+
const { data } = await axios.get(
119+
"http://localhost:4040/register-ens",
120+
{
121+
params: {
122+
domain: testEns,
123+
cid
124+
}
125+
}
126+
);
127+
128+
console.log("HERERERERE")
129+
console.log(data.success)
130+
}
131+
}
132+
133+
process.exitCode = 0;
99134
}
100135
}
101136
}

Diff for: packages/cli/src/lib/Compiler.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import * as asc from "assemblyscript/cli/asc";
1111
const fsExtra = require("fs-extra");
1212
const spawn = require("spawn-command");
1313
const toolbox = require("gluegun/toolbox");
14-
const IPFSClient = require("ipfs-http-client");
1514

1615
// We cannot `require.resolve('@graphprotocol/graph-cli')`, because it's not a require-able package
1716
const graphCli = path.resolve(
@@ -20,29 +19,14 @@ const graphCli = path.resolve(
2019

2120
export interface ICompilerConfig {
2221
manifestPath: string;
23-
ipfs?: string;
2422
outputDir: string;
2523
outputFormat: string;
2624
}
2725

2826
export class Compiler {
29-
// @ts-ignore
30-
private _ipfs: IPFSClient | undefined;
31-
3227
private _manifestDir: string;
3328

3429
constructor(private _config: ICompilerConfig) {
35-
if (_config.ipfs) {
36-
let url;
37-
try {
38-
url = new URL(_config.ipfs);
39-
} catch (e) {
40-
throw Error(`IPFS URL Malformed: ${_config.ipfs}\n${e}`)
41-
}
42-
43-
this._ipfs = new IPFSClient(_config.ipfs);
44-
}
45-
4630
this._manifestDir = path.dirname(_config.manifestPath);
4731
}
4832

@@ -105,9 +89,15 @@ export class Compiler {
10589
const { mutation, query, subgraph } = manifest;
10690
const { outputDir, manifestPath } = this._config;
10791

92+
const appendPath = (root: string, subPath: string) => {
93+
return path.join(path.dirname(root), subPath)
94+
}
95+
10896
let schema = "";
10997
const loadSchema = (schemaPath: string) => {
110-
schema += `${fs.readFileSync(schemaPath, "utf-8")}\n`;
98+
schema += `${fs.readFileSync(
99+
appendPath(manifestPath, schemaPath), "utf-8"
100+
)}\n`;
111101
}
112102

113103
if (mutation) {
@@ -135,16 +125,14 @@ export class Compiler {
135125
}
136126

137127
if (subgraph) {
138-
const subgraphFile = path.join(
139-
path.dirname(manifestPath), subgraph.file
140-
);
128+
const subgraphFile = appendPath(manifestPath, subgraph.file);
141129
const str: any = fs.readFileSync(
142130
subgraphFile, "utf-8"
143131
);
144132
const subgraphManifest: any = YAML.safeLoad(str);
145-
loadSchema(path.join(
146-
path.dirname(subgraphFile), subgraphManifest.schema.file
147-
));
133+
loadSchema(
134+
appendPath(subgraphFile, subgraphManifest.schema.file)
135+
);
148136
const id = await this._compileSubgraph(
149137
subgraph.file,
150138
`${outputDir}/subgraph`,

Diff for: packages/cli/src/lib/Publisher.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import fs from "fs";
2+
3+
const IPFSClient = require("ipfs-http-client");
4+
const { globSource } = IPFSClient;
5+
6+
export interface IPublisherConfig {
7+
buildPath: string;
8+
ipfs: string;
9+
}
10+
11+
export class Publisher {
12+
// @ts-ignore
13+
private _ipfs: IPFSClient;
14+
15+
constructor(private _config: IPublisherConfig) {
16+
let url;
17+
try {
18+
url = new URL(_config.ipfs);
19+
} catch (e) {
20+
throw Error(`IPFS URL Malformed: ${_config.ipfs}\n${e}`)
21+
}
22+
23+
this._ipfs = new IPFSClient(_config.ipfs);
24+
}
25+
26+
public async publishToIPFS(): Promise<string> {
27+
const globOptions = {
28+
recursive: true
29+
};
30+
31+
const addOptions = {
32+
wrapWithDirectory: false
33+
};
34+
35+
let rootCID = '';
36+
37+
for await (const file of this._ipfs.addAll(globSource(
38+
this._config.buildPath, globOptions
39+
), addOptions)) {
40+
if (file.path.indexOf('/') === -1) {
41+
rootCID = file.cid.toString();
42+
}
43+
}
44+
45+
return rootCID;
46+
}
47+
}

0 commit comments

Comments
 (0)