Skip to content

Commit

Permalink
Custom prompts never return undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Mar 3, 2024
1 parent f18f29a commit 5fe9247
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/ethernaut-interact-ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ extendEnvironment((hre) => {
const contract = hre.scopes.interact.tasks.contract
contract.paramDefinitions.abi.suggest = require('./suggest/abi')
contract.paramDefinitions.abi.prompt = require('./prompts/abi')
contract.paramDefinitions.address.prompt = require('./prompts/address')
contract.paramDefinitions.address.suggest = require('./suggest/address')
contract.paramDefinitions.fn.prompt = require('./prompts/fn')
contract.paramDefinitions.params.prompt = require('./prompts/params')
contract.paramDefinitions.value.prompt = require('./prompts/value')
contract.paramDefinitions.value.suggest = require('./suggest/value')
})
4 changes: 1 addition & 3 deletions packages/ethernaut-interact-ui/src/prompts/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ const {
const debug = require('common/src/debug')
const prompt = require('common/src/prompt')

module.exports = async function promptFn({ fn, abi }) {
module.exports = async function promptFn({ abi }) {
if (!abi) return

if (fn !== undefined) return

try {
const _abi = loadAbi(abi)
const isFunction = (fn) => fn.type === 'function'
Expand Down
4 changes: 1 addition & 3 deletions packages/ethernaut-interact-ui/src/prompts/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ const loadAbi = require('ethernaut-interact/src/tasks/contract/load-abi')
const debug = require('common/src/debug')
const prompt = require('common/src/prompt')

module.exports = async function promptParams({ params, abi, fn }) {
module.exports = async function promptParams({ abi, fn }) {
if (!abi) return

if (params !== undefined) return

try {
const _abi = loadAbi(abi)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const storage = require('ethernaut-interact/src/internal/storage')
const debug = require('common/src/debug')
const { getNetworkName } = require('common/src/network')

module.exports = async function promptAddress({ abi, address, hre }) {
module.exports = async function suggestAddress({ abi, address, hre }) {
if (!abi) return

if (address !== undefined) return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const loadAbi = require('ethernaut-interact/src/tasks/contract/load-abi')
const debug = require('common/src/debug')

module.exports = async function promptValue({ value, abi, fn }) {
module.exports = async function suggestValue({ abi, fn }) {
if (!abi) return

if (value !== undefined) return

try {
const _abi = loadAbi(abi)

Expand Down
3 changes: 1 addition & 2 deletions packages/ethernaut-network-ui/src/prompts/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const prompt = require('common/src/prompt')
const storage = require('ethernaut-network/src/internal/storage')

module.exports = function promptAlias(message = 'Select a network') {
return async function promptAlias({ alias }) {
if (alias) return undefined
return async function promptAlias() {
const choices = Object.keys(storage.readNetworks()).filter(
(alias) => alias !== 'activeNetwork',
)
Expand Down
2 changes: 1 addition & 1 deletion packages/ethernaut-network-ui/src/prompts/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = async function promptFork({ fork, paramDefault }) {
const isDefault = fork === paramDefault

// No need to prompt?
if (valueProvided && !isDefault) return undefined
if (valueProvided && !isDefault) return fork

const choices = ['none'].concat(
Object.keys(storage.readNetworks()).filter(
Expand Down
4 changes: 1 addition & 3 deletions packages/ethernaut-network-ui/src/prompts/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const strategies = {
MANUAL: 'Enter url manually',
}

module.exports = async function promptUrl({ url }) {
if (url) return undefined

module.exports = async function promptUrl() {
const choice = await selectStrategy()
switch (choice) {
case strategies.KNOWN:
Expand Down
6 changes: 3 additions & 3 deletions packages/ethernaut-ui/src/internal/collect-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function collectArg(paramDef, providedArg, parsedArg, argsSoFar) {

// Is the parameter already provided,
// but is not the default value injected by hardhat?
// If so, skip autocompletion
// If so, skip collection.
if (providedArg) {
const isInjectedDefault =
providedArg === paramDef.defaultValue && parsedArg === undefined
Expand All @@ -70,12 +70,12 @@ async function collectArg(paramDef, providedArg, parsedArg, argsSoFar) {
// Does the parameter provide its own custom prompt function?
if (!suggested && paramDef.prompt) {
collectedArg = await customPrompt(paramDef, argsSoFar)
if (collectedArg !== undefined) return collectedArg
}

// Mmnope, ok. Ask the user to input the parameter in raw text
if (collectedArg === undefined)
if (collectedArg === undefined) {
collectedArg = await rawPrompt(paramDef, suggested || paramDef.defaultValue)
}

return collectedArg
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ethernaut-util-ui/src/prompts/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = async function promptUnit({
paramName === 'from' ? from === paramDefault : to === paramDefault

// No need to prompt?
if (valueProvided && !isDefault) return undefined
if (valueProvided && !isDefault) return paramName === 'from' ? from : to

// Choices are all units minus the one used
let choices = units.concat()
Expand Down
4 changes: 1 addition & 3 deletions packages/ethernaut-wallet-ui/src/prompts/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const prompt = require('common/src/prompt')
const storage = require('ethernaut-wallet/src/internal/storage')

module.exports = (message = 'Select a wallet') => {
return async function promptAlias({ alias }) {
if (alias) return undefined

return async function promptAlias() {
const choices = Object.keys(storage.readSigners()).filter(
(alias) => alias !== 'activeSigner',
)
Expand Down

0 comments on commit 5fe9247

Please sign in to comment.