Skip to content

Commit

Permalink
Added task to set active network
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Feb 26, 2024
1 parent ba2115a commit 7beaf44
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 24 deletions.
9 changes: 1 addition & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/common/src/network.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { chains } = require('common/src/chains')
const debug = require('common/src/debug')

module.exports = async function getNetworkName(hre) {
// Get the chain id from the ethers provider
const provider = hre.ethers.provider
debug.log(provider, 'network-deep')
const network = (await provider.getNetwork()).toJSON()
const chainId = Number(network.chainId)

Expand Down
7 changes: 1 addition & 6 deletions packages/ethernaut-cli/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ require('ethernaut-network')

module.exports = {
solidity: '0.8.19',
defaultNetwork: 'local',
networks: {
local: {
url: 'http://localhost:8545',
},
},
defaultNetwork: 'localhost',
ethernaut: {
ai: {
interpreter: {
Expand Down
3 changes: 2 additions & 1 deletion packages/ethernaut-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"@nomicfoundation/hardhat-ethers": "^3.0.5"
}
}
3 changes: 3 additions & 0 deletions packages/ethernaut-network/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ const { extendEnvironment } = require('hardhat/config')
const requireAll = require('common/src/require-all')
const spinner = require('common/src/spinner')
const storage = require('./internal/storage')
const { setActiveNetwork } = require('./internal/set-network')

requireAll(__dirname, 'tasks')

extendEnvironment((hre) => {
spinner.enable(!hre.hardhatArguments.verbose)

storage.init()

setActiveNetwork(hre)
})
50 changes: 50 additions & 0 deletions packages/ethernaut-network/src/internal/set-network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const storage = require('../internal/storage')
const {
createProvider,
} = require('hardhat/internal/core/providers/construction')
const {
HardhatEthersProvider,
} = require('@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider')
const debug = require('common/src/debug')

async function setActiveNetwork(hre) {
debug.log('Setting active network', 'network')
const networks = storage.readNetworks()
await setNetwork(networks.activeNetwork, hre)
}

async function setNetwork(alias, hre) {
debug.log(`Setting network ${alias}`, 'network')
const networks = storage.readNetworks()

let network
if (alias !== 'localhost') {
network = networks[alias]
injectNetworkInConfig(alias, network, hre.config)
} else {
network = hre.config.networks[alias]
}

const provider = await createProvider(hre.config, alias)

hre.ethers.provider = new HardhatEthersProvider(provider, alias)
}

function injectNetworkInConfig(alias, network, config) {
if (config.networks[alias]) return

config.networks[alias] = {
accounts: 'remote',
gas: 'auto',
gasPrice: 'auto',
gasMultiplier: 1,
httpHeaders: {},
timeout: 40000,
url: network.url,
}
}

module.exports = {
setActiveNetwork,
setNetwork,
}
9 changes: 7 additions & 2 deletions packages/ethernaut-network/src/internal/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ const {
*
* networks.json schema:
* {
* activeNetwork: 'networkAlias1',
* networkAlias1: {
* url: 'http://some-url/${SOME_API_KEY}'
* }
* },
* networkAlias2: { .. },
* ...
* }
*/

Expand All @@ -30,7 +33,9 @@ function storeNetworks(networks) {

function init() {
createFolderIfMissing(getNetworkFolderPath())
createFileIfMissing(getNetworksFilePath(), {})
createFileIfMissing(getNetworksFilePath(), {
activeNetwork: 'localhost',
})
}

function getNetworkFolderPath() {
Expand Down
13 changes: 13 additions & 0 deletions packages/ethernaut-network/src/tasks/active.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const output = require('common/src/output')
const storage = require('../internal/storage')

require('../scopes/net')
.task('active', 'Prints the active network')
.setAction(async () => {
try {
const networks = storage.readNetworks()
output.resultBox(`Active network is ${networks.activeNetwork}`)
} catch (err) {
return output.errorBox(err)
}
})
4 changes: 3 additions & 1 deletion packages/ethernaut-network/src/tasks/autocomplete/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const storage = require('../../internal/storage')
module.exports = async function autocompleteAlias({ alias }) {
if (alias) return undefined

const choices = Object.keys(storage.readNetworks())
const choices = Object.keys(storage.readNetworks()).filter(
(alias) => alias !== 'activeNetwork',
)

return await prompt({
type: 'autocomplete',
Expand Down
4 changes: 4 additions & 0 deletions packages/ethernaut-network/src/tasks/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const remove = require('../scopes/net')

delete networks[alias]

if (networks.activeNetwork === alias) {
networks.activeNetwork = 'localhost'
}

storage.storeNetworks(networks)

output.resultBox(`Removed network ${alias}`)
Expand Down
34 changes: 34 additions & 0 deletions packages/ethernaut-network/src/tasks/set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { types } = require('hardhat/config')
const output = require('common/src/output')
const autocompleteAlias = require('./autocomplete/alias')
const storage = require('../internal/storage')
const { setNetwork } = require('../internal/set-network')

const set = require('../scopes/net')
.task('set', 'Activates a network on the cli')
.addOptionalParam(
'alias',
'How the network is referenced in the cli',
undefined,
types.string,
)
.setAction(async ({ alias }, hre) => {
try {
const networks = storage.readNetworks()

if (!(alias in networks)) {
throw new Error(`The network alias ${alias} does not exist`)
}

await setNetwork(alias, hre)

networks.activeNetwork = alias
storage.storeNetworks(networks)

output.resultBox(`The active network is now "${alias}"`)
} catch (err) {
return output.errorBox(err)
}
})

set.paramDefinitions.alias.autocomplete = autocompleteAlias
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ require('../../../../ethernaut-ui/src/index')

module.exports = {
solidity: '0.8.24',
defaultNetwork: 'local',
networks: {
local: {
url: 'http://localhost:8545',
},
},
defaultNetwork: 'localhost',
}

0 comments on commit 7beaf44

Please sign in to comment.