From 81a826a2d8b4ecd34fde3111260ae0c52f7ad34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renaud?= Date: Mon, 18 Mar 2024 15:43:23 +0100 Subject: [PATCH] add script to create localhost file --- packages/networks/bin/localhost.js | 83 ++++++++++++++++++++++++++++++ packages/networks/package.json | 3 ++ 2 files changed, 86 insertions(+) create mode 100644 packages/networks/bin/localhost.js diff --git a/packages/networks/bin/localhost.js b/packages/networks/bin/localhost.js new file mode 100644 index 00000000000..73f74dc819c --- /dev/null +++ b/packages/networks/bin/localhost.js @@ -0,0 +1,83 @@ +import fs from 'fs-extra' +import path from 'path' + +// We use Partial for localhost as we don't have all the information +const defaultLocalhost = { + chain: 'localhost', + description: 'Localhost network.', + featured: false, + fullySubsidizedGas: true, + id: 31337, + isTestNetwork: true, + name: 'Localhost', + nativeCurrency: { + coingecko: 'ethereum', + decimals: 18, + name: 'ETH', + symbol: 'ETH', + }, + provider: 'http://127.0.0.1:8545', + publicLockVersionToDeploy: 13, + publicProvider: 'http://127.0.0.1:8545', + subgraph: { + endpoint: '', + }, +} + +const generateLocalhostNetworkFile = ({ + unlockAddress, + subgraphEnpoint = 'http://localhost:8000/subgraphs/name/testgraph', +}) => { + const localhost = { + ...defaultLocalhost, + subgraph: { + endpoint: subgraphEnpoint, + }, + unlockAddress, + } + + // log for debug purposes + console.log(localhost) + + // output to js file + const parsed = `import { NetworkConfig } from '@unlock-protocol/types' + +// We use Partial for localhost as we don't have all the information +export const localhost: Partial = ${JSON.stringify(localhost)} +export default localhost + ` + return parsed +} + +const run = async () => { + const [networkInfoPath, subgraphEnpoint] = process.argv.slice(2) + + const networkInfo = await fs.readJSON(networkInfoPath) + const { + localhost: { + Unlock: { address: unlockAddress }, + }, + } = networkInfo + + console.log(networkInfo) + + console.log(`Creating localhost file for unlockAddress ${unlockAddress}`) + + if (!unlockAddress) { + throw new Error('Missing unlockAddress arg') + } + + const fileContent = generateLocalhostNetworkFile({ + subgraphEnpoint, + unlockAddress, + }) + + const filePath = path.resolve('./src/networks/localhost.ts') + + await fs.writeFile(filePath, fileContent) +} +run() + .then(() => console.log('done')) + .catch((err) => { + throw err + }) diff --git a/packages/networks/package.json b/packages/networks/package.json index 887df4a7616..e88bbab2d3e 100644 --- a/packages/networks/package.json +++ b/packages/networks/package.json @@ -18,6 +18,7 @@ "build": "tsup src/index.ts --dts --format esm,cjs", "check-tokens": "tsx ./bin/check-tokens", "clean": "rm -rf ./dist", + "create-localhost": "tsx ./bin/localhost", "check": "tsx ./bin/keys", "prepublish": "yarn build", "doc": "node ./bin/doc.js", @@ -32,7 +33,9 @@ "@unlock-protocol/tsconfig": "workspace:^", "@unlock-protocol/types": "workspace:^", "eslint": "8.54.0", + "fs-extra": "11.2.0", "tsup": "8.0.2", + "typescript": "5.3.3" }, "files": [