Skip to content

Commit

Permalink
Display level names
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Sep 24, 2024
1 parent 143d84c commit 767b50e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/ethernaut-zeronaut/src/internal/connect.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const fs = require('fs')
const path = require('path')
const debug = require('ethernaut-common/src/ui/debug')

async function connect(network, hre) {
debug.log(`Connecting to ${network}`, 'zeronaut')

// Retrieve the deployed addresses for the given network
const addresses = _getDeployedAddresses(network)
const address = addresses['Zeronaut#Zeronaut']
debug.log(`Main contract address: ${address}`, 'zeronaut')

// Retrieve the game abi
const abi = _getGameAbi(network)
// debug.log(`Game abi: ${JSON.stringify(abi, null, 2)}`, 'zeronaut')

// Create the game contract instance
const contract = await hre.ethers.getContractAt(abi, address)
Expand Down
2 changes: 1 addition & 1 deletion packages/ethernaut-zeronaut/src/scopes/zeronaut.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { scope } = require('hardhat/config')
const { description } = require('../../package.json')

module.exports = scope('zeronaut', description)
module.exports = scope('zero', description)
33 changes: 33 additions & 0 deletions packages/ethernaut-zeronaut/src/tasks/create-level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const types = require('ethernaut-common/src/validation/types')
const output = require('ethernaut-common/src/ui/output')
const { getChainId } = require('ethernaut-common/src/util/network')
const { connect } = require('../internal/connect')

require('../scopes/zeronaut')
.task('create-level', 'Adds a level to a campaign')
.addPositionalParam(
'level',
'The address of the level',
undefined,
types.address,
)
.addPositionalParam(
'campaign',
'The name of the campaign',
undefined,
types.string,
)
.setAction(async ({ campaign, level }, hre) => {
try {
const chainId = await getChainId(hre)
const contract = await connect(`chain-${chainId}`, hre)

const id = hre.ethers.encodeBytes32String(campaign)
const tx = await contract.createLevel(id, level)
await tx.wait()

return output.resultBox(`Added level ${level} to campaign ${campaign}`)
} catch (err) {
return output.errorBox(err)
}
})
37 changes: 37 additions & 0 deletions packages/ethernaut-zeronaut/src/tasks/get-campaign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const types = require('ethernaut-common/src/validation/types')
const output = require('ethernaut-common/src/ui/output')
const { getChainId } = require('ethernaut-common/src/util/network')
const { connect } = require('../internal/connect')
const debug = require('ethernaut-common/src/ui/debug')

require('../scopes/zeronaut')
.task('get-campaign', 'finds an existing campaign')
.addPositionalParam(
'name',
'The name of the campaign',
undefined,
types.string,
)
.setAction(async ({ name }, hre) => {
try {
const chainId = await getChainId(hre)
const contract = await connect(`chain-${chainId}`, hre)

const id = hre.ethers.encodeBytes32String(name)
const campaign = await contract.getCampaign(id)

let str = ''
str += `\n name: ${hre.ethers.decodeBytes32String(campaign.id)}`
str += `\n levels: ${campaign.levels.length}`

for (let i = 0; i < campaign.levels.length; i++) {
const levelAddress = campaign.levels[i]
const levelName = await contract.getLevelName(levelAddress)
str += `\n - Level ${i}: "${hre.ethers.decodeBytes32String(levelName)}"`
}

return output.resultBox(str)
} catch (err) {
return output.errorBox(err)
}
})

0 comments on commit 767b50e

Please sign in to comment.