From 767b50e6bca0363af2cd2ecbc978e1f96fe1c057 Mon Sep 17 00:00:00 2001 From: Alejandro Date: Tue, 24 Sep 2024 10:15:48 -0300 Subject: [PATCH] Display level names --- .../src/internal/connect.js | 5 +++ .../ethernaut-zeronaut/src/scopes/zeronaut.js | 2 +- .../src/tasks/create-level.js | 33 +++++++++++++++++ .../src/tasks/get-campaign.js | 37 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 packages/ethernaut-zeronaut/src/tasks/create-level.js create mode 100644 packages/ethernaut-zeronaut/src/tasks/get-campaign.js diff --git a/packages/ethernaut-zeronaut/src/internal/connect.js b/packages/ethernaut-zeronaut/src/internal/connect.js index 5933f38..a5e68aa 100644 --- a/packages/ethernaut-zeronaut/src/internal/connect.js +++ b/packages/ethernaut-zeronaut/src/internal/connect.js @@ -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) diff --git a/packages/ethernaut-zeronaut/src/scopes/zeronaut.js b/packages/ethernaut-zeronaut/src/scopes/zeronaut.js index 1bc71e8..aff89ec 100644 --- a/packages/ethernaut-zeronaut/src/scopes/zeronaut.js +++ b/packages/ethernaut-zeronaut/src/scopes/zeronaut.js @@ -1,4 +1,4 @@ const { scope } = require('hardhat/config') const { description } = require('../../package.json') -module.exports = scope('zeronaut', description) +module.exports = scope('zero', description) diff --git a/packages/ethernaut-zeronaut/src/tasks/create-level.js b/packages/ethernaut-zeronaut/src/tasks/create-level.js new file mode 100644 index 0000000..2e6bbfb --- /dev/null +++ b/packages/ethernaut-zeronaut/src/tasks/create-level.js @@ -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) + } + }) diff --git a/packages/ethernaut-zeronaut/src/tasks/get-campaign.js b/packages/ethernaut-zeronaut/src/tasks/get-campaign.js new file mode 100644 index 0000000..38768c8 --- /dev/null +++ b/packages/ethernaut-zeronaut/src/tasks/get-campaign.js @@ -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) + } + })