Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Games SOLVED #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test/game1Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
5 changes: 3 additions & 2 deletions test/game2Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
25 changes: 13 additions & 12 deletions test/game3Test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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');
});
});
13 changes: 6 additions & 7 deletions test/game4Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
27 changes: 22 additions & 5 deletions test/game5Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope you don't mind me asking, but how does comparing a string (address) with a number (threshold) work here?

I was struggling with this test file as I couldn't figure out how to identify which address would be less than the threshold address; when I convert an address to a number (either with Number(address) or parseInt(address)) I could find an address that was less than the threshold number, but they wouldn't work.
I'm guessing I don't really understand how Hexadecimal numbers work? 🤷

Copy link

@SantiagoDevRel SantiagoDevRel Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skplunkerin JS let you compare the numbers just like that, but a better way can be adding an "n" to the threshold, so JS knows this is a bigInt and can compare them more accurately

the If statement can be something like that:
if(BigInt(address) < BigInt(threshold){
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aw, that makes sense. Thanks for replying! I forget that an address is a hexadecimal, and that hexadecimal's are numbers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how long the loop ran?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was supposed to be ajoke?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apoorv-2204 I tried several times and it ran between 100-300 times, which took a couple of seconds.

}

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');
});
});