Skip to content

Commit 3cbc224

Browse files
authored
Merge branch 'main' into lf/fix/portal
2 parents 8e0e549 + 741510b commit 3cbc224

26 files changed

+266
-119
lines changed

.github/actions/docker-publish/action.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ inputs:
1919
dockerfile:
2020
description: 'Path to the Dockerfile'
2121
required: true
22+
context:
23+
description: 'Path to the Context'
24+
default: .
25+
required: true
2226
build-args:
2327
description: 'List of build-time variables'
2428
required: false
@@ -65,7 +69,7 @@ runs:
6569
uses: docker/build-push-action@v4
6670
id: publish
6771
with:
68-
context: .
72+
context: ${{ inputs.context }}
6973
file: ${{ inputs.dockerfile }}
7074
push: true
7175
tags: ${{ steps.meta.outputs.tags }}

.github/workflows/docker-publish.yml

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ jobs:
3030
password: ${{ secrets.GITHUB_TOKEN }}
3131
image: ghcr.io/fuellabs/fuel-explorer
3232
dockerfile: deployment/Dockerfile
33+
context: ./packages/graphql

contracts/predicate/scripts/run-predicate.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { BaseAssetId, Predicate, Provider, Wallet, bn, hexlify } from 'fuels';
22
import { promises as fs } from 'node:fs';
33
import { resolve } from 'node:path';
44

5-
const { FUEL_PROVIDER_BETA5, PRIVATE_KEY } = process.env;
5+
const { FUEL_PROVIDER, PRIVATE_KEY } = process.env;
66
const BIN_PATH = resolve(__dirname, '../out/debug/predicate-app.bin');
77
const AMOUNT = 300_000;
88

9-
if (!FUEL_PROVIDER_BETA5 || !PRIVATE_KEY) {
10-
throw new Error('Missing some config on your .env file');
9+
if (!FUEL_PROVIDER || !PRIVATE_KEY) {
10+
throw new Error('Missing some config in .env file. Should have FUEL_PROVIDER and PRIVATE_KEY');
1111
}
1212

1313
async function main() {
1414
const binHex = hexlify(await fs.readFile(BIN_PATH));
15-
const provider = await Provider.create(FUEL_PROVIDER_BETA5!);
15+
const provider = await Provider.create(FUEL_PROVIDER!);
1616
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY!, provider);
1717
const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();
1818
const walletAddress = wallet.address.toB256();

deployment/Dockerfile

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ FROM node:20-slim AS base
44

55
# Expose the ENVs to the env of the container
66
ENV PORT="${PORT}"
7-
ENV FUEL_PROVIDER_BETA5="${FUEL_PROVIDER_BETA5}"
7+
ENV FUEL_PROVIDER="${FUEL_PROVIDER:-https://beta-5.fuel.network/graphql}"
88
ENV PNPM_HOME="/pnpm"
99
ENV PATH="$PNPM_HOME:$PATH"
10+
ENV SERVER_BUILD=true
1011

1112
# Enable pnpm using corepack form node.js
1213
RUN corepack enable
1314

1415
COPY . /app-explorer
1516
WORKDIR /app-explorer
1617

17-
RUN pnpm install --frozen-lockfile
18+
RUN pnpm install
1819

19-
EXPOSE 4444
20+
EXPOSE ${PORT}
2021

21-
WORKDIR /app-explorer/packages/graphql
22+
WORKDIR /app-explorer
2223

23-
CMD ["pnpm", "start"]
24+
CMD ["pnpm", "server:start"]

packages/app-explorer/.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
FUEL_PROVIDER_BETA5=https://beta-5.fuel.network/graphql
1+
FUEL_PROVIDER=https://beta-5.fuel.network/graphql

packages/app-explorer/.env.production

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
FUEL_PROVIDER_BETA5=https://beta-5.fuel.network/graphql
1+
FUEL_PROVIDER=https://beta-5.fuel.network/graphql

packages/app-explorer/src/app/api/graphql/route.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { createExecutor, createSchema } from '@fuel-explorer/graphql';
22
import { ContextDomain } from '@fuel-explorer/graphql/src/domains/Context';
33
import { createYoga } from 'graphql-yoga';
4+
import { requireEnv } from '~/systems/utils/requireEnv';
5+
6+
const { FUEL_PROVIDER: url } = requireEnv(['FUEL_PROVIDER']);
47

5-
const url = process.env.FUEL_PROVIDER_BETA5!;
68
const executor = createExecutor(async ({ body }) => {
79
return fetch(url, {
810
body,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function requireEnv<
2+
A extends string[],
3+
B extends { [key in A[number]]: string },
4+
>(keys: string[]): B {
5+
return keys.reduce((ret, key) => {
6+
if (!process.env[key]) {
7+
throw new Error(`Environment variable ${key} is required`);
8+
}
9+
ret[key] = process.env[key]!;
10+
return ret;
11+
}, {} as B);
12+
}

packages/graphql/.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_moduels
2+
.turbo
3+
dist

packages/graphql/.env.example

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
FUEL_PROVIDER_BETA5=https://beta-5.fuel.network/graphql
2-
SERVER_PORT=4000
1+
FUEL_PROVIDER=https://beta-5.fuel.network/graphql
2+
SERVER_PORT=4444

packages/graphql/.env.production

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
FUEL_PROVIDER_BETA5=https://beta-5.fuel.network/graphql
1+
FUEL_PROVIDER=https://beta-5.fuel.network/graphql
2+
SERVER_PORT=4444

packages/graphql/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ This is a mock api for block-explorer
77
```sh
88
pnpm dev
99
```
10+
11+
## Docker
12+
13+
```
14+
docker run \
15+
-e FUEL_PROVIDER=https://beta-5.fuel.network/graphql \
16+
-e SERVER_PORT=3000 \
17+
-p 3333:3000 \
18+
ghcr.io/fuellabs/fuel-explorer:main
19+
```

packages/graphql/codegen.fuel.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import type { CodegenConfig } from '@graphql-codegen/cli';
22

3-
console.log(`process.env.FUEL_PROVIDER_BETA5`, process.env.FUEL_PROVIDER_BETA5);
3+
import { requireEnv } from './src/utils/requireEnv';
4+
5+
const { FUEL_PROVIDER } = requireEnv([
6+
['FUEL_PROVIDER', 'https://beta-5.fuel.network/graphql'],
7+
]);
48

59
const config: CodegenConfig = {
610
generates: {
711
'./src/schemas/fuelcore.graphql': {
8-
schema:
9-
process.env.FUEL_PROVIDER_BETA5 ||
10-
'https://beta-5.fuel.network/graphql',
12+
schema: FUEL_PROVIDER,
1113
plugins: ['schema-ast'],
1214
config: {
1315
includeDirectives: true,

packages/graphql/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"typings": "./src/index.ts",
1111
"scripts": {
1212
"build": "run-s codegen:fuel build:lib",
13-
"build:lib": "tsup --dts",
13+
"build:lib": "tsup",
1414
"build:watch": "pnpm build:lib --watch",
1515
"codegen:fuel": "gql-gen -r dotenv/config --config codegen.fuel.ts",
1616
"codegen:app": "gql-gen -r dotenv/config --config codegen.ts",
1717
"dev": "pnpm build:watch",
18-
"start": "pnpm dev",
18+
"server:start": "node ./dist/index.js",
1919
"fix:generated": "node ./scripts/fix-generated.mjs",
2020
"ts:check": "tsc --noEmit",
2121
"prepare": "pnpm build"
@@ -43,7 +43,8 @@
4343
"graphql-tag": "2.12.6",
4444
"lodash": "^4.17.21",
4545
"tai64": "1.0.0",
46-
"typescript": "5.3.3"
46+
"typescript": "5.3.3",
47+
"wait-port": "1.1.0"
4748
},
4849
"devDependencies": {
4950
"@babel/cli": "7.23.9",
@@ -63,7 +64,9 @@
6364
"execa": "8.0.1",
6465
"graphql-codegen-typescript-common": "0.18.2",
6566
"graphql-codegen-typescript-mock-data": "3.7.1",
67+
"npm-run-all": "^4.1.5",
6668
"tsconfig-paths": "^4.2.0",
69+
"tsup": "8.0.1",
6770
"tsx": "4.7.0"
6871
}
6972
}

packages/graphql/src/bin.ts

-73
This file was deleted.

packages/graphql/src/bin/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { runServerCodegen, runServer } from './server';
2+
3+
const { CODE_GEN = 'false' } = process.env;
4+
5+
if (CODE_GEN === 'true') {
6+
runServerCodegen();
7+
} else {
8+
runServer();
9+
}

packages/graphql/src/bin/server.ts

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import chokidar from 'chokidar';
2+
import { execa } from 'execa';
3+
import { createServer } from 'http';
4+
import { resolve } from 'path';
5+
6+
import app from '../server';
7+
import { requireEnv } from '../utils/requireEnv';
8+
9+
const { SERVER_PORT } = requireEnv([['SERVER_PORT', '4444']]);
10+
const { WATCH = 'false' } = process.env;
11+
12+
const server = createServer(app);
13+
14+
export async function runServer() {
15+
return new Promise((resolve) => {
16+
server.listen(SERVER_PORT, async () => {
17+
console.log(
18+
`🚀 Server running at http://localhost:${SERVER_PORT}/graphql`
19+
);
20+
resolve(null);
21+
});
22+
});
23+
}
24+
25+
export async function closeServer() {
26+
return new Promise((resolve) => {
27+
server.close(() => {
28+
resolve(null);
29+
console.log('🛑 GraphQL server stopped!');
30+
});
31+
});
32+
}
33+
34+
export async function runServerCodegen() {
35+
const cwd = resolve(__dirname, '../');
36+
const gqlWatcher = chokidar.watch(['src/**/*.graphql'], {
37+
cwd,
38+
ignoreInitial: true,
39+
ignored: ['src/schemas'],
40+
});
41+
42+
async function codegen() {
43+
console.log('⌛️ Generating GraphQL code...');
44+
try {
45+
await execa('pnpm', ['codegen:app'], { stdio: 'inherit' });
46+
console.log('✅ GraphQL code generated!');
47+
} catch (err) {
48+
console.log('❌ GraphQL error!');
49+
}
50+
51+
console.log('👀 Watching for GraphQL changes...');
52+
}
53+
54+
await runServer();
55+
await codegen();
56+
57+
async function exitHandler() {
58+
await closeServer();
59+
gqlWatcher.close();
60+
}
61+
62+
if (WATCH !== 'true') {
63+
exitHandler();
64+
return;
65+
}
66+
67+
process.on('exit', exitHandler);
68+
process.on('SIGTERM', exitHandler);
69+
process.on('SIGINT', exitHandler);
70+
process.on('SIGUSR1', exitHandler);
71+
72+
gqlWatcher.on('all', async () => {
73+
await closeServer();
74+
await runServer();
75+
await codegen();
76+
});
77+
78+
return exitHandler;
79+
}
+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { Provider } from 'fuels';
22

3+
import { Cache } from '../utils';
4+
5+
const CHAIN_CACHE = 43200000;
6+
37
export class NetworkDomain {
8+
static cache = new Cache();
9+
410
static async getChainInfo(url: string) {
5-
const provider = await Provider.create(url);
11+
let provider = this.cache.get<Provider>(url);
12+
if (!provider) {
13+
provider = await Provider.create(url);
14+
}
15+
this.cache.put(url, provider, CHAIN_CACHE);
616
return provider.getChain();
717
}
818
}

0 commit comments

Comments
 (0)