Skip to content

Commit

Permalink
fix(governance,networks): remove localhost from networks (again) (#13321
Browse files Browse the repository at this point in the history
)
  • Loading branch information
clemsos committed Mar 20, 2024
1 parent 385a951 commit dfa3e48
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 104 deletions.
10 changes: 6 additions & 4 deletions governance/scripts/deployments/index.js
Expand Up @@ -2,7 +2,11 @@
const { ethers, run, upgrades, network } = require('hardhat')
const { networks } = require('@unlock-protocol/networks')
const createLock = require('../lock/create')
const { getUnlock, ADDRESS_ZERO } = require('@unlock-protocol/hardhat-helpers')
const {
getUnlock,
ADDRESS_ZERO,
getNetwork,
} = require('@unlock-protocol/hardhat-helpers')

const log = (...message) => {
// eslint-disable-next-line no-console
Expand All @@ -26,8 +30,7 @@ async function main({
const [deployer, minter] = await ethers.getSigners()

// fetch chain info
const { chainId } = await ethers.provider.getNetwork()
const networkName = networks[chainId].name
const { id: chainId, name: networkName, multisig } = await getNetwork()
const isLocalNet = networkName === 'localhost'
log(
`Deploying contracts on ${networkName} with the account: ${deployer.address}`
Expand Down Expand Up @@ -107,7 +110,6 @@ async function main({
}

// Transfer ownership of Unlock + Proxy admin
const multisig = networks[chainId.toString()].multisig
if (!owner && multisig) {
owner = multisig
if (owner) {
Expand Down
39 changes: 15 additions & 24 deletions governance/tasks/deploy.js
Expand Up @@ -34,30 +34,21 @@ task('deploy', 'Deploy the entire Unlock protocol')
)
.addFlag('setTemplate', 'set the PublicLock instance in Unlock')
.setAction(
async (
{
unlockAddress,
unlockVersion,
publicLockVersion,
udtAddress,
publicLockAddress,
wethAddress,
oracleAddress,
premintAmount,
liquidity,
setTemplate,
estimatedGasForPurchase,
locksmithURI,
owner,
},
{ ethers }
) => {
const { chainId } = await ethers.provider.getNetwork()
const networkName = networks[chainId].name

// eslint-disable-next-line no-console
console.log(`Starting deployments on ${networkName}...`)

async ({
unlockAddress,
unlockVersion,
publicLockVersion,
udtAddress,
publicLockAddress,
wethAddress,
oracleAddress,
premintAmount,
liquidity,
setTemplate,
estimatedGasForPurchase,
locksmithURI,
owner,
}) => {
// eslint-disable-next-line global-require
const mainDeployer = require('../scripts/deployments')
const {
Expand Down
9 changes: 9 additions & 0 deletions locksmith/__tests__/fulfillment/dispatcher.test.ts
Expand Up @@ -62,6 +62,15 @@ vi.mock('@unlock-protocol/unlock-js', () => ({
},
}))

vi.mock('@unlock-protocol/networks', () => ({
default: {
31337: {
provider: 'http://127.0.0.1:8545',
publicProvider: 'http://127.0.0.1:8545',
},
},
}))

vi.mock('../../src/utils/gasSettings', () => ({
getGasSettings: vi.fn(() => {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-helpers/src/networks.js
Expand Up @@ -50,7 +50,7 @@ const hardhatNetworks = {
}

Object.keys(networks).forEach((key) => {
if (['default', 'networks', 'localhost'].indexOf(key) === -1) {
if (['default', 'networks'].indexOf(key) === -1) {
hardhatNetworks[key] = {
chainId: networks[key].id,
name: networks[key].name,
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-helpers/src/unlock.js
Expand Up @@ -30,7 +30,7 @@ export const getNetwork = async (chainId) => {
if (!chainId) {
;({ chainId } = await ethers.provider.getNetwork())
}
return networks[chainId]
return networks[chainId] || { name: 'localhost', id: chainId }
}

export default {
Expand Down
Expand Up @@ -23,6 +23,9 @@ const config: HardhatUserConfig = {
endpoint: 'here goes a subgraph URI',
},
},
1: {
unlockAddress: 'newAddress',
},
12345: {
name: 'New Network',
id: 12345,
Expand Down
11 changes: 8 additions & 3 deletions packages/hardhat-plugin/test/index.test.ts
Expand Up @@ -34,13 +34,18 @@ describe('Unlock Hardhat plugin', function () {
assert.instanceOf(this.hre.unlock, Object)
})

it('should store networks info', function () {
it('should store and extend existing networks info', function () {
assert.isTrue(Object.keys(this.hre.unlock.networks).includes('1'))
assert.equal(this.hre.unlock.networks['1'].name, networks['1'].name)
assert.equal(this.hre.unlock.networks['1'].id, networks['1'].id)
assert.equal(this.hre.unlock.networks['1'].unlockAddress, 'newAddress')
})
it('should store additional networks info', function () {
assert.isTrue(Object.keys(this.hre.unlock.networks).includes('31337'))
assert.equal(
this.hre.unlock.networks['31337'].name,
'Custom Localhost Name'
)
assert.equal(this.hre.unlock.networks['31337'].id, networks['31337'].id)
assert.equal(
this.hre.unlock.networks['31337'].subgraph?.endpoint,
'here goes a subgraph URI'
Expand Down Expand Up @@ -213,7 +218,7 @@ describe('HardhatConfig unlock extension', function () {
assert.isTrue(Object.keys(this.hre.config).includes('unlock'))
assert.deepEqual(
Object.keys(this.hre.config.unlock).sort(),
[...Object.keys(networks), '12345'].sort()
[...Object.keys(networks), '12345', '31337'].sort()
)
})

Expand Down
3 changes: 2 additions & 1 deletion packages/networks/.gitignore
@@ -1,2 +1,3 @@
dist
build
build
src/networks/localhost.ts
33 changes: 33 additions & 0 deletions packages/networks/README.md
Expand Up @@ -19,6 +19,39 @@ console.log(networks[1].name) // mainnet
yarn start
```

## Localhost

You can create a `localhost` network file with the following command:

```sh
# create file
yarn create-localhost <unlockAddress> <subgraphEnpoint>

# export file by appending to network index
echo "export * from './localhost'" >> src/networks/index.ts

# rebuild
yarn build
```

#### Read Unlock Address from a file

Optionally, you can parse the Unlock address from a subgraph manifest file formatted as follow

```json
{
"localhost": {
"Unlock": { "address": "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6" }
}
}
```

with the following command

```sh
yarn workspace @unlock-protocol/networks unlock-address <filepath>.json
```

## Scripts

- Check that networks are configured correctly: `yarn run check`. Some networks might be missing some properties.
Expand Down
73 changes: 73 additions & 0 deletions packages/networks/bin/localhost.js
@@ -0,0 +1,73 @@
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 on CI
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 [unlockAddress, subgraphEnpoint] = process.argv.slice(2)
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
})
19 changes: 19 additions & 0 deletions packages/networks/bin/readSubgraphInfo.js
@@ -0,0 +1,19 @@
import fs from 'fs-extra'

// this is used to read a network
const run = async () => {
const [networkInfoPath] = process.argv.slice(2)
const networkInfo = await fs.readJSON(networkInfoPath)
const {
localhost: {
Unlock: { address: unlockAddress },
},
} = networkInfo
return unlockAddress
}

run()
.then((unlockAddress) => console.log(unlockAddress))
.catch((err) => {
throw err
})
3 changes: 3 additions & 0 deletions packages/networks/package.json
Expand Up @@ -18,6 +18,8 @@
"build": "tsup src/index.ts --dts --format esm,cjs",
"check-tokens": "tsx ./bin/check-tokens",
"clean": "rm -rf ./dist",
"create-localhost": "tsx ./bin/localhost",
"unlock-address": "tsx ./bin/readSubgraphInfo",
"check": "tsx ./bin/keys",
"prepublish": "yarn build",
"doc": "node ./bin/doc.js",
Expand All @@ -32,6 +34,7 @@
"@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"
},
Expand Down
1 change: 0 additions & 1 deletion packages/networks/src/networks/index.ts
@@ -1,4 +1,3 @@
export * from './localhost'
export * from './mainnet'
export * from './gnosis'
export * from './polygon'
Expand Down
24 changes: 0 additions & 24 deletions packages/networks/src/networks/localhost.ts

This file was deleted.

16 changes: 13 additions & 3 deletions scripts/integration-tests.sh
Expand Up @@ -3,11 +3,21 @@ set -e

# First this script will deploy
REPO_ROOT=`dirname "$0"`/..
INTEGRATION_TESTS_FOLDER=$REPO_ROOT/tests
EXTRA_ARGS=$*

# fetch compose config paths
BASE_DOCKER_FOLDER=$REPO_ROOT/docker
BASE_DOCKER_COMPOSE=$BASE_DOCKER_FOLDER/docker-compose.yml
DOCKER_COMPOSE_FILE=$BASE_DOCKER_FOLDER/docker-compose.integration.yml
COMPOSE_CONFIG="-f $BASE_DOCKER_COMPOSE -f $DOCKER_COMPOSE_FILE"

# run Unlock Protocol stack
sh -c "$REPO_ROOT/scripts/run-stack-dockerized.sh"

# Setting the right env var
export UNLOCK_ENV=test

# run the actual tests
sh $INTEGRATION_TESTS_FOLDER/bin/tests.sh
echo "Running integration tests \n"
COMMAND="yarn workspace tests ci --network docker"
docker-compose $COMPOSE_CONFIG build integration-tests
docker-compose $COMPOSE_CONFIG run -e UNLOCK_ENV=test -e CI=true $EXTRA_ARGS integration-tests bash -c "$COMMAND"
1 change: 0 additions & 1 deletion tests/.eslintrc.js
Expand Up @@ -6,7 +6,6 @@ const rulesToIgnore = [

module.exports = {
extends: ['@unlock-protocol/eslint-config'],
plugins: ['mocha'],
globals: {
it: true,
artifacts: true,
Expand Down

0 comments on commit dfa3e48

Please sign in to comment.