Section 4 NFT : I can't find the solution #120
Replies: 3 comments 3 replies
-
@clement-touzet After your question I tried this challenge and this code is working in Remix. // SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "hardhat/console.sol";
contract A {
B b;
constructor(address _address) {
b = B(_address);
}
function solve() public {
b.solveChallenge(0);
}
function owner() public view returns(address) {
return address(this);
}
function go() public {
uint256 guess =
uint256(keccak256(abi.encodePacked(address(this), block.prevrandao, block.timestamp))) % 1_000_000;
b.solveChallenge(guess);
}
}
contract B {
error S4__BadReturn();
error S4__BadOwner();
error S4__BadGuess();
uint256 myVal = 0;
function solveChallenge(uint256 guess) external {
(bool success, bytes memory returnData) = msg.sender.staticcall(abi.encodeWithSignature("owner()"));
address ownerAddress;
assembly {
ownerAddress := mload(add(returnData, 32))
}
console.log(ownerAddress);
if (!success || ownerAddress != msg.sender) {
revert S4__BadOwner();
}
console.log("condition 1 clear");
if (myVal == 1) {
uint256 rng = uint256(keccak256(abi.encodePacked(msg.sender, block.prevrandao, block.timestamp))) % 1_000_000;
if (rng != guess) {
revert S4__BadGuess();
}
console.log("successed!");
} else {
myVal = 1;
(bool succ,) = msg.sender.call(abi.encodeWithSignature("go()"));
if (!succ) {
revert S4__BadReturn();
}
}
myVal = 0;
}
} ![]() But this is failing on Testnet, I am figuring out this until then you can also check. |
Beta Was this translation helpful? Give feedback.
-
@clement-touzet Part of the solution is to clearly define what the problem is :) I will not give any SPOILERS (just some hints) so if someone wants to solve this on his/her own should stop reading here!!! *************************************** HINTS **************************************To give you both a hint, your code is correct but incomplete. Answer these hints below to help you understand:
PS. Above hints will help you in ALL NFT challenges, not only 4 |
Beta Was this translation helpful? Give feedback.
-
@clement-touzet I stopped reading after the Spoiler alert! I know there was some assembly code where I am still weak so I have to solve these challenges. Waiting for the second part of the course so after it I will try to solve these. Otherwise, I will read your hint part! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to solve the exercice in the section 4, but I can't find what is wrong with my smart contrat.
Can someone show me their solution or explain to me what should I do to complete this exercice ?
This is my code that doesn't work :
Beta Was this translation helpful? Give feedback.
All reactions