From 8848e3116262eb6e515cdf868637e51620928ea4 Mon Sep 17 00:00:00 2001 From: alice3773 Date: Sun, 5 Jun 2022 19:25:32 +0800 Subject: [PATCH] finished classroom activities --- .DS_Store | Bin 0 -> 6148 bytes contracts/Game1.sol | 1 + contracts/Game2.sol | 1 + contracts/Game4.sol | 1 + test/game1Test.js | 21 ++++++++-------- test/game2Test.js | 23 ++++++++++-------- test/game3Test.js | 57 +++++++++++++++++++++++++------------------- test/game4Test.js | 26 ++++++++++++-------- test/game5Test.js | 38 +++++++++++++++++++++-------- 9 files changed, 104 insertions(+), 64 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..33ef30c03d6288447ad01e983a6572824def16d9 GIT binary patch literal 6148 zcmeHK%Z}496us^gNO*{jgaj;*vSC-Mh8ZDPAcZn4W``663qUhT6WTDDq)JjMC`H-x z2k;U62S32Ku!3`KgPOK%p+dgW_3`W2@kx9g6OkB9BbTT{L>8Q}eICgM<97BnYv~$p z6mpD$0vb|9ecmVJHkcQkwMmQ@c zn(;N9q)A!z``<)sr+wz^Im@;!`;z_AALlc_3aV*22#V)idK^^aQ0JfIdHOpagin)r zcGS7@Fwd$W$>KtilPE@)7tfL`%4dUonnk4?$5#zJL-CJ~>`Muly zuCu>)XR)xYt2f>IhsURH-_74IK75j^A%W;6?Xt#S@EMs6>b&ubEX}hKTA8^^W5m6J zB8e6Cw4Sdhp%F^2IUY9j`~udpp1U-Wk@E!Uh+?`$Dc;DCP86HpPICltV`YSkDHpxVYKm`#8gt%?AKy7me+)$v8`4>fA!Xdt$s@La7F>6 zz`sy{U{s-+>%KMc%qGC{;C2$ E0i92(C;$Ke literal 0 HcmV?d00001 diff --git a/contracts/Game1.sol b/contracts/Game1.sol index b06a2b5..b8aeb0e 100644 --- a/contracts/Game1.sol +++ b/contracts/Game1.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: Unlicense pragma solidity ^0.7.0; +import "hardhat/console.sol"; contract Game1 { bool public isWon; diff --git a/contracts/Game2.sol b/contracts/Game2.sol index fe61893..8d80eac 100644 --- a/contracts/Game2.sol +++ b/contracts/Game2.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: Unlicense pragma solidity ^0.7.0; +import "hardhat/console.sol"; contract Game2 { bool public isWon; diff --git a/contracts/Game4.sol b/contracts/Game4.sol index 3d356a2..5a1ffc0 100644 --- a/contracts/Game4.sol +++ b/contracts/Game4.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: Unlicense pragma solidity ^0.7.0; +import "hardhat/console.sol"; contract Game4 { bool public isWon; diff --git a/test/game1Test.js b/test/game1Test.js index 57d7506..01b7688 100644 --- a/test/game1Test.js +++ b/test/game1Test.js @@ -1,16 +1,17 @@ const { assert } = require("chai"); describe("Game1", function() { - it("should be a winner", async function() { - const Game = await ethers.getContractFactory("Game1"); - const game = await Game.deploy(); - await game.deployed(); + it("should be a winner", async function() { + const Game = await ethers.getContractFactory("Game1"); + const game = await Game.deploy(); + await game.deployed(); - // you must call unlock before you can win + // you must call unlock before you can win + await game.unlock(); - await game.win(); + await game.win(); - // leave this assertion as-is - assert(await game.isWon(), "You did not win the game"); - }); -}); + // leave this assertion as-is + assert(await game.isWon(), "You did not win the game"); + }); +}); \ No newline at end of file diff --git a/test/game2Test.js b/test/game2Test.js index f92dc83..22eb37a 100644 --- a/test/game2Test.js +++ b/test/game2Test.js @@ -1,16 +1,19 @@ const { assert } = require("chai"); describe("Game2", function() { - it("should be a winner", async function() { - const Game = await ethers.getContractFactory("Game2"); - const game = await Game.deploy(); - await game.deployed(); + it("should be a winner", async function() { + const Game = await ethers.getContractFactory("Game2"); + const game = await Game.deploy(); + await game.deployed(); - // press all the right switches to win this stage + // press all the right switches to win this stage + game.switchOn(20); + game.switchOn(47); + game.switchOn(212); - await game.win(); + await game.win(); - // leave this assertion as-is - assert(await game.isWon(), "You did not win the game"); - }); -}); + // leave this assertion as-is + assert(await game.isWon(), "You did not win the game"); + }); +}); \ No newline at end of file diff --git a/test/game3Test.js b/test/game3Test.js index b2e3003..0ae2de5 100644 --- a/test/game3Test.js +++ b/test/game3Test.js @@ -1,27 +1,36 @@ const { assert } = require("chai"); +const { ethers } = require("hardhat"); describe("Game3", function() { - it("should be a winner", async function() { - const Game = await ethers.getContractFactory("Game3"); - const game = await Game.deploy(); - await game.deployed(); - - // three addresses, three balances - // you'll need to update the mapping to win this stage - - // 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 address = await signer.getAddress(); - - // to call a contract as a signer you can use contract.connect - await game.connect(signer).buy({ value: "1" }); - - // TODO: win expects three arguments - await game.win(); - - // leave this assertion as-is - assert(await game.isWon(), "You did not win the game"); - }); -}); + it("should be a winner", async function() { + const Game = await ethers.getContractFactory("Game3"); + const game = await Game.deploy(); + await game.deployed(); + + // three addresses, three balances + // you'll need to update the mapping to win this stage + + // 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 signer1 = ethers.provider.getSigner(0); + const address1 = await signer1.getAddress(); + + const signer2 = ethers.provider.getSigner(1); + const address2 = await signer2.getAddress(); + + const signer3 = ethers.provider.getSigner(2); + const address3 = await signer3.getAddress(); + + // to call a contract as a signer you can use contract.connect + await game.connect(signer1).buy({ value: "5" }); + await game.connect(signer2).buy({ value: "10" }); + await game.connect(signer3).buy({ value: "1" }); + + // TODO: win expects three arguments + await game.win(address1, address2, address3); + + // leave this assertion as-is + assert(await game.isWon(), "You did not win the game"); + }); +}); \ No newline at end of file diff --git a/test/game4Test.js b/test/game4Test.js index dab5b45..12422f6 100644 --- a/test/game4Test.js +++ b/test/game4Test.js @@ -1,16 +1,22 @@ const { assert } = require("chai"); describe("Game4", function() { - it("should be a winner", async function() { - const Game = await ethers.getContractFactory("Game4"); - const game = await Game.deploy(); - await game.deployed(); + it("should be a winner", async function() { + const Game = await ethers.getContractFactory("Game4"); + const game = await Game.deploy(); + await game.deployed(); - // nested mappings are rough :} + // nested mappings are rough :} + const signer1 = ethers.provider.getSigner(0); + const address1 = await signer1.getAddress(); - await game.win(); + const signer2 = ethers.provider.getSigner(1); + const address2 = await signer2.getAddress(); - // leave this assertion as-is - assert(await game.isWon(), "You did not win the game"); - }); -}); + await game.connect(signer1).write(address2); + await game.connect(signer2).win(address1); + + // leave this assertion as-is + assert(await game.isWon(), "You did not win the game"); + }); +}); \ No newline at end of file diff --git a/test/game5Test.js b/test/game5Test.js index aeeb41c..3b948c1 100644 --- a/test/game5Test.js +++ b/test/game5Test.js @@ -1,16 +1,34 @@ const { assert } = require("chai"); +const { Signer } = require("ethers"); +const { ethers } = require("hardhat"); describe("Game5", function() { - it("should be a winner", async function() { - const Game = await ethers.getContractFactory("Game5"); - const game = await Game.deploy(); - await game.deployed(); + it("should be a winner", async function() { + const Game = await ethers.getContractFactory("Game5"); + const game = await Game.deploy(); + await game.deployed(); - // good luck + // good luck + const threshold = 0x00FfFFfFFFfFFFFFfFfFfffFFFfffFfFffFfFFFf; + let validAddress; + while (!validAddress) { + wallet = ethers.Wallet.createRandom(); + address = await wallet.getAddress(); + if (address < threshold) { + validAddress = true; + } + }; - await game.win(); + wallet = wallet.connect(ethers.provider); + const signer = ethers.provider.getSigner(0); + await signer.sendTransaction({ + to: address, + value: ethers.utils.parseEther("100") + }); - // leave this assertion as-is - assert(await game.isWon(), "You did not win the game"); - }); -}); + await game.connect(wallet).win(); + + // leave this assertion as-is + assert(await game.isWon(), "You did not win the game"); + }); +}); \ No newline at end of file