Skip to content

Commit

Permalink
Add port param
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Feb 28, 2024
1 parent f3c4148 commit b4484a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
13 changes: 8 additions & 5 deletions packages/ethernaut-network/src/tasks/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ const applyEnvVars = require('../internal/apply-env-vars')
const local = require('../scopes/net')
.task('local', 'Starts a local chain')
.addOptionalParam('fork', 'The alias or url of the network to fork')
.setAction(async ({ fork }) => {
.addOptionalParam('port', 'The port to run the local chain on')
.setAction(async ({ fork, port }) => {
try {
const forkUrl = getForkUrl(fork)

port = Number(port) || 8545

if (forkUrl) {
output.info(`Starting local chain with fork ${forkUrl.url}...`)
} else {
output.info('Starting local chain...')
}

startAnvil(forkUrl.unfoldedUrl)
startAnvil(forkUrl.unfoldedUrl, port)
} catch (err) {
return output.errorBox(err)
}
Expand Down Expand Up @@ -47,13 +50,13 @@ function getForkUrl(fork) {
return urlInfo
}

function startAnvil(forkUrl) {
function startAnvil(forkUrl, port) {
execSync('anvil --version', { stdio: 'inherit' })

if (!forkUrl) {
execSync('anvil', { stdio: 'inherit' })
execSync(`anvil --port ${port}`, { stdio: 'inherit' })
} else {
execSync(`anvil --fork-url ${forkUrl}`, { stdio: 'inherit' })
execSync(`anvil --fork-url ${forkUrl} --port ${port}`, { stdio: 'inherit' })
}
}

Expand Down
35 changes: 24 additions & 11 deletions packages/ethernaut-network/test/tasks/local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const storage = require('../../src/internal/storage')
describe('local', function () {
const terminal = new Terminal()

describe('when the fork is not provided', function () {
describe('when parameters are not provided', function () {
before('run', async function () {
await terminal.run('npx hardhat net local', 2000)
})
Expand All @@ -19,27 +19,40 @@ describe('local', function () {
})

describe('when none is selected', function () {
before('press enter', async function () {
before('enter parameters', async function () {
await terminal.input('\n', 2000)
})

after('close', async function () {
await terminal.input(keys.CTRLC, 1000)
it('queries for a port number', async function () {
assert.ok(terminal.output.includes('Enter port'), terminal.output)
})

it('starts a local chain', async function () {
assert.ok(
terminal.output.includes('Available Accounts'),
terminal.output,
)
describe('when 8546 is entered', function () {
before('input', async function () {
await terminal.input('8546\n', 2000)
})

after('close', async function () {
await terminal.input(keys.CTRLC, 1000)
})

it('starts a local chain', async function () {
assert.ok(
terminal.output.includes('Listening on 127.0.0.1:8546'),
terminal.output,
)
})
})
})
})

describe('when the fork is provided', function () {
describe('when parameters are provided', function () {
describe('with none', function () {
before('run', async function () {
await terminal.run('npx hardhat net local --fork none', 2000)
await terminal.run(
'npx hardhat net local --fork none --port 8545',
2000,
)
})

it('starts a local chain', async function () {
Expand Down

0 comments on commit b4484a4

Please sign in to comment.