From 4605b9f33df835fb9cf35dd8b379659d55a4c5c0 Mon Sep 17 00:00:00 2001 From: Brivan-26 Date: Tue, 27 Dec 2022 15:11:10 +0100 Subject: [PATCH] Games SOLVED --- package-lock.json | 8 ++++++-- test/game1Test.js | 4 +--- test/game2Test.js | 5 +++-- test/game3Test.js | 25 +++++++++++++------------ test/game4Test.js | 13 ++++++------- test/game5Test.js | 27 ++++++++++++++++++++++----- 6 files changed, 51 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index af1aaa4..7b9fa2e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13460,7 +13460,9 @@ "node_modules/heap": { "version": "0.2.7", "resolved": "https://registry.npmjs.org/heap/-/heap-0.2.7.tgz", - "integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg==" + "integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg==", + "dev": true, + "peer": true }, "node_modules/hmac-drbg": { "version": "1.0.1", @@ -28218,7 +28220,9 @@ "heap": { "version": "0.2.7", "resolved": "https://registry.npmjs.org/heap/-/heap-0.2.7.tgz", - "integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg==" + "integrity": "sha512-2bsegYkkHO+h/9MGbn6KWcE45cHZgPANo5LXF7EvWdT0yT2EguSVO1nDgU5c8+ZOPwp2vMNa7YFsJhVcDR9Sdg==", + "dev": true, + "peer": true }, "hmac-drbg": { "version": "1.0.1", diff --git a/test/game1Test.js b/test/game1Test.js index 0a99a2f..586a3b5 100644 --- a/test/game1Test.js +++ b/test/game1Test.js @@ -13,12 +13,10 @@ describe('Game1', function () { // leave this as-is const { game } = await loadFixture(deployContractAndSetVariables); - // you must call unlock before you can win + await game.unlock() - // leave this call to game.win() as-is await game.win(); - // leave this testing assertion as-is assert(await game.isWon(), 'You did not win the game'); }); }); diff --git a/test/game2Test.js b/test/game2Test.js index 4cf5e3b..49c8898 100644 --- a/test/game2Test.js +++ b/test/game2Test.js @@ -12,11 +12,12 @@ describe('Game2', function () { it('should be a winner', async function () { const { game } = await loadFixture(deployContractAndSetVariables); - // press all the right switches to win this stage + await game.switchOn(20) + await game.switchOn(47) + await game.switchOn(212) await game.win(); - // leave this assertion as-is assert(await game.isWon(), 'You did not win the game'); }); }); diff --git a/test/game3Test.js b/test/game3Test.js index df0c52b..003a7dc 100644 --- a/test/game3Test.js +++ b/test/game3Test.js @@ -1,6 +1,5 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); const { assert } = require('chai'); - describe('Game3', function () { async function deployContractAndSetVariables() { const Game = await ethers.getContractFactory('Game3'); @@ -9,27 +8,29 @@ describe('Game3', function () { // Hardhat will create 10 accounts for you by default // you can get one of this accounts with ethers.provider.getSigner // and passing in the zero-based indexed of the signer you want: - const signer = ethers.provider.getSigner(0); + const signer1 = ethers.provider.getSigner(0); + const signer2 = ethers.provider.getSigner(1); + const signer3 = ethers.provider.getSigner(2); // you can get that signer's address via .getAddress() // this variable is NOT used for Contract 3, just here as an example - const address = await signer.getAddress(); + const address = await signer1.getAddress(); - return { game, signer }; + return { game, signer1, signer2, signer3 }; } it('should be a winner', async function () { - const { game, signer } = await loadFixture(deployContractAndSetVariables); - - // you'll need to update the `balances` mapping to win this stage + const { game, signer1, signer2, signer3 } = await loadFixture(deployContractAndSetVariables); - // to call a contract as a signer you can use contract.connect - await game.connect(signer).buy({ value: '1' }); + await game.connect(signer3).buy({ value: ethers.utils.parseEther('0.5') }); + await game.connect(signer1).buy({ value: ethers.utils.parseEther('1') }); + await game.connect(signer2).buy({ value: ethers.utils.parseEther('1.5') }); - // TODO: win expects three arguments - await game.win(); + const address1 = await signer1.getAddress() + const address2 = await signer2.getAddress() + const address3 = await signer3.getAddress() + await game.win(address1, address2, address3); - // leave this assertion as-is assert(await game.isWon(), 'You did not win the game'); }); }); diff --git a/test/game4Test.js b/test/game4Test.js index d688d53..d69615e 100644 --- a/test/game4Test.js +++ b/test/game4Test.js @@ -5,17 +5,16 @@ describe('Game4', function () { async function deployContractAndSetVariables() { const Game = await ethers.getContractFactory('Game4'); const game = await Game.deploy(); - - return { game }; + const signer = ethers.provider.getSigner(0); + return { game, signer }; } it('should be a winner', async function () { - const { game } = await loadFixture(deployContractAndSetVariables); - - // nested mappings are rough :} + const { game, signer } = await loadFixture(deployContractAndSetVariables); - await game.win(); + const address = await signer.getAddress() + await game.connect(signer).write(address) + await game.win(address); - // leave this assertion as-is assert(await game.isWon(), 'You did not win the game'); }); }); diff --git a/test/game5Test.js b/test/game5Test.js index 66461ed..31dd865 100644 --- a/test/game5Test.js +++ b/test/game5Test.js @@ -6,16 +6,33 @@ describe('Game5', function () { const Game = await ethers.getContractFactory('Game5'); const game = await Game.deploy(); - return { game }; + return { game}; } it('should be a winner', async function () { - const { game } = await loadFixture(deployContractAndSetVariables); + const { game} = await loadFixture(deployContractAndSetVariables); - // good luck + // creating random wallet that its address matches the condition + const threshold = 0x00FfFFfFFFfFFFFFfFfFfffFFFfffFfFffFfFFFf + let validAddress = false; + let wallet; + let address; + while (!validAddress) { + wallet = ethers.Wallet.createRandom() + address = await wallet.getAddress() + if(address < threshold) validAddress = true + } - await game.win(); + // init the wallet with the found address + wallet = wallet.connect(ethers.provider) + const signer = ethers.provider.getSigner(0) + + // send some ether(needed for gas fees on the upcoming function call) + await signer.sendTransaction({ + to: address, + value: ethers.utils.parseEther('200') + }) + await game.connect(wallet).win(); - // leave this assertion as-is assert(await game.isWon(), 'You did not win the game'); }); });