Skip to content

Commit

Permalink
add script to create localhost file
Browse files Browse the repository at this point in the history
  • Loading branch information
clemsos committed Mar 18, 2024
1 parent 390253c commit 81a826a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
83 changes: 83 additions & 0 deletions packages/networks/bin/localhost.js
@@ -0,0 +1,83 @@
import fs from 'fs-extra'
import path from 'path'

// We use Partial<NetworkConfig> 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<NetworkConfig> for localhost as we don't have all the information
export const localhost: Partial<NetworkConfig> = ${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
})
3 changes: 3 additions & 0 deletions packages/networks/package.json
Expand Up @@ -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",
Expand All @@ -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": [
Expand Down

0 comments on commit 81a826a

Please sign in to comment.