Skip to content

Commit

Permalink
Solve and show solved levels
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Sep 26, 2024
1 parent bbfc535 commit 4b59c42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion packages/ethernaut-zeronaut/src/tasks/get-campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,33 @@ require('../scopes/zeronaut')
)
.setAction(async ({ name }, hre) => {
try {
// Connect to the game contract
const chainId = await getChainId(hre)
const contract = await connect(`chain-${chainId}`, hre)

// Get the campaign details
const id = hre.ethers.encodeBytes32String(name)
const campaign = await contract.getCampaign(id)

// Get the player address
const signer = (await hre.ethers.getSigners())[0]
const playerAddress = signer.address

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

// Get each level's details
for (let i = 0; i < campaign.levels.length; i++) {
const levelId = campaign.levels[i]
const levelData = await contract.getLevel(levelId)
const levelAddress = levelData.addr
const level = await getLevelContract(hre, levelAddress)
const solved = await contract.isLevelSolved(levelId, playerAddress)
const levelName = hre.ethers.decodeBytes32String(await level.name())
str += `\n [${i + 1}] "${levelName}"`
str += `\n [${i + 1}] "${levelName}" ${solved ? '✅' : '❌'}`
}

return output.resultBox(str)
Expand Down
9 changes: 9 additions & 0 deletions packages/ethernaut-zeronaut/src/tasks/get-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ require('../scopes/zeronaut')
const chainId = await getChainId(hre)
const contract = await connect(`chain-${chainId}`, hre)

// Get the player address
const signer = (await hre.ethers.getSigners())[0]
const playerAddress = signer.address

// Retrieve the level address
const id = hre.ethers.encodeBytes32String(name)
const levelData = await contract.getLevel(id)
Expand All @@ -29,10 +33,15 @@ require('../scopes/zeronaut')
const levelName = await level.name()
const levelInstructions = await level.instructions()

// Check if the level is completed
const solved = await contract.isLevelSolved(id, playerAddress)

// Display the level details
let str = ''
str += ` name: ${hre.ethers.decodeBytes32String(levelName)}`
str += `\n solved: ${solved}`
str += `\n instructions: ${levelInstructions}`

return output.resultBox(str)
} catch (err) {
return output.errorBox(err)
Expand Down
5 changes: 3 additions & 2 deletions packages/ethernaut-zeronaut/src/tasks/play-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ require('../scopes/zeronaut')
const success = await level.check(proof, publicInputs)

// Submit the proof
// TODO
const tx = await contract.solveLevel(levelId, proof, publicInputs)
await tx.wait()

return output.resultBox(`Success: ${success}`)
return output.resultBox(`Level solved: ${success}`)
} catch (err) {
return output.errorBox(err)
}
Expand Down

0 comments on commit 4b59c42

Please sign in to comment.